2
2
/* global describe, it */
3
3
4
4
var assert = require ( 'assert' ) ,
5
+ fs = require ( 'fs' ) ,
5
6
_ = require ( 'lodash' ) ,
6
7
n_ = require ( '../lib/n_' ) ,
7
8
line = n_ . rli . _events . line ,
9
+ osHomedir = require ( 'os-homedir' ) ,
10
+ path = require ( 'path' ) ,
8
11
result = null ;
9
12
10
13
n_ . writer = _ . wrap ( n_ . writer , function ( writer , obj ) {
@@ -113,13 +116,13 @@ describe('n_', function () {
113
116
114
117
describe ( 'enabling fp mode' , function ( ) {
115
118
it ( 'should use lodash/fp' , function ( done ) {
116
- var previousArgv = process . argv ;
117
119
// enable fp mode
120
+ var previousArgv = process . argv ;
118
121
process . argv = _ . concat ( previousArgv , [ '--fp' ] ) ;
119
122
120
123
// now require and setup n_ (it should now use lodash/fp)
121
124
reset ( ) ;
122
- // Reset argv to previous value
125
+ // reset argv to previous value
123
126
process . argv = previousArgv ;
124
127
125
128
line ( '_.map(function(v) { return v * 2; }, [1, 2, 3]);' ) ;
@@ -137,29 +140,56 @@ describe('n_', function () {
137
140
} ) ;
138
141
it ( 'should throw in strict mode set via environment variable' , function ( done ) {
139
142
// enable strict mode
143
+ var previousReplMode = process . env . NODE_REPL_MODE ;
140
144
process . env . NODE_REPL_MODE = 'strict' ;
141
145
142
146
// now require and setup n_ (it should now run in strict mode)
143
147
reset ( ) ;
148
+ // reset NODE_REPL_MODE to previous value
149
+ process . env . NODE_REPL_MODE = previousReplMode ;
144
150
145
151
line ( 'var fixed = {}; Object.preventExtensions(fixed); fixed.newProp = 1;' ) ;
146
152
assert . equal ( result , null ) ;
147
153
done ( ) ;
148
154
} ) ;
149
155
it ( 'should throw in strict mode set via command line option' , function ( done ) {
150
- // reset environment variab
151
- process . env . NODE_REPL_MODE = undefined ;
152
-
153
156
// enable strict mode
154
- process . argv . push ( '--use_strict' ) ;
157
+ var previousArgv = process . argv ;
158
+ process . argv = _ . concat ( previousArgv , [ '--use_strict' ] ) ;
155
159
156
160
// now require and setup n_ (it should now run with strict mode enabled)
157
161
reset ( ) ;
162
+ // reset argv to previous value
163
+ process . argv = previousArgv ;
158
164
159
165
line ( 'var fixed = {}; Object.preventExtensions(fixed); fixed.newProp = 1;' ) ;
160
166
assert . equal ( result , null ) ;
161
167
done ( ) ;
162
168
} ) ;
163
169
} ) ;
164
170
}
171
+
172
+ describe ( 'repl history' , function ( ) {
173
+ it ( 'should save and load repl history across multiple sessions' , function ( done ) {
174
+ var historyPath = path . join ( osHomedir ( ) , '.n_repl_history' ) ;
175
+
176
+ // delete any previously created history file
177
+ fs . unlinkSync ( historyPath ) ;
178
+
179
+ reset ( ) ; // new session
180
+ line ( '1+2' ) ;
181
+ reset ( ) ; // new session
182
+ line ( 'null' ) ;
183
+ reset ( ) ; // new session
184
+ line ( '"foobar"' ) ;
185
+ reset ( ) ; // new session
186
+
187
+ // check history (as thoroughly as possible)
188
+ var historyFileContent = fs . readFileSync ( historyPath , 'utf-8' ) ;
189
+ assert . equal ( historyFileContent , [ '1+2' , 'null' , '"foobar"' , '' ] . join ( '\n' ) ) ;
190
+ line ( '.load ' + historyPath ) ;
191
+ assert . equal ( result , 'foobar' ) ;
192
+ done ( ) ;
193
+ } ) ;
194
+ } ) ;
165
195
} ) ;
0 commit comments