1'use strict';
2const common = require('../common');
3const assert = require('assert');
4const spawn = require('child_process').spawn;
5const stream = require('stream');
6const fs = require('fs');
7const path = require('path');
8
9// require('internal/fs/utils').SyncWriteStream is used as a stdio
10// implementation when stdout/stderr point to files.
11
12if (process.argv[2] === 'child') {
13  // Note: Calling console.log() is part of this test as it exercises the
14  // SyncWriteStream#_write() code path.
15  console.log(JSON.stringify([process.stdout, process.stderr].map((stdio) => ({
16    instance: stdio instanceof stream.Writable,
17    readable: stdio.readable,
18    writable: stdio.writable,
19  }))));
20
21  return;
22}
23
24const tmpdir = require('../common/tmpdir');
25tmpdir.refresh();
26
27const filename = path.join(tmpdir.path, 'stdout');
28const stdoutFd = fs.openSync(filename, 'w');
29
30const proc = spawn(process.execPath, [__filename, 'child'], {
31  stdio: ['inherit', stdoutFd, stdoutFd ]
32});
33
34proc.on('close', common.mustCall(() => {
35  fs.closeSync(stdoutFd);
36
37  assert.deepStrictEqual(JSON.parse(fs.readFileSync(filename, 'utf8')), [
38    { instance: true, readable: false, writable: true },
39    { instance: true, readable: false, writable: true },
40  ]);
41}));
42