|
1 | 1 | 'use strict';
|
2 | 2 |
|
3 | 3 | const common = require('../common');
|
4 |
| - |
5 |
| -const tty_fd = common.getTTYfd(); |
6 |
| -if (tty_fd < 0) |
7 |
| - common.skip('no valid TTY fd available'); |
8 |
| - |
9 | 4 | const assert = require('assert');
|
| 5 | + |
| 6 | +// general hook test setup |
10 | 7 | const tick = require('./tick');
|
11 | 8 | const initHooks = require('./init-hooks');
|
12 | 9 | const { checkInvocations } = require('./hook-checks');
|
13 | 10 |
|
14 |
| -const ttyStream = (() => { |
15 |
| - try { |
16 |
| - return new (require('tty').WriteStream)(tty_fd); |
17 |
| - } catch (e) { |
18 |
| - return null; |
19 |
| - } |
20 |
| -})(); |
21 |
| -if (ttyStream === null) |
22 |
| - common.skip('no valid TTY fd available'); |
23 |
| - |
24 | 11 | const hooks = initHooks();
|
25 | 12 | hooks.enable();
|
26 | 13 |
|
27 |
| -const as = hooks.activitiesOfTypes('TTYWRAP'); |
28 |
| -assert.strictEqual(as.length, 1); |
29 |
| -const tty = as[0]; |
30 |
| -assert.strictEqual(tty.type, 'TTYWRAP'); |
31 |
| -assert.strictEqual(typeof tty.uid, 'number'); |
32 |
| -assert.strictEqual(typeof tty.triggerAsyncId, 'number'); |
33 |
| -checkInvocations(tty, { init: 1 }, 'when tty created'); |
| 14 | +if (!process.stdout.isTTY) |
| 15 | + return common.skip('no valid writable TTY available'); |
34 | 16 |
|
35 |
| -ttyStream |
36 |
| - .on('finish', common.mustCall(onfinish)) |
37 |
| - .end(common.mustCall(onend)); |
| 17 | +// test specific setup |
| 18 | +const checkInitOpts = { init: 1 }; |
| 19 | +const checkEndedOpts = { init: 1, before: 1, after: 1, destroy: 1 }; |
38 | 20 |
|
39 |
| -checkInvocations(tty, { init: 1 }, 'when tty.end() was invoked '); |
| 21 | +// test code |
| 22 | +// |
| 23 | +// listen to stdin except on Windows |
| 24 | +const activities = hooks.activitiesOfTypes('TTYWRAP'); |
| 25 | +assert.strictEqual(activities.length, 1); |
40 | 26 |
|
41 |
| -function onend() { |
42 |
| - tick(2, common.mustCall(() => |
43 |
| - checkInvocations( |
44 |
| - tty, { init: 1, before: 1, after: 1, destroy: 1 }, |
45 |
| - 'when tty ended ') |
46 |
| - )); |
47 |
| -} |
48 |
| - |
49 |
| -function onfinish() { |
50 |
| - tick(2, common.mustCall(() => |
51 |
| - checkInvocations( |
52 |
| - tty, { init: 1, before: 1, after: 1, destroy: 1 }, |
53 |
| - 'when tty ended ') |
54 |
| - )); |
55 |
| -} |
56 |
| - |
57 |
| -process.on('exit', onexit); |
58 |
| - |
59 |
| -function onexit() { |
| 27 | +const tty = activities[0]; |
| 28 | +assert.strictEqual(tty.type, 'TTYWRAP'); |
| 29 | +assert.strictEqual(typeof tty.uid, 'number'); |
| 30 | +assert.strictEqual(typeof tty.triggerAsyncId, 'number'); |
| 31 | +checkInvocations(tty, checkInitOpts, 'when tty created'); |
| 32 | + |
| 33 | +const delayedOnCloseHandler = common.mustCall(() => { |
| 34 | + checkInvocations(tty, checkEndedOpts, 'when tty ended'); |
| 35 | +}); |
| 36 | +process.stdout.on('error', (err) => assert.fail(err)); |
| 37 | +process.stdout.on('close', common.mustCall(() => |
| 38 | + tick(2, delayedOnCloseHandler) |
| 39 | +)); |
| 40 | +process.stdout.destroy(); |
| 41 | +checkInvocations(tty, checkInitOpts, 'when tty.end() was invoked'); |
| 42 | + |
| 43 | +process.on('exit', () => { |
60 | 44 | hooks.disable();
|
61 | 45 | hooks.sanityCheck('TTYWRAP');
|
62 |
| - checkInvocations(tty, { init: 1, before: 1, after: 1, destroy: 1 }, |
63 |
| - 'when process exits'); |
64 |
| -} |
| 46 | + checkInvocations(tty, checkEndedOpts, 'when process exits'); |
| 47 | +}); |
0 commit comments