2020// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121
2222'use strict' ;
23- require ( '../common' ) ;
23+ const common = require ( '../common' ) ;
2424const ArrayStream = require ( '../common/arraystream' ) ;
2525const assert = require ( 'assert' ) ;
2626const join = require ( 'path' ) . join ;
@@ -36,96 +36,103 @@ const works = [['inner.one'], 'inner.o'];
3636const putIn = new ArrayStream ( ) ;
3737const testMe = repl . start ( '' , putIn ) ;
3838
39+ // Some errors might be passed to the domain.
40+ testMe . _domain . on ( 'error' , function ( reason ) {
41+ const err = new Error ( 'Test failed' ) ;
42+ err . reason = reason ;
43+ throw err ;
44+ } ) ;
3945
4046const testFile = [
4147 'var top = function() {' ,
4248 'var inner = {one:1};'
4349] ;
4450const saveFileName = join ( tmpdir . path , 'test.save.js' ) ;
4551
46- // input some data
52+ // Add some data.
4753putIn . run ( testFile ) ;
4854
49- // save it to a file
55+ // Save it to a file.
5056putIn . run ( [ `.save ${ saveFileName } ` ] ) ;
5157
52- // The file should have what I wrote
58+ // The file should have what I wrote.
5359assert . strictEqual ( fs . readFileSync ( saveFileName , 'utf8' ) ,
54- ` ${ testFile . join ( '\n' ) } \n` ) ;
60+ testFile . join ( '\n' ) ) ;
5561
56- {
57- // save .editor mode code
58- const cmds = [
59- 'function testSave() {' ,
60- 'return "saved";' ,
61- '}'
62- ] ;
63- const putIn = new ArrayStream ( ) ;
64- const replServer = repl . start ( { terminal : true , stream : putIn } ) ;
65-
66- putIn . run ( [ '.editor' ] ) ;
67- putIn . run ( cmds ) ;
68- replServer . write ( '' , { ctrl : true , name : 'd' } ) ;
69-
70- putIn . run ( [ `.save ${ saveFileName } ` ] ) ;
71- replServer . close ( ) ;
72- assert . strictEqual ( fs . readFileSync ( saveFileName , 'utf8' ) ,
73- `${ cmds . join ( '\n' ) } \n\n` ) ;
74- }
75-
76- // Make sure that the REPL data is "correct"
77- // so when I load it back I know I'm good
78- testMe . complete ( 'inner.o' , function ( error , data ) {
62+ // Make sure that the REPL data is "correct".
63+ testMe . complete ( 'inner.o' , common . mustCall ( function ( error , data ) {
64+ assert . ifError ( error ) ;
7965 assert . deepStrictEqual ( data , works ) ;
80- } ) ;
66+ } ) ) ;
8167
82- // clear the REPL
68+ // Clear the REPL.
8369putIn . run ( [ '.clear' ] ) ;
8470
85- // Load the file back in
71+ // Load the file back in.
8672putIn . run ( [ `.load ${ saveFileName } ` ] ) ;
8773
88- // Make sure that the REPL data is "correct"
89- testMe . complete ( 'inner.o' , function ( error , data ) {
74+ // Make sure that the REPL data is "correct".
75+ testMe . complete ( 'inner.o' , common . mustCall ( function ( error , data ) {
76+ assert . ifError ( error ) ;
9077 assert . deepStrictEqual ( data , works ) ;
91- } ) ;
78+ } ) ) ;
9279
93- // clear the REPL
80+ // Clear the REPL.
9481putIn . run ( [ '.clear' ] ) ;
9582
9683let loadFile = join ( tmpdir . path , 'file.does.not.exist' ) ;
9784
98- // should not break
99- putIn . write = function ( data ) {
100- // Make sure I get a failed to load message and not some crazy error
101- assert . strictEqual ( data , `Failed to load:${ loadFile } \n` ) ;
102- // Eat me to avoid work
85+ // Should not break.
86+ putIn . write = common . mustCall ( function ( data ) {
87+ // Make sure I get a failed to load message and not some crazy error.
88+ assert . strictEqual ( data , `Failed to load: ${ loadFile } \n` ) ;
89+ // Eat me to avoid work.
10390 putIn . write = ( ) => { } ;
104- } ;
91+ } ) ;
10592putIn . run ( [ `.load ${ loadFile } ` ] ) ;
10693
107- // Throw error on loading directory
94+ // Throw error on loading directory.
10895loadFile = tmpdir . path ;
109- putIn . write = function ( data ) {
110- assert . strictEqual ( data , `Failed to load:${ loadFile } is not a valid file\n` ) ;
96+ putIn . write = common . mustCall ( function ( data ) {
97+ assert . strictEqual ( data , `Failed to load: ${ loadFile } is not a valid file\n` ) ;
11198 putIn . write = ( ) => { } ;
112- } ;
99+ } ) ;
113100putIn . run ( [ `.load ${ loadFile } ` ] ) ;
114101
115- // clear the REPL
102+ // Clear the REPL.
116103putIn . run ( [ '.clear' ] ) ;
117104
118105// NUL (\0) is disallowed in filenames in UNIX-like operating systems and
119- // Windows so we can use that to test failed saves
106+ // Windows so we can use that to test failed saves.
120107const invalidFileName = join ( tmpdir . path , '\0\0\0\0\0' ) ;
121108
122- // should not break
123- putIn . write = function ( data ) {
124- // Make sure I get a failed to save message and not some other error
125- assert . strictEqual ( data , `Failed to save:${ invalidFileName } \n` ) ;
126- // reset to no-op
109+ // Should not break.
110+ putIn . write = common . mustCall ( function ( data ) {
111+ // Make sure I get a failed to save message and not some other error.
112+ assert . strictEqual ( data , `Failed to save: ${ invalidFileName } \n` ) ;
113+ // Reset to no-op.
127114 putIn . write = ( ) => { } ;
128- } ;
115+ } ) ;
129116
130- // save it to a file
117+ // Save it to a file.
131118putIn . run ( [ `.save ${ invalidFileName } ` ] ) ;
119+
120+ {
121+ // Save .editor mode code.
122+ const cmds = [
123+ 'function testSave() {' ,
124+ 'return "saved";' ,
125+ '}'
126+ ] ;
127+ const putIn = new ArrayStream ( ) ;
128+ const replServer = repl . start ( { terminal : true , stream : putIn } ) ;
129+
130+ putIn . run ( [ '.editor' ] ) ;
131+ putIn . run ( cmds ) ;
132+ replServer . write ( '' , { ctrl : true , name : 'd' } ) ;
133+
134+ putIn . run ( [ `.save ${ saveFileName } ` ] ) ;
135+ replServer . close ( ) ;
136+ assert . strictEqual ( fs . readFileSync ( saveFileName , 'utf8' ) ,
137+ `${ cmds . join ( '\n' ) } \n` ) ;
138+ }
0 commit comments