• 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 { execSync } = require('child_process');
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  assert.throws(() => {
20    execSync(`"${process.execPath}" ${args.join(' ')}`, { maxBuffer: 1 });
21  }, (e) => {
22    assert.ok(e, 'maxBuffer should error');
23    assert.strictEqual(e.errno, 'ENOBUFS');
24    // We can have buffers larger than maxBuffer because underneath we alloc 64k
25    // that matches our read sizes.
26    assert.deepStrictEqual(e.stdout, msgOutBuf);
27    return true;
28  });
29}
30
31// Verify that a maxBuffer size of Infinity works.
32{
33  const ret = execSync(
34    `"${process.execPath}" ${args.join(' ')}`,
35    { maxBuffer: Infinity }
36  );
37
38  assert.deepStrictEqual(ret, msgOutBuf);
39}
40
41// Default maxBuffer size is 1024 * 1024.
42{
43  assert.throws(() => {
44    execSync(
45      `"${process.execPath}" -e "console.log('a'.repeat(1024 * 1024))"`
46    );
47  }, (e) => {
48    assert.ok(e, 'maxBuffer should error');
49    assert.strictEqual(e.errno, 'ENOBUFS');
50    return true;
51  });
52}
53
54// Default maxBuffer size is 1024 * 1024.
55{
56  const ret = execSync(
57    `"${process.execPath}" -e "console.log('a'.repeat(1024 * 1024 - 1))"`
58  );
59
60  assert.deepStrictEqual(
61    ret.toString().trim(),
62    'a'.repeat(1024 * 1024 - 1)
63  );
64}
65