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

tty,doc: add type-check to isatty #15567

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion doc/api/tty.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,5 @@ added: v0.5.8
* `fd` {number} A numeric file descriptor

The `tty.isatty()` method returns `true` if the given `fd` is associated with
a TTY and `false` if is not.
a TTY and `false` if it is not, including whenever `fd` is not a non-negative
Copy link
Member

Choose a reason for hiding this comment

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

Seems like there ought to be clearer wording than not a non-negative integer. How about just a negative integer?

Copy link
Member

Choose a reason for hiding this comment

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

I'd also be fine with simply removing the additional text here, as a negative number is not a valid fd. But since a negative integer can be returned from a sys call to retrieve a file descriptor if there is an error, I'm OK with leaving it too for explicitness.

integer.
2 changes: 1 addition & 1 deletion lib/tty.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const errors = require('internal/errors');
const readline = require('readline');

exports.isatty = function(fd) {
return isTTY(fd);
return Number.isInteger(fd) && fd >= 0 && isTTY(fd);
};


Expand Down
17 changes: 17 additions & 0 deletions test/pseudo-tty/test-tty-isatty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

require('../common');
const { strictEqual } = require('assert');
const { isatty } = require('tty');

strictEqual(isatty(0), true, 'stdin reported to not be a tty, but it is');
strictEqual(isatty(1), true, 'stdout reported to not be a tty, but it is');
strictEqual(isatty(2), true, 'stderr reported to not be a tty, but it is');

strictEqual(isatty(-1), false, '-1 reported to be a tty, but it is not');
strictEqual(isatty(55555), false, '55555 reported to be a tty, but it is not');
strictEqual(isatty(1.1), false, '1.1 reported to be a tty, but it is not');
strictEqual(isatty('1'), false, '\'1\' reported to be a tty, but it is not');
strictEqual(isatty({}), false, '{} reported to be a tty, but it is not');
strictEqual(isatty(() => {}), false,
'() => {} reported to be a tty, but it is not');
Empty file.