• 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.mustSucceed((stdout, stderr) => {
29      assert.strictEqual(stdout.trim(), 'a'.repeat(1024 * 1024 - 1));
30      assert.strictEqual(stderr, '');
31    })
32  );
33}
34
35{
36  const options = { maxBuffer: Infinity };
37
38  execFile(
39    process.execPath,
40    ['-e', 'console.log("hello world");'],
41    options,
42    common.mustSucceed((stdout, stderr) => {
43      assert.strictEqual(stdout.trim(), 'hello world');
44      assert.strictEqual(stderr, '');
45    })
46  );
47}
48
49{
50  execFile('echo', ['hello world'], { maxBuffer: 5 }, checkFactory('stdout'));
51}
52
53const unicode = '中文测试'; // length = 4, byte length = 12
54
55{
56  execFile(
57    process.execPath,
58    ['-e', `console.log('${unicode}');`],
59    { maxBuffer: 10 },
60    checkFactory('stdout'));
61}
62
63{
64  execFile(
65    process.execPath,
66    ['-e', `console.error('${unicode}');`],
67    { maxBuffer: 10 },
68    checkFactory('stderr')
69  );
70}
71
72{
73  const child = execFile(
74    process.execPath,
75    ['-e', `console.log('${unicode}');`],
76    { encoding: null, maxBuffer: 10 },
77    checkFactory('stdout')
78  );
79
80  child.stdout.setEncoding('utf-8');
81}
82
83{
84  const child = execFile(
85    process.execPath,
86    ['-e', `console.error('${unicode}');`],
87    { encoding: null, maxBuffer: 10 },
88    checkFactory('stderr')
89  );
90
91  child.stderr.setEncoding('utf-8');
92}
93