-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Correctly document the default maxBuffer size for execSync, execFileSync, and spawnSync. It is 200 * 1024, not Infinity. Add tests to verify behaviour is as documented. PR-URL: #22894 Reviewed-By: Sam Roberts <[email protected]>
- Loading branch information
1 parent
b180a15
commit ceb80f4
Showing
6 changed files
with
211 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const { execFile } = require('child_process'); | ||
|
||
function checkFactory(streamName) { | ||
return common.mustCall((err) => { | ||
assert(err instanceof RangeError); | ||
assert.strictEqual(err.message, `${streamName} maxBuffer length exceeded`); | ||
assert.strictEqual(err.code, 'ERR_CHILD_PROCESS_STDIO_MAXBUFFER'); | ||
}); | ||
} | ||
|
||
// default value | ||
{ | ||
execFile( | ||
process.execPath, | ||
['-e', 'console.log("a".repeat(200 * 1024))'], | ||
checkFactory('stdout') | ||
); | ||
} | ||
|
||
// default value | ||
{ | ||
execFile( | ||
process.execPath, | ||
['-e', 'console.log("a".repeat(200 * 1024 - 1))'], | ||
common.mustCall((err, stdout, stderr) => { | ||
assert.ifError(err); | ||
assert.strictEqual(stdout.trim(), 'a'.repeat(200 * 1024 - 1)); | ||
assert.strictEqual(stderr, ''); | ||
}) | ||
); | ||
} | ||
|
||
{ | ||
const options = { maxBuffer: Infinity }; | ||
|
||
execFile( | ||
process.execPath, | ||
['-e', 'console.log("hello world");'], | ||
options, | ||
common.mustCall((err, stdout, stderr) => { | ||
assert.ifError(err); | ||
assert.strictEqual(stdout.trim(), 'hello world'); | ||
assert.strictEqual(stderr, ''); | ||
}) | ||
); | ||
} | ||
|
||
{ | ||
execFile('echo', ['hello world'], { maxBuffer: 5 }, checkFactory('stdout')); | ||
} | ||
|
||
const unicode = '中文测试'; // length = 4, byte length = 12 | ||
|
||
{ | ||
execFile( | ||
process.execPath, | ||
['-e', `console.log('${unicode}');`], | ||
{ maxBuffer: 10 }, | ||
checkFactory('stdout')); | ||
} | ||
|
||
{ | ||
execFile( | ||
process.execPath, | ||
['-e', `console.error('${unicode}');`], | ||
{ maxBuffer: 10 }, | ||
checkFactory('stderr') | ||
); | ||
} | ||
|
||
{ | ||
const child = execFile( | ||
process.execPath, | ||
['-e', `console.log('${unicode}');`], | ||
{ encoding: null, maxBuffer: 10 }, | ||
checkFactory('stdout') | ||
); | ||
|
||
child.stdout.setEncoding('utf-8'); | ||
} | ||
|
||
{ | ||
const child = execFile( | ||
process.execPath, | ||
['-e', `console.error('${unicode}');`], | ||
{ encoding: null, maxBuffer: 10 }, | ||
checkFactory('stderr') | ||
); | ||
|
||
child.stderr.setEncoding('utf-8'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
'use strict'; | ||
require('../common'); | ||
|
||
// This test checks that the maxBuffer option for child_process.spawnSync() | ||
// works as expected. | ||
|
||
const assert = require('assert'); | ||
const execFileSync = require('child_process').execFileSync; | ||
const msgOut = 'this is stdout'; | ||
const msgOutBuf = Buffer.from(`${msgOut}\n`); | ||
|
||
const args = [ | ||
'-e', | ||
`console.log("${msgOut}");` | ||
]; | ||
|
||
// Verify that an error is returned if maxBuffer is surpassed. | ||
{ | ||
assert.throws( | ||
() => execFileSync(process.execPath, args, { maxBuffer: 1 }), | ||
(e) => { | ||
assert.ok(e, 'maxBuffer should error'); | ||
assert.strictEqual(e.errno, 'ENOBUFS'); | ||
assert.deepStrictEqual(e.stdout, msgOutBuf); | ||
return true; | ||
} | ||
); | ||
} | ||
|
||
// Verify that a maxBuffer size of Infinity works. | ||
{ | ||
const ret = execFileSync(process.execPath, args, { maxBuffer: Infinity }); | ||
|
||
assert.deepStrictEqual(ret, msgOutBuf); | ||
} | ||
|
||
// maxBuffer size is Infinity at default. | ||
{ | ||
const ret = execFileSync( | ||
process.execPath, | ||
['-e', "console.log('a'.repeat(200 * 1024))"], | ||
{ encoding: 'utf-8' } | ||
); | ||
|
||
assert.ifError(ret.error); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
'use strict'; | ||
require('../common'); | ||
|
||
// This test checks that the maxBuffer option for child_process.spawnSync() | ||
// works as expected. | ||
|
||
const assert = require('assert'); | ||
const { execSync } = require('child_process'); | ||
const msgOut = 'this is stdout'; | ||
const msgOutBuf = Buffer.from(`${msgOut}\n`); | ||
|
||
const args = [ | ||
'-e', | ||
`"console.log('${msgOut}')";` | ||
]; | ||
|
||
// Verify that an error is returned if maxBuffer is surpassed. | ||
{ | ||
assert.throws(() => { | ||
execSync(`"${process.execPath}" ${args.join(' ')}`, { maxBuffer: 1 }); | ||
}, (e) => { | ||
assert.ok(e, 'maxBuffer should error'); | ||
assert.strictEqual(e.errno, 'ENOBUFS'); | ||
assert.deepStrictEqual(e.stdout, msgOutBuf); | ||
return true; | ||
}); | ||
} | ||
|
||
// Verify that a maxBuffer size of Infinity works. | ||
{ | ||
const ret = execSync( | ||
`"${process.execPath}" ${args.join(' ')}`, | ||
{ maxBuffer: Infinity } | ||
); | ||
|
||
assert.deepStrictEqual(ret, msgOutBuf); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters