|
| 1 | +'use strict'; |
| 2 | +require('../common'); |
| 3 | +const tmpdir = require('../common/tmpdir'); |
| 4 | +const assert = require('assert'); |
| 5 | +const fs = require('fs'); |
| 6 | +const join = require('path').join; |
| 7 | +const spawn = require('child_process').spawnSync; |
| 8 | + |
| 9 | +// Test that invoking node with require, and piping stderr to file, |
| 10 | +// does not result in exception, |
| 11 | +// see: https://github.com/nodejs/node/issues/11257 |
| 12 | + |
| 13 | +tmpdir.refresh(); |
| 14 | +const fakeModulePath = join(tmpdir.path, 'batman.js'); |
| 15 | +const stderrOutputPath = join(tmpdir.path, 'stderr-output.txt'); |
| 16 | +// we need to redirect stderr to a file to produce #11257 |
| 17 | +const stream = fs.createWriteStream(stderrOutputPath); |
| 18 | + |
| 19 | +// the error described in #11257 only happens when we require a |
| 20 | +// non-built-in module. |
| 21 | +fs.writeFileSync(fakeModulePath, '', 'utf8'); |
| 22 | + |
| 23 | +stream.on('open', () => { |
| 24 | + spawn(process.execPath, { |
| 25 | + input: `require("${fakeModulePath.replace(/\\/g, '/')}")`, |
| 26 | + stdio: ['pipe', 'pipe', stream] |
| 27 | + }); |
| 28 | + const stderr = fs.readFileSync(stderrOutputPath, 'utf8').trim(); |
| 29 | + assert.strictEqual( |
| 30 | + stderr, |
| 31 | + '', |
| 32 | + `piping stderr to file should not result in exception: ${stderr}` |
| 33 | + ); |
| 34 | + stream.end(); |
| 35 | + fs.unlinkSync(stderrOutputPath); |
| 36 | + fs.unlinkSync(fakeModulePath); |
| 37 | +}); |
0 commit comments