• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2require('../common');
3
4// This test checks that the maxBuffer option for child_process.spawnSync()
5// works as expected.
6
7const assert = require('assert');
8const spawnSync = require('child_process').spawnSync;
9const msgOut = 'this is stdout';
10const msgOutBuf = Buffer.from(`${msgOut}\n`);
11
12const args = [
13  '-e',
14  `console.log("${msgOut}");`
15];
16
17// Verify that an error is returned if maxBuffer is surpassed.
18{
19  const ret = spawnSync(process.execPath, args, { maxBuffer: 1 });
20
21  assert.ok(ret.error, 'maxBuffer should error');
22  assert.strictEqual(ret.error.errno, 'ENOBUFS');
23  // We can have buffers larger than maxBuffer because underneath we alloc 64k
24  // that matches our read sizes.
25  assert.deepStrictEqual(ret.stdout, msgOutBuf);
26}
27
28// Verify that a maxBuffer size of Infinity works.
29{
30  const ret = spawnSync(process.execPath, args, { maxBuffer: Infinity });
31
32  assert.ifError(ret.error);
33  assert.deepStrictEqual(ret.stdout, msgOutBuf);
34}
35
36// Default maxBuffer size is 1024 * 1024.
37{
38  const args = ['-e', "console.log('a'.repeat(1024 * 1024))"];
39  const ret = spawnSync(process.execPath, args);
40
41  assert.ok(ret.error, 'maxBuffer should error');
42  assert.strictEqual(ret.error.errno, 'ENOBUFS');
43}
44
45// Default maxBuffer size is 1024 * 1024.
46{
47  const args = ['-e', "console.log('a'.repeat(1024 * 1024 - 1))"];
48  const ret = spawnSync(process.execPath, args);
49
50  assert.ifError(ret.error);
51  assert.deepStrictEqual(
52    ret.stdout.toString().trim(),
53    'a'.repeat(1024 * 1024 - 1)
54  );
55}
56