11'use strict' ;
22if ( 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.
45 console . log ( 'Loaded as a module, exiting with status code 42.' ) ;
56 process . exit ( 42 ) ;
67}
@@ -9,114 +10,114 @@ const common = require('../common');
910const assert = require ( 'assert' ) ;
1011const child = require ( 'child_process' ) ;
1112const path = require ( 'path' ) ;
12- const nodejs = '"' + process . execPath + '"' ;
13+ const nodejs = `" ${ process . execPath } "` ;
1314
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+ } ) ) ;
1421
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 ) => {
3025 assert . ifError ( err ) ;
3126 assert . strictEqual ( stdout , '' ) ;
3227 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+ } ) ) ;
5245} ) ;
5346
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+ }
6261
6362// 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 ) => {
6665 assert . ifError ( err ) ;
6766 assert . strictEqual ( stderr , '' ) ;
6867 assert . strictEqual ( stdout . trim ( ) , require ( 'os' ) . platform ( ) ) ;
69- } ) ;
68+ } ) ) ;
7069
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 ) => {
7473 assert . strictEqual ( err . code , 42 ) ;
7574 assert . strictEqual (
7675 stdout , 'Loaded as a module, exiting with status code 42.\n' ) ;
7776 assert . strictEqual ( stderr , '' ) ;
78- } ) ;
77+ } ) ) ;
7978
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 ) => {
8281 assert . strictEqual ( err . code , 9 ) ;
8382 assert . strictEqual ( stdout , '' ) ;
8483 assert . strictEqual ( stderr . trim ( ) ,
8584 `${ process . execPath } : -e requires an argument` ) ;
8685} ) ) ;
8786
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 ) => {
9089 assert . ifError ( err ) ;
9190 assert . strictEqual ( stdout , '' ) ;
9291 assert . strictEqual ( stderr , '' ) ;
93- } ) ;
92+ } ) ) ;
9493
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+ } ) ) ;
102100
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 ) => {
105103 assert . ifError ( err ) ;
106104 assert . strictEqual (
107105 stdout , "[ '--use-strict', '-p', 'process.execArgv' ]\n"
108106 ) ;
109107 assert . strictEqual ( stderr , '' ) ;
110- } ) ;
108+ } ) ) ;
111109
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+ }
120121
121122// Regression test for https://github.com/nodejs/node/issues/8534.
122123{
0 commit comments