• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  }
41});
42
43// Create a dummy stream that does nothing
44const stream = new ArrayStream();
45
46// 1, mostly defaults
47const r1 = repl.start({
48  input: stream,
49  output: stream,
50  terminal: true
51});
52
53assert.strictEqual(r1.input, stream);
54assert.strictEqual(r1.output, stream);
55assert.strictEqual(r1.input, r1.inputStream);
56assert.strictEqual(r1.output, r1.outputStream);
57assert.strictEqual(r1.terminal, true);
58assert.strictEqual(r1.useColors, r1.terminal);
59assert.strictEqual(r1.useGlobal, false);
60assert.strictEqual(r1.ignoreUndefined, false);
61assert.strictEqual(r1.replMode, repl.REPL_MODE_SLOPPY);
62assert.strictEqual(r1.historySize, 30);
63
64// 2
65function writer() {}
66
67function evaler() {}
68const r2 = repl.start({
69  input: stream,
70  output: stream,
71  terminal: false,
72  useColors: true,
73  useGlobal: true,
74  ignoreUndefined: true,
75  eval: evaler,
76  writer: writer,
77  replMode: repl.REPL_MODE_STRICT,
78  historySize: 50
79});
80assert.strictEqual(r2.input, stream);
81assert.strictEqual(r2.output, stream);
82assert.strictEqual(r2.input, r2.inputStream);
83assert.strictEqual(r2.output, r2.outputStream);
84assert.strictEqual(r2.terminal, false);
85assert.strictEqual(r2.useColors, true);
86assert.strictEqual(r2.useGlobal, true);
87assert.strictEqual(r2.ignoreUndefined, true);
88assert.strictEqual(r2.writer, writer);
89assert.strictEqual(r2.replMode, repl.REPL_MODE_STRICT);
90assert.strictEqual(r2.historySize, 50);
91
92// 3, breakEvalOnSigint and eval supplied together should cause a throw
93const r3 = () => repl.start({
94  breakEvalOnSigint: true,
95  eval: true
96});
97
98assert.throws(r3, {
99  code: 'ERR_INVALID_REPL_EVAL_CONFIG',
100  name: 'TypeError',
101  message: 'Cannot specify both "breakEvalOnSigint" and "eval" for REPL'
102});
103
104// 4, Verify that defaults are used when no arguments are provided
105const r4 = repl.start();
106
107assert.strictEqual(r4.getPrompt(), '> ');
108assert.strictEqual(r4.input, process.stdin);
109assert.strictEqual(r4.output, process.stdout);
110assert.strictEqual(r4.terminal, !!r4.output.isTTY);
111assert.strictEqual(r4.useColors, r4.terminal);
112assert.strictEqual(r4.useGlobal, false);
113assert.strictEqual(r4.ignoreUndefined, false);
114assert.strictEqual(r4.replMode, repl.REPL_MODE_SLOPPY);
115assert.strictEqual(r4.historySize, 30);
116r4.close();
117
118// Check the standalone REPL
119{
120  const child = cp.spawn(process.execPath, ['--interactive']);
121  let output = '';
122
123  child.stdout.setEncoding('utf8');
124  child.stdout.on('data', (data) => {
125    output += data;
126  });
127
128  child.on('exit', common.mustCall(() => {
129    const results = output.replace(/^> /mg, '').split('\n').slice(2);
130    assert.deepStrictEqual(results, ['undefined', '']);
131  }));
132
133  child.stdin.write(
134    'assert.ok(util.inspect(repl.repl, {depth: -1}).includes("REPLServer"));\n'
135  );
136  child.stdin.write('.exit');
137  child.stdin.end();
138}
139