20
20
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
21
22
22
'use strict' ;
23
- require ( '../common' ) ;
23
+ const common = require ( '../common' ) ;
24
24
const ArrayStream = require ( '../common/arraystream' ) ;
25
25
const assert = require ( 'assert' ) ;
26
26
const join = require ( 'path' ) . join ;
@@ -36,96 +36,103 @@ const works = [['inner.one'], 'inner.o'];
36
36
const putIn = new ArrayStream ( ) ;
37
37
const testMe = repl . start ( '' , putIn ) ;
38
38
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
+ } ) ;
39
45
40
46
const testFile = [
41
47
'var top = function() {' ,
42
48
'var inner = {one:1};'
43
49
] ;
44
50
const saveFileName = join ( tmpdir . path , 'test.save.js' ) ;
45
51
46
- // input some data
52
+ // Add some data.
47
53
putIn . run ( testFile ) ;
48
54
49
- // save it to a file
55
+ // Save it to a file.
50
56
putIn . run ( [ `.save ${ saveFileName } ` ] ) ;
51
57
52
- // The file should have what I wrote
58
+ // The file should have what I wrote.
53
59
assert . strictEqual ( fs . readFileSync ( saveFileName , 'utf8' ) ,
54
- ` ${ testFile . join ( '\n' ) } \n` ) ;
60
+ testFile . join ( '\n' ) ) ;
55
61
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 ) ;
79
65
assert . deepStrictEqual ( data , works ) ;
80
- } ) ;
66
+ } ) ) ;
81
67
82
- // clear the REPL
68
+ // Clear the REPL.
83
69
putIn . run ( [ '.clear' ] ) ;
84
70
85
- // Load the file back in
71
+ // Load the file back in.
86
72
putIn . run ( [ `.load ${ saveFileName } ` ] ) ;
87
73
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 ) ;
90
77
assert . deepStrictEqual ( data , works ) ;
91
- } ) ;
78
+ } ) ) ;
92
79
93
- // clear the REPL
80
+ // Clear the REPL.
94
81
putIn . run ( [ '.clear' ] ) ;
95
82
96
83
let loadFile = join ( tmpdir . path , 'file.does.not.exist' ) ;
97
84
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.
103
90
putIn . write = ( ) => { } ;
104
- } ;
91
+ } ) ;
105
92
putIn . run ( [ `.load ${ loadFile } ` ] ) ;
106
93
107
- // Throw error on loading directory
94
+ // Throw error on loading directory.
108
95
loadFile = 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` ) ;
111
98
putIn . write = ( ) => { } ;
112
- } ;
99
+ } ) ;
113
100
putIn . run ( [ `.load ${ loadFile } ` ] ) ;
114
101
115
- // clear the REPL
102
+ // Clear the REPL.
116
103
putIn . run ( [ '.clear' ] ) ;
117
104
118
105
// 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.
120
107
const invalidFileName = join ( tmpdir . path , '\0\0\0\0\0' ) ;
121
108
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.
127
114
putIn . write = ( ) => { } ;
128
- } ;
115
+ } ) ;
129
116
130
- // save it to a file
117
+ // Save it to a file.
131
118
putIn . 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