|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +// Flags: --expose-internals --experimental-permission --allow-fs-read=* |
| 4 | + |
| 5 | +const common = require('../common'); |
| 6 | +const stream = require('stream'); |
| 7 | +const REPL = require('internal/repl'); |
| 8 | +const assert = require('assert'); |
| 9 | +const { inspect } = require('util'); |
| 10 | + |
| 11 | +common.skipIfDumbTerminal(); |
| 12 | + |
| 13 | +// Create an input stream specialized for testing an array of actions |
| 14 | +class ActionStream extends stream.Stream { |
| 15 | + run(data) { |
| 16 | + const _iter = data[Symbol.iterator](); |
| 17 | + const doAction = () => { |
| 18 | + const next = _iter.next(); |
| 19 | + if (next.done) { |
| 20 | + // Close the repl. Note that it must have a clean prompt to do so. |
| 21 | + this.emit('keypress', '', { ctrl: true, name: 'd' }); |
| 22 | + return; |
| 23 | + } |
| 24 | + const action = next.value; |
| 25 | + |
| 26 | + if (typeof action === 'object') { |
| 27 | + this.emit('keypress', '', action); |
| 28 | + } else { |
| 29 | + this.emit('data', `${action}`); |
| 30 | + } |
| 31 | + setImmediate(doAction); |
| 32 | + }; |
| 33 | + doAction(); |
| 34 | + } |
| 35 | + resume() {} |
| 36 | + pause() {} |
| 37 | +} |
| 38 | +ActionStream.prototype.readable = true; |
| 39 | + |
| 40 | +// Mock keys |
| 41 | +const ENTER = { name: 'enter' }; |
| 42 | +const TABULATION = { name: 'tab' }; |
| 43 | + |
| 44 | +const prompt = '> '; |
| 45 | + |
| 46 | +const tests = [ |
| 47 | + { |
| 48 | + test: (function*() { |
| 49 | + yield 'f'; |
| 50 | + yield TABULATION; |
| 51 | + yield ENTER; |
| 52 | + })(), |
| 53 | + expected: [], |
| 54 | + env: {} |
| 55 | + }, |
| 56 | +]; |
| 57 | + |
| 58 | +const numtests = tests.length; |
| 59 | + |
| 60 | +const runTestWrap = common.mustCall(runTest, numtests); |
| 61 | + |
| 62 | +function runTest() { |
| 63 | + const opts = tests.shift(); |
| 64 | + if (!opts) return; // All done |
| 65 | + |
| 66 | + const { expected, skip } = opts; |
| 67 | + |
| 68 | + // Test unsupported on platform. |
| 69 | + if (skip) { |
| 70 | + setImmediate(runTestWrap, true); |
| 71 | + return; |
| 72 | + } |
| 73 | + const lastChunks = []; |
| 74 | + let i = 0; |
| 75 | + |
| 76 | + REPL.createInternalRepl(opts.env, { |
| 77 | + input: new ActionStream(), |
| 78 | + output: new stream.Writable({ |
| 79 | + write(chunk, _, next) { |
| 80 | + const output = chunk.toString(); |
| 81 | + |
| 82 | + if (!opts.showEscapeCodes && |
| 83 | + (output[0] === '\x1B' || /^[\r\n]+$/.test(output))) { |
| 84 | + return next(); |
| 85 | + } |
| 86 | + |
| 87 | + lastChunks.push(output); |
| 88 | + |
| 89 | + if (expected.length && !opts.checkTotal) { |
| 90 | + try { |
| 91 | + assert.strictEqual(output, expected[i]); |
| 92 | + } catch (e) { |
| 93 | + console.error(`Failed test # ${numtests - tests.length}`); |
| 94 | + console.error('Last outputs: ' + inspect(lastChunks, { |
| 95 | + breakLength: 5, colors: true |
| 96 | + })); |
| 97 | + throw e; |
| 98 | + } |
| 99 | + // TODO(BridgeAR): Auto close on last chunk! |
| 100 | + i++; |
| 101 | + } |
| 102 | + |
| 103 | + next(); |
| 104 | + } |
| 105 | + }), |
| 106 | + allowBlockingCompletions: true, |
| 107 | + completer: opts.completer, |
| 108 | + prompt, |
| 109 | + useColors: false, |
| 110 | + preview: opts.preview, |
| 111 | + terminal: true |
| 112 | + }, function(err, repl) { |
| 113 | + if (err) { |
| 114 | + console.error(`Failed test # ${numtests - tests.length}`); |
| 115 | + throw err; |
| 116 | + } |
| 117 | + |
| 118 | + repl.once('close', () => { |
| 119 | + |
| 120 | + if (opts.checkTotal) { |
| 121 | + assert.deepStrictEqual(lastChunks, expected); |
| 122 | + } else if (expected.length !== i) { |
| 123 | + console.error(tests[numtests - tests.length - 1]); |
| 124 | + throw new Error(`Failed test # ${numtests - tests.length}`); |
| 125 | + } |
| 126 | + |
| 127 | + setImmediate(runTestWrap, true); |
| 128 | + }); |
| 129 | + |
| 130 | + repl.input.run(opts.test); |
| 131 | + }); |
| 132 | +} |
| 133 | + |
| 134 | +// run the tests |
| 135 | +runTest(); |
0 commit comments