-
Notifications
You must be signed in to change notification settings - Fork 30.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
test: add hijackStdout and hijackStderr #13439
Changes from 1 commit
82dd2c2
d261893
bda1fd1
5a3a666
ab93f52
2a42456
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -116,15 +116,15 @@ Checks whether `IPv6` is supported on this platform. | |
|
||
Checks if there are multiple localhosts available. | ||
|
||
### hijackStdout(listener) | ||
### hijackStderr(listener) | ||
* `listener` [<Function>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Normal_objects_and_functions): An listener with a single parameter called `data`; | ||
|
||
Hijack `process.stdout` to listen `write` action. Once `process.stdout.write` is called, `listener` will also be called and the `data` of `write` function will be passed to `listener`. What's more, `process.stdout.writeTimes` will plus one then. | ||
Hijack `process.stderr` to listen `write` action. Once `process.stderr.write` is called, `listener` will also be called and the `data` of `write` function will be passed to `listener`. What's more, `process.stderr.writeTimes` will plus one then. | ||
|
||
### hijackStderr(listener) | ||
### hijackStdout(listener) | ||
* `listener` [<Function>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Normal_objects_and_functions): An listener with a single parameter called `data`; | ||
|
||
Hijack `process.stderr` to listen `write` action. Once `process.stderr.write` is called, `listener` will also be called and the `data` of `write` function will be passed to `listener`. What's more, `process.stderr.writeTimes` will plus one then. | ||
Hijack `process.stdout` to listen `write` action. Once `process.stdout.write` is called, `listener` will also be called and the `data` of `write` function will be passed to `listener`. What's more, `process.stdout.writeTimes` will plus one then. | ||
|
||
### inFreeBSDJail | ||
* return [<Boolean>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type) | ||
|
@@ -260,6 +260,14 @@ Port tests are running on. | |
|
||
Deletes the 'tmp' dir and recreates it | ||
|
||
### restoreStderr | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If these are functions, then please include the |
||
|
||
Restore the original `process.stderr.write`. | ||
|
||
### restoreStdout | ||
|
||
Restore the original `process.stdout.write`. | ||
|
||
### rootDir | ||
* return [<String>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -761,17 +761,25 @@ exports.getTTYfd = function getTTYfd() { | |
}; | ||
|
||
// Hijack stdout and stderr | ||
const stdWrite = {}; | ||
function hijackStdWritable(name, listener) { | ||
const stream = process[name]; | ||
const _write = stream.write.bind(stream); | ||
const _write = stdWrite[name] = stream.write.bind(stream); | ||
|
||
stream.writeTimes = 0; | ||
stream.write = function(data) { | ||
stream.write = function(data, callback) { | ||
listener(data); | ||
_write(data); | ||
_write(data, callback); | ||
stream.writeTimes++; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This logic seem not go well with the writable.write specification:
Your logic will cause:
I propose: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👌 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In fact, And I've added the |
||
}; | ||
} | ||
|
||
function restoreWritable(name) { | ||
process[name].write = stdWrite[name]; | ||
delete process[name].writeTimes; | ||
} | ||
|
||
exports.hijackStdout = hijackStdWritable.bind(null, 'stdout'); | ||
exports.hijackStderr = hijackStdWritable.bind(null, 'stderr'); | ||
exports.restoreStdout = restoreWritable.bind(null, 'stdout'); | ||
exports.restoreStderr = restoreWritable.bind(null, 'stderr'); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,9 +26,15 @@ const fs = require('fs'); | |
|
||
process.stdout.write('hello world\r\n'); | ||
|
||
var stdin = process.openStdin(); | ||
const stdin = process.openStdin(); | ||
|
||
let current; | ||
common.hijackStdout(function(data) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is an assertion in the listener function, it is better to have it wrapped in |
||
assert.equal(data, current); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use strict equal. |
||
}); | ||
|
||
stdin.on('data', function(data) { | ||
current = data; | ||
process.stdout.write(data.toString()); | ||
}); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,12 @@ process.stdout.write('hello world\r\n'); | |
|
||
var stdin = process.openStdin(); | ||
|
||
let current; | ||
stdin.on('data', function(data) { | ||
current = data; | ||
process.stdout.write(data.toString()); | ||
}); | ||
|
||
common.hijackStdout(function(data) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same |
||
assert.equal(data, current); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,16 +45,14 @@ assert.doesNotThrow(function() { | |
// an Object with a custom .inspect() function | ||
const custom_inspect = { foo: 'bar', inspect: () => 'inspect' }; | ||
|
||
const stdout_write = global.process.stdout.write; | ||
const stderr_write = global.process.stderr.write; | ||
const strings = []; | ||
const errStrings = []; | ||
global.process.stdout.write = function(string) { | ||
strings.push(string); | ||
}; | ||
global.process.stderr.write = function(string) { | ||
errStrings.push(string); | ||
}; | ||
common.hijackStdout(function(data) { | ||
strings.push(data); | ||
}); | ||
common.hijackStderr(function(data) { | ||
errStrings.push(data); | ||
}); | ||
|
||
// test console.log() goes to stdout | ||
console.log('foo'); | ||
|
@@ -105,8 +103,10 @@ console.timeEnd('constructor'); | |
console.time('hasOwnProperty'); | ||
console.timeEnd('hasOwnProperty'); | ||
|
||
global.process.stdout.write = stdout_write; | ||
global.process.stderr.write = stderr_write; | ||
assert.strictEqual(strings.length, process.stdout.writeTimes); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. won't this fail after you implemented @gireeshpunathil's async comment? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In fact, I just added the |
||
assert.strictEqual(errStrings.length, process.stderr.writeTimes); | ||
common.restoreStdout(); | ||
common.restoreStderr(); | ||
|
||
// verify that console.timeEnd() doesn't leave dead links | ||
const timesMapSize = console._times.size; | ||
|
@@ -146,9 +146,6 @@ assert.ok(/^hasOwnProperty: \d+\.\d{3}ms$/.test(strings.shift().trim())); | |
assert.strictEqual('Trace: This is a {"formatted":"trace"} 10 foo', | ||
errStrings.shift().split('\n').shift()); | ||
|
||
assert.strictEqual(strings.length, 0); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Keep this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same which mentioned before. And |
||
assert.strictEqual(errStrings.length, 0); | ||
|
||
assert.throws(() => { | ||
console.assert(false, 'should throw'); | ||
}, common.expectsError({ | ||
|
@@ -159,3 +156,13 @@ assert.throws(() => { | |
assert.doesNotThrow(() => { | ||
console.assert(true, 'this should not throw'); | ||
}); | ||
|
||
// hijack stderr to catch `process.emitWarning` which using `process.nextTick` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. which is using |
||
common.hijackStderr(common.mustCall(function(data) { | ||
common.restoreStderr(); | ||
|
||
// stderr.write will catch sync error, so use `process.nextTick` here | ||
process.nextTick(function() { | ||
assert(/no such label/.test(data)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👌 |
||
}); | ||
})); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,7 @@ | |
// USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
|
||
'use strict'; | ||
require('../common'); | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const os = require('os'); | ||
|
||
|
@@ -63,10 +63,7 @@ function child() { | |
throw new Error('No ticking!'); | ||
}; | ||
|
||
const stderr = process.stderr; | ||
stderr.write = function() { | ||
throw new Error('No writing to stderr!'); | ||
}; | ||
common.hijackStderr(common.mustNotCall('stderr.write mustn\'t be called.')); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd avoid the contraction or use template strings. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sorry, I don't quite understand what's your meaning There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Replace
`stderr.write mustn't be called.` There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
|
||
process._rawDebug('I can still %s!', 'debug'); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"A listener"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, please break the lines at 80 chars if possible.