11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_cirequire('../common');
31cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ciconst fs = require('fs');
61cb0ef41Sopenharmony_ciconst join = require('path').join;
71cb0ef41Sopenharmony_ciconst { spawnSync } = require('child_process');
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ci// Test that invoking node with require, and piping stderr to file,
101cb0ef41Sopenharmony_ci// does not result in exception,
111cb0ef41Sopenharmony_ci// see: https://github.com/nodejs/node/issues/11257
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_citmpdir.refresh();
141cb0ef41Sopenharmony_ciconst fakeModulePath = join(tmpdir.path, 'batman.js');
151cb0ef41Sopenharmony_ciconst stderrOutputPath = join(tmpdir.path, 'stderr-output.txt');
161cb0ef41Sopenharmony_ci// We need to redirect stderr to a file to produce #11257
171cb0ef41Sopenharmony_ciconst stream = fs.createWriteStream(stderrOutputPath);
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ci// The error described in #11257 only happens when we require a
201cb0ef41Sopenharmony_ci// non-built-in module.
211cb0ef41Sopenharmony_cifs.writeFileSync(fakeModulePath, '', 'utf8');
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_cistream.on('open', () => {
241cb0ef41Sopenharmony_ci  spawnSync(process.execPath, {
251cb0ef41Sopenharmony_ci    input: `require(${JSON.stringify(fakeModulePath)})`,
261cb0ef41Sopenharmony_ci    stdio: ['pipe', 'pipe', stream]
271cb0ef41Sopenharmony_ci  });
281cb0ef41Sopenharmony_ci  const stderr = fs.readFileSync(stderrOutputPath, 'utf8').trim();
291cb0ef41Sopenharmony_ci  assert.strictEqual(
301cb0ef41Sopenharmony_ci    stderr,
311cb0ef41Sopenharmony_ci    '',
321cb0ef41Sopenharmony_ci    `piping stderr to file should not result in exception: ${stderr}`
331cb0ef41Sopenharmony_ci  );
341cb0ef41Sopenharmony_ci  stream.end();
351cb0ef41Sopenharmony_ci  fs.unlinkSync(stderrOutputPath);
361cb0ef41Sopenharmony_ci  fs.unlinkSync(fakeModulePath);
371cb0ef41Sopenharmony_ci});
38