-
Notifications
You must be signed in to change notification settings - Fork 29.7k
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 |
---|---|---|
|
@@ -759,3 +759,19 @@ exports.getTTYfd = function getTTYfd() { | |
} | ||
return tty_fd; | ||
}; | ||
|
||
// Hijack stdout and stderr | ||
function hijackStdWritable(name, listener) { | ||
const stream = process[name]; | ||
const _write = stream.write.bind(stream); | ||
|
||
stream.writeTimes = 0; | ||
stream.write = function(data) { | ||
listener(data); | ||
_write(data); | ||
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 |
||
}; | ||
} | ||
|
||
exports.hijackStdout = hijackStdWritable.bind(null, 'stdout'); | ||
exports.hijackStderr = hijackStdWritable.bind(null, 'stderr'); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,37 +7,30 @@ | |
const common = require('../common'); | ||
const assert = require('assert'); | ||
const EventEmitter = require('events'); | ||
const leak_warning = /EventEmitter memory leak detected\. 2 hello listeners/; | ||
const leakWarning = /EventEmitter memory leak detected\. 2 hello listeners/; | ||
|
||
let write_calls = 0; | ||
common.hijackStderr(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.
|
||
if (process.stderr.writeTimes === 0) { | ||
assert.ok(data.match(leakWarning)); | ||
} else { | ||
assert.fail('stderr.write should be called only once'); | ||
} | ||
}); | ||
|
||
process.on('warning', (warning) => { | ||
process.on('warning', function(warning) { | ||
// This will be called after the default internal | ||
// process warning handler is called. The default | ||
// process warning writes to the console, which will | ||
// invoke the monkeypatched process.stderr.write | ||
// below. | ||
assert.strictEqual(write_calls, 1); | ||
EventEmitter.defaultMaxListeners = old_default; | ||
assert.strictEqual(process.stderr.writeTimes, 1); | ||
EventEmitter.defaultMaxListeners = oldDefault; | ||
// when we get here, we should be done | ||
}); | ||
|
||
process.stderr.write = (data) => { | ||
if (write_calls === 0) | ||
assert.ok(data.match(leak_warning)); | ||
else | ||
assert.fail('stderr.write should be called only once'); | ||
|
||
write_calls++; | ||
}; | ||
|
||
const old_default = EventEmitter.defaultMaxListeners; | ||
const oldDefault = EventEmitter.defaultMaxListeners; | ||
EventEmitter.defaultMaxListeners = 1; | ||
|
||
const e = new EventEmitter(); | ||
e.on('hello', common.noop); | ||
e.on('hello', common.noop); | ||
|
||
// TODO: Figure out how to validate console. Currently, | ||
// there is no obvious way of validating that console | ||
// exists here exactly when it should. |
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.