|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +// Flags: --expose-internals |
| 4 | + |
| 5 | +constcommon=require('../common'); |
| 6 | +conststream=require('stream'); |
| 7 | +constREPL=require('internal/repl'); |
| 8 | +constassert=require('assert'); |
| 9 | +constfs=require('fs'); |
| 10 | +constpath=require('path'); |
| 11 | + |
| 12 | +consttmpdir=require('../common/tmpdir'); |
| 13 | +tmpdir.refresh(); |
| 14 | + |
| 15 | +constdefaultHistoryPath=path.join(tmpdir.path,'.node_repl_history'); |
| 16 | + |
| 17 | +// Create an input stream specialized for testing an array of actions |
| 18 | +classActionStreamextendsstream.Stream{ |
| 19 | +run(data){ |
| 20 | +const_iter=data[Symbol.iterator](); |
| 21 | +constdoAction=()=>{ |
| 22 | +constnext=_iter.next(); |
| 23 | +if(next.done){ |
| 24 | +// Close the repl. Note that it must have a clean prompt to do so. |
| 25 | +this.emit('keypress','',{ctrl: true,name: 'd'}); |
| 26 | +return; |
| 27 | +} |
| 28 | +constaction=next.value; |
| 29 | + |
| 30 | +if(typeofaction==='object'){ |
| 31 | +this.emit('keypress','',action); |
| 32 | +}else{ |
| 33 | +this.emit('data',`${action}`); |
| 34 | +} |
| 35 | +setImmediate(doAction); |
| 36 | +}; |
| 37 | +setImmediate(doAction); |
| 38 | +} |
| 39 | +resume(){} |
| 40 | +pause(){} |
| 41 | +} |
| 42 | +ActionStream.prototype.readable=true; |
| 43 | + |
| 44 | + |
| 45 | +// Mock keys |
| 46 | +constENTER={name: 'enter'}; |
| 47 | +constUP={name: 'up'}; |
| 48 | +constDOWN={name: 'down'}; |
| 49 | + |
| 50 | +constprompt='> '; |
| 51 | + |
| 52 | +consttests=[ |
| 53 | +{// creates few history to navigate for |
| 54 | +env: {NODE_REPL_HISTORY: defaultHistoryPath}, |
| 55 | +test: ['let ab = 45',ENTER, |
| 56 | +'555 + 909',ENTER, |
| 57 | +'{key : {key2 :[] }}',ENTER], |
| 58 | +expected: [], |
| 59 | +clean: false |
| 60 | +}, |
| 61 | +{ |
| 62 | +env: {NODE_REPL_HISTORY: defaultHistoryPath}, |
| 63 | +test: [UP,UP,UP,UP,DOWN,DOWN,DOWN], |
| 64 | +expected: [prompt, |
| 65 | +`${prompt}{key : {key2 :[] }}`, |
| 66 | +`${prompt}555 + 909`, |
| 67 | +`${prompt}let ab = 45`, |
| 68 | +`${prompt}555 + 909`, |
| 69 | +`${prompt}{key : {key2 :[] }}`, |
| 70 | +prompt], |
| 71 | +clean: true |
| 72 | +} |
| 73 | +]; |
| 74 | +constnumtests=tests.length; |
| 75 | + |
| 76 | +construnTestWrap=common.mustCall(runTest,numtests); |
| 77 | + |
| 78 | +functioncleanupTmpFile(){ |
| 79 | +try{ |
| 80 | +// Write over the file, clearing any history |
| 81 | +fs.writeFileSync(defaultHistoryPath,''); |
| 82 | +}catch(err){ |
| 83 | +if(err.code==='ENOENT')returntrue; |
| 84 | +throwerr; |
| 85 | +} |
| 86 | +returntrue; |
| 87 | +} |
| 88 | + |
| 89 | +functionrunTest(){ |
| 90 | +constopts=tests.shift(); |
| 91 | +if(!opts)return;// All done |
| 92 | + |
| 93 | +constenv=opts.env; |
| 94 | +consttest=opts.test; |
| 95 | +constexpected=opts.expected; |
| 96 | + |
| 97 | +REPL.createInternalRepl(env,{ |
| 98 | +input: newActionStream(), |
| 99 | +output: newstream.Writable({ |
| 100 | +write(chunk,_,next){ |
| 101 | +constoutput=chunk.toString(); |
| 102 | + |
| 103 | +if(output.charCodeAt(0)===27||/^[\r\n]+$/.test(output)){ |
| 104 | +returnnext(); |
| 105 | +} |
| 106 | + |
| 107 | +if(expected.length){ |
| 108 | +assert.strictEqual(output,expected[0]); |
| 109 | +expected.shift(); |
| 110 | +} |
| 111 | + |
| 112 | +next(); |
| 113 | +} |
| 114 | +}), |
| 115 | +prompt: prompt, |
| 116 | +useColors: false, |
| 117 | +terminal: true |
| 118 | +},function(err,repl){ |
| 119 | +if(err){ |
| 120 | +console.error(`Failed test # ${numtests-tests.length}`); |
| 121 | +throwerr; |
| 122 | +} |
| 123 | + |
| 124 | +repl.once('close',()=>{ |
| 125 | +if(opts.clean) |
| 126 | +cleanupTmpFile(); |
| 127 | + |
| 128 | +if(expected.length!==0){ |
| 129 | +thrownewError(`Failed test # ${numtests-tests.length}`); |
| 130 | +} |
| 131 | +setImmediate(runTestWrap,true); |
| 132 | +}); |
| 133 | + |
| 134 | +repl.inputStream.run(test); |
| 135 | +}); |
| 136 | +} |
| 137 | + |
| 138 | +// run the tests |
| 139 | +runTest(); |
0 commit comments