-
-
Notifications
You must be signed in to change notification settings - Fork 35.5k
Expand file tree
/
Copy pathtest-watch-mode-watch-flags.mjs
More file actions
145 lines (127 loc) Β· 4.71 KB
/
test-watch-mode-watch-flags.mjs
File metadata and controls
145 lines (127 loc) Β· 4.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import * as common from '../common/index.mjs';
import tmpdir from '../common/tmpdir.js';
import assert from 'node:assert';
import path from 'node:path';
import { execPath } from 'node:process';
import { describe, it } from 'node:test';
import { spawn } from 'node:child_process';
import { writeFileSync, mkdirSync } from 'node:fs';
import { inspect } from 'node:util';
import { createInterface } from 'node:readline';
if (common.isIBMi)
common.skip('IBMi does not support `fs.watch()`');
let tmpFiles = 0;
function createTmpFile(content, ext, basename) {
const file = path.join(basename, `${tmpFiles++}${ext}`);
writeFileSync(file, content);
return file;
}
async function runNode({
args,
expectedCompletionLog = 'Completed running',
options = {},
}) {
const child = spawn(execPath, args, { encoding: 'utf8', stdio: 'pipe', ...options });
let stderr = '';
const stdout = [];
child.stderr.on('data', (data) => {
stderr += data;
});
try {
// Break the chunks into lines
for await (const data of createInterface({ input: child.stdout })) {
if (!data.startsWith('Waiting for graceful termination') && !data.startsWith('Gracefully restarted')) {
stdout.push(data);
}
if (data.startsWith(expectedCompletionLog)) {
break;
}
}
} finally {
child.kill();
}
return { stdout, stderr, pid: child.pid };
}
tmpdir.refresh();
describe('watch mode - watch flags', { concurrency: !process.env.TEST_PARALLEL, timeout: 60_000 }, () => {
it('when multiple `--watch` flags are provided should run as if only one was', async () => {
const projectDir = tmpdir.resolve('project-multi-flag');
mkdirSync(projectDir);
const file = createTmpFile(`
console.log(
process.argv.some(arg => arg === '--watch')
? 'Error: unexpected --watch args present'
: 'no --watch args present'
);`, '.js', projectDir);
const args = ['--watch', '--watch', file];
const { stdout, stderr } = await runNode({
file, args, options: { cwd: projectDir },
});
assert.strictEqual(stderr, '');
assert.deepStrictEqual(stdout, [
'no --watch args present',
`Completed running ${inspect(file)}. Waiting for file changes before restarting...`,
]);
});
it('`--watch-path` args without `=` used alongside `--watch` should not make it into the script', async () => {
const projectDir = tmpdir.resolve('project-watch-watch-path-args');
mkdirSync(projectDir);
const file = createTmpFile(`
console.log(
process.argv.slice(2).some(arg => arg.endsWith('.js'))
? 'some cli args end with .js'
: 'no cli arg ends with .js'
);`, '.js', projectDir);
const args = ['--watch', `--watch-path`, file, file];
const { stdout, stderr } = await runNode({
file, args, options: { cwd: projectDir },
});
assert.strictEqual(stderr, '');
assert.deepStrictEqual(stdout, [
'no cli arg ends with .js',
`Completed running ${inspect(file)}. Waiting for file changes before restarting...`,
]);
});
it('exposes watch flags through process.execArgv inside the watched script', async () => {
const projectDir = tmpdir.resolve('project-watch-exec-argv');
mkdirSync(projectDir);
const file = createTmpFile(`
console.log(JSON.stringify(process.execArgv));
`, '.js', projectDir);
const watchPath = path.join(projectDir, 'template.html');
writeFileSync(watchPath, '');
async function assertExecArgv(args, expectedSubsequences) {
const { stdout, stderr } = await runNode({
args, options: { cwd: projectDir },
});
assert.strictEqual(stderr, '');
const execArgvLine = stdout[0];
const execArgv = JSON.parse(execArgvLine);
assert.ok(Array.isArray(execArgv));
const matched = expectedSubsequences.some((expectedSeq) => {
for (let i = 0; i <= execArgv.length - expectedSeq.length; i++) {
let ok = true;
for (let j = 0; j < expectedSeq.length; j++) {
if (execArgv[i + j] !== expectedSeq[j]) {
ok = false;
break;
}
}
if (ok) return true;
}
return false;
});
assert.ok(matched,
`execArgv (${execArgv}) does not contain any expected sequence (${expectedSubsequences.map((seq) => `[${seq}]`).join(', ')})`);
assert.match(stdout.at(-1), /^Completed running/);
}
await assertExecArgv(['--watch', file], [['--watch']]);
await assertExecArgv(['--watch-path=template.html', file], [['--watch-path=template.html']]);
await assertExecArgv(
['--watch-path', 'template.html', file],
[
['--watch-path', 'template.html'],
['--watch-path=template.html'],
]);
});
});