summaryrefslogtreecommitdiff
path: root/test/parallel/test-repl-reverse-search.js
blob: 5027bd7da497fed1f4a2b0fec602a72659b76e78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
'use strict';

// Flags: --expose-internals

const common = require('../common');
const stream = require('stream');
const REPL = require('internal/repl');
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const { inspect } = require('util');

common.skipIfDumbTerminal();
common.allowGlobals('aaaa');

const tmpdir = require('../common/tmpdir');
tmpdir.refresh();

const defaultHistoryPath = path.join(tmpdir.path, '.node_repl_history');

// Create an input stream specialized for testing an array of actions
class ActionStream extends stream.Stream {
  run(data) {
    const _iter = data[Symbol.iterator]();
    const doAction = () => {
      const next = _iter.next();
      if (next.done) {
        // Close the repl. Note that it must have a clean prompt to do so.
        this.emit('keypress', '', { ctrl: true, name: 'd' });
        return;
      }
      const action = next.value;

      if (typeof action === 'object') {
        this.emit('keypress', '', action);
      } else {
        this.emit('data', `${action}`);
      }
      setImmediate(doAction);
    };
    doAction();
  }
  resume() {}
  pause() {}
}
ActionStream.prototype.readable = true;

// Mock keys
const ENTER = { name: 'enter' };
const UP = { name: 'up' };
const DOWN = { name: 'down' };
const BACKSPACE = { name: 'backspace' };
const SEARCH_BACKWARDS = { name: 'r', ctrl: true };
const SEARCH_FORWARDS = { name: 's', ctrl: true };
const ESCAPE = { name: 'escape' };
const CTRL_C = { name: 'c', ctrl: true };
const DELETE_WORD_LEFT = { name: 'w', ctrl: true };

const prompt = '> ';

