Skip to content
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
Prev Previous commit
Next Next commit
update after @refack's reviewing
  • Loading branch information
XadillaX committed Jun 5, 2017
commit ab93f52b467415f76173728b4c7787402bb82a72
28 changes: 9 additions & 19 deletions test/common/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,25 +116,13 @@ Checks whether `IPv6` is supported on this platform.

Checks if there are multiple localhosts available.

### hijackStderr(listener)
* `listener`
[<Function>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Normal_objects_and_functions):
a 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.

### hijackStdout(listener)
* `listener`
[<Function>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Normal_objects_and_functions):
a 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.
### hijackStderr/Stdout(listener)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

These should be listed as separate functions (even if it means duplicating the text)

Copy link
Copy Markdown
Contributor

@refack refack Jun 5, 2017

Choose a reason for hiding this comment

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

@jasnell I suggested the the merge (since it's an internal guide) #13439 (comment)
Please 👍 / 👎 again, just so we don't ask @XadillaX to do unnecessary work

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'd rather keep the style consistent throughout, so would really prefer them separate.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ack.
Sorry @XadillaX

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

👌

* `listener` [<Function>][MDN-Function]: a listener with a single parameter called `data`.

Eavesdrop to `process.stderr.write` or `process.stdout.write` calls. Once
`process.std*.write` is called, `listener` will also be called and the `data` of
`write` function will be passed to `listener`. What's more,
`process.std*.writeTimes` is a count of the number of calls.

### inFreeBSDJail
* return [<Boolean>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type)
Expand Down Expand Up @@ -318,3 +306,5 @@ Node.js
[WHATWG URL API](https://nodejs.org/api/url.html#url_the_whatwg_url_api)
implementation with tests from
[W3C Web Platform Tests](https://github.com/w3c/web-platform-tests).

[MDN-Function]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Normal_objects_and_functions
4 changes: 2 additions & 2 deletions test/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -764,12 +764,12 @@ exports.getTTYfd = function getTTYfd() {
const stdWrite = {};
function hijackStdWritable(name, listener) {
const stream = process[name];
const _write = stdWrite[name] = stream.write.bind(stream);
const _write = stdWrite[name] = stream.write;

stream.writeTimes = 0;
stream.write = function(data, callback) {
listener(data);
_write(data, callback);
_write.call(stream, data, callback);
stream.writeTimes++;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This logic seem not go well with the writable.write specification:

callback (Function) :Callback for when this chunk of data is flushed

Your logic will cause:

  • Issue the completion handler before the actual write (_write)
  • Listener will be called in the same loop tick, making it a direct call, not a callback.

I propose:
_wite(data, listener)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

👌

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

In fact, listener is something like .emit('write') but not .emit('written'), so it's sync.

And I've added the callback in _write now.

};
}
Expand Down
6 changes: 0 additions & 6 deletions test/fixtures/echo-close-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,7 @@ process.stdout.write('hello world\r\n');

const stdin = process.openStdin();

let current;
common.hijackStdout(common.mustCall(function(data) {
assert.strictEqual(data, current);
}));

stdin.on('data', function(data) {
current = data.toString();
process.stdout.write(data.toString());
});

Expand Down
6 changes: 0 additions & 6 deletions test/fixtures/echo.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,6 @@ 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) {
assert.equal(data, current);
});
20 changes: 20 additions & 0 deletions test/parallel/test-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,23 @@ for (const p of failFixtures) {
assert.strictEqual(firstLine, expected);
}));
}

// hijackStderr and hijackStdout
const HIJACK_TEST_ARRAY = [ 'foo\n', 'bar\n', 'baz\n' ];
[ 'err', 'out' ].forEach((txt) => {
const stream = process[`std${txt}`];
const originalWrite = stream.write;

common[`hijackStd${txt}`](common.mustCall(function(data) {
assert.strictEqual(data, HIJACK_TEST_ARRAY[stream.writeTimes]);
}, HIJACK_TEST_ARRAY.length));
assert.notStrictEqual(originalWrite, stream.write);

HIJACK_TEST_ARRAY.forEach((val) => {
stream.write(val, common.mustCall(common.noop));
});

assert.strictEqual(HIJACK_TEST_ARRAY.length, stream.writeTimes);
common[`restoreStd${txt}`]();
assert.strictEqual(originalWrite, stream.write);
});
2 changes: 1 addition & 1 deletion test/parallel/test-console.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,6 @@ common.hijackStderr(common.mustCall(function(data) {

// stderr.write will catch sync error, so use `process.nextTick` here
process.nextTick(function() {
assert(/no such label/.test(data));
assert.strictEqual(data.includes('no such label'), true);
});
}));