Skip to content
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: fix hijackStdout behavior in console #14647

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion test/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,14 @@ function hijackStdWritable(name, listener) {

stream.writeTimes = 0;
stream.write = function(data, callback) {
listener(data);
try {
listener(data);
} catch(e) {
process.nextTick(function() {
Copy link
Contributor

@refack refack Aug 9, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I know you like functions but this could be a single line.
IMHO the indentation is weird

throw e;
});
}

_write.call(stream, data, callback);
stream.writeTimes++;
};
Expand Down
20 changes: 20 additions & 0 deletions test/parallel/test-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,23 @@ const HIJACK_TEST_ARRAY = [ 'foo\n', 'bar\n', 'baz\n' ];
common[`restoreStd${txt}`]();
assert.strictEqual(originalWrite, stream.write);
});

// hijackStderr and hijackStdout again
// for console
[ 'err', 'out' ].forEach((txt) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: do [['err', 'error'], ['out', 'log']].forEach(([ext, method]) => {
instead of console[txt === 'err' ? 'error' : 'log']('test');

common[`hijackStd${txt}`](common.mustCall(function(data) {
assert.strictEqual(data, 'test\n');

// throw an error
throw new Error(`console ${txt} error`);
}));

console[txt === 'err' ? 'error' : 'log']('test');
common[`restoreStd${txt}`]();
});

let uncaughtTimes = 0;
process.on('uncaughtException', common.mustCallAtLeast(function(e) {
Copy link
Contributor

@refack refack Aug 9, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CR: How is the mustCallAtLeast fit with the ${([ 'err', 'out' ])[uncaughtTimes++]} trick?

Copy link
Contributor Author

@XadillaX XadillaX Aug 10, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The second parameter of common.mustCallAtLeast is 2.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I wasn't clear. I meant it should be mustCall, as any more calls would generate console undefined error

assert.strictEqual(e instanceof Error, true);
assert.strictEqual(e.message, `console ${([ 'err', 'out' ])[uncaughtTimes++]} error`);
}, 2));