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// Flags: --pending-deprecation 23 24'use strict'; 25const common = require('../common'); 26const ArrayStream = require('../common/arraystream'); 27const assert = require('assert'); 28const repl = require('repl'); 29const cp = require('child_process'); 30 31assert.strictEqual(repl.repl, undefined); 32repl._builtinLibs; // eslint-disable-line no-unused-expressions 33 34common.expectWarning({ 35 DeprecationWarning: { 36 DEP0142: 37 'repl._builtinLibs is deprecated. Check module.builtinModules instead', 38 DEP0141: 'repl.inputStream and repl.outputStream are deprecated. ' + 39 'Use repl.input and repl.output instead', 40 DEP0124: 'REPLServer.rli is deprecated', 41 } 42}); 43 44// Create a dummy stream that does nothing 45const stream = new ArrayStream(); 46 47// 1, mostly defaults 48const r1 = repl.start({ 49 input: stream, 50 output: stream, 51 terminal: true 52}); 53 54assert.strictEqual(r1.input, stream); 55assert.strictEqual(r1.output, stream); 56assert.strictEqual(r1.input, r1.inputStream); 57assert.strictEqual(r1.output, r1.outputStream); 58assert.strictEqual(r1.terminal, true); 59assert.strictEqual(r1.useColors, r1.terminal); 60assert.strictEqual(r1.useGlobal, false); 61assert.strictEqual(r1.ignoreUndefined, false); 62assert.strictEqual(r1.replMode, repl.REPL_MODE_SLOPPY); 63assert.strictEqual(r1.historySize, 30); 64 65// Test r1 for backwards compact 66assert.strictEqual(r1.rli.input, stream); 67assert.strictEqual(r1.rli.output, stream); 68assert.strictEqual(r1.rli.input, r1.inputStream); 69assert.strictEqual(r1.rli.output, r1.outputStream); 70assert.strictEqual(r1.rli.terminal, true); 71assert.strictEqual(r1.useColors, r1.rli.terminal); 72 73// 2 74function writer() {} 75 76function evaler() {} 77const r2 = repl.start({ 78 input: stream, 79 output: stream, 80 terminal: false, 81 useColors: true, 82 useGlobal: true, 83 ignoreUndefined: true, 84 eval: evaler, 85 writer: writer, 86 replMode: repl.REPL_MODE_STRICT, 87 historySize: 50 88}); 89assert.strictEqual(r2.input, stream); 90assert.strictEqual(r2.output, stream); 91assert.strictEqual(r2.input, r2.inputStream); 92assert.strictEqual(r2.output, r2.outputStream); 93assert.strictEqual(r2.terminal, false); 94assert.strictEqual(r2.useColors, true); 95assert.strictEqual(r2.useGlobal, true); 96assert.strictEqual(r2.ignoreUndefined, true); 97assert.strictEqual(r2.writer, writer); 98assert.strictEqual(r2.replMode, repl.REPL_MODE_STRICT); 99assert.strictEqual(r2.historySize, 50); 100 101// Test r2 for backwards compact 102assert.strictEqual(r2.rli.input, stream); 103assert.strictEqual(r2.rli.output, stream); 104assert.strictEqual(r2.rli.input, r2.inputStream); 105assert.strictEqual(r2.rli.output, r2.outputStream); 106assert.strictEqual(r2.rli.terminal, false); 107 108// 3, breakEvalOnSigint and eval supplied together should cause a throw 109const r3 = () => repl.start({ 110 breakEvalOnSigint: true, 111 eval: true 112}); 113 114assert.throws(r3, { 115 code: 'ERR_INVALID_REPL_EVAL_CONFIG', 116 name: 'TypeError', 117 message: 'Cannot specify both "breakEvalOnSigint" and "eval" for REPL' 118}); 119 120// 4, Verify that defaults are used when no arguments are provided 121const r4 = repl.start(); 122 123assert.strictEqual(r4.getPrompt(), '> '); 124assert.strictEqual(r4.input, process.stdin); 125assert.strictEqual(r4.output, process.stdout); 126assert.strictEqual(r4.terminal, !!r4.output.isTTY); 127assert.strictEqual(r4.useColors, r4.terminal); 128assert.strictEqual(r4.useGlobal, false); 129assert.strictEqual(r4.ignoreUndefined, false); 130assert.strictEqual(r4.replMode, repl.REPL_MODE_SLOPPY); 131assert.strictEqual(r4.historySize, 30); 132r4.close(); 133 134// Check the standalone REPL 135{ 136 const child = cp.spawn(process.execPath, ['--interactive']); 137 let output = ''; 138 139 child.stdout.setEncoding('utf8'); 140 child.stdout.on('data', (data) => { 141 output += data; 142 }); 143 144 child.on('exit', common.mustCall(() => { 145 const results = output.replace(/^> /mg, '').split('\n').slice(2); 146 assert.deepStrictEqual(results, ['undefined', '']); 147 })); 148 149 child.stdin.write( 150 'assert.ok(util.inspect(repl.repl, {depth: -1}).includes("REPLServer"));\n' 151 ); 152 child.stdin.write('.exit'); 153 child.stdin.end(); 154} 155