11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci// Flags: --expose-internals
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciconst common = require('../common');
61cb0ef41Sopenharmony_ciconst stream = require('stream');
71cb0ef41Sopenharmony_ciconst repl = require('internal/repl');
81cb0ef41Sopenharmony_ciconst assert = require('assert');
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci// Array of [useGlobal, expectedResult] pairs
111cb0ef41Sopenharmony_ciconst globalTestCases = [
121cb0ef41Sopenharmony_ci  [false, 'undefined'],
131cb0ef41Sopenharmony_ci  [true, '\'tacos\''],
141cb0ef41Sopenharmony_ci  [undefined, 'undefined'],
151cb0ef41Sopenharmony_ci];
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ciconst globalTest = (useGlobal, cb, output) => (err, repl) => {
181cb0ef41Sopenharmony_ci  if (err)
191cb0ef41Sopenharmony_ci    return cb(err);
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci  let str = '';
221cb0ef41Sopenharmony_ci  output.on('data', (data) => (str += data));
231cb0ef41Sopenharmony_ci  global.lunch = 'tacos';
241cb0ef41Sopenharmony_ci  repl.write('global.lunch;\n');
251cb0ef41Sopenharmony_ci  repl.close();
261cb0ef41Sopenharmony_ci  delete global.lunch;
271cb0ef41Sopenharmony_ci  cb(null, str.trim());
281cb0ef41Sopenharmony_ci};
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci// Test how the global object behaves in each state for useGlobal
311cb0ef41Sopenharmony_cifor (const [option, expected] of globalTestCases) {
321cb0ef41Sopenharmony_ci  runRepl(option, globalTest, common.mustSucceed((output) => {
331cb0ef41Sopenharmony_ci    assert.strictEqual(output, expected);
341cb0ef41Sopenharmony_ci  }));
351cb0ef41Sopenharmony_ci}
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci// Test how shadowing the process object via `let`
381cb0ef41Sopenharmony_ci// behaves in each useGlobal state. Note: we can't
391cb0ef41Sopenharmony_ci// actually test the state when useGlobal is true,
401cb0ef41Sopenharmony_ci// because the exception that's generated is caught
411cb0ef41Sopenharmony_ci// (see below), but errors are printed, and the test
421cb0ef41Sopenharmony_ci// suite is aware of it, causing a failure to be flagged.
431cb0ef41Sopenharmony_ci//
441cb0ef41Sopenharmony_ciconst processTestCases = [false, undefined];
451cb0ef41Sopenharmony_ciconst processTest = (useGlobal, cb, output) => (err, repl) => {
461cb0ef41Sopenharmony_ci  if (err)
471cb0ef41Sopenharmony_ci    return cb(err);
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci  let str = '';
501cb0ef41Sopenharmony_ci  output.on('data', (data) => (str += data));
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci  // If useGlobal is false, then `let process` should work
531cb0ef41Sopenharmony_ci  repl.write('let process;\n');
541cb0ef41Sopenharmony_ci  repl.write('21 * 2;\n');
551cb0ef41Sopenharmony_ci  repl.close();
561cb0ef41Sopenharmony_ci  cb(null, str.trim());
571cb0ef41Sopenharmony_ci};
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_cifor (const option of processTestCases) {
601cb0ef41Sopenharmony_ci  runRepl(option, processTest, common.mustSucceed((output) => {
611cb0ef41Sopenharmony_ci    assert.strictEqual(output, 'undefined\n42');
621cb0ef41Sopenharmony_ci  }));
631cb0ef41Sopenharmony_ci}
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_cifunction runRepl(useGlobal, testFunc, cb) {
661cb0ef41Sopenharmony_ci  const inputStream = new stream.PassThrough();
671cb0ef41Sopenharmony_ci  const outputStream = new stream.PassThrough();
681cb0ef41Sopenharmony_ci  const opts = {
691cb0ef41Sopenharmony_ci    input: inputStream,
701cb0ef41Sopenharmony_ci    output: outputStream,
711cb0ef41Sopenharmony_ci    useGlobal: useGlobal,
721cb0ef41Sopenharmony_ci    useColors: false,
731cb0ef41Sopenharmony_ci    terminal: false,
741cb0ef41Sopenharmony_ci    prompt: ''
751cb0ef41Sopenharmony_ci  };
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_ci  repl.createInternalRepl(
781cb0ef41Sopenharmony_ci    process.env,
791cb0ef41Sopenharmony_ci    opts,
801cb0ef41Sopenharmony_ci    testFunc(useGlobal, cb, opts.output));
811cb0ef41Sopenharmony_ci}
82