1'use strict'; 2 3// Flags: --expose-internals 4 5require('../common'); 6const stream = require('stream'); 7const { describe, test } = require('node:test'); 8const REPL = require('internal/repl'); 9const assert = require('assert'); 10const inspect = require('util').inspect; 11const { REPL_MODE_SLOPPY, REPL_MODE_STRICT } = require('repl'); 12 13const tests = [ 14 { 15 env: {}, 16 expected: { terminal: true, useColors: true } 17 }, 18 { 19 env: { NODE_DISABLE_COLORS: '1' }, 20 expected: { terminal: true, useColors: false } 21 }, 22 { 23 env: { NODE_DISABLE_COLORS: '1', FORCE_COLOR: '1' }, 24 expected: { terminal: true, useColors: true } 25 }, 26 { 27 env: { NODE_NO_READLINE: '1' }, 28 expected: { terminal: false, useColors: false } 29 }, 30 { 31 env: { TERM: 'dumb' }, 32 expected: { terminal: true, useColors: true } 33 }, 34 { 35 env: { TERM: 'dumb', FORCE_COLOR: '1' }, 36 expected: { terminal: true, useColors: true } 37 }, 38 { 39 env: { NODE_NO_READLINE: '1', NODE_DISABLE_COLORS: '1' }, 40 expected: { terminal: false, useColors: false } 41 }, 42 { 43 env: { NODE_NO_READLINE: '0' }, 44 expected: { terminal: true, useColors: true } 45 }, 46 { 47 env: { NODE_REPL_MODE: 'sloppy' }, 48 expected: { terminal: true, useColors: true, replMode: REPL_MODE_SLOPPY } 49 }, 50 { 51 env: { NODE_REPL_MODE: 'strict' }, 52 expected: { terminal: true, useColors: true, replMode: REPL_MODE_STRICT } 53 }, 54]; 55 56function run(test) { 57 const env = test.env; 58 const expected = test.expected; 59 60 const opts = { 61 terminal: true, 62 input: new stream.Readable({ read() {} }), 63 output: new stream.Writable({ write() {} }) 64 }; 65 66 Object.assign(process.env, env); 67 68 return new Promise((resolve) => { 69 REPL.createInternalRepl(process.env, opts, function(err, repl) { 70 assert.ifError(err); 71 72 assert.strictEqual(repl.terminal, expected.terminal, 73 `Expected ${inspect(expected)} with ${inspect(env)}`); 74 assert.strictEqual(repl.useColors, expected.useColors, 75 `Expected ${inspect(expected)} with ${inspect(env)}`); 76 assert.strictEqual(repl.replMode, expected.replMode || REPL_MODE_SLOPPY, 77 `Expected ${inspect(expected)} with ${inspect(env)}`); 78 for (const key of Object.keys(env)) { 79 delete process.env[key]; 80 } 81 repl.close(); 82 resolve(); 83 }); 84 }); 85} 86 87describe('REPL environment variables', { concurrency: 1 }, () => { 88 tests.forEach((testCase) => test(inspect(testCase.env), () => run(testCase))); 89}); 90