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'; 23const common = require('../common'); 24const ArrayStream = require('../common/arraystream'); 25const assert = require('assert'); 26const util = require('util'); 27const repl = require('repl'); 28 29const putIn = new ArrayStream(); 30repl.start('', putIn, null, true); 31 32test1(); 33 34function test1() { 35 let gotWrite = false; 36 putIn.write = function(data) { 37 gotWrite = true; 38 if (data.length) { 39 40 // Inspect output matches repl output 41 assert.strictEqual(data, 42 `${util.inspect(require('fs'), null, 2, false)}\n`); 43 // Globally added lib matches required lib 44 assert.strictEqual(global.fs, require('fs')); 45 test2(); 46 } 47 }; 48 assert(!gotWrite); 49 putIn.run(['fs']); 50 assert(gotWrite); 51} 52 53function test2() { 54 let gotWrite = false; 55 putIn.write = function(data) { 56 gotWrite = true; 57 if (data.length) { 58 // REPL response error message 59 assert.strictEqual(data, '{}\n'); 60 // Original value wasn't overwritten 61 assert.strictEqual(val, global.url); 62 } 63 }; 64 const val = {}; 65 global.url = val; 66 common.allowGlobals(val); 67 assert(!gotWrite); 68 putIn.run(['url']); 69 assert(gotWrite); 70} 71