Skip to content

Commit

Permalink
Update fake-tty for changes in Node.js 12.7.0
Browse files Browse the repository at this point in the history
  • Loading branch information
novemberborn committed Aug 18, 2019
1 parent 9baca8c commit 7dcb473
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 13 deletions.
67 changes: 54 additions & 13 deletions lib/worker/fake-tty.js
Expand Up @@ -9,30 +9,71 @@ const fakeTTYs = new Set();
const {isatty} = tty;
tty.isatty = fd => fakeTTYs.has(fd) || isatty(fd);

const simulateTTY = (stream, {colorDepth, hasColors, columns, rows}) => {
Object.assign(stream, {isTTY: true, columns, rows});
const takesCallbackAndReturnWriteResult = tty.WriteStream.prototype.clearLine.length === 1;

const assertCallback = callback => {
// FIXME: Better replicate Node.js' internal errors.
if (callback !== undefined && typeof callback !== 'function') {
const error = new TypeError('Callback must be a function');
error.code = 'ERR_INVALID_CALLBACK';
throw error;
}
};

const fakeWriters = {
clearLine(dir, callback) {
assertCallback(callback);

stream.clearLine = dir => {
switch (dir) {
case -1:
stream.write(ansiEscapes.eraseStartLine);
break;
return this.write(ansiEscapes.eraseStartLine, callback);
case 1:
stream.write(ansiEscapes.eraseEndLine);
break;
return this.write(ansiEscapes.eraseEndLine, callback);
default:
stream.write(ansiEscapes.eraseLine);
return this.write(ansiEscapes.eraseLine, callback);
}
};
},

stream.clearScreenDown = () => stream.write(ansiEscapes.eraseDown);
clearScreenDown(callback) {
assertCallback(callback);

stream.cursorTo = (x, y) => stream.write(ansiEscapes.cursorTo(x, y));
return this.write(ansiEscapes.eraseDown, callback);
},

stream.getWindowSize = () => [80, 24];
cursorTo(x, y, callback) {
assertCallback(callback);
return this.write(ansiEscapes.cursorTo(x, y), callback);
},

stream.moveCursor = (x, y) => stream.write(ansiEscapes.cursorMove(x, y));
moveCursor(x, y, callback) {
assertCallback(callback);
return this.write(ansiEscapes.cursorMove(x, y), callback);
}
};

const simulateTTY = (stream, {colorDepth, hasColors, columns, rows}) => {
Object.assign(stream, {isTTY: true, columns, rows});

if (takesCallbackAndReturnWriteResult) {
Object.assign(stream, fakeWriters);
} else {
Object.assign(stream, {
clearLine(dir) {
fakeWriters.clearLine.call(this, dir);
},
clearScreenDown() {
fakeWriters.clearScreenDown.call(this);
},
cursorTo(x, y) {
fakeWriters.cursorTo.call(this, x, y);
},
moveCursor(x, y) {
fakeWriters.moveCursor.call(this, x, y);
}
});
}

stream.getWindowSize = () => [80, 24];
if (colorDepth !== undefined) {
stream.getColorDepth = () => colorDepth;
}
Expand Down
24 changes: 24 additions & 0 deletions test/fixture/tty/callbacks.js
@@ -0,0 +1,24 @@
import test from '../../..';

const takesCallbackAndReturnWriteResult = require('tty').WriteStream.prototype.clearLine.length === 1;

const assert = takesCallbackAndReturnWriteResult ?
async (t, stream) => {
t.is(stream.clearLine.length, 2, 'clearLine');
await new Promise(resolve => stream.clearLine(0, resolve));
t.is(stream.clearScreenDown.length, 1, 'clearScreenDown');
await new Promise(resolve => stream.clearScreenDown(resolve));
t.is(stream.cursorTo.length, 3, 'cursorTo');
await new Promise(resolve => stream.cursorTo(0, 0, resolve));
t.is(stream.moveCursor.length, 3, 'moveCursor');
await new Promise(resolve => stream.moveCursor(0, 0, resolve));
} :
(t, stream) => {
t.is(stream.clearLine.length, 1, 'clearLine');
t.is(stream.clearScreenDown.length, 0, 'clearScreenDown');
t.is(stream.cursorTo.length, 2, 'cursorTo');
t.is(stream.moveCursor.length, 2, 'cursorTo');
};

test('stderr tty write methods take / do not take a callback', assert, process.stderr);
test('stdout tty write methods take / do not take a callback', assert, process.stdout);
14 changes: 14 additions & 0 deletions test/integration/tty.js
Expand Up @@ -62,3 +62,17 @@ test('test worker TTYs inherit color support from the parent TTY', t => {
t.end();
});
});

test('test worker TTYs take / do not take callbacks', t => {
const options = {
dirname: 'fixture/tty',
env: {
AVA_SIMULATE_TTY: true
}
};

execCli('callbacks.js', options, err => {
t.ifError(err);
t.end();
});
});

0 comments on commit 7dcb473

Please sign in to comment.