|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +// Lines can be evaluated while the history file is still being loaded |
| 4 | +// asynchronously by `setupHistory()`, e.g. when the input stream does not |
| 5 | +// support pausing. Entries added to the in-memory history in the meantime |
| 6 | +// must not be discarded once the file load completes. |
| 7 | +// Refs: https://github.com/nodejs/node/issues/64508 |
| 8 | + |
| 9 | +const common = require('../common'); |
| 10 | +const assert = require('assert'); |
| 11 | +const fs = require('fs'); |
| 12 | +const stream = require('stream'); |
| 13 | +const repl = require('repl'); |
| 14 | + |
| 15 | +if (process.env.TERM === 'dumb') { |
| 16 | + common.skip('skipping - dumb terminal'); |
| 17 | +} |
| 18 | + |
| 19 | +common.skipIfInspectorDisabled(); |
| 20 | + |
| 21 | +const tmpdir = require('../common/tmpdir'); |
| 22 | +tmpdir.refresh(); |
| 23 | + |
| 24 | +const historyPath = tmpdir.resolve('.repl_history'); |
| 25 | +fs.writeFileSync(historyPath, 'persisted entry'); |
| 26 | + |
| 27 | +// An input stream that, unlike a TTY, does not buffer data while paused. |
| 28 | +class FakeInput extends stream.Stream { |
| 29 | + resume() {} |
| 30 | + pause() {} |
| 31 | +} |
| 32 | +FakeInput.prototype.readable = true; |
| 33 | + |
| 34 | +const input = new FakeInput(); |
| 35 | +const output = new stream.Writable({ |
| 36 | + write(chunk, encoding, callback) { |
| 37 | + callback(); |
| 38 | + }, |
| 39 | +}); |
| 40 | + |
| 41 | +const r = repl.start({ |
| 42 | + input, |
| 43 | + output, |
| 44 | + prompt: '', |
| 45 | + terminal: true, |
| 46 | + useColors: false, |
| 47 | +}); |
| 48 | + |
| 49 | +r.setupHistory(historyPath, common.mustSucceed(() => { |
| 50 | + // The lines evaluated while the history file was being read must be kept, |
| 51 | + // newest first, followed by the persisted entries. |
| 52 | + assert.deepStrictEqual( |
| 53 | + r.history, |
| 54 | + ['const b = 2', 'const a = 1', 'persisted entry'], |
| 55 | + ); |
| 56 | + assert.strictEqual( |
| 57 | + fs.readFileSync(historyPath, 'utf8'), |
| 58 | + 'const b = 2\nconst a = 1\npersisted entry', |
| 59 | + ); |
| 60 | + r.close(); |
| 61 | +})); |
| 62 | + |
| 63 | +// Evaluated synchronously, before the history file has been read. |
| 64 | +input.emit('data', 'const a = 1\nconst b = 2\n'); |
0 commit comments