|
1 | 1 | 'use strict'; |
2 | | - |
3 | 2 | constcommon=require('../common'); |
4 | 3 | constassert=require('assert'); |
| 4 | + |
| 5 | +// general hook test setup |
5 | 6 | consttick=require('./tick'); |
6 | 7 | constinitHooks=require('./init-hooks'); |
7 | 8 | const{ checkInvocations }=require('./hook-checks'); |
8 | 9 |
|
9 | 10 | consthooks=initHooks(); |
10 | 11 | hooks.enable(); |
11 | 12 |
|
12 | | -constReadStream=require('tty').ReadStream; |
13 | | -constttyStream=newReadStream(0); |
14 | | - |
15 | | -constas=hooks.activitiesOfTypes('TTYWRAP'); |
16 | | -assert.strictEqual(as.length,1); |
17 | | -consttty=as[0]; |
| 13 | +// test specific setup |
| 14 | +const{ ReadStream }=require('tty'); |
| 15 | +constcheckInitOpts={init: 1}; |
| 16 | +constcheckEndedOpts={init: 1,before: 1,after: 1,destroy: 1}; |
| 17 | + |
| 18 | +// test code |
| 19 | +// |
| 20 | +// listen to stdin except on Windows |
| 21 | +consttargetFD=common.isWindows ? 1 : 0; |
| 22 | +constttyStream=newReadStream(targetFD); |
| 23 | +constactivities=hooks.activitiesOfTypes('TTYWRAP'); |
| 24 | +assert.strictEqual(activities.length,1); |
| 25 | +consttty=activities[0]; |
18 | 26 | assert.strictEqual(tty.type,'TTYWRAP'); |
19 | 27 | assert.strictEqual(typeoftty.uid,'number'); |
20 | 28 | assert.strictEqual(typeoftty.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 | | -functiononend(){ |
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 | | -functiononexit(){ |
| 29 | +checkInvocations(tty,checkInitOpts,'when tty created'); |
| 30 | +constdelayedOnCloseHandler=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