@@ -70,6 +70,7 @@ const historyFixturePath = path.join(fixtures, '.node_repl_history');
70
70
const historyPath = path . join ( common . tmpDir , '.fixture_copy_repl_history' ) ;
71
71
const oldHistoryPath = path . join ( fixtures , 'old-repl-history-file.json' ) ;
72
72
const enoentHistoryPath = path . join ( fixtures , 'enoent-repl-history-file.json' ) ;
73
+ const defaultHistoryPath = path . join ( common . tmpDir , '.node_repl_history' ) ;
73
74
74
75
75
76
const tests = [ {
@@ -113,11 +114,7 @@ const tests = [{
113
114
} ,
114
115
{
115
116
env : { NODE_REPL_HISTORY_FILE : oldHistoryPath } ,
116
- test : [ UP , CLEAR , '\'42\'' , ENTER /*, function(cb) {
117
- // XXX(Fishrock123) Allow the REPL to save to disk.
118
- // There isn't a way to do this programmatically right now.
119
- setTimeout(cb, 50);
120
- }*/ ] ,
117
+ test : [ UP , CLEAR , '\'42\'' , ENTER ] ,
121
118
expected : [ prompt , convertMsg , prompt , prompt + '\'=^.^=\'' , prompt , '\'' ,
122
119
'4' , '2' , '\'' , '\'42\'\n' , prompt , prompt ] ,
123
120
after : function ensureHistoryFixture ( ) {
@@ -132,7 +129,7 @@ const tests = [{
132
129
'\'Stay Fresh~\'' + os . EOL ) ;
133
130
}
134
131
} ,
135
- {
132
+ { // Requires the above testcase
136
133
env : { } ,
137
134
test : [ UP , UP , ENTER ] ,
138
135
expected : [ prompt , prompt + '\'42\'' , prompt + '\'=^.^=\'' , '\'=^.^=\'\n' ,
@@ -149,16 +146,45 @@ const tests = [{
149
146
test : [ UP ] ,
150
147
expected : [ prompt , homedirErr , prompt , replDisabled , prompt ]
151
148
} ] ;
149
+ const numtests = tests . length ;
150
+
151
+
152
+ var testsNotRan = tests . length ;
152
153
154
+ process . on ( 'beforeExit' , ( ) =>
155
+ assert . strictEqual ( testsNotRan , 0 )
156
+ ) ;
157
+
158
+ function cleanupTmpFile ( ) {
159
+ try {
160
+ // Write over the file, clearing any history
161
+ fs . writeFileSync ( defaultHistoryPath , '' ) ;
162
+ } catch ( err ) {
163
+ if ( err . code === 'ENOENT' ) return true ;
164
+ throw err ;
165
+ }
166
+ return true ;
167
+ }
153
168
154
169
// Copy our fixture to the tmp directory
155
170
fs . createReadStream ( historyFixturePath )
156
- . pipe ( fs . createWriteStream ( historyPath ) ) . on ( 'unpipe' , runTest ) ;
171
+ . pipe ( fs . createWriteStream ( historyPath ) ) . on ( 'unpipe' , ( ) => runTest ( ) ) ;
157
172
158
- function runTest ( ) {
173
+ function runTest ( assertCleaned ) {
159
174
const opts = tests . shift ( ) ;
160
175
if ( ! opts ) return ; // All done
161
176
177
+ if ( assertCleaned ) {
178
+ try {
179
+ assert . strictEqual ( fs . readFileSync ( defaultHistoryPath , 'utf8' ) , '' ) ;
180
+ } catch ( e ) {
181
+ if ( e . code !== 'ENOENT' ) {
182
+ console . error ( `Failed test # ${ numtests - tests . length } ` ) ;
183
+ throw e ;
184
+ }
185
+ }
186
+ }
187
+
162
188
const env = opts . env ;
163
189
const test = opts . test ;
164
190
const expected = opts . expected ;
@@ -177,24 +203,51 @@ function runTest() {
177
203
if ( output . charCodeAt ( 0 ) === 27 || / ^ [ \r \n ] + $ / . test ( output ) )
178
204
return next ( ) ;
179
205
180
- assert . strictEqual ( output , expected . shift ( ) ) ;
206
+ try {
207
+ assert . strictEqual ( output , expected . shift ( ) ) ;
208
+ } catch ( err ) {
209
+ console . error ( `Failed test # ${ numtests - tests . length } ` ) ;
210
+ throw err ;
211
+ }
181
212
next ( ) ;
182
213
}
183
214
} ) ,
184
215
prompt : prompt ,
185
216
useColors : false ,
186
217
terminal : true
187
218
} , function ( err , repl ) {
188
- if ( err ) throw err ;
219
+ if ( err ) {
220
+ console . error ( `Failed test # ${ numtests - tests . length } ` ) ;
221
+ throw err ;
222
+ }
189
223
190
- if ( after ) repl . on ( 'close' , after ) ;
224
+ repl . once ( 'close' , ( ) => {
225
+ if ( repl . _flushing ) {
226
+ repl . once ( 'flushHistory' , onClose ) ;
227
+ return ;
228
+ }
191
229
192
- repl . on ( 'close' , function ( ) {
193
- // Ensure everything that we expected was output
194
- assert . strictEqual ( expected . length , 0 ) ;
195
- setImmediate ( runTest ) ;
230
+ onClose ( ) ;
196
231
} ) ;
197
232
233
+ function onClose ( ) {
234
+ if ( after ) {
235
+ var cleaned = after ( ) ;
236
+ } else {
237
+ var cleaned = cleanupTmpFile ( ) ;
238
+ }
239
+
240
+ try {
241
+ // Ensure everything that we expected was output
242
+ assert . strictEqual ( expected . length , 0 ) ;
243
+ testsNotRan -- ;
244
+ setImmediate ( runTest , cleaned ) ;
245
+ } catch ( err ) {
246
+ console . error ( `Failed test # ${ numtests - tests . length } ` ) ;
247
+ throw err ;
248
+ }
249
+ }
250
+
198
251
repl . inputStream . run ( test ) ;
199
252
} ) ;
200
253
}
0 commit comments