|
| 1 | +// Copyright Joyent, Inc. and other Node contributors. |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a |
| 4 | +// copy of this software and associated documentation files (the |
| 5 | +// "Software"), to deal in the Software without restriction, including |
| 6 | +// without limitation the rights to use, copy, modify, merge, publish, |
| 7 | +// distribute, sublicense, and/or sell copies of the Software, and to permit |
| 8 | +// persons to whom the Software is furnished to do so, subject to the |
| 9 | +// following conditions: |
| 10 | +// |
| 11 | +// The above copyright notice and this permission notice shall be included |
| 12 | +// in all copies or substantial portions of the Software. |
| 13 | +// |
| 14 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 15 | +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 16 | +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN |
| 17 | +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| 18 | +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 19 | +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
| 20 | +// USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 21 | + |
| 22 | +'use strict'; |
| 23 | + |
| 24 | +const common = require('../common'); |
| 25 | +const ArrayStream = require('../common/arraystream'); |
| 26 | +const { describe, it } = require('node:test'); |
| 27 | +const assert = require('assert'); |
| 28 | + |
| 29 | +const repl = require('repl'); |
| 30 | + |
| 31 | +function prepareREPL() { |
| 32 | + const input = new ArrayStream(); |
| 33 | + const replServer = repl.start({ |
| 34 | + prompt: '', |
| 35 | + input, |
| 36 | + output: process.stdout, |
| 37 | + allowBlockingCompletions: true, |
| 38 | + }); |
| 39 | + |
| 40 | + // Some errors are passed to the domain, but do not callback |
| 41 | + replServer._domain.on('error', assert.ifError); |
| 42 | + |
| 43 | + return { replServer, input }; |
| 44 | +} |
| 45 | + |
| 46 | +function getNoResultsFunction() { |
| 47 | + return common.mustSucceed((data) => { |
| 48 | + assert.deepStrictEqual(data[0], []); |
| 49 | + }); |
| 50 | +} |
| 51 | + |
| 52 | +describe('REPL tab completion without side effects', () => { |
| 53 | + const setup = [ |
| 54 | + 'globalThis.counter = 0;', |
| 55 | + 'function incCounter() { return counter++; }', |
| 56 | + 'const arr = [{ bar: "baz" }];', |
| 57 | + ]; |
| 58 | + // None of these expressions should affect the value of `counter` |
| 59 | + for (const code of [ |
| 60 | + 'incCounter().', |
| 61 | + 'a=(counter+=1).foo.', |
| 62 | + 'a=(counter++).foo.', |
| 63 | + 'for((counter)of[1])foo.', |
| 64 | + 'for((counter)in{1:1})foo.', |
| 65 | + 'arr[incCounter()].b', |
| 66 | + ]) { |
| 67 | + it(`does not evaluate with side effects (${code})`, async () => { |
| 68 | + const { replServer, input } = prepareREPL(); |
| 69 | + input.run(setup); |
| 70 | + |
| 71 | + replServer.complete(code, getNoResultsFunction()); |
| 72 | + |
| 73 | + assert.strictEqual(replServer.context.counter, 0); |
| 74 | + replServer.close(); |
| 75 | + }); |
| 76 | + } |
| 77 | +}); |
0 commit comments