|
1 | 1 | 'use strict'; |
2 | | - |
3 | 2 | const common = require('../common'); |
4 | 3 | const assert = require('assert'); |
| 4 | + |
| 5 | +// general hook test setup |
5 | 6 | const tick = require('./tick'); |
6 | 7 | const initHooks = require('./init-hooks'); |
7 | 8 | const { checkInvocations } = require('./hook-checks'); |
8 | 9 |
|
9 | 10 | const hooks = initHooks(); |
10 | 11 | hooks.enable(); |
11 | 12 |
|
12 | | -const ReadStream = require('tty').ReadStream; |
13 | | -const ttyStream = new ReadStream(0); |
14 | | - |
15 | | -const as = hooks.activitiesOfTypes('TTYWRAP'); |
16 | | -assert.strictEqual(as.length, 1); |
17 | | -const tty = as[0]; |
| 13 | +// test specific setup |
| 14 | +const { ReadStream } = require('tty'); |
| 15 | +const checkInitOpts = { init: 1 }; |
| 16 | +const checkEndedOpts = { init: 1, before: 1, after: 1, destroy: 1 }; |
| 17 | + |
| 18 | +// test code |
| 19 | +// |
| 20 | +// listen to stdin except on Windows |
| 21 | +const targetFD = common.isWindows ? 1 : 0; |
| 22 | +const ttyStream = new ReadStream(targetFD); |
| 23 | +const activities = hooks.activitiesOfTypes('TTYWRAP'); |
| 24 | +assert.strictEqual(activities.length, 1); |
| 25 | +const tty = activities[0]; |
18 | 26 | assert.strictEqual(tty.type, 'TTYWRAP'); |
19 | 27 | assert.strictEqual(typeof tty.uid, 'number'); |
20 | 28 | assert.strictEqual(typeof tty.triggerAsyncId, 'number'); |
21 | | -checkInvocations(tty, { init: 1 }, 'when tty created'); |
22 | | - |
23 | | -ttyStream.end(common.mustCall(onend)); |
24 | | - |
25 | | -checkInvocations(tty, { init: 1 }, 'when tty.end() was invoked '); |
26 | | - |
27 | | -function onend() { |
28 | | - tick(2, common.mustCall(() => |
29 | | - checkInvocations( |
30 | | - tty, { init: 1, before: 1, after: 1, destroy: 1 }, |
31 | | - 'when tty ended ') |
32 | | - )); |
33 | | -} |
34 | | - |
35 | | -process.on('exit', onexit); |
36 | | - |
37 | | -function onexit() { |
| 29 | +checkInvocations(tty, checkInitOpts, 'when tty created'); |
| 30 | +const delayedOnCloseHandler = common.mustCall(() => { |
| 31 | + checkInvocations(tty, checkEndedOpts, 'when tty ended'); |
| 32 | +}); |
| 33 | +ttyStream.on('error', (err) => assert.fail(err)); |
| 34 | +ttyStream.on('close', common.mustCall(() => |
| 35 | + tick(2, delayedOnCloseHandler) |
| 36 | +)); |
| 37 | +ttyStream.destroy(); |
| 38 | +checkInvocations(tty, checkInitOpts, 'when tty.end() was invoked'); |
| 39 | + |
| 40 | +process.on('exit', () => { |
38 | 41 | hooks.disable(); |
39 | 42 | hooks.sanityCheck('TTYWRAP'); |
40 | | - checkInvocations(tty, { init: 1, before: 1, after: 1, destroy: 1 }, |
41 | | - 'when process exits'); |
42 | | -} |
| 43 | + checkInvocations(tty, checkEndedOpts, 'when process exits'); |
| 44 | +}); |
0 commit comments