11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ci// Simulate `cat readfile.js | node readfile.js`
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciif (common.isWindows || common.isAIX || common.isIBMi)
71cb0ef41Sopenharmony_ci  common.skip(`No /dev/stdin on ${process.platform}.`);
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciconst assert = require('assert');
101cb0ef41Sopenharmony_ciconst path = require('path');
111cb0ef41Sopenharmony_ciconst fs = require('fs');
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ciif (process.argv[2] === 'child') {
141cb0ef41Sopenharmony_ci  fs.readFile('/dev/stdin', function(er, data) {
151cb0ef41Sopenharmony_ci    assert.ifError(er);
161cb0ef41Sopenharmony_ci    process.stdout.write(data);
171cb0ef41Sopenharmony_ci  });
181cb0ef41Sopenharmony_ci  return;
191cb0ef41Sopenharmony_ci}
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir');
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ciconst filename = path.join(tmpdir.path, '/readfile_pipe_large_test.txt');
241cb0ef41Sopenharmony_ciconst dataExpected = 'a'.repeat(999999);
251cb0ef41Sopenharmony_citmpdir.refresh();
261cb0ef41Sopenharmony_cifs.writeFileSync(filename, dataExpected);
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ciconst exec = require('child_process').exec;
291cb0ef41Sopenharmony_ciconst f = JSON.stringify(__filename);
301cb0ef41Sopenharmony_ciconst node = JSON.stringify(process.execPath);
311cb0ef41Sopenharmony_ciconst cmd = `cat ${filename} | ${node} ${f} child`;
321cb0ef41Sopenharmony_ciexec(cmd, { maxBuffer: 1000000 }, common.mustSucceed((stdout, stderr) => {
331cb0ef41Sopenharmony_ci  assert.strictEqual(
341cb0ef41Sopenharmony_ci    stdout,
351cb0ef41Sopenharmony_ci    dataExpected,
361cb0ef41Sopenharmony_ci    `expect it reads the file and outputs 999999 'a' but got : ${stdout}`
371cb0ef41Sopenharmony_ci  );
381cb0ef41Sopenharmony_ci  assert.strictEqual(
391cb0ef41Sopenharmony_ci    stderr,
401cb0ef41Sopenharmony_ci    '',
411cb0ef41Sopenharmony_ci    `expect that it does not write to stderr, but got : ${stderr}`
421cb0ef41Sopenharmony_ci  );
431cb0ef41Sopenharmony_ci  console.log('ok');
441cb0ef41Sopenharmony_ci}));
45