-
Notifications
You must be signed in to change notification settings - Fork 3
/
tests.js
561 lines (488 loc) · 24.9 KB
/
tests.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
var assert = require('assert');
var readcommand = require('../index');
var CommandAutocomplete = require('../lib/autocomplete');
var InOut = require('./util/inout');
describe('Read', function() {
it('returns a simple single-line command', function(callback) {
var inout = new InOut();
readcommand.read({'input': inout.input(), 'output': inout.output()}, function(err, args, str) {
assert.strictEqual(args.length, 3);
assert.strictEqual(args[0], 'curl');
assert.strictEqual(args[1], 'https://www.google.ca');
assert.strictEqual(args[2], '--insecure');
assert.strictEqual(str, 'curl https://www.google.ca --insecure');
return callback();
});
inout.write('curl https://www.google.ca --insecure\n');
});
it('parses double-quotes with single-quotes properly in a single-line command', function(callback) {
var inout = new InOut();
readcommand.read({'input': inout.input(), 'output': inout.output()}, function(err, args, str) {
assert.strictEqual(args.length, 2);
assert.strictEqual(args[0], 'with \' single quote');
assert.strictEqual(args[1], 'with " double quote');
assert.strictEqual(str, '"with \' single quote" \'with " double quote\'');
return callback();
});
inout.write('"with \' single quote" \'with " double quote\'\n');
});
it('reads escaped quotes and back-slashes as verbatim input', function(callback) {
var inout = new InOut();
readcommand.read({'input': inout.input(), 'output': inout.output()}, function(err, args, str) {
assert.strictEqual(args.length, 3);
assert.strictEqual(args[0], '\'');
assert.strictEqual(args[1], '"');
assert.strictEqual(args[2], '\\');
assert.strictEqual(str, '\\\' \\" \\\\');
return callback();
});
inout.write('\\\' \\" \\\\\n');
});
it('spans to new line when newline is escaped', function(callback) {
var inout = new InOut();
// Receive first prompt
inout.once('data', function(data) {
assert.strictEqual(data.toString(), '> ');
// Write first line and wait for second prompt
inout.write('firstline \\\n');
inout.once('data', function(data) {
assert.strictEqual(data.toString(), '> ');
// Write second line. This will invoke the readcommand.read callback and we finish
// the test there
inout.write('secondline\n');
});
});
readcommand.read({'input': inout.input(), 'output': inout.output()}, function(err, args, str) {
assert.strictEqual(args.length, 2);
assert.strictEqual(args[0], 'firstline');
assert.strictEqual(args[1], 'secondline');
return callback();
});
});
it('consumes new line when there is an open double-quote', function(callback) {
var inout = new InOut();
// Receive first prompt
inout.once('data', function(data) {
assert.strictEqual(data.toString(), '> ');
// Write first line and wait for second prompt
inout.write('this line is "constrained by\n');
inout.once('data', function(data) {
assert.strictEqual(data.toString(), '> ');
// Write second line, which still doesn't close, wait for third prompt
inout.write('a double-\'\n');
inout.once('data', function(data) {
assert.strictEqual(data.toString(), '> ');
// Finish the last line. Test will finish in the readcommand.read callback
inout.write('and now we finish" it\n');
});
});
});
readcommand.read({'input': inout.input(), 'output': inout.output()}, function(err, args, str) {
assert.strictEqual(args.length, 5);
assert.strictEqual(args[0], 'this');
assert.strictEqual(args[1], 'line');
assert.strictEqual(args[2], 'is');
assert.strictEqual(args[3], 'constrained by\na double-\'\nand now we finish');
assert.strictEqual(args[4], 'it');
assert.strictEqual(str, 'this line is "constrained by\na double-\'\nand now we finish" it');
return callback();
});
});
it('consumes new line when there is an open single-quote', function(callback) {
var inout = new InOut();
// Receive first prompt
inout.once('data', function(data) {
assert.strictEqual(data.toString(), '> ');
// Write first line and wait for second prompt
inout.write('this line is \'constrained by\n');
inout.once('data', function(data) {
assert.strictEqual(data.toString(), '> ');
// Write second line, which still doesn't close, wait for third prompt
inout.write('a single-\\\'\n');
inout.once('data', function(data) {
assert.strictEqual(data.toString(), '> ');
// Finish the last line. Test will finish in the readcommand.read callback
inout.write('and now we finish\' it\n');
});
});
});
readcommand.read({'input': inout.input(), 'output': inout.output()}, function(err, args, str) {
assert.strictEqual(args.length, 5);
assert.strictEqual(args[0], 'this');
assert.strictEqual(args[1], 'line');
assert.strictEqual(args[2], 'is');
assert.strictEqual(args[3], 'constrained by\na single-\'\nand now we finish');
assert.strictEqual(args[4], 'it');
assert.strictEqual(str, 'this line is \'constrained by\na single-\\\'\nand now we finish\' it');
return callback();
});
});
it('shows custom prompts', function(callback) {
var inout = new InOut();
// Receive first prompt
inout.once('data', function(data) {
// Ensure the prompt is the custom "ps1" prompt
assert.strictEqual(data.toString(), 'ps1');
// Write first line and wait for second prompt
inout.write('firstline \\\n');
inout.once('data', function(data) {
// Ensure the prompt is the custom "ps2" prompt
assert.strictEqual(data.toString(), 'ps2');
// Write second line. This will invoke the readcommand.read callback and we finish
// the test there
inout.write('secondline\n');
});
});
readcommand.read({'input': inout.input(), 'output': inout.output(), 'ps1': 'ps1', 'ps2': 'ps2'}, function(err, args, str) {
assert.strictEqual(args.length, 2);
assert.strictEqual(args[0], 'firstline');
assert.strictEqual(args[1], 'secondline');
return callback();
});
});
});
describe('Loop', function() {
it('reads a series of commands from the input', function(callback) {
var inout = new InOut();
var one = false;
var two = false;
var three = false;
// Kick off the first prompt
inout.once('data', function(data) {
assert.strictEqual(data.toString(), '> ');
inout.write('one\n');
});
readcommand.loop({'input': inout.input(), 'output': inout.output()}, function(err, args, str, next) {
// Receive the "one" command
if (args[0] === 'one') {
one = true;
assert.strictEqual(two, false);
assert.strictEqual(three, false);
next();
inout.once('data', function(data) {
assert.strictEqual(data.toString(), '> ');
inout.write('two\n');
});
} else if (args[0] === 'two') {
two = true;
assert.strictEqual(one, true);
assert.strictEqual(three, false);
next();
inout.once('data', function(data) {
assert.strictEqual(data.toString(), '> ');
inout.write('three\n');
});
} else if (args[0] === 'three') {
three = true;
assert.strictEqual(one, true);
assert.strictEqual(two, true);
return callback();
} else {
assert.fail();
}
});
});
});
describe('Auto-complete', function() {
describe('#getAutocompleteArguments', function() {
it('returns an empty args array with an empty string', function(callback) {
CommandAutocomplete.getAutocompleteArguments(null, null, function(abort, args) {
assert.ok(!abort);
assert.strictEqual(args.length, 0);
return callback();
});
});
it('aborts when input string ends in an escape sequence', function(callback) {
CommandAutocomplete.getAutocompleteArguments(null, '\\', function(abort, args) {
assert.ok(abort);
return callback();
});
});
it('aborts when input string spans two lines due to escape sequence', function(callback) {
CommandAutocomplete.getAutocompleteArguments('my args\\\n', 'are', function(abort, args) {
assert.ok(abort);
return callback();
});
});
it('aborts when input string spans two lines due to double-quote', function(callback) {
CommandAutocomplete.getAutocompleteArguments('my "args\n', 'are', function(abort, args) {
assert.ok(abort);
return callback();
});
});
it('aborts when input string spans two lines due to single-quote', function(callback) {
CommandAutocomplete.getAutocompleteArguments('my \'args\n', 'are', function(abort, args) {
assert.ok(abort);
return callback();
});
});
it('does not abort when empty argument spans to second line with escape sequence', function(callback) {
CommandAutocomplete.getAutocompleteArguments('my args \\\n', '', function(abort, args) {
assert.ok(!abort);
assert.strictEqual(args.length, 3);
assert.strictEqual(args[0], 'my');
assert.strictEqual(args[1], 'args');
assert.strictEqual(args[2], '');
return callback();
});
});
it('provides unquoted arguments', function(callback) {
CommandAutocomplete.getAutocompleteArguments(null, 'my commands are', function(abort, args) {
assert.ok(!abort);
assert.strictEqual(args.length, 3);
assert.strictEqual(args[0], 'my');
assert.strictEqual(args[1], 'commands');
assert.strictEqual(args[2], 'are');
return callback();
});
});
it('provides double-quote arguments that are unclosed at the end', function(callback) {
CommandAutocomplete.getAutocompleteArguments(null, 'my commands "are so', function(abort, args) {
assert.ok(!abort);
assert.strictEqual(args.length, 3);
assert.strictEqual(args[0], 'my');
assert.strictEqual(args[1], 'commands');
assert.strictEqual(args[2], 'are so');
return callback();
});
});
it('provides single-quote arguments that are unclosed at the end', function(callback) {
CommandAutocomplete.getAutocompleteArguments(null, 'my commands \'are so', function(abort, args) {
assert.ok(!abort);
assert.strictEqual(args.length, 3);
assert.strictEqual(args[0], 'my');
assert.strictEqual(args[1], 'commands');
assert.strictEqual(args[2], 'are so');
return callback();
});
});
it('provides double-quote arguments that are closed at the end', function(callback) {
CommandAutocomplete.getAutocompleteArguments(null, 'my commands "are so"', function(abort, args) {
assert.ok(!abort);
assert.strictEqual(args.length, 3);
assert.strictEqual(args[0], 'my');
assert.strictEqual(args[1], 'commands');
assert.strictEqual(args[2], 'are so');
return callback();
});
});
it('provides single-quote arguments that are closed at the end', function(callback) {
CommandAutocomplete.getAutocompleteArguments(null, 'my commands \'are so\'', function(abort, args) {
assert.ok(!abort);
assert.strictEqual(args.length, 3);
assert.strictEqual(args[0], 'my');
assert.strictEqual(args[1], 'commands');
assert.strictEqual(args[2], 'are so');
return callback();
});
});
it('filters empty arguments except for the last one', function(callback) {
CommandAutocomplete.getAutocompleteArguments(null, ' my commands are ', function(abort, args) {
assert.ok(!abort);
assert.strictEqual(args.length, 4);
assert.strictEqual(args[0], 'my');
assert.strictEqual(args[1], 'commands');
assert.strictEqual(args[2], 'are');
assert.strictEqual(args[3], '');
return callback();
});
});
it('does not abort when final argument is fully in buffer due to escape sequence', function(callback) {
CommandAutocomplete.getAutocompleteArguments('first line wi\\\n', 'th args', function(abort, args) {
assert.ok(!abort);
assert.strictEqual(args.length, 4);
assert.strictEqual(args[0], 'first');
assert.strictEqual(args[1], 'line');
assert.strictEqual(args[2], 'with');
assert.strictEqual(args[3], 'args');
return callback();
});
});
it('does not abort when final argument is full in buffer due to double-quote', function(callback) {
CommandAutocomplete.getAutocompleteArguments('first line "wi\n', 'th" args', function(abort, args) {
assert.ok(!abort);
assert.strictEqual(args.length, 4);
assert.strictEqual(args[0], 'first');
assert.strictEqual(args[1], 'line');
assert.strictEqual(args[2], 'wi\nth');
assert.strictEqual(args[3], 'args');
return callback();
});
});
it('does not abort when final argument is full in buffer due to single-quote', function(callback) {
CommandAutocomplete.getAutocompleteArguments('first line \'wi\n', 'th\' args', function(abort, args) {
assert.ok(!abort);
assert.strictEqual(args.length, 4);
assert.strictEqual(args[0], 'first');
assert.strictEqual(args[1], 'line');
assert.strictEqual(args[2], 'wi\nth');
assert.strictEqual(args[3], 'args');
return callback();
});
});
it('does not abort when quoted arguments starts immediately on new line', function(callback) {
CommandAutocomplete.getAutocompleteArguments('first line \\\n', '"my argument"', function(abort, args) {
assert.ok(!abort);
assert.strictEqual(args.length, 3);
assert.strictEqual(args[0], 'first');
assert.strictEqual(args[1], 'line');
assert.strictEqual(args[2], 'my argument');
return callback();
});
});
});
describe('#getAutocompleteReplacements', function() {
it('replaces single line command at very start of line', function(callback) {
CommandAutocomplete.getAutocompleteReplacements(null, 'my', ['mycommandisbad'], function(replacements, toReplace) {
assert.strictEqual(replacements.length, 1);
assert.strictEqual(replacements[0], 'mycommandisbad ');
assert.strictEqual(toReplace, 'my');
return callback();
});
});
it('replaces single line command without quotes', function(callback) {
CommandAutocomplete.getAutocompleteReplacements(null, 'my command is good', ['bad'], function(replacements, toReplace) {
assert.strictEqual(replacements.length, 1);
assert.strictEqual(replacements[0], 'bad ');
assert.strictEqual(toReplace, 'good');
return callback();
});
});
it('replaces single line command with double-quotes', function(callback) {
CommandAutocomplete.getAutocompleteReplacements(null, 'my command is "good yes', ['good no'], function(replacements, toReplace) {
assert.strictEqual(replacements.length, 1);
assert.strictEqual(replacements[0], '"good no" ');
assert.strictEqual(toReplace, '"good yes');
return callback();
});
});
it('replaces single line command with single-quotes', function(callback) {
CommandAutocomplete.getAutocompleteReplacements(null, 'my command is \'good yes', ['good no'], function(replacements, toReplace) {
assert.strictEqual(replacements.length, 1);
assert.strictEqual(replacements[0], '\'good no\' ');
assert.strictEqual(toReplace, '\'good yes');
return callback();
});
});
it('replaces escaped multi-line command without quotes', function(callback) {
CommandAutocomplete.getAutocompleteReplacements('my command i\\\n', 's good', ['bad'], function(replacements, toReplace) {
assert.strictEqual(replacements.length, 1);
assert.strictEqual(replacements[0], 'bad ');
assert.strictEqual(toReplace, 'good');
return callback();
});
});
it('replaces escaped multi-line commands with double-quotes', function(callback) {
CommandAutocomplete.getAutocompleteReplacements('my command i\\\n', 's "good yes', ['good no'], function(replacements, toReplace) {
assert.strictEqual(replacements.length, 1);
assert.strictEqual(replacements[0], '"good no" ');
assert.strictEqual(toReplace, '"good yes');
return callback();
});
});
it('replaces escaped multi-line commands with single-quotes', function(callback) {
CommandAutocomplete.getAutocompleteReplacements('my command i\\\n', 's \'good yes', ['good no'], function(replacements, toReplace) {
assert.strictEqual(replacements.length, 1);
assert.strictEqual(replacements[0], '\'good no\' ');
assert.strictEqual(toReplace, '\'good yes');
return callback();
});
});
it('replaces double-quoted multi-line command without quotes', function(callback) {
CommandAutocomplete.getAutocompleteReplacements('my command "i\n', 's" good', ['bad'], function(replacements, toReplace) {
assert.strictEqual(replacements.length, 1);
assert.strictEqual(replacements[0], 'bad ');
assert.strictEqual(toReplace, 'good');
return callback();
});
});
it('replaces double-quoted multi-line commands with double-quotes', function(callback) {
CommandAutocomplete.getAutocompleteReplacements('my command "i\n', 's" "good yes', ['good no'], function(replacements, toReplace) {
assert.strictEqual(replacements.length, 1);
assert.strictEqual(replacements[0], '"good no" ');
assert.strictEqual(toReplace, '"good yes');
return callback();
});
});
it('replaces double-quoted multi-line commands with single-quotes', function(callback) {
CommandAutocomplete.getAutocompleteReplacements('my command "i\n', 's" \'good yes', ['good no'], function(replacements, toReplace) {
assert.strictEqual(replacements.length, 1);
assert.strictEqual(replacements[0], '\'good no\' ');
assert.strictEqual(toReplace, '\'good yes');
return callback();
});
});
it('replaces single-quoted multi-line command without quotes', function(callback) {
CommandAutocomplete.getAutocompleteReplacements('my command \'i\n', 's\' good', ['bad'], function(replacements, toReplace) {
assert.strictEqual(replacements.length, 1);
assert.strictEqual(replacements[0], 'bad ');
assert.strictEqual(toReplace, 'good');
return callback();
});
});
it('replaces single-quoted multi-line commands with double-quotes', function(callback) {
CommandAutocomplete.getAutocompleteReplacements('my command \'i\n', 's\' "good yes', ['good no'], function(replacements, toReplace) {
assert.strictEqual(replacements.length, 1);
assert.strictEqual(replacements[0], '"good no" ');
assert.strictEqual(toReplace, '"good yes');
return callback();
});
});
it('replaces single-quoted multi-line commands with single-quotes', function(callback) {
CommandAutocomplete.getAutocompleteReplacements('my command \'i\n', 's\' \'good yes', ['good no'], function(replacements, toReplace) {
assert.strictEqual(replacements.length, 1);
assert.strictEqual(replacements[0], '\'good no\' ');
assert.strictEqual(toReplace, '\'good yes');
return callback();
});
});
it('replaces multi-line command at very start of line', function(callback) {
CommandAutocomplete.getAutocompleteReplacements('my command \\\n', 'is', ['isbad'], function(replacements, toReplace) {
assert.strictEqual(replacements.length, 1);
assert.strictEqual(replacements[0], 'isbad ');
assert.strictEqual(toReplace, 'is');
return callback();
});
});
it('quotes replacements that contain spaces', function(callback) {
CommandAutocomplete.getAutocompleteReplacements(null, 'my', ['command is bad'], function(replacements, toReplace) {
assert.strictEqual(replacements.length, 1);
assert.strictEqual(replacements[0], '"command is bad" ');
assert.strictEqual(toReplace, 'my');
return callback();
});
});
it('appends replacements to the command string when command ends with single white-space', function(callback) {
CommandAutocomplete.getAutocompleteReplacements(null, 'number ', ['123456789'], function(replacements, toReplace) {
assert.strictEqual(replacements.length, 1);
assert.strictEqual(replacements[0], '123456789 ');
assert.strictEqual(toReplace, '');
return callback();
});
});
it('appends replacements to the command string when command ends with multiple white-space', function(callback) {
CommandAutocomplete.getAutocompleteReplacements(null, 'number ', ['123456789'], function(replacements, toReplace) {
assert.strictEqual(replacements.length, 1);
assert.strictEqual(replacements[0], '123456789 ');
assert.strictEqual(toReplace, '');
return callback();
});
});
it('appends replacements to the command string when command ends with all white-space then a new line', function(callback) {
CommandAutocomplete.getAutocompleteReplacements('number \\\n', null, ['123456789'], function(replacements, toReplace) {
assert.strictEqual(replacements.length, 1);
assert.strictEqual(replacements[0], '123456789 ');
assert.strictEqual(toReplace, '');
return callback();
});
});
it('appends replacements to the command string when command ends with all white-space then a new line and more white-space', function(callback) {
CommandAutocomplete.getAutocompleteReplacements('number \\\n', ' ', ['123456789'], function(replacements, toReplace) {
assert.strictEqual(replacements.length, 1);
assert.strictEqual(replacements[0], '123456789 ');
assert.strictEqual(toReplace, '');
return callback();
});
});
});
});