-
Notifications
You must be signed in to change notification settings - Fork 30k
/
test-repl.js
867 lines (810 loc) Β· 19.9 KB
/
test-repl.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
'use strict';
const common = require('../common');
const fixtures = require('../common/fixtures');
const tmpdir = require('../common/tmpdir');
const assert = require('assert');
const net = require('net');
const repl = require('repl');
const { inspect } = require('util');
const message = 'Read, Eval, Print Loop';
const prompt_unix = 'node via Unix socket> ';
const prompt_tcp = 'node via TCP socket> ';
// absolute path to test/fixtures/a.js
const moduleFilename = fixtures.path('a');
// function for REPL to run
global.invoke_me = function(arg) {
return `invoked ${arg}`;
};
// Helpers for describing the expected output:
const kArrow = /^ *\^+ *$/; // Arrow of ^ pointing to syntax error location
const kSource = Symbol('kSource'); // Placeholder standing for input readback
async function runReplTests(socket, prompt, tests) {
let lineBuffer = '';
for (const { send, expect } of tests) {
// Expect can be a single line or multiple lines
const expectedLines = Array.isArray(expect) ? expect : [ expect ];
console.error('out:', JSON.stringify(send));
socket.write(`${send}\n`);
for (let expectedLine of expectedLines) {
// Special value: kSource refers to last sent source text
if (expectedLine === kSource)
expectedLine = send;
while (!lineBuffer.includes('\n')) {
lineBuffer += await event(socket, expect);
// Cut away the initial prompt
while (lineBuffer.startsWith(prompt))
lineBuffer = lineBuffer.substr(prompt.length);
// Allow to match partial text if no newline was received, because
// sending newlines from the REPL itself would be redundant
// (e.g. in the `... ` multiline prompt: The user already pressed
// enter for that, so the REPL shouldn't do it again!).
if (lineBuffer === expectedLine && !expectedLine.includes('\n'))
lineBuffer += '\n';
}
// Split off the current line.
const newlineOffset = lineBuffer.indexOf('\n');
let actualLine = lineBuffer.substr(0, newlineOffset);
lineBuffer = lineBuffer.substr(newlineOffset + 1);
// This might have been skipped in the loop above because the buffer
// already contained a \n to begin with and the entire loop was skipped.
while (actualLine.startsWith(prompt))
actualLine = actualLine.substr(prompt.length);
console.error('in:', JSON.stringify(actualLine));
// Match a string directly, or a RegExp through .test().
if (typeof expectedLine === 'string') {
assert.strictEqual(actualLine, expectedLine);
} else {
assert(expectedLine.test(actualLine),
`${actualLine} match ${expectedLine}`);
}
}
}
const remainder = socket.read();
assert(remainder === '' || remainder === null);
}
const unixTests = [
{
send: '',
expect: ''
},
{
send: 'message',
expect: `'${message}'`
},
{
send: 'invoke_me(987)',
expect: '\'invoked 987\''
},
{
send: 'a = 12345',
expect: '12345'
},
{
send: '{a:1}',
expect: '{ a: 1 }'
}
];
const strictModeTests = [
{
send: 'ref = 1',
expect: ['Thrown:', /^ReferenceError:\s/]
}
];
const errorTests = [
// Uncaught error throws and prints out
{
send: 'throw new Error(\'test error\');',
expect: ['Thrown:', 'Error: test error']
},
{
send: "throw { foo: 'bar' };",
expect: "Thrown: { foo: 'bar' }"
},
// Common syntax error is treated as multiline command
{
send: 'function test_func() {',
expect: '... '
},
// You can recover with the .break command
{
send: '.break',
expect: ''
},
// But passing the same string to eval() should throw
{
send: 'eval("function test_func() {")',
expect: ['Thrown:', /^SyntaxError: /]
},
// Can handle multiline template literals
{
send: '`io.js',
expect: '... '
},
// Special REPL commands still available
{
send: '.break',
expect: ''
},
// Template expressions
{
send: '`io.js ${"1.0"',
expect: '... '
},
{
send: '+ ".2"}`',
expect: '\'io.js 1.0.2\''
},
{
send: '`io.js ${',
expect: '... '
},
{
send: '"1.0" + ".2"}`',
expect: '\'io.js 1.0.2\''
},
// Dot prefix in multiline commands aren't treated as commands
{
send: '("a"',
expect: '... '
},
{
send: '.charAt(0))',
expect: '\'a\''
},
// Floating point numbers are not interpreted as REPL commands.
{
send: '.1234',
expect: '0.1234'
},
// Floating point expressions are not interpreted as REPL commands
{
send: '.1+.1',
expect: '0.2'
},
// Can parse valid JSON
{
send: 'JSON.parse(\'{"valid": "json"}\');',
expect: '{ valid: \'json\' }'
},
// Invalid input to JSON.parse error is special case of syntax error,
// should throw
{
send: 'JSON.parse(\'{invalid: \\\'json\\\'}\');',
expect: ['Thrown:', /^SyntaxError: /]
},
// End of input to JSON.parse error is special case of syntax error,
// should throw
{
send: 'JSON.parse(\'066\');',
expect: ['Thrown:', /^SyntaxError: /]
},
// should throw
{
send: 'JSON.parse(\'{\');',
expect: ['Thrown:', /^SyntaxError: /]
},
// invalid RegExps are a special case of syntax error,
// should throw
{
send: '/(/;',
expect: ['Thrown:', /^SyntaxError: /]
},
// invalid RegExp modifiers are a special case of syntax error,
// should throw (GH-4012)
{
send: 'new RegExp("foo", "wrong modifier");',
expect: ['Thrown:', /^SyntaxError: /]
},
// Strict mode syntax errors should be caught (GH-5178)
{
send: '(function() { "use strict"; return 0755; })()',
expect: [
'Thrown:',
kSource,
kArrow,
'',
/^SyntaxError: /
]
},
{
send: '(function(a, a, b) { "use strict"; return a + b + c; })()',
expect: [
'Thrown:',
kSource,
kArrow,
'',
/^SyntaxError: /
]
},
{
send: '(function() { "use strict"; with (this) {} })()',
expect: [
'Thrown:',
kSource,
kArrow,
'',
/^SyntaxError: /
]
},
{
send: '(function() { "use strict"; var x; delete x; })()',
expect: [
'Thrown:',
kSource,
kArrow,
'',
/^SyntaxError: /
]
},
{
send: '(function() { "use strict"; eval = 17; })()',
expect: [
'Thrown:',
kSource,
kArrow,
'',
/^SyntaxError: /
]
},
{
send: '(function() { "use strict"; if (true) function f() { } })()',
expect: [
'Thrown:',
kSource,
kArrow,
'',
/^SyntaxError: /
]
},
// Named functions can be used:
{
send: 'function blah() { return 1; }',
expect: 'undefined'
},
{
send: 'blah()',
expect: '1'
},
// Functions should not evaluate twice (#2773)
{
send: 'var I = [1,2,3,function() {}]; I.pop()',
expect: '[Function]'
},
// Multiline object
{
send: '{ a: ',
expect: '... '
},
{
send: '1 }',
expect: '{ a: 1 }'
},
// Multiline string-keyed object (e.g. JSON)
{
send: '{ "a": ',
expect: '... '
},
{
send: '1 }',
expect: '{ a: 1 }'
},
// Multiline anonymous function with comment
{
send: '(function() {',
expect: '... '
},
{
send: '// blah',
expect: '... '
},
{
send: 'return 1;',
expect: '... '
},
{
send: '})()',
expect: '1'
},
// Multiline function call
{
send: 'function f(){}; f(f(1,',
expect: '... '
},
{
send: '2)',
expect: '... '
},
{
send: ')',
expect: 'undefined'
},
// npm prompt error message
{
send: 'npm install foobar',
expect: [
'npm should be run outside of the node repl, in your normal shell.',
'(Press Control-D to exit.)'
]
},
{
send: '(function() {\n\nreturn 1;\n})()',
expect: '... ... ... 1'
},
{
send: '{\n\na: 1\n}',
expect: '... ... ... { a: 1 }'
},
{
send: 'url.format("http://google.com")',
expect: '\'http://google.com/\''
},
{
send: 'var path = 42; path',
expect: '42'
},
// This makes sure that we don't print `undefined` when we actually print
// the error message
{
send: '.invalid_repl_command',
expect: 'Invalid REPL keyword'
},
// This makes sure that we don't crash when we use an inherited property as
// a REPL command
{
send: '.toString',
expect: 'Invalid REPL keyword'
},
// Fail when we are not inside a String and a line continuation is used
{
send: '[] \\',
expect: [
'Thrown:',
kSource,
kArrow,
'',
/^SyntaxError: /
]
},
// Do not fail when a String is created with line continuation
{
send: '\'the\\\nfourth\\\neye\'',
expect: ['... ... \'thefourtheye\'']
},
// Don't fail when a partial String is created and line continuation is used
// with whitespace characters at the end of the string. We are to ignore it.
// This test is to make sure that we properly remove the whitespace
// characters at the end of line, unlike the buggy `trimWhitespace` function
{
send: ' \t .break \t ',
expect: ''
},
// Multiline strings preserve whitespace characters in them
{
send: '\'the \\\n fourth\t\t\\\n eye \'',
expect: '... ... \'the fourth\\t\\t eye \''
},
// More than one multiline strings also should preserve whitespace chars
{
send: '\'the \\\n fourth\' + \'\t\t\\\n eye \'',
expect: '... ... \'the fourth\\t\\t eye \''
},
// using REPL commands within a string literal should still work
{
send: '\'\\\n.break',
expect: '... ' + prompt_unix
},
// Using REPL command "help" within a string literal should still work
{
send: '\'thefourth\\\n.help\neye\'',
expect: [
/\.break/,
/\.clear/,
/\.exit/,
/\.help/,
/\.load/,
/\.save/,
'',
'Press ^C to abort current expression, ^D to exit the repl',
/'thefourtheye'/
]
},
// Empty lines in the REPL should be allowed
{
send: '\n\r\n\r\n',
expect: ''
},
// Empty lines in the string literals should not affect the string
{
send: '\'the\\\n\\\nfourtheye\'\n',
expect: '... ... \'thefourtheye\''
},
// Regression test for https://github.com/nodejs/node/issues/597
{
send: '/(.)(.)(.)(.)(.)(.)(.)(.)(.)/.test(\'123456789\')\n',
expect: 'true'
},
// The following test's result depends on the RegExp's match from the above
{
send: 'RegExp.$1\nRegExp.$2\nRegExp.$3\nRegExp.$4\nRegExp.$5\n' +
'RegExp.$6\nRegExp.$7\nRegExp.$8\nRegExp.$9\n',
expect: ['\'1\'', '\'2\'', '\'3\'', '\'4\'', '\'5\'', '\'6\'',
'\'7\'', '\'8\'', '\'9\'']
},
// Regression tests for https://github.com/nodejs/node/issues/2749
{
send: 'function x() {\nreturn \'\\n\';\n }',
expect: '... ... undefined'
},
{
send: 'function x() {\nreturn \'\\\\\';\n }',
expect: '... ... undefined'
},
// Regression tests for https://github.com/nodejs/node/issues/3421
{
send: 'function x() {\n//\'\n }',
expect: '... ... undefined'
},
{
send: 'function x() {\n//"\n }',
expect: '... ... undefined'
},
{
send: 'function x() {//\'\n }',
expect: '... undefined'
},
{
send: 'function x() {//"\n }',
expect: '... undefined'
},
{
send: 'function x() {\nvar i = "\'";\n }',
expect: '... ... undefined'
},
{
send: 'function x(/*optional*/) {}',
expect: 'undefined'
},
{
send: 'function x(/* // 5 */) {}',
expect: 'undefined'
},
{
send: '// /* 5 */',
expect: 'undefined'
},
{
send: '"//"',
expect: '\'//\''
},
{
send: '"data /*with*/ comment"',
expect: '\'data /*with*/ comment\''
},
{
send: 'function x(/*fn\'s optional params*/) {}',
expect: 'undefined'
},
{
send: '/* \'\n"\n\'"\'\n*/',
expect: '... ... ... undefined'
},
// REPL should get a normal require() function, not one that allows
// access to internal modules without the --expose_internals flag.
{
send: 'require("internal/repl")',
expect: [
'Thrown:',
/^{ Error: Cannot find module 'internal\/repl'/,
/^Require stack:/,
/^- <repl>/,
/^ at .*/,
/^ at .*/,
/^ at .*/,
/^ at .*/
]
},
// REPL should handle quotes within regexp literal in multiline mode
{
send: "function x(s) {\nreturn s.replace(/'/,'');\n}",
expect: '... ... undefined'
},
{
send: "function x(s) {\nreturn s.replace(/'/,'');\n}",
expect: '... ... undefined'
},
{
send: 'function x(s) {\nreturn s.replace(/"/,"");\n}',
expect: '... ... undefined'
},
{
send: 'function x(s) {\nreturn s.replace(/.*/,"");\n}',
expect: '... ... undefined'
},
{
send: '{ var x = 4; }',
expect: 'undefined'
},
// Illegal token is not recoverable outside string literal, RegExp literal,
// or block comment. https://github.com/nodejs/node/issues/3611
{
send: 'a = 3.5e',
expect: [
'Thrown:',
kSource,
kArrow,
'',
/^SyntaxError: /
]
},
// Mitigate https://github.com/nodejs/node/issues/548
{
send: 'function name(){ return "node"; };name()',
expect: '\'node\''
},
{
send: 'function name(){ return "nodejs"; };name()',
expect: '\'nodejs\''
},
// Avoid emitting repl:line-number for SyntaxError
{
send: 'a = 3.5e',
expect: [
'Thrown:',
kSource,
kArrow,
'',
/^SyntaxError: /
]
},
// Avoid emitting stack trace
{
send: 'a = 3.5e',
expect: [
'Thrown:',
kSource,
kArrow,
'',
/^SyntaxError: /
]
},
// https://github.com/nodejs/node/issues/9850
{
send: 'function* foo() {}; foo().next();',
expect: '{ value: undefined, done: true }'
},
{
send: 'function *foo() {}; foo().next();',
expect: '{ value: undefined, done: true }'
},
{
send: 'function*foo() {}; foo().next();',
expect: '{ value: undefined, done: true }'
},
{
send: 'function * foo() {}; foo().next()',
expect: '{ value: undefined, done: true }'
},
// https://github.com/nodejs/node/issues/9300
{
send: 'function foo() {\nvar bar = 1 / 1; // "/"\n}',
expect: '... ... undefined'
},
{
send: '(function() {\nreturn /foo/ / /bar/;\n}())',
expect: '... ... NaN'
},
{
send: '(function() {\nif (false) {} /bar"/;\n}())',
expect: '... ... undefined'
},
// https://github.com/nodejs/node/issues/16483
{
send: 'new Proxy({x:42}, {get(){throw null}});',
expect: 'Proxy [ { x: 42 }, { get: [Function: get] } ]'
},
{
send: 'repl.writer.options.showProxy = false, new Proxy({x:42}, {});',
expect: '{ x: 42 }'
},
// Newline within template string maintains whitespace.
{
send: '`foo \n`',
expect: '... \'foo \\n\''
},
// Whitespace is not evaluated.
{
send: ' \t \n',
expect: 'undefined'
},
// Do not parse `...[]` as a REPL keyword
{
send: '...[]',
expect: [
'Thrown:',
kSource,
kArrow,
'',
/^SyntaxError: /
]
},
// bring back the repl to prompt
{
send: '.break',
expect: ''
},
{
send: 'console.log("Missing comma in arg list" process.version)',
expect: [
'Thrown:',
kSource,
kArrow,
'',
/^SyntaxError: /
]
},
{
send: 'x = {\nfield\n{',
expect: [
'... ... Thrown:',
'{',
kArrow,
'',
/^SyntaxError: /
]
},
{
send: '(2 + 3))',
expect: [
'Thrown:',
kSource,
kArrow,
'',
/^SyntaxError: /
]
},
{
send: 'if (typeof process === "object"); {',
expect: '... '
},
{
send: 'console.log("process is defined");',
expect: '... '
},
{
send: '} else {',
expect: [
'Thrown:',
kSource,
kArrow,
'',
/^SyntaxError: /
]
},
];
const tcpTests = [
{
send: '',
expect: ''
},
{
send: 'invoke_me(333)',
expect: '\'invoked 333\''
},
{
send: 'a += 1',
expect: '12346'
},
{
send: `require(${JSON.stringify(moduleFilename)}).number`,
expect: '42'
}
];
(async function() {
{
const [ socket, replServer ] = await startUnixRepl();
await runReplTests(socket, prompt_unix, unixTests);
await runReplTests(socket, prompt_unix, errorTests);
replServer.replMode = repl.REPL_MODE_STRICT;
await runReplTests(socket, prompt_unix, strictModeTests);
socket.end();
}
{
const [ socket ] = await startTCPRepl();
await runReplTests(socket, prompt_tcp, tcpTests);
socket.end();
}
common.allowGlobals(...Object.values(global));
})();
function startTCPRepl() {
let resolveSocket, resolveReplServer;
const server = net.createServer(common.mustCall((socket) => {
assert.strictEqual(server, socket.server);
socket.on('end', common.mustCall(() => {
socket.end();
}));
resolveReplServer(repl.start(prompt_tcp, socket));
}));
server.listen(0, common.mustCall(() => {
const client = net.createConnection(server.address().port);
client.setEncoding('utf8');
client.on('connect', common.mustCall(() => {
assert.strictEqual(client.readable, true);
assert.strictEqual(client.writable, true);
resolveSocket(client);
}));
client.on('close', common.mustCall(() => {
server.close();
}));
}));
return Promise.all([
new Promise((resolve) => resolveSocket = resolve),
new Promise((resolve) => resolveReplServer = resolve)
]);
}
function startUnixRepl() {
let resolveSocket, resolveReplServer;
const server = net.createServer(common.mustCall((socket) => {
assert.strictEqual(server, socket.server);
socket.on('end', common.mustCall(() => {
socket.end();
}));
const replServer = repl.start({
prompt: prompt_unix,
input: socket,
output: socket,
useGlobal: true
});
replServer.context.message = message;
resolveReplServer(replServer);
}));
tmpdir.refresh();
server.listen(common.PIPE, common.mustCall(() => {
const client = net.createConnection(common.PIPE);
client.setEncoding('utf8');
client.on('connect', common.mustCall(() => {
assert.strictEqual(client.readable, true);
assert.strictEqual(client.writable, true);
resolveSocket(client);
}));
client.on('close', common.mustCall(() => {
server.close();
}));
}));
return Promise.all([
new Promise((resolve) => resolveSocket = resolve),
new Promise((resolve) => resolveReplServer = resolve)
]);
}
function event(ee, expected) {
return new Promise((resolve, reject) => {
const timeout = setTimeout(() => {
const data = inspect(expected, { compact: false });
const msg = `The REPL did not reply as expected for:\n\n${data}`;
reject(new Error(msg));
}, common.platformTimeout(500));
ee.once('data', common.mustCall((...args) => {
clearTimeout(timeout);
resolve(...args);
}));
});
}