Skip to content

Commit

Permalink
test: add tests for run functions
Browse files Browse the repository at this point in the history
Signed-off-by: Amin Yahyaabadi <[email protected]>
  • Loading branch information
aminya committed Apr 3, 2024
1 parent d2e9ac7 commit 2a1e987
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 7 deletions.
4 changes: 2 additions & 2 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion src/extract-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import fs from 'fs/promises';
import path from 'path';
import { CacheOptions, Opts, getCacheMap, getMountArgsString, getTargetPath } from './opts.js';
import { run, runPiped } from './run.js';
import { spawn } from 'child_process';

async function extractCache(cacheSource: string, cacheOptions: CacheOptions, scratchDir: string) {
// Prepare Timestamp for Layer Cache Busting
Expand Down
6 changes: 3 additions & 3 deletions src/run.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import spawnPlease from 'spawn-please'
import cp, { type SpawnOptions, type ChildProcess } from 'child_process';
import cp, { type ChildProcess } from 'child_process';

export async function run(command: string, args: string[]) {
try {
Expand All @@ -10,8 +10,8 @@ export async function run(command: string, args: string[]) {
}
}

export async function runPiped([commannd1, args1]: [string, string[]], [command2, args2]: [string, string[]]) {
const cp1 = cp.spawn(commannd1, args1, { stdio: ['inherit', 'pipe', 'inherit'] });
export async function runPiped([command1, args1]: [string, string[]], [command2, args2]: [string, string[]]) {
const cp1 = cp.spawn(command1, args1, { stdio: ['inherit', 'pipe', 'inherit'] });
const cp2 = cp.spawn(command2, args2, { stdio: ['pipe', 'inherit', 'inherit'] });

cp1.stdout.pipe(cp2.stdin);
Expand Down
22 changes: 22 additions & 0 deletions tests/run.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { expect, test } from 'vitest'
import { run, runPiped } from '../src/run.js'

test('run with valid command', async () => {
expect(await run('echo', ['hello'])).toStrictEqual({ stdout: 'hello\n', stderr: '' })
})

test('run with invalid command', async () => {
await expect(run('invalid', [])).rejects.toThrow()
})

test('runPiped with valid commands', async () => {
await expect(runPiped(['echo', ['hello']], ['cat', []])).resolves.not.toThrow()
})

test('runPiped with invalid first command', async () => {
await expect(runPiped(['invalid', []], ['cat', []])).rejects.toThrow()
})

test('runPiped with invalid second command', async () => {
await expect(runPiped(['echo', ['hello']], ['invalid', []])).rejects.toThrow()
})

0 comments on commit 2a1e987

Please sign in to comment.