Skip to content

Commit 7dcb473

Browse files
committed
Update fake-tty for changes in Node.js 12.7.0
1 parent 9baca8c commit 7dcb473

File tree

3 files changed

+92
-13
lines changed

3 files changed

+92
-13
lines changed

lib/worker/fake-tty.js

Lines changed: 54 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,71 @@ const fakeTTYs = new Set();
99
const {isatty} = tty;
1010
tty.isatty = fd => fakeTTYs.has(fd) || isatty(fd);
1111

12-
const simulateTTY = (stream, {colorDepth, hasColors, columns, rows}) => {
13-
Object.assign(stream, {isTTY: true, columns, rows});
12+
const takesCallbackAndReturnWriteResult = tty.WriteStream.prototype.clearLine.length === 1;
13+
14+
const assertCallback = callback => {
15+
// FIXME: Better replicate Node.js' internal errors.
16+
if (callback !== undefined && typeof callback !== 'function') {
17+
const error = new TypeError('Callback must be a function');
18+
error.code = 'ERR_INVALID_CALLBACK';
19+
throw error;
20+
}
21+
};
22+
23+
const fakeWriters = {
24+
clearLine(dir, callback) {
25+
assertCallback(callback);
1426

15-
stream.clearLine = dir => {
1627
switch (dir) {
1728
case -1:
18-
stream.write(ansiEscapes.eraseStartLine);
19-
break;
29+
return this.write(ansiEscapes.eraseStartLine, callback);
2030
case 1:
21-
stream.write(ansiEscapes.eraseEndLine);
22-
break;
31+
return this.write(ansiEscapes.eraseEndLine, callback);
2332
default:
24-
stream.write(ansiEscapes.eraseLine);
33+
return this.write(ansiEscapes.eraseLine, callback);
2534
}
26-
};
35+
},
2736

28-
stream.clearScreenDown = () => stream.write(ansiEscapes.eraseDown);
37+
clearScreenDown(callback) {
38+
assertCallback(callback);
2939

30-
stream.cursorTo = (x, y) => stream.write(ansiEscapes.cursorTo(x, y));
40+
return this.write(ansiEscapes.eraseDown, callback);
41+
},
3142

32-
stream.getWindowSize = () => [80, 24];
43+
cursorTo(x, y, callback) {
44+
assertCallback(callback);
45+
return this.write(ansiEscapes.cursorTo(x, y), callback);
46+
},
3347

34-
stream.moveCursor = (x, y) => stream.write(ansiEscapes.cursorMove(x, y));
48+
moveCursor(x, y, callback) {
49+
assertCallback(callback);
50+
return this.write(ansiEscapes.cursorMove(x, y), callback);
51+
}
52+
};
53+
54+
const simulateTTY = (stream, {colorDepth, hasColors, columns, rows}) => {
55+
Object.assign(stream, {isTTY: true, columns, rows});
3556

57+
if (takesCallbackAndReturnWriteResult) {
58+
Object.assign(stream, fakeWriters);
59+
} else {
60+
Object.assign(stream, {
61+
clearLine(dir) {
62+
fakeWriters.clearLine.call(this, dir);
63+
},
64+
clearScreenDown() {
65+
fakeWriters.clearScreenDown.call(this);
66+
},
67+
cursorTo(x, y) {
68+
fakeWriters.cursorTo.call(this, x, y);
69+
},
70+
moveCursor(x, y) {
71+
fakeWriters.moveCursor.call(this, x, y);
72+
}
73+
});
74+
}
75+
76+
stream.getWindowSize = () => [80, 24];
3677
if (colorDepth !== undefined) {
3778
stream.getColorDepth = () => colorDepth;
3879
}

test/fixture/tty/callbacks.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import test from '../../..';
2+
3+
const takesCallbackAndReturnWriteResult = require('tty').WriteStream.prototype.clearLine.length === 1;
4+
5+
const assert = takesCallbackAndReturnWriteResult ?
6+
async (t, stream) => {
7+
t.is(stream.clearLine.length, 2, 'clearLine');
8+
await new Promise(resolve => stream.clearLine(0, resolve));
9+
t.is(stream.clearScreenDown.length, 1, 'clearScreenDown');
10+
await new Promise(resolve => stream.clearScreenDown(resolve));
11+
t.is(stream.cursorTo.length, 3, 'cursorTo');
12+
await new Promise(resolve => stream.cursorTo(0, 0, resolve));
13+
t.is(stream.moveCursor.length, 3, 'moveCursor');
14+
await new Promise(resolve => stream.moveCursor(0, 0, resolve));
15+
} :
16+
(t, stream) => {
17+
t.is(stream.clearLine.length, 1, 'clearLine');
18+
t.is(stream.clearScreenDown.length, 0, 'clearScreenDown');
19+
t.is(stream.cursorTo.length, 2, 'cursorTo');
20+
t.is(stream.moveCursor.length, 2, 'cursorTo');
21+
};
22+
23+
test('stderr tty write methods take / do not take a callback', assert, process.stderr);
24+
test('stdout tty write methods take / do not take a callback', assert, process.stdout);

test/integration/tty.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,17 @@ test('test worker TTYs inherit color support from the parent TTY', t => {
6262
t.end();
6363
});
6464
});
65+
66+
test('test worker TTYs take / do not take callbacks', t => {
67+
const options = {
68+
dirname: 'fixture/tty',
69+
env: {
70+
AVA_SIMULATE_TTY: true
71+
}
72+
};
73+
74+
execCli('callbacks.js', options, err => {
75+
t.ifError(err);
76+
t.end();
77+
});
78+
});

0 commit comments

Comments
 (0)