@@ -5,27 +5,128 @@ require('../common');
5
5
const ArrayStream = require ( '../common/arraystream' ) ;
6
6
7
7
const assert = require ( 'assert' ) ;
8
- const repl = require ( 'repl' ) ;
9
8
10
9
function * expectedLines ( lines ) {
11
10
for ( const line of lines ) {
12
11
yield line ;
13
12
}
14
- throw new Error ( 'Requested more lines than expected' ) ;
15
13
}
16
14
15
+ const expectedDebug = expectedLines ( [
16
+ [
17
+ 'line %j' ,
18
+ 'const x = await new Promise((r) => setTimeout(() => r(1), 500));' ,
19
+ ] ,
20
+ [
21
+ 'eval %j' ,
22
+ 'const x = await new Promise((r) => setTimeout(() => r(1), 500));\n' ,
23
+ ] ,
24
+ [ 'queued line %j' , 'x;' ] ,
25
+ [ 'finish' , null , undefined ] ,
26
+ [ 'line %j' , 'x;' ] ,
27
+ [ 'eval %j' , 'x;\n' ] ,
28
+ [ 'finish' , null , 1 ] ,
29
+ [ 'line %j' , 'const y = 3;' ] ,
30
+ [ 'eval %j' , 'const y = 3;\n' ] ,
31
+ [ 'finish' , null , undefined ] ,
32
+ [ 'line %j' , 'x + y;' ] ,
33
+ [ 'eval %j' , 'x + y;\n' ] ,
34
+ [ 'finish' , null , 4 ] ,
35
+ [ 'line %j' , 'const z = 4;' ] ,
36
+ [ 'eval %j' , 'const z = 4;\n' ] ,
37
+ [ 'finish' , null , undefined ] ,
38
+ [ 'queued line %j' , 'z + 1' ] ,
39
+ [ 'line %j' , 'z + 1' ] ,
40
+ [ 'eval %j' , 'z + 1\n' ] ,
41
+ [ 'finish' , null , 5 ] ,
42
+ ] ) ;
43
+
44
+ let calledDebug = false ;
45
+ require ( 'internal/util/debuglog' ) . debuglog = ( ) => {
46
+ return ( ...args ) => {
47
+ calledDebug = true ;
48
+ assert . deepStrictEqual ( args , expectedDebug . next ( ) . value ) ;
49
+ } ;
50
+ } ;
51
+
52
+ // Import `repl` after replacing original `debuglog`
53
+ const repl = require ( 'repl' ) ;
54
+
17
55
const putIn = new ArrayStream ( ) ;
18
56
repl . start ( {
19
57
input : putIn ,
20
58
output : putIn ,
21
- useGlobal : false
59
+ useGlobal : false ,
22
60
} ) ;
23
61
24
- const expectedOutput = expectedLines ( [ 'undefined\n' , '> ' , '1\n' , '> ' ] ) ;
25
- putIn . write = function ( data ) {
62
+ const expectedOutput = expectedLines ( [
63
+ 'undefined\n' ,
64
+ '> ' ,
65
+ '1\n' ,
66
+ '> ' ,
67
+ 'undefined\n' ,
68
+ '> ' ,
69
+ '4\n' ,
70
+ '> ' ,
71
+ 'undefined\n' ,
72
+ '> ' ,
73
+ '5\n' ,
74
+ '> ' ,
75
+ ] ) ;
76
+
77
+ let calledOutput = false ;
78
+ function writeCallback ( data ) {
79
+ calledOutput = true ;
26
80
assert . strictEqual ( data , expectedOutput . next ( ) . value ) ;
27
- } ;
81
+ }
28
82
29
- putIn . run ( [
30
- 'const x = await new Promise((r) => setTimeout(() => r(1), 500));\nx;' ,
31
- ] ) ;
83
+ putIn . write = writeCallback ;
84
+
85
+ function clearCalled ( ) {
86
+ calledDebug = false ;
87
+ calledOutput = false ;
88
+ }
89
+
90
+ // Lines sent after an async command that hasn't finished
91
+ // in the same write are enqueued
92
+ function test1 ( ) {
93
+ putIn . run ( [
94
+ 'const x = await new Promise((r) => setTimeout(() => r(1), 500));\nx;' ,
95
+ ] ) ;
96
+ assert ( calledDebug ) ;
97
+
98
+ setTimeout ( ( ) => {
99
+ assert ( calledOutput ) ;
100
+ clearCalled ( ) ;
101
+
102
+ test2 ( ) ;
103
+ } , 1000 ) ;
104
+ }
105
+
106
+ // Lines sent after a sync command in the same write are not enqueued
107
+ function test2 ( ) {
108
+ putIn . run ( [ 'const y = 3;\nx + y;' ] ) ;
109
+
110
+ assert ( calledDebug ) ;
111
+ assert ( calledOutput ) ;
112
+ clearCalled ( ) ;
113
+
114
+ test3 ( ) ;
115
+ }
116
+
117
+ // Lines sent during an output write event of a previous command
118
+ // are enqueued
119
+ function test3 ( ) {
120
+ putIn . write = ( data ) => {
121
+ writeCallback ( data ) ;
122
+ putIn . write = writeCallback ;
123
+ putIn . run ( [ 'z + 1' ] ) ;
124
+ } ;
125
+
126
+ putIn . run ( [ 'const z = 4;' ] ) ;
127
+
128
+ assert ( calledDebug ) ;
129
+ assert ( calledOutput ) ;
130
+ }
131
+
132
+ test1 ( ) ;
0 commit comments