// TODO(BridgeAR): Add tests for lines that exceed the maximum columns.
const tests = [
  { // Creates few history to navigate for
    env: { NODE_REPL_HISTORY: defaultHistoryPath },
    test: [
      'console.log("foo")', ENTER,
      'ab = "aaaa"', ENTER,
      'repl.repl.historyIndex', ENTER,
      'console.log("foo")', ENTER,
      'let ba = 9', ENTER,
      'ab = "aaaa"', ENTER,
      '555 - 909', ENTER,
      '{key : {key2 :[] }}', ENTER,
      'Array(100).fill(1)', ENTER
    ],
    expected: [],
    clean: false
  },
  {
    env: { NODE_REPL_HISTORY: defaultHistoryPath },
    showEscapeCodes: true,
    checkTotal: true,
    useColors: true,
    test: [
      '7',              // 1
      SEARCH_FORWARDS,
      SEARCH_FORWARDS,  // 3
      'a',
      SEARCH_BACKWARDS, // 5
      SEARCH_FORWARDS,
      SEARCH_BACKWARDS, // 7
      'a',
      BACKSPACE,        // 9
      DELETE_WORD_LEFT,
      'aa',             // 11
      SEARCH_BACKWARDS,
      SEARCH_BACKWARDS, // 13
      SEARCH_BACKWARDS,
      SEARCH_BACKWARDS, // 15
      SEARCH_FORWARDS,
      ESCAPE,           // 17
      ENTER
    ],
    // A = Cursor n up
    // B = Cursor n down
    // C = Cursor n forward
    // D = Cursor n back
    // G = Cursor to column n
    // J = Erase in screen; 0 = right; 1 = left; 2 = total
    // K = Erase in line; 0 = right; 1 = left; 2 = total
    expected: [
      // 0. Start
      '\x1B[1G', '\x1B[0J',
      prompt, '\x1B[3G',
      // 1. '7'
      '7',
      // 2. SEARCH FORWARDS
      '\nfwd-i-search: _', '\x1B[1A', '\x1B[4G',
      // 3. SEARCH FORWARDS
      '\x1B[3G', '\x1B[0J',
      '7\nfwd-i-search: _', '\x1B[1A', '\x1B[4G',
      // 4. 'a'
      '\x1B[3G', '\x1B[0J',
      '7\nfailed-fwd-i-search: a_', '\x1B[1A', '\x1B[4G',
      // 5. SEARCH BACKWARDS
      '\x1B[3G', '\x1B[0J',
      'Arr\x1B[4ma\x1B[24my(100).fill(1)\nbck-i-search: a_',
      '\x1B[1A', '\x1B[6G',
      // 6. SEARCH FORWARDS
      '\x1B[3G', '\x1B[0J',
      '7\nfailed-fwd-i-search: a_', '\x1B[1A', '\x1B[4G',
      // 7. SEARCH BACKWARDS
      '\x1B[3G', '\x1B[0J',
      'Arr\x1B[4ma\x1B[24my(100).fill(1)\nbck-i-search: a_',
      '\x1B[1A', '\x1B[6G',
      // 8. 'a'
      '\x1B[3G', '\x1B[0J',
      'ab = "aa\x1B[4maa\x1B[24m"\nbck-i-search: aa_',
      '\x1B[1A', '\x1B[11G',
      // 9. BACKSPACE
      '\x1B[3G', '\x1B[0J',
      'Arr\x1B[4ma\x1B[24my(100).fill(1)\nbck-i-search: a_',
      '\x1B[1A', '\x1B[6G',
      // 10. DELETE WORD LEFT (works as backspace)
      '\x1B[3G', '\x1B[0J',
      '7\nbck-i-search: _', '\x1B[1A', '\x1B[4G',
      // 11. 'a'
      '\x1B[3G', '\x1B[0J',
      'Arr\x1B[4ma\x1B[24my(100).fill(1)\nbck-i-search: a_',
      '\x1B[1A', '\x1B[6G',
      // 11. 'aa' - continued
      '\x1B[3G', '\x1B[0J',
      'ab = "aa\x1B[4maa\x1B[24m"\nbck-i-search: aa_',
      '\x1B[1A', '\x1B[11G',
      // 12. SEARCH BACKWARDS
      '\x1B[3G', '\x1B[0J',
      'ab = "a\x1B[4maa\x1B[24ma"\nbck-i-search: aa_',
      '\x1B[1A', '\x1B[10G',
      // 13. SEARCH BACKWARDS
      '\x1B[3G', '\x1B[0J',
      'ab = "\x1B[4maa\x1B[24maa"\nbck-i-search: aa_',
      '\x1B[1A', '\x1B[9G',
      // 14. SEARCH BACKWARDS
      '\x1B[3G', '\x1B[0J',
      '7\nfailed-bck-i-search: aa_', '\x1B[1A', '\x1B[4G',
      // 15. SEARCH BACKWARDS
      '\x1B[3G', '\x1B[0J',
      '7\nfailed-bck-i-search: aa_', '\x1B[1A', '\x1B[4G',
      // 16. SEARCH FORWARDS
      '\x1B[3G', '\x1B[0J',
      'ab = "\x1B[4maa\x1B[24maa"\nfwd-i-search: aa_',
      '\x1B[1A', '\x1B[9G',
      // 17. ESCAPE
      '\x1B[3G', '\x1B[0J',
      '7',
      // 18. ENTER
      '\r\n',
      '\x1B[33m7\x1B[39m\n',
      '\x1B[1G', '\x1B[0J',
      prompt,
      '\x1B[3G',
      '\r\n'
    ],
    clean: false
  },
  {
    env: { NODE_REPL_HISTORY: defaultHistoryPath },
    showEscapeCodes: true,
    skip: !process.features.inspector,
    checkTotal: true,
    useColors: false,
    test: [
      'fu',              // 1
      SEARCH_BACKWARDS,
      '}',               // 3
      SEARCH_BACKWARDS,
      CTRL_C,            // 5
      CTRL_C,
      '1+1',             // 7
      ENTER,
      SEARCH_BACKWARDS,  // 9
      '+',
      '\r',              // 11
      '2',
      SEARCH_BACKWARDS,  // 13
      're',
      UP,                // 15
      DOWN,
      SEARCH_FORWARDS,   // 17
      '\n'
    ],
    expected: [
      '\x1B[1G', '\x1B[0J',
      prompt, '\x1B[3G',
      'f', 'u', ' // nction',
      '\x1B[5G', '\x1B[0K',
      '\nbck-i-search: _', '\x1B[1A', '\x1B[5G',
      '\x1B[3G', '\x1B[0J',
      '{key : {key2 :[] }}\nbck-i-search: }_', '\x1B[1A', '\x1B[21G',
      '\x1B[3G', '\x1B[0J',
      '{key : {key2 :[] }}\nbck-i-search: }_', '\x1B[1A', '\x1B[20G',
      '\x1B[3G', '\x1B[0J',
      'fu',
      '\r\n',
      '\x1B[1G', '\x1B[0J',
      prompt, '\x1B[3G',
      '1', '+', '1', '\n// 2', '\x1B[6G', '\x1B[1A',
      '\x1B[1B', '\x1B[2K', '\x1B[1A',
      '\r\n',
      '2\n',
      '\x1B[1G', '\x1B[0J',
      prompt, '\x1B[3G',
      '\nbck-i-search: _', '\x1B[1A',
      '\x1B[3G', '\x1B[0J',
      '1+1\nbck-i-search: +_', '\x1B[1A', '\x1B[4G',
      '\x1B[3G', '\x1B[0J',
      '1+1', '\x1B[4G',
      '\x1B[2C',
      '\r\n',
      '2\n',
      '\x1B[1G', '\x1B[0J',
      prompt, '\x1B[3G',
      '2', '\n// 2', '\x1B[4G', '\x1B[1A',
      '\x1B[1B', '\x1B[2K', '\x1B[1A',
      '\nbck-i-search: _', '\x1B[1A', '\x1B[4G',
      '\x1B[3G', '\x1B[0J',
      'Array(100).fill(1)\nbck-i-search: r_', '\x1B[1A', '\x1B[5G',
      '\x1B[3G', '\x1B[0J',
      'repl.repl.historyIndex\nbck-i-search: re_', '\x1B[1A', '\x1B[8G',
      '\x1B[3G', '\x1B[0J',
      'repl.repl.historyIndex', '\x1B[8G',
      '\x1B[1G', '\x1B[0J',
      `${prompt}ab = "aaaa"`, '\x1B[14G',
      '\x1B[1G', '\x1B[0J',
      `${prompt}repl.repl.historyIndex`, '\x1B[25G', '\n// 8',
      '\x1B[25G', '\x1B[1A',
      '\x1B[1B', '\x1B[2K', '\x1B[1A',
      '\nfwd-i-search: _', '\x1B[1A', '\x1B[25G',
      '\x1B[3G', '\x1B[0J',
      'repl.repl.historyIndex',
      '\r\n',
      '-1\n',
      '\x1B[1G', '\x1B[0J',
      prompt, '\x1B[3G',
      '\r\n'
    ],
    clean: false
  }
];
const numtests = tests.length;

