• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common');
3
4// Simulate `cat readfile.js | node readfile.js`
5
6if (common.isWindows || common.isAIX)
7  common.skip(`No /dev/stdin on ${process.platform}.`);
8
9const assert = require('assert');
10const path = require('path');
11const fs = require('fs');
12
13if (process.argv[2] === 'child') {
14  fs.readFile('/dev/stdin', function(er, data) {
15    assert.ifError(er);
16    process.stdout.write(data);
17  });
18  return;
19}
20
21const tmpdir = require('../common/tmpdir');
22
23const filename = path.join(tmpdir.path, '/readfile_pipe_large_test.txt');
24const dataExpected = 'a'.repeat(999999);
25tmpdir.refresh();
26fs.writeFileSync(filename, dataExpected);
27
28const exec = require('child_process').exec;
29const f = JSON.stringify(__filename);
30const node = JSON.stringify(process.execPath);
31const cmd = `cat ${filename} | ${node} ${f} child`;
32exec(cmd, { maxBuffer: 1000000 }, common.mustCall((err, stdout, stderr) => {
33  assert.ifError(err);
34  assert.strictEqual(
35    stdout,
36    dataExpected,
37    `expect it reads the file and outputs 999999 'a' but got : ${stdout}`
38  );
39  assert.strictEqual(
40    stderr,
41    '',
42    `expect that it does not write to stderr, but got : ${stderr}`
43  );
44  console.log('ok');
45}));
46
47process.on('exit', function() {
48  fs.unlinkSync(filename);
49});
50