• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common');
3const assert = require('assert');
4const { execFile } = require('child_process');
5
6function checkFactory(streamName) {
7  return common.mustCall((err) => {
8    assert(err instanceof RangeError);
9    assert.strictEqual(err.message, `${streamName} maxBuffer length exceeded`);
10    assert.strictEqual(err.code, 'ERR_CHILD_PROCESS_STDIO_MAXBUFFER');
11  });
12}
13
14// default value
15{
16  execFile(
17    process.execPath,
18    ['-e', 'console.log("a".repeat(1024 * 1024))'],
19    checkFactory('stdout')
20  );
21}
22
23// default value
24{
25  execFile(
26    process.execPath,
27    ['-e', 'console.log("a".repeat(1024 * 1024 - 1))'],
28    common.mustCall((err, stdout, stderr) => {
29      assert.ifError(err);
30      assert.strictEqual(stdout.trim(), 'a'.repeat(1024 * 1024 - 1));
31      assert.strictEqual(stderr, '');
32    })
33  );
34}
35
36{
37  const options = { maxBuffer: Infinity };
38
39  execFile(
40    process.execPath,
41    ['-e', 'console.log("hello world");'],
42    options,
43    common.mustCall((err, stdout, stderr) => {
44      assert.ifError(err);
45      assert.strictEqual(stdout.trim(), 'hello world');
46      assert.strictEqual(stderr, '');
47    })
48  );
49}
50
51{
52  execFile('echo', ['hello world'], { maxBuffer: 5 }, checkFactory('stdout'));
53}
54
55const unicode = '中文测试'; // length = 4, byte length = 12
56
57{
58  execFile(
59    process.execPath,
60    ['-e', `console.log('${unicode}');`],
61    { maxBuffer: 10 },
62    checkFactory('stdout'));
63}
64
65{
66  execFile(
67    process.execPath,
68    ['-e', `console.error('${unicode}');`],
69    { maxBuffer: 10 },
70    checkFactory('stderr')
71  );
72}
73
74{
75  const child = execFile(
76    process.execPath,
77    ['-e', `console.log('${unicode}');`],
78    { encoding: null, maxBuffer: 10 },
79    checkFactory('stdout')
80  );
81
82  child.stdout.setEncoding('utf-8');
83}
84
85{
86  const child = execFile(
87    process.execPath,
88    ['-e', `console.error('${unicode}');`],
89    { encoding: null, maxBuffer: 10 },
90    checkFactory('stderr')
91  );
92
93  child.stderr.setEncoding('utf-8');
94}
95