Skip to content

Commit f80cf5a

Browse files
cjihrigMylesBorins
authored andcommitted
test: add coverage to tty module
PR-URL: #16959 Reviewed-By: Gireesh Punathil <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent 4e3aa9a commit f80cf5a

4 files changed

+106
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
require('../common');
3+
const assert = require('assert');
4+
const { ReadStream, WriteStream } = require('tty');
5+
6+
{
7+
// Verify that tty.ReadStream can be constructed without new.
8+
const stream = ReadStream(0);
9+
10+
stream.unref();
11+
assert(stream instanceof ReadStream);
12+
assert.strictEqual(stream.isTTY, true);
13+
}
14+
15+
{
16+
// Verify that tty.WriteStream can be constructed without new.
17+
const stream = WriteStream(1);
18+
19+
assert(stream instanceof WriteStream);
20+
assert.strictEqual(stream.isTTY, true);
21+
}

test/pseudo-tty/test-tty-stream-constructors.out

Whitespace-only changes.
+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const { WriteStream } = require('tty');
5+
const { TTY } = process.binding('tty_wrap');
6+
const getWindowSize = TTY.prototype.getWindowSize;
7+
8+
function monkeyPatchGetWindowSize(fn) {
9+
TTY.prototype.getWindowSize = function() {
10+
TTY.prototype.getWindowSize = getWindowSize;
11+
return fn.apply(this, arguments);
12+
};
13+
}
14+
15+
{
16+
// tty.WriteStream constructor does not define columns and rows if an error
17+
// occurs while retrieving the window size from the handle.
18+
monkeyPatchGetWindowSize(function() {
19+
return -1;
20+
});
21+
22+
const stream = WriteStream(1);
23+
24+
assert(stream instanceof WriteStream);
25+
assert.strictEqual(stream.columns, undefined);
26+
assert.strictEqual(stream.rows, undefined);
27+
}
28+
29+
{
30+
// _refreshSize() emits an error if an error occurs while retrieving the
31+
// window size from the handle.
32+
const stream = WriteStream(1);
33+
34+
stream.on('error', common.mustCall((err) => {
35+
assert.strictEqual(err.syscall, 'getWindowSize');
36+
}));
37+
38+
monkeyPatchGetWindowSize(function() {
39+
return -1;
40+
});
41+
42+
stream._refreshSize();
43+
}
44+
45+
{
46+
// _refreshSize() emits a 'resize' event when the window size changes.
47+
monkeyPatchGetWindowSize(function(size) {
48+
size[0] = 80;
49+
size[1] = 24;
50+
return 0;
51+
});
52+
53+
const stream = WriteStream(1);
54+
55+
stream.on('resize', common.mustCall(() => {
56+
assert.strictEqual(stream.columns, 82);
57+
assert.strictEqual(stream.rows, 26);
58+
}));
59+
60+
assert.strictEqual(stream.columns, 80);
61+
assert.strictEqual(stream.rows, 24);
62+
63+
monkeyPatchGetWindowSize(function(size) {
64+
size[0] = 82;
65+
size[1] = 26;
66+
return 0;
67+
});
68+
69+
stream._refreshSize();
70+
}
71+
72+
{
73+
// WriteStream.prototype.getWindowSize() returns the current columns and rows.
74+
monkeyPatchGetWindowSize(function(size) {
75+
size[0] = 99;
76+
size[1] = 32;
77+
return 0;
78+
});
79+
80+
const stream = WriteStream(1);
81+
82+
assert.strictEqual(stream.columns, 99);
83+
assert.strictEqual(stream.rows, 32);
84+
assert.deepStrictEqual(stream.getWindowSize(), [99, 32]);
85+
}

test/pseudo-tty/test-tty-window-size.out

Whitespace-only changes.

0 commit comments

Comments
 (0)