Skip to content

Commit d366676

Browse files
TrottBethGriggs
authored andcommitted
test: split test-cli-syntax into multiple tests
Split test-cli-syntax into multiple files to improve reliability and/or isolate unreliable test cases. Move test cases back to parallel as appropriate. PR-URL: #24922 Reviewed-By: Bryan English <bryan@bryanenglish.com> Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
1 parent 331f604 commit d366676

9 files changed

+253
-172
lines changed

test/parallel/test-cli-syntax-eval.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const { exec } = require('child_process');
6+
7+
const node = process.execPath;
8+
9+
// should throw if -c and -e flags are both passed
10+
['-c', '--check'].forEach(function(checkFlag) {
11+
['-e', '--eval'].forEach(function(evalFlag) {
12+
const args = [checkFlag, evalFlag, 'foo'];
13+
const cmd = [node, ...args].join(' ');
14+
exec(cmd, common.mustCall((err, stdout, stderr) => {
15+
assert.strictEqual(err instanceof Error, true);
16+
assert.strictEqual(err.code, 9);
17+
assert(
18+
stderr.startsWith(
19+
`${node}: either --check or --eval can be used, not both`
20+
)
21+
);
22+
}));
23+
});
24+
});
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict';
2+
3+
require('../common');
4+
const assert = require('assert');
5+
const { spawnSync } = require('child_process');
6+
7+
const node = process.execPath;
8+
9+
// test both sets of arguments that check syntax
10+
const syntaxArgs = [
11+
['-c'],
12+
['--check']
13+
];
14+
15+
// Match on the name of the `Error` but not the message as it is different
16+
// depending on the JavaScript engine.
17+
const syntaxErrorRE = /^SyntaxError: \b/m;
18+
19+
// Should throw if code piped from stdin with --check has bad syntax
20+
// loop each possible option, `-c` or `--check`
21+
syntaxArgs.forEach(function(args) {
22+
const stdin = 'var foo bar;';
23+
const c = spawnSync(node, args, { encoding: 'utf8', input: stdin });
24+
25+
// stderr should include '[stdin]' as the filename
26+
assert(c.stderr.startsWith('[stdin]'), `${c.stderr} starts with ${stdin}`);
27+
28+
// no stdout or stderr should be produced
29+
assert.strictEqual(c.stdout, '');
30+
31+
// stderr should have a syntax error message
32+
assert(syntaxErrorRE.test(c.stderr), `${syntaxErrorRE} === ${c.stderr}`);
33+
34+
assert.strictEqual(c.status, 1);
35+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict';
2+
3+
require('../common');
4+
const assert = require('assert');
5+
const { spawnSync } = require('child_process');
6+
7+
const node = process.execPath;
8+
9+
// test both sets of arguments that check syntax
10+
const syntaxArgs = [
11+
['-c'],
12+
['--check']
13+
];
14+
15+
// should not execute code piped from stdin with --check
16+
// loop each possible option, `-c` or `--check`
17+
syntaxArgs.forEach(function(args) {
18+
const stdin = 'throw new Error("should not get run");';
19+
const c = spawnSync(node, args, { encoding: 'utf8', input: stdin });
20+
21+
// no stdout or stderr should be produced
22+
assert.strictEqual(c.stdout, '');
23+
assert.strictEqual(c.stderr, '');
24+
25+
assert.strictEqual(c.status, 0);
26+
});

test/sequential/sequential.status

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ prefix sequential
88
# https://github.com/nodejs/node/issues/22336
99
test-gc-http-client: PASS,FLAKY
1010
# https://github.com/nodejs/node/issues/24403
11-
test-cli-syntax: PASS,FLAKY
11+
test-cli-syntax-bad: PASS,FLAKY
12+
test-cli-syntax-file-not-found: PASS,FLAKY
13+
test-cli-syntax-good: PASS,FLAKY
14+
test-cli-syntax-require: PASS,FLAKY
1215

1316
[$system==win32]
1417
# https://github.com/nodejs/node/issues/22327
+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const { exec } = require('child_process');
6+
const fixtures = require('../common/fixtures');
7+
8+
const node = process.execPath;
9+
10+
// test both sets of arguments that check syntax
11+
const syntaxArgs = [
12+
['-c'],
13+
['--check']
14+
];
15+
16+
// Match on the name of the `Error` but not the message as it is different
17+
// depending on the JavaScript engine.
18+
const syntaxErrorRE = /^SyntaxError: \b/m;
19+
20+
// test bad syntax with and without shebang
21+
[
22+
'syntax/bad_syntax.js',
23+
'syntax/bad_syntax',
24+
'syntax/bad_syntax_shebang.js',
25+
'syntax/bad_syntax_shebang'
26+
].forEach(function(file) {
27+
file = fixtures.path(file);
28+
29+
// loop each possible option, `-c` or `--check`
30+
syntaxArgs.forEach(function(args) {
31+
const _args = args.concat(file);
32+
const cmd = [node, ..._args].join(' ');
33+
exec(cmd, common.mustCall((err, stdout, stderr) => {
34+
assert.strictEqual(err instanceof Error, true);
35+
assert.strictEqual(err.code, 1);
36+
37+
// no stdout should be produced
38+
assert.strictEqual(stdout, '');
39+
40+
// stderr should have a syntax error message
41+
assert(syntaxErrorRE.test(stderr), `${syntaxErrorRE} === ${stderr}`);
42+
43+
// stderr should include the filename
44+
assert(stderr.startsWith(file), `${stderr} starts with ${file}`);
45+
}));
46+
});
47+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const { exec } = require('child_process');
6+
const fixtures = require('../common/fixtures');
7+
8+
const node = process.execPath;
9+
10+
// test both sets of arguments that check syntax
11+
const syntaxArgs = [
12+
['-c'],
13+
['--check']
14+
];
15+
16+
const notFoundRE = /^Error: Cannot find module/m;
17+
18+
// test file not found
19+
[
20+
'syntax/file_not_found.js',
21+
'syntax/file_not_found'
22+
].forEach(function(file) {
23+
file = fixtures.path(file);
24+
25+
// loop each possible option, `-c` or `--check`
26+
syntaxArgs.forEach(function(args) {
27+
const _args = args.concat(file);
28+
const cmd = [node, ..._args].join(' ');
29+
exec(cmd, common.mustCall((err, stdout, stderr) => {
30+
// no stdout should be produced
31+
assert.strictEqual(stdout, '');
32+
33+
// stderr should have a module not found error message
34+
assert(notFoundRE.test(stderr), `${notFoundRE} === ${stderr}`);
35+
36+
assert.strictEqual(err.code, 1);
37+
}));
38+
});
39+
});
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const { exec } = require('child_process');
6+
const fixtures = require('../common/fixtures');
7+
8+
const node = process.execPath;
9+
10+
// test both sets of arguments that check syntax
11+
const syntaxArgs = [
12+
['-c'],
13+
['--check']
14+
];
15+
16+
// test good syntax with and without shebang
17+
[
18+
'syntax/good_syntax.js',
19+
'syntax/good_syntax',
20+
'syntax/good_syntax_shebang.js',
21+
'syntax/good_syntax_shebang',
22+
'syntax/illegal_if_not_wrapped.js'
23+
].forEach(function(file) {
24+
file = fixtures.path(file);
25+
26+
// loop each possible option, `-c` or `--check`
27+
syntaxArgs.forEach(function(args) {
28+
const _args = args.concat(file);
29+
30+
const cmd = [node, ..._args].join(' ');
31+
exec(cmd, common.mustCall((err, stdout, stderr) => {
32+
if (err) {
33+
console.log('-- stdout --');
34+
console.log(stdout);
35+
console.log('-- stderr --');
36+
console.log(stderr);
37+
}
38+
assert.ifError(err);
39+
assert.strictEqual(stdout, '');
40+
assert.strictEqual(stderr, '');
41+
}));
42+
});
43+
});
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const { exec } = require('child_process');
6+
const fixtures = require('../common/fixtures');
7+
8+
const node = process.execPath;
9+
10+
// Match on the name of the `Error` but not the message as it is different
11+
// depending on the JavaScript engine.
12+
const syntaxErrorRE = /^SyntaxError: \b/m;
13+
14+
// should work with -r flags
15+
['-c', '--check'].forEach(function(checkFlag) {
16+
['-r', '--require'].forEach(function(requireFlag) {
17+
const preloadFile = fixtures.path('no-wrapper.js');
18+
const file = fixtures.path('syntax', 'illegal_if_not_wrapped.js');
19+
const args = [requireFlag, preloadFile, checkFlag, file];
20+
const cmd = [node, ...args].join(' ');
21+
exec(cmd, common.mustCall((err, stdout, stderr) => {
22+
assert.strictEqual(err instanceof Error, true);
23+
assert.strictEqual(err.code, 1);
24+
25+
// no stdout should be produced
26+
assert.strictEqual(stdout, '');
27+
28+
// stderr should have a syntax error message
29+
assert(syntaxErrorRE.test(stderr), `${syntaxErrorRE} === ${stderr}`);
30+
31+
// stderr should include the filename
32+
assert(stderr.startsWith(file), `${stderr} starts with ${file}`);
33+
}));
34+
});
35+
});

0 commit comments

Comments
 (0)