|
| 1 | +import * as common from '../common/index.mjs'; |
| 2 | +import tmpdir from '../common/tmpdir.js'; |
| 3 | +import assert from 'node:assert'; |
| 4 | +import path from 'node:path'; |
| 5 | +import { execPath } from 'node:process'; |
| 6 | +import { describe, it } from 'node:test'; |
| 7 | +import { spawn } from 'node:child_process'; |
| 8 | +import { writeFileSync, mkdirSync } from 'node:fs'; |
| 9 | +import { inspect } from 'node:util'; |
| 10 | +import { createInterface } from 'node:readline'; |
| 11 | + |
| 12 | +if (common.isIBMi) |
| 13 | + common.skip('IBMi does not support `fs.watch()`'); |
| 14 | + |
| 15 | +let tmpFiles = 0; |
| 16 | +function createTmpFile(content, ext, basename) { |
| 17 | + const file = path.join(basename, `${tmpFiles++}${ext}`); |
| 18 | + writeFileSync(file, content); |
| 19 | + return file; |
| 20 | +} |
| 21 | + |
| 22 | +async function runNode({ |
| 23 | + args, |
| 24 | + expectedCompletionLog = 'Completed running', |
| 25 | + options = {}, |
| 26 | +}) { |
| 27 | + const child = spawn(execPath, args, { encoding: 'utf8', stdio: 'pipe', ...options }); |
| 28 | + let stderr = ''; |
| 29 | + const stdout = []; |
| 30 | + |
| 31 | + child.stderr.on('data', (data) => { |
| 32 | + stderr += data; |
| 33 | + }); |
| 34 | + |
| 35 | + try { |
| 36 | + // Break the chunks into lines |
| 37 | + for await (const data of createInterface({ input: child.stdout })) { |
| 38 | + if (!data.startsWith('Waiting for graceful termination') && !data.startsWith('Gracefully restarted')) { |
| 39 | + stdout.push(data); |
| 40 | + } |
| 41 | + if (data.startsWith(expectedCompletionLog)) { |
| 42 | + break; |
| 43 | + } |
| 44 | + } |
| 45 | + } finally { |
| 46 | + child.kill(); |
| 47 | + } |
| 48 | + return { stdout, stderr, pid: child.pid }; |
| 49 | +} |
| 50 | + |
| 51 | +tmpdir.refresh(); |
| 52 | + |
| 53 | +describe('watch mode - watch flags', { concurrency: !process.env.TEST_PARALLEL, timeout: 60_000 }, () => { |
| 54 | + it('when multiple `--watch` flags are provided should run as if only one was', async () => { |
| 55 | + const projectDir = tmpdir.resolve('project-multi-flag'); |
| 56 | + mkdirSync(projectDir); |
| 57 | + |
| 58 | + const file = createTmpFile(` |
| 59 | + console.log( |
| 60 | + process.argv.some(arg => arg === '--watch') |
| 61 | + ? 'Error: unexpected --watch args present' |
| 62 | + : 'no --watch args present' |
| 63 | + );`, '.js', projectDir); |
| 64 | + const args = ['--watch', '--watch', file]; |
| 65 | + const { stdout, stderr } = await runNode({ |
| 66 | + file, args, options: { cwd: projectDir } |
| 67 | + }); |
| 68 | + |
| 69 | + assert.strictEqual(stderr, ''); |
| 70 | + assert.deepStrictEqual(stdout, [ |
| 71 | + 'no --watch args present', |
| 72 | + `Completed running ${inspect(file)}. Waiting for file changes before restarting...`, |
| 73 | + ]); |
| 74 | + }); |
| 75 | + |
| 76 | + it('`--watch-path` args without `=` used alongside `--watch` should not make it into the script', async () => { |
| 77 | + const projectDir = tmpdir.resolve('project-watch-watch-path-args'); |
| 78 | + mkdirSync(projectDir); |
| 79 | + |
| 80 | + const file = createTmpFile(` |
| 81 | + console.log( |
| 82 | + process.argv.slice(2).some(arg => arg.endsWith('.js')) |
| 83 | + ? 'some cli args end with .js' |
| 84 | + : 'no cli arg ends with .js' |
| 85 | + );`, '.js', projectDir); |
| 86 | + const args = ['--watch', `--watch-path`, file, file]; |
| 87 | + const { stdout, stderr } = await runNode({ |
| 88 | + file, args, options: { cwd: projectDir } |
| 89 | + }); |
| 90 | + |
| 91 | + assert.strictEqual(stderr, ''); |
| 92 | + assert.deepStrictEqual(stdout, [ |
| 93 | + 'no cli arg ends with .js', |
| 94 | + `Completed running ${inspect(file)}. Waiting for file changes before restarting...`, |
| 95 | + ]); |
| 96 | + }); |
| 97 | +}); |
0 commit comments