• 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 { getSystemErrorName } = require('util');
10const msgOut = 'this is stdout';
11const msgOutBuf = Buffer.from(`${msgOut}\n`);
12
13const args = [
14  '-e',
15  `console.log("${msgOut}");`,
16];
17
18// Verify that an error is returned if maxBuffer is surpassed.
19{
20  const ret = spawnSync(process.execPath, args, { maxBuffer: 1 });
21
22  assert.ok(ret.error, 'maxBuffer should error');
23  assert.strictEqual(ret.error.code, 'ENOBUFS');
24  assert.strictEqual(getSystemErrorName(ret.error.errno), 'ENOBUFS');
25  // We can have buffers larger than maxBuffer because underneath we alloc 64k
26  // that matches our read sizes.
27  assert.deepStrictEqual(ret.stdout, msgOutBuf);
28}
29
30// Verify that a maxBuffer size of Infinity works.
31{
32  const ret = spawnSync(process.execPath, args, { maxBuffer: Infinity });
33
34  assert.ifError(ret.error);
35  assert.deepStrictEqual(ret.stdout, msgOutBuf);
36}
37
38// Default maxBuffer size is 1024 * 1024.
39{
40  const args = ['-e', "console.log('a'.repeat(1024 * 1024))"];
41  const ret = spawnSync(process.execPath, args);
42
43  assert.ok(ret.error, 'maxBuffer should error');
44  assert.strictEqual(ret.error.code, 'ENOBUFS');
45  assert.strictEqual(getSystemErrorName(ret.error.errno), 'ENOBUFS');
46}
47
48// Default maxBuffer size is 1024 * 1024.
49{
50  const args = ['-e', "console.log('a'.repeat(1024 * 1024 - 1))"];
51  const ret = spawnSync(process.execPath, args);
52
53  assert.ifError(ret.error);
54  assert.deepStrictEqual(
55    ret.stdout.toString().trim(),
56    'a'.repeat(1024 * 1024 - 1)
57  );
58}
59