Skip to content

Commit b60b68d

Browse files
committed
test: do not create temporary script files during tests
1 parent 292cfa7 commit b60b68d

3 files changed

Lines changed: 45 additions & 64 deletions

File tree

src/test/main_test.ts

Lines changed: 23 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import path from 'node:path';
66
import {spawnSync} from 'node:child_process';
77

88
const isWindows = os.platform() === 'win32';
9-
const fixturesDir = path.join(import.meta.dirname, '../../test/fixtures');
10-
const distDir = path.join(import.meta.dirname, '../../dist');
119

1210
const variants = [
1311
{name: 'async', x, isAsync: true},
@@ -368,79 +366,40 @@ if (!isWindows) {
368366
});
369367

370368
test('resolves when grandchild holds piped stdout open', async () => {
371-
const dir = fs.mkdtempSync(
372-
path.join(os.tmpdir(), 'tinyexec-grandchild-')
373-
);
374-
const runnerScript = path.join(dir, 'runner.mjs');
375-
const distPath = JSON.stringify(path.join(distDir, 'main.mjs'));
376-
const fixturePath = JSON.stringify(
377-
path.join(fixturesDir, 'grandchild.mjs')
378-
);
379-
380-
fs.writeFileSync(
381-
runnerScript,
382-
`import { x } from ${distPath}
383-
const result = await x('node', [${fixturePath}])
384-
process.stdout.write(JSON.stringify({ stdout: result.stdout, exitCode: result.exitCode }))
385-
`
386-
);
387-
388-
try {
389-
const proc = spawnSync('node', [runnerScript], {
390-
timeout: 10000,
369+
const proc = spawnSync(
370+
'node',
371+
['test/fixtures/spawn_grandchild.mjs', 'grandchild.mjs'],
372+
{
373+
timeout: 10_000,
391374
encoding: 'utf8',
392375
killSignal: 'SIGKILL',
393376
stdio: ['pipe', 'pipe', 'pipe']
394-
});
377+
}
378+
);
395379

396-
expect(proc.signal).not.toBe('SIGKILL');
397-
expect(proc.status).toBe(0);
398-
const parsed = JSON.parse(proc.stdout.trim());
399-
expect(parsed.exitCode).toBe(0);
400-
expect(parsed.stdout).toBe('output\n');
401-
} finally {
402-
spawnSync('pkill', ['-f', 'tinyexec-test-grandchild']);
403-
fs.rmSync(dir, {recursive: true, force: true});
404-
}
380+
expect(proc.signal).not.toBe('SIGKILL');
381+
expect(proc.status).toBe(0);
382+
const parsed = JSON.parse(proc.stdout.trim());
383+
expect(parsed.exitCode).toBe(0);
384+
expect(parsed.stdout).toBe('output\n');
405385
});
406386

407387
test('iterator completes when grandchild holds piped stdout open', async () => {
408-
const dir = fs.mkdtempSync(
409-
path.join(os.tmpdir(), 'tinyexec-grandchild-')
410-
);
411-
const runnerScript = path.join(dir, 'runner.mjs');
412-
const distPath = JSON.stringify(path.join(distDir, 'main.mjs'));
413-
const fixturePath = JSON.stringify(
414-
path.join(fixturesDir, 'grandchild_multiline.mjs')
415-
);
416-
417-
fs.writeFileSync(
418-
runnerScript,
419-
`import { x } from ${distPath}
420-
const lines = []
421-
for await (const line of x('node', [${fixturePath}])) {
422-
lines.push(line)
423-
}
424-
process.stdout.write(JSON.stringify(lines))
425-
`
426-
);
427-
428-
try {
429-
const proc = spawnSync('node', [runnerScript], {
430-
timeout: 10000,
388+
const proc = spawnSync(
389+
'node',
390+
['test/fixtures/spawn_grandchild_iterator.mjs'],
391+
{
392+
timeout: 10_000,
431393
encoding: 'utf8',
432394
killSignal: 'SIGKILL',
433395
stdio: ['pipe', 'pipe', 'pipe']
434-
});
396+
}
397+
);
435398

436-
expect(proc.signal).not.toBe('SIGKILL');
437-
expect(proc.status).toBe(0);
438-
const parsed = JSON.parse(proc.stdout.trim());
439-
expect(parsed).toEqual(['line1', 'line2']);
440-
} finally {
441-
spawnSync('pkill', ['-f', 'tinyexec-test-grandchild']);
442-
fs.rmSync(dir, {recursive: true, force: true});
443-
}
399+
expect(proc.signal).not.toBe('SIGKILL');
400+
expect(proc.status).toBe(0);
401+
const parsed = JSON.parse(proc.stdout.trim());
402+
expect(parsed).toEqual(['line1', 'line2']);
444403
});
445404
});
446405

test/fixtures/spawn_grandchild.mjs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import path from 'node:path';
2+
import {exec} from '../../dist/main.mjs';
3+
4+
const childScript = path.join(import.meta.dirname, 'grandchild.mjs');
5+
6+
const result = await exec('node', [childScript]);
7+
8+
process.stdout.write(
9+
JSON.stringify({stdout: result.stdout, exitCode: result.exitCode})
10+
);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import path from 'node:path';
2+
import {exec} from '../../dist/main.mjs';
3+
4+
const childScript = path.join(import.meta.dirname, 'grandchild_multiline.mjs');
5+
6+
const lines = [];
7+
8+
for await (const line of exec('node', [childScript])) {
9+
lines.push(line);
10+
}
11+
12+
process.stdout.write(JSON.stringify(lines));

0 commit comments

Comments
 (0)