|
| 1 | +import * as common from '../common/index.mjs'; |
| 2 | +import { describe, it, beforeEach } from 'node:test'; |
| 3 | +import { once } from 'node:events'; |
| 4 | +import assert from 'node:assert'; |
| 5 | +import { spawn } from 'node:child_process'; |
| 6 | +import { writeFileSync } from 'node:fs'; |
| 7 | +import tmpdir from '../common/tmpdir.js'; |
| 8 | + |
| 9 | +if (common.isWindows) { |
| 10 | + common.skip('no signals on Windows'); |
| 11 | +} |
| 12 | + |
| 13 | +if (common.isIBMi) { |
| 14 | + common.skip('IBMi does not support `fs.watch()`'); |
| 15 | +} |
| 16 | + |
| 17 | +if (common.isAIX) { |
| 18 | + common.skip('folder watch capability is limited in AIX.'); |
| 19 | +} |
| 20 | + |
| 21 | +const indexContents = ` |
| 22 | + const { setTimeout } = require("timers/promises"); |
| 23 | + (async () => { |
| 24 | + // Wait a few milliseconds to make sure that the |
| 25 | + // parent process has time to attach its listeners |
| 26 | + await setTimeout(200); |
| 27 | +
|
| 28 | + process.on('SIGTERM', () => { |
| 29 | + console.log('__SIGTERM received__'); |
| 30 | + process.exit(123); |
| 31 | + }); |
| 32 | +
|
| 33 | + process.on('SIGINT', () => { |
| 34 | + console.log('__SIGINT received__'); |
| 35 | + process.exit(124); |
| 36 | + }); |
| 37 | +
|
| 38 | + console.log('ready!'); |
| 39 | +
|
| 40 | + // Wait for a long time (just to keep the process alive) |
| 41 | + await setTimeout(100_000_000); |
| 42 | + })(); |
| 43 | +`; |
| 44 | + |
| 45 | +let indexPath = ''; |
| 46 | + |
| 47 | +function refresh() { |
| 48 | + tmpdir.refresh(); |
| 49 | + indexPath = tmpdir.resolve('index.js'); |
| 50 | + writeFileSync(indexPath, indexContents); |
| 51 | +} |
| 52 | + |
| 53 | +describe('test runner watch mode with --watch-kill-signal', () => { |
| 54 | + beforeEach(refresh); |
| 55 | + |
| 56 | + it('defaults to SIGTERM', async () => { |
| 57 | + let currentRun = Promise.withResolvers(); |
| 58 | + const child = spawn(process.execPath, ['--watch', indexPath], { |
| 59 | + cwd: tmpdir.path, |
| 60 | + }); |
| 61 | + |
| 62 | + let stdout = ''; |
| 63 | + child.stdout.on('data', (data) => { |
| 64 | + stdout += data.toString(); |
| 65 | + currentRun.resolve(); |
| 66 | + }); |
| 67 | + |
| 68 | + await currentRun.promise; |
| 69 | + |
| 70 | + currentRun = Promise.withResolvers(); |
| 71 | + writeFileSync(indexPath, indexContents); |
| 72 | + |
| 73 | + await currentRun.promise; |
| 74 | + child.kill(); |
| 75 | + const [exitCode] = await once(child, 'exit'); |
| 76 | + assert.match(stdout, /__SIGTERM received__/); |
| 77 | + assert.strictEqual(exitCode, 123); |
| 78 | + }); |
| 79 | + |
| 80 | + it('can be overridden (to SIGINT)', async () => { |
| 81 | + let currentRun = Promise.withResolvers(); |
| 82 | + const child = spawn(process.execPath, ['--watch', '--watch-kill-signal', 'SIGINT', indexPath], { |
| 83 | + cwd: tmpdir.path, |
| 84 | + }); |
| 85 | + let stdout = ''; |
| 86 | + |
| 87 | + child.stdout.on('data', (data) => { |
| 88 | + stdout += data.toString(); |
| 89 | + if (stdout.includes('ready!')) { |
| 90 | + currentRun.resolve(); |
| 91 | + } |
| 92 | + }); |
| 93 | + |
| 94 | + await currentRun.promise; |
| 95 | + |
| 96 | + currentRun = Promise.withResolvers(); |
| 97 | + writeFileSync(indexPath, indexContents); |
| 98 | + |
| 99 | + await currentRun.promise; |
| 100 | + child.kill(); |
| 101 | + const [exitCode] = await once(child, 'exit'); |
| 102 | + assert.match(stdout, /__SIGINT received__/); |
| 103 | + assert.strictEqual(exitCode, 124); |
| 104 | + }); |
| 105 | + |
| 106 | + it('errors if an invalid signal is provided', async () => { |
| 107 | + const currentRun = Promise.withResolvers(); |
| 108 | + const child = spawn(process.execPath, ['--watch', '--watch-kill-signal', 'invalid_signal', indexPath], { |
| 109 | + cwd: tmpdir.path, |
| 110 | + }); |
| 111 | + let stdout = ''; |
| 112 | + |
| 113 | + child.stderr.on('data', (data) => { |
| 114 | + stdout += data.toString(); |
| 115 | + currentRun.resolve(); |
| 116 | + }); |
| 117 | + |
| 118 | + await currentRun.promise; |
| 119 | + |
| 120 | + assert.match(stdout, new RegExp(/TypeError \[ERR_UNKNOWN_SIGNAL\]: Unknown signal: invalid_signal/)); |
| 121 | + }); |
| 122 | +}); |
0 commit comments