Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .bumpy/exec-test-coverage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
86 changes: 86 additions & 0 deletions packages/varlock/src/lib/test/exec.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { describe, it, expect } from 'vitest';
import { Readable } from 'node:stream';
import { exec } from '../exec.js';

/**
* Collect all data from a Readable stream into a string.
*/
async function streamToString(stream: Readable): Promise<string> {
const chunks: Array<Buffer | string> = [];
for await (const chunk of stream) {
chunks.push(chunk);
}
return chunks.join('');
}

describe('exec', () => {
it('should forward child stdout with piped stdio', async () => {
// This test validates that stdout data is fully available when the
// Promise resolves. The bug (exit vs close) would cause this to race:
// on a slow machine or Windows the data events might not have fired
// by the time process.exit() is called after `await commandProcess`.
const childProcess = exec('node', ['-p', '1+1'], {
stdout: 'pipe',
stderr: 'pipe',
});

const stdoutData = childProcess.stdout ? streamToString(childProcess.stdout) : Promise.resolve('');

const result = await childProcess;
expect(result.exitCode).toBe(0);

// All pipe data must be available once the Promise resolves (close event)
const output = await stdoutData;
expect(output.trim()).toBe('2');
});

it('should propagate non-zero exit code', async () => {
let exitCode: number;
try {
await exec('node', ['-e', 'process.exit(42)'], {
stdout: 'pipe',
stderr: 'pipe',
});
exitCode = 0;
} catch (error: any) {
exitCode = error.exitCode;
}
expect(exitCode).toBe(42);
});

it('should forward child stderr with piped stdio', async () => {
const childProcess = exec('node', ['-e', 'process.stderr.write("err-output\\n")'], {
stdout: 'pipe',
stderr: 'pipe',
});

const stderrData = childProcess.stderr ? streamToString(childProcess.stderr) : Promise.resolve('');

const result = await childProcess;
expect(result.exitCode).toBe(0);

const output = await stderrData;
expect(output.trim()).toBe('err-output');
});

it('should resolve with inherited stdio', async () => {
const result = await exec('node', ['-e', 'process.exit(0)'], {
stdio: 'inherit',
});
expect(result.exitCode).toBe(0);
});

it('should resolve after child fully runs (not prematurely)', async () => {
const startMs = Date.now();
const result = await exec('node', ['-e', 'setTimeout(() => {}, 300)'], {
stdout: 'pipe',
stderr: 'pipe',
});
const elapsedMs = Date.now() - startMs;
expect(result.exitCode).toBe(0);
// The child runs a 300ms setTimeout, so the Promise must not resolve before
// the child exits. We assert ≥200ms (100ms slack for slow/loaded CI runners)
// to avoid flakiness while still catching a premature resolve (e.g. <50ms).
expect(elapsedMs).toBeGreaterThanOrEqual(200);
});
});
24 changes: 24 additions & 0 deletions smoke-tests/tests/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,30 @@ describe('CLI Commands', () => {
});
});

describe('run command', () => {
test('varlock run should forward child stdout', () => {
const result = varlockRun(['node', '-p', '1+1'], { cwd: 'smoke-test-basic' });
expect(result.exitCode).toBe(0);
expect(result.output.trim()).toBe('2');
});

test('varlock run should propagate non-zero exit code', () => {
const result = varlockRun(['node', '-e', 'process.exit(42)'], {
cwd: 'smoke-test-basic',
});
expect(result.exitCode).toBe(42);
});

test('varlock run should forward child stderr', () => {
const result = varlockRun(
['node', '-e', 'process.stderr.write("error-output\\n")'],
{ cwd: 'smoke-test-basic' },
);
expect(result.exitCode).toBe(0);
expect(result.output).toContain('error-output');
});
});

describe('type generation', () => {
const typeFilePath = join(SMOKE_TESTS_DIR, 'smoke-test-basic', 'env.d.ts');
const typeFilePathAutoFalse = join(SMOKE_TESTS_DIR, 'smoke-test-typegen-auto', 'env.d.ts');
Expand Down
Loading