const runTestWrap = common.mustCall(runTest, numtests);

function cleanupTmpFile() {
  try {
    // Write over the file, clearing any history
    fs.writeFileSync(defaultHistoryPath, '');
  } catch (err) {
    if (err.code === 'ENOENT') return true;
    throw err;
  }
  return true;
}

function runTest() {
  const opts = tests.shift();
  if (!opts) return; // All done

  const { expected, skip } = opts;

  // Test unsupported on platform.
  if (skip) {
    setImmediate(runTestWrap, true);
    return;
  }

  const lastChunks = [];
  let i = 0;

  REPL.createInternalRepl(opts.env, {
    input: new ActionStream(),
    output: new stream.Writable({
      write(chunk, _, next) {
        const output = chunk.toString();

        if (!opts.showEscapeCodes &&
            (output[0] === '\x1B' || /^[\r\n]+$/.test(output))) {
          return next();
        }

        lastChunks.push(output);

        if (expected.length && !opts.checkTotal) {
          try {
            assert.strictEqual(output, expected[i]);
          } catch (e) {
            console.error(`Failed test # ${numtests - tests.length}`);
            console.error('Last outputs: ' + inspect(lastChunks, {
              breakLength: 5, colors: true
            }));
            throw e;
          }
          i++;
        }

        next();
      }
    }),
    completer: opts.completer,
    prompt,
    useColors: opts.useColors || false,
    terminal: true
  }, function(err, repl) {
    if (err) {
      console.error(`Failed test # ${numtests - tests.length}`);
      throw err;
    }

    repl.once('close', () => {
      if (opts.clean)
        cleanupTmpFile();

      if (opts.checkTotal) {
        assert.deepStrictEqual(lastChunks, expected);
      } else if (expected.length !== i) {
        console.error(tests[numtests - tests.length - 1]);
        throw new Error(`Failed test # ${numtests - tests.length}`);
      }

      setImmediate(runTestWrap, true);
    });

    if (opts.columns) {
      Object.defineProperty(repl, 'columns', {
        value: opts.columns,
        enumerable: true
      });
    }
    repl.inputStream.run(opts.test);
  });
}

// run the tests
runTest();