|
1 | 1 | 'use strict'; |
2 | 2 | if (module.parent) { |
3 | | - // signal we've been loaded as a module |
| 3 | + // Signal we've been loaded as a module. |
| 4 | + // The following console.log() is part of the test. |
4 | 5 | console.log('Loaded as a module, exiting with status code 42.'); |
5 | 6 | process.exit(42); |
6 | 7 | } |
@@ -9,114 +10,114 @@ const common = require('../common'); |
9 | 10 | const assert = require('assert'); |
10 | 11 | const child = require('child_process'); |
11 | 12 | const path = require('path'); |
12 | | -const nodejs = '"' + process.execPath + '"'; |
| 13 | +const nodejs = `"${process.execPath}"`; |
13 | 14 |
|
| 15 | +// Assert that nothing is written to stdout. |
| 16 | +child.exec(`${nodejs} --eval 42`, common.mustCall((err, stdout, stderr) => { |
| 17 | + assert.ifError(err); |
| 18 | + assert.strictEqual(stdout, ''); |
| 19 | + assert.strictEqual(stderr, ''); |
| 20 | +})); |
14 | 21 |
|
15 | | -// replace \ by / because windows uses backslashes in paths, but they're still |
16 | | -// interpreted as the escape character when put between quotes. |
17 | | -const filename = __filename.replace(/\\/g, '/'); |
18 | | - |
19 | | -// assert that nothing is written to stdout |
20 | | -child.exec(nodejs + ' --eval 42', |
21 | | - function(err, stdout, stderr) { |
22 | | - assert.ifError(err); |
23 | | - assert.strictEqual(stdout, ''); |
24 | | - assert.strictEqual(stderr, ''); |
25 | | - }); |
26 | | - |
27 | | -// assert that "42\n" is written to stderr |
28 | | -child.exec(nodejs + ' --eval "console.error(42)"', |
29 | | - function(err, stdout, stderr) { |
| 22 | +// Assert that "42\n" is written to stderr. |
| 23 | +child.exec(`${nodejs} --eval "console.error(42)"`, |
| 24 | + common.mustCall((err, stdout, stderr) => { |
30 | 25 | assert.ifError(err); |
31 | 26 | assert.strictEqual(stdout, ''); |
32 | 27 | assert.strictEqual(stderr, '42\n'); |
33 | | - }); |
34 | | - |
35 | | -// assert that the expected output is written to stdout |
36 | | -['--print', '-p -e', '-pe', '-p'].forEach(function(s) { |
37 | | - const cmd = nodejs + ' ' + s + ' '; |
38 | | - |
39 | | - child.exec(cmd + '42', |
40 | | - function(err, stdout, stderr) { |
41 | | - assert.ifError(err); |
42 | | - assert.strictEqual(stdout, '42\n'); |
43 | | - assert.strictEqual(stderr, ''); |
44 | | - }); |
45 | | - |
46 | | - child.exec(cmd + "'[]'", common.mustCall( |
47 | | - function(err, stdout, stderr) { |
48 | | - assert.ifError(err); |
49 | | - assert.strictEqual(stdout, '[]\n'); |
50 | | - assert.strictEqual(stderr, ''); |
51 | | - })); |
| 28 | + })); |
| 29 | + |
| 30 | +// Assert that the expected output is written to stdout. |
| 31 | +['--print', '-p -e', '-pe', '-p'].forEach((s) => { |
| 32 | + const cmd = `${nodejs} ${s} `; |
| 33 | + |
| 34 | + child.exec(`${cmd}42`, common.mustCall((err, stdout, stderr) => { |
| 35 | + assert.ifError(err); |
| 36 | + assert.strictEqual(stdout, '42\n'); |
| 37 | + assert.strictEqual(stderr, ''); |
| 38 | + })); |
| 39 | + |
| 40 | + child.exec(`${cmd} '[]'`, common.mustCall((err, stdout, stderr) => { |
| 41 | + assert.ifError(err); |
| 42 | + assert.strictEqual(stdout, '[]\n'); |
| 43 | + assert.strictEqual(stderr, ''); |
| 44 | + })); |
52 | 45 | }); |
53 | 46 |
|
54 | | -// assert that module loading works |
55 | | -child.exec(nodejs + ' --eval "require(\'' + filename + '\')"', |
56 | | - function(err, stdout, stderr) { |
57 | | - assert.strictEqual(err.code, 42); |
58 | | - assert.strictEqual( |
59 | | - stdout, 'Loaded as a module, exiting with status code 42.\n'); |
60 | | - assert.strictEqual(stderr, ''); |
61 | | - }); |
| 47 | +// Assert that module loading works. |
| 48 | +{ |
| 49 | + // Replace \ by / because Windows uses backslashes in paths, but they're still |
| 50 | + // interpreted as the escape character when put between quotes. |
| 51 | + const filename = __filename.replace(/\\/g, '/'); |
| 52 | + |
| 53 | + child.exec(`${nodejs} --eval "require('${filename}')"`, |
| 54 | + common.mustCall((err, stdout, stderr) => { |
| 55 | + assert.strictEqual(err.code, 42); |
| 56 | + assert.strictEqual( |
| 57 | + stdout, 'Loaded as a module, exiting with status code 42.\n'); |
| 58 | + assert.strictEqual(stderr, ''); |
| 59 | + })); |
| 60 | +} |
62 | 61 |
|
63 | 62 | // Check that builtin modules are pre-defined. |
64 | | -child.exec(nodejs + ' --print "os.platform()"', |
65 | | - function(err, stdout, stderr) { |
| 63 | +child.exec(`${nodejs} --print "os.platform()"`, |
| 64 | + common.mustCall((err, stdout, stderr) => { |
66 | 65 | assert.ifError(err); |
67 | 66 | assert.strictEqual(stderr, ''); |
68 | 67 | assert.strictEqual(stdout.trim(), require('os').platform()); |
69 | | - }); |
| 68 | + })); |
70 | 69 |
|
71 | | -// module path resolve bug, regression test |
72 | | -child.exec(nodejs + ' --eval "require(\'./test/parallel/test-cli-eval.js\')"', |
73 | | - function(err, stdout, stderr) { |
| 70 | +// Module path resolve bug regression test. |
| 71 | +child.exec(`${nodejs} --eval "require('./test/parallel/test-cli-eval.js')"`, |
| 72 | + common.mustCall((err, stdout, stderr) => { |
74 | 73 | assert.strictEqual(err.code, 42); |
75 | 74 | assert.strictEqual( |
76 | 75 | stdout, 'Loaded as a module, exiting with status code 42.\n'); |
77 | 76 | assert.strictEqual(stderr, ''); |
78 | | - }); |
| 77 | + })); |
79 | 78 |
|
80 | | -// Missing argument should not crash |
81 | | -child.exec(nodejs + ' -e', common.mustCall(function(err, stdout, stderr) { |
| 79 | +// Missing argument should not crash. |
| 80 | +child.exec(`${nodejs} -e`, common.mustCall((err, stdout, stderr) => { |
82 | 81 | assert.strictEqual(err.code, 9); |
83 | 82 | assert.strictEqual(stdout, ''); |
84 | 83 | assert.strictEqual(stderr.trim(), |
85 | 84 | `${process.execPath}: -e requires an argument`); |
86 | 85 | })); |
87 | 86 |
|
88 | | -// empty program should do nothing |
89 | | -child.exec(nodejs + ' -e ""', function(err, stdout, stderr) { |
| 87 | +// Empty program should do nothing. |
| 88 | +child.exec(`${nodejs} -e ""`, common.mustCall((err, stdout, stderr) => { |
90 | 89 | assert.ifError(err); |
91 | 90 | assert.strictEqual(stdout, ''); |
92 | 91 | assert.strictEqual(stderr, ''); |
93 | | -}); |
| 92 | +})); |
94 | 93 |
|
95 | | -// "\\-42" should be interpreted as an escaped expression, not a switch |
96 | | -child.exec(nodejs + ' -p "\\-42"', |
97 | | - function(err, stdout, stderr) { |
98 | | - assert.ifError(err); |
99 | | - assert.strictEqual(stdout, '-42\n'); |
100 | | - assert.strictEqual(stderr, ''); |
101 | | - }); |
| 94 | +// "\\-42" should be interpreted as an escaped expression, not a switch. |
| 95 | +child.exec(`${nodejs} -p "\\-42"`, common.mustCall((err, stdout, stderr) => { |
| 96 | + assert.ifError(err); |
| 97 | + assert.strictEqual(stdout, '-42\n'); |
| 98 | + assert.strictEqual(stderr, ''); |
| 99 | +})); |
102 | 100 |
|
103 | | -child.exec(nodejs + ' --use-strict -p process.execArgv', |
104 | | - function(err, stdout, stderr) { |
| 101 | +child.exec(`${nodejs} --use-strict -p process.execArgv`, |
| 102 | + common.mustCall((err, stdout, stderr) => { |
105 | 103 | assert.ifError(err); |
106 | 104 | assert.strictEqual( |
107 | 105 | stdout, "[ '--use-strict', '-p', 'process.execArgv' ]\n" |
108 | 106 | ); |
109 | 107 | assert.strictEqual(stderr, ''); |
110 | | - }); |
| 108 | + })); |
111 | 109 |
|
112 | | -// Regression test for https://github.com/nodejs/node/issues/3574 |
113 | | -const emptyFile = path.join(common.fixturesDir, 'empty.js'); |
114 | | -child.exec(nodejs + ` -e 'require("child_process").fork("${emptyFile}")'`, |
115 | | - function(err, stdout, stderr) { |
116 | | - assert.ifError(err); |
117 | | - assert.strictEqual(stdout, ''); |
118 | | - assert.strictEqual(stderr, ''); |
119 | | - }); |
| 110 | +// Regression test for https://github.com/nodejs/node/issues/3574. |
| 111 | +{ |
| 112 | + const emptyFile = path.join(common.fixturesDir, 'empty.js'); |
| 113 | + |
| 114 | + child.exec(`${nodejs} -e 'require("child_process").fork("${emptyFile}")'`, |
| 115 | + common.mustCall((err, stdout, stderr) => { |
| 116 | + assert.ifError(err); |
| 117 | + assert.strictEqual(stdout, ''); |
| 118 | + assert.strictEqual(stderr, ''); |
| 119 | + })); |
| 120 | +} |
120 | 121 |
|
121 | 122 | // Regression test for https://github.com/nodejs/node/issues/8534. |
122 | 123 | { |
|