Fix randomly failing test (#725)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent 0ff8b34 · Browse files · 5 files·+22 added−29 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+22 added−29 removed
- tests:+22 added−29 removed
Provenance
- Commit
1fbf484- Tree
995068c- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
0ff8b34
5 files changed+22 added−29 removed
modified:test/fixtures/nested.js+1 added-2 removed
12345676#!/usr/bin/env nodeimport process from 'node:process';import {execa} from '../../index.js';
const [options, file, ...args] = process.argv.slice(2);const nestedOptions = {stdio: 'inherit', ...JSON.parse(options)};await execa(file, args, nestedOptions);await execa(file, args, JSON.parse(options));deleted:test/fixtures/sub-process-exit.js-13 removed
12345678910111213#!/usr/bin/env nodeimport process from 'node:process';import {execa} from '../../index.js';
const cleanup = process.argv[2] === 'true';const detached = process.argv[3] === 'true';
try { await execa('noop.js', {cleanup, detached});} catch (error) { console.error(error); process.exit(1);}modified:test/fixtures/sub-process.js+1 added
4 unmodified lines567894 unmodified linesconst cleanup = process.argv[2] === 'true';const detached = process.argv[3] === 'true';const subprocess = execa('forever.js', {cleanup, detached});process.send(subprocess.pid);await subprocess;modified:test/kill.js+17 added-11 removed
171 unmodified lines172 });173 174 // When child process exits before parent process175 const spawnAndExit = async (t, cleanup, detached) => {176- await t.notThrowsAsync(execa('sub-process-exit.js', [cleanup, detached]));176+ await t.notThrowsAsync(execa('nested.js', [JSON.stringify({cleanup, detached}), 'noop.js']));177 };178 179 test('spawnAndExit', spawnAndExit, false, false);180 test('spawnAndExit cleanup', spawnAndExit, true, false);10 unmodified lines191 192 process.kill(subprocess.pid, signal);193 194 await t.throwsAsync(subprocess);195-196- // The `cleanup` option can introduce a race condition in this test.197- // This is especially true when run concurrently, so we use `test.serial()` and a manual timeout.198- if (signal === 'SIGTERM' && cleanup && !detached) {199- await setTimeout(1e3);200- }201-195 t.false(isRunning(subprocess.pid));203- t.not(isRunning(pid), isKilled);196 205- if (!isKilled) {197+ if (isKilled) {198+ await Promise.race([199+ setTimeout(1e4, undefined, {ref: false}),200+ pollForProcessExit(pid),201+ ]);202+ t.is(isRunning(pid), false);203+ } else {204+ t.is(isRunning(pid), true);205 process.kill(pid, 'SIGKILL');206 }207 };208 209+const pollForProcessExit = async pid => {210+ while (isRunning(pid)) {211+ // eslint-disable-next-line no-await-in-loop212+ await setTimeout(100);213+ }214+};215+216 // Without `options.cleanup`:217 // - on Windows subprocesses are killed if `options.detached: false`, but not218 // if `options.detached: true`219 // - on Linux subprocesses are never killed regardless of `options.detached`1 unmodified line221 // - `options.cleanup` with SIGKILL is a noop, since it cannot be handled222 const exitIfWindows = process.platform === 'win32';223 test('spawnAndKill SIGTERM', spawnAndKill, ['SIGTERM', false, false, exitIfWindows]);224 test('spawnAndKill SIGKILL', spawnAndKill, ['SIGKILL', false, false, exitIfWindows]);219-test.serial('spawnAndKill cleanup SIGTERM', spawnAndKill, ['SIGTERM', true, false, true]);225+test('spawnAndKill cleanup SIGTERM', spawnAndKill, ['SIGTERM', true, false, true]);226 test('spawnAndKill cleanup SIGKILL', spawnAndKill, ['SIGKILL', true, false, exitIfWindows]);227 test('spawnAndKill detached SIGTERM', spawnAndKill, ['SIGTERM', false, true, false]);228 test('spawnAndKill detached SIGKILL', spawnAndKill, ['SIGKILL', false, true, false]);229 test('spawnAndKill cleanup detached SIGTERM', spawnAndKill, ['SIGTERM', true, true, false]);84 unmodified linesmodified:test/verbose.js+3 added-3 removed
6 unmodified lines7891011111213141516171718192021222323242526276 unmodified lines6 unmodified linesconst normalizeTimestamp = output => output.replaceAll(/\d/g, '0');const testTimestamp = '[00:00:00.000]';
test('Prints command when "verbose" is true', async t => { const {stdout, stderr} = await execa('nested.js', [JSON.stringify({verbose: true}), 'noop.js', 'test'], {all: true}); const {stdout, stderr} = await execa('nested.js', [JSON.stringify({verbose: true, stdio: 'inherit'}), 'noop.js', 'test'], {all: true}); t.is(stdout, 'test'); t.is(normalizeTimestamp(stderr), `${testTimestamp} noop.js test`);});
test('Prints command with NODE_DEBUG=execa', async t => { const {stdout, stderr} = await execa('nested.js', [JSON.stringify({}), 'noop.js', 'test'], {all: true, env: {NODE_DEBUG: 'execa'}}); const {stdout, stderr} = await execa('nested.js', [JSON.stringify({stdio: 'inherit'}), 'noop.js', 'test'], {all: true, env: {NODE_DEBUG: 'execa'}}); t.is(stdout, 'test'); t.is(normalizeTimestamp(stderr), `${testTimestamp} noop.js test`);});
test('Escape verbose command', async t => { const {stderr} = await execa('nested.js', [JSON.stringify({verbose: true}), 'noop.js', 'one two', '"'], {all: true}); const {stderr} = await execa('nested.js', [JSON.stringify({verbose: true, stdio: 'inherit'}), 'noop.js', 'one two', '"'], {all: true}); t.true(stderr.endsWith('"one two" "\\""'));});
test('Verbose option works with inherit', async t => {6 unmodified lines