| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -11,12 +11,8 @@ const childProcess = require('child_process'); | |||
| 11 | 11 | ||
| 12 | 12 | const nodeBinary = process.argv[0]; | |
| 13 | 13 | ||
| 14 | - const preloadOption = (preloads) => { | ||
| 15 | - let option = ''; | ||
| 16 | - preloads.forEach(function(preload, index) { | ||
| 17 | - option += `-r "${preload}" `; | ||
| 18 | - }); | ||
| 19 | - return option; | ||
| 14 | + const preloadOption = (...preloads) => { | ||
| 15 | + return preloads.flatMap((preload) => ['-r', preload]); | ||
| 20 | 16 | }; | |
| 21 | 17 | ||
| 22 | 18 | const fixtureA = fixtures.path('printA.js'); | |
@@ -34,75 +30,52 @@ assert(!module.isPreloading); | |||
| 34 | 30 | ||
| 35 | 31 | // Test that module.isPreloading is set in preloaded module | |
| 36 | 32 | // Test preloading a single module works | |
| 37 | - childProcess.exec( | ||
| 38 | - `"${nodeBinary}" ${preloadOption([fixtureIsPreloading])} "${fixtureB}"`, | ||
| 39 | - function(err, stdout, stderr) { | ||
| 40 | - assert.ifError(err); | ||
| 41 | - assert.strictEqual(stdout, 'B\n'); | ||
| 42 | - }); | ||
| 33 | + common.spawnPromisified(nodeBinary, [...preloadOption(fixtureIsPreloading), fixtureB]).then(common.mustCall( | ||
| 34 | + ({ stdout }) => { | ||
| 35 | + assert.strictEqual(stdout.trim(), 'B'); | ||
| 36 | + } | ||
| 37 | + )); | ||
| 43 | 38 | ||
| 44 | 39 | // Test preloading a single module works | |
| 45 | - childProcess.exec(`"${nodeBinary}" ${preloadOption([fixtureA])} "${fixtureB}"`, | ||
| 46 | - function(err, stdout, stderr) { | ||
| 47 | - assert.ifError(err); | ||
| 48 | - assert.strictEqual(stdout, 'A\nB\n'); | ||
| 49 | - }); | ||
| 40 | + common.spawnPromisified(nodeBinary, [...preloadOption(fixtureA), fixtureB]).then(common.mustCall( | ||
| 41 | + ({ stdout }) => { | ||
| 42 | + assert.strictEqual(stdout, 'A\nB\n'); | ||
| 43 | + })); | ||
| 50 | 44 | ||
| 51 | 45 | // Test preloading multiple modules works | |
| 52 | - childProcess.exec( | ||
| 53 | - `"${nodeBinary}" ${preloadOption([fixtureA, fixtureB])} "${fixtureC}"`, | ||
| 54 | - function(err, stdout, stderr) { | ||
| 55 | - assert.ifError(err); | ||
| 46 | + common.spawnPromisified(nodeBinary, [...preloadOption(fixtureA, fixtureB), fixtureC]).then(common.mustCall( | ||
| 47 | + ({ stdout }) => { | ||
| 56 | 48 | assert.strictEqual(stdout, 'A\nB\nC\n'); | |
| 57 | 49 | } | |
| 58 | - ); | ||
| 50 | + )); | ||
| 59 | 51 | ||
| 60 | 52 | // Test that preloading a throwing module aborts | |
| 61 | - childProcess.exec( | ||
| 62 | - `"${nodeBinary}" ${preloadOption([fixtureA, fixtureThrows])} "${fixtureB}"`, | ||
| 63 | - function(err, stdout, stderr) { | ||
| 64 | - if (err) { | ||
| 65 | - assert.strictEqual(stdout, 'A\n'); | ||
| 66 | - } else { | ||
| 67 | - throw new Error('Preload should have failed'); | ||
| 68 | - } | ||
| 53 | + common.spawnPromisified(nodeBinary, [...preloadOption(fixtureA, fixtureThrows), fixtureB]).then(common.mustCall( | ||
| 54 | + ({ code, stdout }) => { | ||
| 55 | + assert.strictEqual(stdout, 'A\n'); | ||
| 56 | + assert.strictEqual(code, 1); | ||
| 69 | 57 | } | |
| 70 | - ); | ||
| 58 | + )); | ||
| 71 | 59 | ||
| 72 | 60 | // Test that preload can be used with --eval | |
| 73 | - childProcess.exec( | ||
| 74 | - `"${nodeBinary}" ${preloadOption([fixtureA])}-e "console.log('hello');"`, | ||
| 75 | - function(err, stdout, stderr) { | ||
| 76 | - assert.ifError(err); | ||
| 61 | + common.spawnPromisified(nodeBinary, [...preloadOption(fixtureA), '-e', 'console.log("hello");']).then(common.mustCall( | ||
| 62 | + ({ stdout }) => { | ||
| 77 | 63 | assert.strictEqual(stdout, 'A\nhello\n'); | |
| 78 | 64 | } | |
| 79 | - ); | ||
| 65 | + )); | ||
| 80 | 66 | ||
| 81 | 67 | // Test that preload can be used with --frozen-intrinsics | |
| 82 | - childProcess.exec( | ||
| 83 | - `"${nodeBinary}" --frozen-intrinsics ${ | ||
| 84 | - preloadOption([fixtureE]) | ||
| 85 | - } ${ | ||
| 86 | - fixtureF | ||
| 87 | - }`, | ||
| 88 | - function(err, stdout) { | ||
| 89 | - assert.ifError(err); | ||
| 90 | - assert.strictEqual(stdout, 'smoosh\n'); | ||
| 91 | - } | ||
| 92 | - ); | ||
| 93 | - childProcess.exec( | ||
| 94 | - `"${ | ||
| 95 | - nodeBinary | ||
| 96 | - }" --frozen-intrinsics ${ | ||
| 97 | - preloadOption([fixtureE]) | ||
| 98 | - } ${ | ||
| 99 | - fixtureG | ||
| 100 | - } ${fixtureF}`, | ||
| 101 | - function(err, stdout) { | ||
| 102 | - assert.ifError(err); | ||
| 68 | + common.spawnPromisified(nodeBinary, ['--frozen-intrinsics', ...preloadOption(fixtureE), fixtureF]).then(common.mustCall( | ||
| 69 | + ({ stdout }) => { | ||
| 103 | 70 | assert.strictEqual(stdout, 'smoosh\n'); | |
| 104 | 71 | } | |
| 105 | - ); | ||
| 72 | + )); | ||
| 73 | + common.spawnPromisified(nodeBinary, ['--frozen-intrinsics', ...preloadOption(fixtureE), fixtureG, fixtureF]) | ||
| 74 | + .then(common.mustCall( | ||
| 75 | + ({ stdout }) => { | ||
| 76 | + assert.strictEqual(stdout, 'smoosh\n'); | ||
| 77 | + } | ||
| 78 | + )); | ||
| 106 | 79 | ||
| 107 | 80 | // Test that preload can be used with stdin | |
| 108 | 81 | const stdinProc = childProcess.spawn( | |
@@ -143,61 +116,62 @@ replProc.on('close', function(code) { | |||
| 143 | 116 | ||
| 144 | 117 | // Test that preload placement at other points in the cmdline | |
| 145 | 118 | // also test that duplicated preload only gets loaded once | |
| 146 | - childProcess.exec( | ||
| 147 | - `"${nodeBinary}" ${preloadOption([fixtureA])}-e "console.log('hello');" ${ | ||
| 148 | - preloadOption([fixtureA, fixtureB])}`, | ||
| 149 | - function(err, stdout, stderr) { | ||
| 150 | - assert.ifError(err); | ||
| 119 | + common.spawnPromisified(nodeBinary, [ | ||
| 120 | + ...preloadOption(fixtureA), | ||
| 121 | + '-e', 'console.log("hello");', | ||
| 122 | + ...preloadOption(fixtureA, fixtureB), | ||
| 123 | + ]).then(common.mustCall( | ||
| 124 | + ({ stdout }) => { | ||
| 151 | 125 | assert.strictEqual(stdout, 'A\nB\nhello\n'); | |
| 152 | 126 | } | |
| 153 | - ); | ||
| 127 | + )); | ||
| 154 | 128 | ||
| 155 | 129 | // Test that preload works with -i | |
| 156 | - const interactive = childProcess.exec( | ||
| 157 | - `"${nodeBinary}" ${preloadOption([fixtureD])}-i`, | ||
| 158 | - common.mustSucceed((stdout, stderr) => { | ||
| 159 | - assert.ok(stdout.endsWith("> 'test'\n> ")); | ||
| 160 | - }) | ||
| 161 | - ); | ||
| 130 | + const interactive = childProcess.spawn(nodeBinary, [...preloadOption(fixtureD), '-i']); | ||
| 131 | + | ||
| 132 | + { | ||
| 133 | + const stdout = []; | ||
| 134 | + interactive.stdout.on('data', (chunk) => { | ||
| 135 | + stdout.push(chunk); | ||
| 136 | + }); | ||
| 137 | + interactive.on('close', common.mustCall(() => { | ||
| 138 | + assert.match(Buffer.concat(stdout).toString('utf8'), /> 'test'\r?\n> $/); | ||
| 139 | + })); | ||
| 140 | + } | ||
| 162 | 141 | ||
| 163 | 142 | interactive.stdin.write('a\n'); | |
| 164 | 143 | interactive.stdin.write('process.exit()\n'); | |
| 165 | 144 | ||
| 166 | - childProcess.exec( | ||
| 167 | - `"${nodeBinary}" --require "${fixtures.path('cluster-preload.js')}" "${ | ||
| 168 | - fixtures.path('cluster-preload-test.js')}"`, | ||
| 169 | - function(err, stdout, stderr) { | ||
| 170 | - assert.ifError(err); | ||
| 145 | + common.spawnPromisified(nodeBinary, | ||
| 146 | + ['--require', fixtures.path('cluster-preload.js'), fixtures.path('cluster-preload-test.js')]) | ||
| 147 | + .then(common.mustCall(({ stdout }) => { | ||
| 171 | 148 | assert.match(stdout, /worker terminated with code 43/); | |
| 172 | - } | ||
| 173 | - ); | ||
| 149 | + })); | ||
| 174 | 150 | ||
| 175 | 151 | // Test that preloading with a relative path works | |
| 176 | - childProcess.exec( | ||
| 177 | - `"${nodeBinary}" ${preloadOption(['./printA.js'])} "${fixtureB}"`, | ||
| 178 | - { cwd: fixtures.fixturesDir }, | ||
| 179 | - common.mustSucceed((stdout, stderr) => { | ||
| 152 | + common.spawnPromisified(nodeBinary, | ||
| 153 | + [...preloadOption('./printA.js'), fixtureB], | ||
| 154 | + { cwd: fixtures.fixturesDir }).then(common.mustCall( | ||
| 155 | + ({ stdout }) => { | ||
| 180 | 156 | assert.strictEqual(stdout, 'A\nB\n'); | |
| 181 | 157 | }) | |
| 182 | 158 | ); | |
| 183 | 159 | if (common.isWindows) { | |
| 184 | 160 | // https://github.com/nodejs/node/issues/21918 | |
| 185 | - childProcess.exec( | ||
| 186 | - `"${nodeBinary}" ${preloadOption(['.\\printA.js'])} "${fixtureB}"`, | ||
| 187 | - { cwd: fixtures.fixturesDir }, | ||
| 188 | - common.mustSucceed((stdout, stderr) => { | ||
| 161 | + common.spawnPromisified(nodeBinary, | ||
| 162 | + [...preloadOption('.\\printA.js'), fixtureB], | ||
| 163 | + { cwd: fixtures.fixturesDir }).then(common.mustCall( | ||
| 164 | + ({ stdout }) => { | ||
| 189 | 165 | assert.strictEqual(stdout, 'A\nB\n'); | |
| 190 | 166 | }) | |
| 191 | 167 | ); | |
| 192 | 168 | } | |
| 193 | 169 | ||
| 194 | 170 | // https://github.com/nodejs/node/issues/1691 | |
| 195 | - childProcess.exec( | ||
| 196 | - `"${nodeBinary}" --require ` + | ||
| 197 | - `"${fixtures.path('cluster-preload.js')}" cluster-preload-test.js`, | ||
| 198 | - { cwd: fixtures.fixturesDir }, | ||
| 199 | - function(err, stdout, stderr) { | ||
| 200 | - assert.ifError(err); | ||
| 171 | + common.spawnPromisified(nodeBinary, | ||
| 172 | + ['--require', fixtures.path('cluster-preload.js'), 'cluster-preload-test.js'], | ||
| 173 | + { cwd: fixtures.fixturesDir }).then(common.mustCall( | ||
| 174 | + ({ stdout }) => { | ||
| 201 | 175 | assert.match(stdout, /worker terminated with code 43/); | |
| 202 | 176 | } | |
| 203 | - ); | ||
| 177 | + )); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,15 +4,13 @@ const common = require('../common'); | |||
| 4 | 4 | const assert = require('assert'); | |
| 5 | 5 | const timers = require('timers'); | |
| 6 | 6 | const { promisify } = require('util'); | |
| 7 | - const child_process = require('child_process'); | ||
| 8 | 7 | ||
| 9 | 8 | const { getEventListeners } = require('events'); | |
| 10 | 9 | const { NodeEventTarget } = require('internal/event_target'); | |
| 11 | 10 | ||
| 12 | 11 | const timerPromises = require('timers/promises'); | |
| 13 | 12 | ||
| 14 | 13 | const setPromiseImmediate = promisify(timers.setImmediate); | |
| 15 | - const exec = promisify(child_process.exec); | ||
| 16 | 14 | ||
| 17 | 15 | assert.strictEqual(setPromiseImmediate, timerPromises.setImmediate); | |
| 18 | 16 | ||
@@ -91,9 +89,9 @@ process.on('multipleResolves', common.mustNotCall()); | |||
| 91 | 89 | } | |
| 92 | 90 | ||
| 93 | 91 | { | |
| 94 | - exec(`${process.execPath} -pe "const assert = require('assert');` + | ||
| 92 | + common.spawnPromisified(process.execPath, ['-pe', "const assert = require('assert');" + | ||
| 95 | 93 | 'require(\'timers/promises\').setImmediate(null, { ref: false }).' + | |
| 96 | - 'then(assert.fail)"').then(common.mustCall(({ stderr }) => { | ||
| 94 | + 'then(assert.fail)']).then(common.mustCall(({ stderr }) => { | ||
| 97 | 95 | assert.strictEqual(stderr, ''); | |
| 98 | 96 | })); | |
| 99 | 97 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,15 +4,13 @@ const common = require('../common'); | |||
| 4 | 4 | const assert = require('assert'); | |
| 5 | 5 | const timers = require('timers'); | |
| 6 | 6 | const { promisify } = require('util'); | |
| 7 | - const child_process = require('child_process'); | ||
| 8 | 7 | ||
| 9 | 8 | const { getEventListeners } = require('events'); | |
| 10 | 9 | const { NodeEventTarget } = require('internal/event_target'); | |
| 11 | 10 | ||
| 12 | 11 | const timerPromises = require('timers/promises'); | |
| 13 | 12 | ||
| 14 | 13 | const setPromiseTimeout = promisify(timers.setTimeout); | |
| 15 | - const exec = promisify(child_process.exec); | ||
| 16 | 14 | ||
| 17 | 15 | const { setInterval } = timerPromises; | |
| 18 | 16 | ||
@@ -154,11 +152,11 @@ process.on('multipleResolves', common.mustNotCall()); | |||
| 154 | 152 | } | |
| 155 | 153 | ||
| 156 | 154 | { | |
| 157 | - exec(`${process.execPath} -pe "const assert = require('assert');` + | ||
| 155 | + common.spawnPromisified(process.execPath, ['-pe', "const assert = require('assert');" + | ||
| 158 | 156 | 'const interval = require(\'timers/promises\')' + | |
| 159 | 157 | '.setInterval(1000, null, { ref: false });' + | |
| 160 | 158 | 'interval[Symbol.asyncIterator]().next()' + | |
| 161 | - '.then(assert.fail)"').then(common.mustCall(({ stderr }) => { | ||
| 159 | + '.then(assert.fail)']).then(common.mustCall(({ stderr }) => { | ||
| 162 | 160 | assert.strictEqual(stderr, ''); | |
| 163 | 161 | })); | |
| 164 | 162 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,15 +4,13 @@ const common = require('../common'); | |||
| 4 | 4 | const assert = require('assert'); | |
| 5 | 5 | const timers = require('timers'); | |
| 6 | 6 | const { promisify } = require('util'); | |
| 7 | - const child_process = require('child_process'); | ||
| 8 | 7 | ||
| 9 | 8 | const { getEventListeners } = require('events'); | |
| 10 | 9 | const { NodeEventTarget } = require('internal/event_target'); | |
| 11 | 10 | ||
| 12 | 11 | const timerPromises = require('timers/promises'); | |
| 13 | 12 | ||
| 14 | 13 | const setPromiseTimeout = promisify(timers.setTimeout); | |
| 15 | - const exec = promisify(child_process.exec); | ||
| 16 | 14 | ||
| 17 | 15 | assert.strictEqual(setPromiseTimeout, timerPromises.setTimeout); | |
| 18 | 16 | ||
@@ -91,9 +89,9 @@ process.on('multipleResolves', common.mustNotCall()); | |||
| 91 | 89 | } | |
| 92 | 90 | ||
| 93 | 91 | { | |
| 94 | - exec(`${process.execPath} -pe "const assert = require('assert');` + | ||
| 92 | + common.spawnPromisified(process.execPath, ['-pe', 'const assert = require(\'assert\');' + | ||
| 95 | 93 | 'require(\'timers/promises\').setTimeout(1000, null, { ref: false }).' + | |
| 96 | - 'then(assert.fail)"').then(common.mustCall(({ stderr }) => { | ||
| 94 | + 'then(assert.fail)']).then(common.mustCall(({ stderr }) => { | ||
| 97 | 95 | assert.strictEqual(stderr, ''); | |
| 98 | 96 | })); | |
| 99 | 97 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,7 +1,6 @@ | |||
| 1 | 1 | 'use strict'; | |
| 2 | 2 | const common = require('../common'); | |
| 3 | 3 | const assert = require('assert'); | |
| 4 | - const childProcess = require('child_process'); | ||
| 5 | 4 | const url = require('url'); | |
| 6 | 5 | ||
| 7 | 6 | // https://github.com/joyent/node/issues/568 | |
@@ -82,13 +81,12 @@ if (common.hasIntl) { | |||
| 82 | 81 | 'git+ssh://git@github.com:npm/npm', | |
| 83 | 82 | ]; | |
| 84 | 83 | badURLs.forEach((badURL) => { | |
| 85 | - childProcess.exec(`${process.execPath} -e "url.parse('${badURL}')"`, | ||
| 86 | - common.mustCall((err, stdout, stderr) => { | ||
| 87 | - assert.strictEqual(err, null); | ||
| 88 | - assert.strictEqual(stdout, ''); | ||
| 89 | - assert.match(stderr, /\[DEP0170\] DeprecationWarning:/); | ||
| 90 | - }) | ||
| 91 | - ); | ||
| 84 | + common.spawnPromisified(process.execPath, ['-e', `url.parse(${JSON.stringify(badURL)})`]) | ||
| 85 | + .then(common.mustCall(({ code, stdout, stderr }) => { | ||
| 86 | + assert.strictEqual(code, 0); | ||
| 87 | + assert.strictEqual(stdout, ''); | ||
| 88 | + assert.match(stderr, /\[DEP0170\] DeprecationWarning:/); | ||
| 89 | + })); | ||
| 92 | 90 | }); | |
| 93 | 91 | ||
| 94 | 92 | // Warning should only happen once per process. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,7 +2,7 @@ | |||
| 2 | 2 | ||
| 3 | 3 | const common = require('../common'); | |
| 4 | 4 | const assert = require('assert'); | |
| 5 | - const { exec } = require('child_process'); | ||
| 5 | + const { spawn } = require('child_process'); | ||
| 6 | 6 | const fixtures = require('../common/fixtures'); | |
| 7 | 7 | ||
| 8 | 8 | const node = process.execPath; | |
@@ -17,20 +17,25 @@ const syntaxErrorRE = /^SyntaxError: \b/m; | |||
| 17 | 17 | const preloadFile = fixtures.path('no-wrapper.js'); | |
| 18 | 18 | const file = fixtures.path('syntax', 'illegal_if_not_wrapped.js'); | |
| 19 | 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 | - `code ${err.code} !== 1 for error:\n\n${err}`); | ||
| 20 | + const cp = spawn(node, args); | ||
| 25 | 21 | ||
| 26 | - // No stdout should be produced | ||
| 27 | - assert.strictEqual(stdout, ''); | ||
| 22 | + // No stdout should be produced | ||
| 23 | + cp.stdout.on('data', common.mustNotCall('stdout data event')); | ||
| 28 | 24 | ||
| 25 | + const stderrBuffer = []; | ||
| 26 | + cp.stderr.on('data', (chunk) => stderrBuffer.push(chunk)); | ||
| 27 | + | ||
| 28 | + cp.on('exit', common.mustCall((code, signal) => { | ||
| 29 | + assert.strictEqual(code, 1); | ||
| 30 | + assert.strictEqual(signal, null); | ||
| 31 | + | ||
| 32 | + const stderr = Buffer.concat(stderrBuffer).toString('utf-8'); | ||
| 29 | 33 | // stderr should have a syntax error message | |
| 30 | 34 | assert.match(stderr, syntaxErrorRE); | |
| 31 | 35 | ||
| 32 | 36 | // stderr should include the filename | |
| 33 | 37 | assert(stderr.startsWith(file), `${stderr} starts with ${file}`); | |
| 34 | 38 | })); | |
| 39 | + | ||
| 35 | 40 | }); | |
| 36 | 41 | }); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments