Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 1671fe4

Browse files
evanlucasaddaleax
authored andcommittedJul 18, 2017
test: decrease duration of test-cli-syntax
Previously, test/parallel/test-cli-syntax.js was spawning a lot of child processes, but using spawnSync, which made the test run each child process serially. This switches most of the test cases to use exec so that they are asynchronous. Locally, the test went from > 5 seconds to under 2 seconds. PR-URL: #14187 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Refael Ackermann <[email protected]>
1 parent 3fcc7e6 commit 1671fe4

File tree

1 file changed

+36
-31
lines changed

1 file changed

+36
-31
lines changed
 

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

+36-31
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const common = require('../common');
44
const assert = require('assert');
5-
const spawnSync = require('child_process').spawnSync;
5+
const {exec, spawnSync} = require('child_process');
66
const path = require('path');
77

88
const node = process.execPath;
@@ -29,12 +29,13 @@ const notFoundRE = /^Error: Cannot find module/m;
2929
// loop each possible option, `-c` or `--check`
3030
syntaxArgs.forEach(function(args) {
3131
const _args = args.concat(file);
32-
const c = spawnSync(node, _args, {encoding: 'utf8'});
3332

34-
// no output should be produced
35-
assert.strictEqual(c.stdout, '', 'stdout produced');
36-
assert.strictEqual(c.stderr, '', 'stderr produced');
37-
assert.strictEqual(c.status, 0, `code === ${c.status}`);
33+
const cmd = [node, ..._args].join(' ');
34+
exec(cmd, common.mustCall((err, stdout, stderr) => {
35+
assert.ifError(err);
36+
assert.strictEqual(stdout, '', 'stdout produced');
37+
assert.strictEqual(stderr, '', 'stderr produced');
38+
}));
3839
});
3940
});
4041

@@ -50,18 +51,20 @@ const notFoundRE = /^Error: Cannot find module/m;
5051
// loop each possible option, `-c` or `--check`
5152
syntaxArgs.forEach(function(args) {
5253
const _args = args.concat(file);
53-
const c = spawnSync(node, _args, {encoding: 'utf8'});
54+
const cmd = [node, ..._args].join(' ');
55+
exec(cmd, common.mustCall((err, stdout, stderr) => {
56+
assert.strictEqual(err instanceof Error, true);
57+
assert.strictEqual(err.code, 1, `code === ${err.code}`);
5458

55-
// no stdout should be produced
56-
assert.strictEqual(c.stdout, '', 'stdout produced');
59+
// no stdout should be produced
60+
assert.strictEqual(stdout, '', 'stdout produced');
5761

58-
// stderr should include the filename
59-
assert(c.stderr.startsWith(file), "stderr doesn't start with the filename");
62+
// stderr should have a syntax error message
63+
assert(syntaxErrorRE.test(stderr), 'stderr incorrect');
6064

61-
// stderr should have a syntax error message
62-
assert(syntaxErrorRE.test(c.stderr), 'stderr incorrect');
63-
64-
assert.strictEqual(c.status, 1, `code === ${c.status}`);
65+
// stderr should include the filename
66+
assert(stderr.startsWith(file), "stderr doesn't start with the filename");
67+
}));
6568
});
6669
});
6770

@@ -75,15 +78,16 @@ const notFoundRE = /^Error: Cannot find module/m;
7578
// loop each possible option, `-c` or `--check`
7679
syntaxArgs.forEach(function(args) {
7780
const _args = args.concat(file);
78-
const c = spawnSync(node, _args, {encoding: 'utf8'});
79-
80-
// no stdout should be produced
81-
assert.strictEqual(c.stdout, '', 'stdout produced');
81+
const cmd = [node, ..._args].join(' ');
82+
exec(cmd, common.mustCall((err, stdout, stderr) => {
83+
// no stdout should be produced
84+
assert.strictEqual(stdout, '', 'stdout produced');
8285

83-
// stderr should have a module not found error message
84-
assert(notFoundRE.test(c.stderr), 'stderr incorrect');
86+
// stderr should have a module not found error message
87+
assert(notFoundRE.test(stderr), 'stderr incorrect');
8588

86-
assert.strictEqual(c.status, 1, `code === ${c.status}`);
89+
assert.strictEqual(err.code, 1, `code === ${err.code}`);
90+
}));
8791
});
8892
});
8993

@@ -122,14 +126,15 @@ syntaxArgs.forEach(function(args) {
122126
['-c', '--check'].forEach(function(checkFlag) {
123127
['-e', '--eval'].forEach(function(evalFlag) {
124128
const args = [checkFlag, evalFlag, 'foo'];
125-
const c = spawnSync(node, args, {encoding: 'utf8'});
126-
127-
assert(
128-
c.stderr.startsWith(
129-
`${node}: either --check or --eval can be used, not both`
130-
)
131-
);
132-
133-
assert.strictEqual(c.status, 9, `code === ${c.status}`);
129+
const cmd = [node, ...args].join(' ');
130+
exec(cmd, common.mustCall((err, stdout, stderr) => {
131+
assert.strictEqual(err instanceof Error, true);
132+
assert.strictEqual(err.code, 9, `code === ${err.code}`);
133+
assert(
134+
stderr.startsWith(
135+
`${node}: either --check or --eval can be used, not both`
136+
)
137+
);
138+
}));
134139
});
135140
});

0 commit comments

Comments
 (0)
Please sign in to comment.