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, test: refactor exports and add coverage #16959

Merged
merged 2 commits into from
Nov 14, 2017
Merged
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
10 changes: 5 additions & 5 deletions lib/tty.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ const errnoException = util._errnoException;
const errors = require('internal/errors');
const readline = require('readline');

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


function ReadStream(fd, options) {
Expand Down Expand Up @@ -60,8 +60,6 @@ function ReadStream(fd, options) {
}
inherits(ReadStream, net.Socket);

exports.ReadStream = ReadStream;

ReadStream.prototype.setRawMode = function(flag) {
flag = !!flag;
this._handle.setRawMode(flag);
Expand Down Expand Up @@ -102,7 +100,6 @@ function WriteStream(fd) {
}
}
inherits(WriteStream, net.Socket);
exports.WriteStream = WriteStream;


WriteStream.prototype.isTTY = true;
Expand Down Expand Up @@ -143,3 +140,6 @@ WriteStream.prototype.clearScreenDown = function() {
WriteStream.prototype.getWindowSize = function() {
return [this.columns, this.rows];
};


module.exports = { isatty, ReadStream, WriteStream };
21 changes: 21 additions & 0 deletions test/pseudo-tty/test-tty-stream-constructors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';
require('../common');
const assert = require('assert');
const { ReadStream, WriteStream } = require('tty');

{
// Verify that tty.ReadStream can be constructed without new.
const stream = ReadStream(0);

stream.unref();
assert(stream instanceof ReadStream);
assert.strictEqual(stream.isTTY, true);
}

{
// Verify that tty.WriteStream can be constructed without new.
const stream = WriteStream(1);

assert(stream instanceof WriteStream);
assert.strictEqual(stream.isTTY, true);
}
Empty file.
85 changes: 85 additions & 0 deletions test/pseudo-tty/test-tty-window-size.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const { WriteStream } = require('tty');
const { TTY } = process.binding('tty_wrap');
const getWindowSize = TTY.prototype.getWindowSize;

function monkeyPatchGetWindowSize(fn) {
TTY.prototype.getWindowSize = function() {
TTY.prototype.getWindowSize = getWindowSize;
return fn.apply(this, arguments);
};
}

{
// tty.WriteStream constructor does not define columns and rows if an error
// occurs while retrieving the window size from the handle.
monkeyPatchGetWindowSize(function() {
return -1;
});

const stream = WriteStream(1);

assert(stream instanceof WriteStream);
assert.strictEqual(stream.columns, undefined);
assert.strictEqual(stream.rows, undefined);
}

{
// _refreshSize() emits an error if an error occurs while retrieving the
// window size from the handle.
const stream = WriteStream(1);

stream.on('error', common.mustCall((err) => {
assert.strictEqual(err.syscall, 'getWindowSize');
}));

monkeyPatchGetWindowSize(function() {
return -1;
});

stream._refreshSize();
}

{
// _refreshSize() emits a 'resize' event when the window size changes.
monkeyPatchGetWindowSize(function(size) {
size[0] = 80;
size[1] = 24;
return 0;
});

const stream = WriteStream(1);

stream.on('resize', common.mustCall(() => {
assert.strictEqual(stream.columns, 82);
assert.strictEqual(stream.rows, 26);
}));

assert.strictEqual(stream.columns, 80);
assert.strictEqual(stream.rows, 24);

monkeyPatchGetWindowSize(function(size) {
size[0] = 82;
size[1] = 26;
return 0;
});

stream._refreshSize();
}

{
// WriteStream.prototype.getWindowSize() returns the current columns and rows.
monkeyPatchGetWindowSize(function(size) {
size[0] = 99;
size[1] = 32;
return 0;
});

const stream = WriteStream(1);

assert.strictEqual(stream.columns, 99);
assert.strictEqual(stream.rows, 32);
assert.deepStrictEqual(stream.getWindowSize(), [99, 32]);
}
Empty file.