Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ad331f2

Browse filesBrowse files
cjihriggibfahn
authored andcommitted
test: add coverage to tty module
PR-URL: #16959 Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent 5ee05f2 commit ad331f2
Copy full SHA for ad331f2

File tree

Expand file treeCollapse file tree

4 files changed

+106
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

4 files changed

+106
-0
lines changed
Open diff view settings
Collapse file
+21Lines changed: 21 additions & 0 deletions
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+
}
Collapse file

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

Copy file name to clipboardExpand all lines: test/pseudo-tty/test-tty-stream-constructors.out
Whitespace-only changes.
Collapse file
+85Lines changed: 85 additions & 0 deletions
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+
}
Collapse file

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

Copy file name to clipboardExpand all lines: test/pseudo-tty/test-tty-window-size.out
Whitespace-only changes.

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.