• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common');
3const assert = require('assert');
4const cp = require('child_process');
5
6function runChecks(err, stdio, streamName, expected) {
7  assert.strictEqual(err.message, `${streamName} maxBuffer length exceeded`);
8  assert(err instanceof RangeError);
9  assert.strictEqual(err.code, 'ERR_CHILD_PROCESS_STDIO_MAXBUFFER');
10  assert.deepStrictEqual(stdio[streamName], expected);
11}
12
13// default value
14{
15  const cmd =
16    `"${process.execPath}" -e "console.log('a'.repeat(1024 * 1024))"`;
17
18  cp.exec(cmd, common.mustCall((err) => {
19    assert(err instanceof RangeError);
20    assert.strictEqual(err.message, 'stdout maxBuffer length exceeded');
21    assert.strictEqual(err.code, 'ERR_CHILD_PROCESS_STDIO_MAXBUFFER');
22  }));
23}
24
25// default value
26{
27  const cmd =
28    `${process.execPath} -e "console.log('a'.repeat(1024 * 1024 - 1))"`;
29
30  cp.exec(cmd, common.mustCall((err, stdout, stderr) => {
31    assert.ifError(err);
32    assert.strictEqual(stdout.trim(), 'a'.repeat(1024 * 1024 - 1));
33    assert.strictEqual(stderr, '');
34  }));
35}
36
37{
38  const cmd = `"${process.execPath}" -e "console.log('hello world');"`;
39  const options = { maxBuffer: Infinity };
40
41  cp.exec(cmd, options, common.mustCall((err, stdout, stderr) => {
42    assert.ifError(err);
43    assert.strictEqual(stdout.trim(), 'hello world');
44    assert.strictEqual(stderr, '');
45  }));
46}
47
48{
49  const cmd = 'echo hello world';
50
51  cp.exec(
52    cmd,
53    { maxBuffer: 5 },
54    common.mustCall((err, stdout, stderr) => {
55      runChecks(err, { stdout, stderr }, 'stdout', 'hello');
56    })
57  );
58}
59
60// default value
61{
62  const cmd =
63    `"${process.execPath}" -e "console.log('a'.repeat(1024 * 1024))"`;
64
65  cp.exec(
66    cmd,
67    common.mustCall((err, stdout, stderr) => {
68      runChecks(
69        err,
70        { stdout, stderr },
71        'stdout',
72        'a'.repeat(1024 * 1024)
73      );
74    })
75  );
76}
77
78// default value
79{
80  const cmd =
81    `"${process.execPath}" -e "console.log('a'.repeat(1024 * 1024 - 1))"`;
82
83  cp.exec(cmd, common.mustCall((err, stdout, stderr) => {
84    assert.ifError(err);
85    assert.strictEqual(stdout.trim(), 'a'.repeat(1024 * 1024 - 1));
86    assert.strictEqual(stderr, '');
87  }));
88}
89
90const unicode = '中文测试'; // length = 4, byte length = 12
91
92{
93  const cmd = `"${process.execPath}" -e "console.log('${unicode}');"`;
94
95  cp.exec(
96    cmd,
97    { maxBuffer: 10 },
98    common.mustCall((err, stdout, stderr) => {
99      runChecks(err, { stdout, stderr }, 'stdout', '中文测试\n');
100    })
101  );
102}
103
104{
105  const cmd = `"${process.execPath}" -e "console.error('${unicode}');"`;
106
107  cp.exec(
108    cmd,
109    { maxBuffer: 3 },
110    common.mustCall((err, stdout, stderr) => {
111      runChecks(err, { stdout, stderr }, 'stderr', '中文测');
112    })
113  );
114}
115
116{
117  const cmd = `"${process.execPath}" -e "console.log('${unicode}');"`;
118
119  const child = cp.exec(
120    cmd,
121    { encoding: null, maxBuffer: 10 },
122    common.mustCall((err, stdout, stderr) => {
123      runChecks(err, { stdout, stderr }, 'stdout', '中文测试\n');
124    })
125  );
126
127  child.stdout.setEncoding('utf-8');
128}
129
130{
131  const cmd = `"${process.execPath}" -e "console.error('${unicode}');"`;
132
133  const child = cp.exec(
134    cmd,
135    { encoding: null, maxBuffer: 3 },
136    common.mustCall((err, stdout, stderr) => {
137      runChecks(err, { stdout, stderr }, 'stderr', '中文测');
138    })
139  );
140
141  child.stderr.setEncoding('utf-8');
142}
143
144{
145  const cmd = `"${process.execPath}" -e "console.error('${unicode}');"`;
146
147  cp.exec(
148    cmd,
149    { encoding: null, maxBuffer: 5 },
150    common.mustCall((err, stdout, stderr) => {
151      const buf = Buffer.from(unicode).slice(0, 5);
152      runChecks(err, { stdout, stderr }, 'stderr', buf);
153    })
154  );
155}
156