Skip to content

Commit 9e98155

Browse files
Fishrock123rvagg
authored andcommitted
test: cleanup, improve repl-persistent-history
- Now cleans up the history file unless told otherwise. - Now also logs which test case failed. - Waits for flush after repl close if necessary. Fixes: #2319 PR-URL: #2356 Reviewed-By: Roman Reiss <[email protected]> Reviewed By: Evan Lucas <[email protected]>
1 parent 2296a4f commit 9e98155

File tree

1 file changed

+68
-15
lines changed

1 file changed

+68
-15
lines changed

test/sequential/test-repl-persistent-history.js

+68-15
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ const historyFixturePath = path.join(fixtures, '.node_repl_history');
7070
const historyPath = path.join(common.tmpDir, '.fixture_copy_repl_history');
7171
const oldHistoryPath = path.join(fixtures, 'old-repl-history-file.json');
7272
const enoentHistoryPath = path.join(fixtures, 'enoent-repl-history-file.json');
73+
const defaultHistoryPath = path.join(common.tmpDir, '.node_repl_history');
7374

7475

7576
const tests = [{
@@ -113,11 +114,7 @@ const tests = [{
113114
},
114115
{
115116
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],
121118
expected: [prompt, convertMsg, prompt, prompt + '\'=^.^=\'', prompt, '\'',
122119
'4', '2', '\'', '\'42\'\n', prompt, prompt],
123120
after: function ensureHistoryFixture() {
@@ -132,7 +129,7 @@ const tests = [{
132129
'\'Stay Fresh~\'' + os.EOL);
133130
}
134131
},
135-
{
132+
{ // Requires the above testcase
136133
env: {},
137134
test: [UP, UP, ENTER],
138135
expected: [prompt, prompt + '\'42\'', prompt + '\'=^.^=\'', '\'=^.^=\'\n',
@@ -149,16 +146,45 @@ const tests = [{
149146
test: [UP],
150147
expected: [prompt, homedirErr, prompt, replDisabled, prompt]
151148
}];
149+
const numtests = tests.length;
150+
151+
152+
var testsNotRan = tests.length;
152153

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+
}
153168

154169
// Copy our fixture to the tmp directory
155170
fs.createReadStream(historyFixturePath)
156-
.pipe(fs.createWriteStream(historyPath)).on('unpipe', runTest);
171+
.pipe(fs.createWriteStream(historyPath)).on('unpipe', () => runTest());
157172

158-
function runTest() {
173+
function runTest(assertCleaned) {
159174
const opts = tests.shift();
160175
if (!opts) return; // All done
161176

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+
162188
const env = opts.env;
163189
const test = opts.test;
164190
const expected = opts.expected;
@@ -177,24 +203,51 @@ function runTest() {
177203
if (output.charCodeAt(0) === 27 || /^[\r\n]+$/.test(output))
178204
return next();
179205

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+
}
181212
next();
182213
}
183214
}),
184215
prompt: prompt,
185216
useColors: false,
186217
terminal: true
187218
}, function(err, repl) {
188-
if (err) throw err;
219+
if (err) {
220+
console.error(`Failed test # ${numtests - tests.length}`);
221+
throw err;
222+
}
189223

190-
if (after) repl.on('close', after);
224+
repl.once('close', () => {
225+
if (repl._flushing) {
226+
repl.once('flushHistory', onClose);
227+
return;
228+
}
191229

192-
repl.on('close', function() {
193-
// Ensure everything that we expected was output
194-
assert.strictEqual(expected.length, 0);
195-
setImmediate(runTest);
230+
onClose();
196231
});
197232

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+
198251
repl.inputStream.run(test);
199252
});
200253
}

0 commit comments

Comments
 (0)