This repository has been archived by the owner on May 29, 2020. It is now read-only.
forked from wking-io/elm-live
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.js
662 lines (495 loc) · 14.1 KB
/
test.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
'use strict';
const path = require('path');
const test = require('ava');
const proxyquire = require('proxyquire').noPreserveCache().noCallThru();
const devnull = require('dev-null');
const qs = require('q-stream');
const naked = require('strip-ansi');
const dummyConfig = { inputStream: devnull(), outputStream: devnull() };
const dummyCrossSpawn = { sync: () => ({ status: 0 }) };
const dummyBudoServer = { on: () => {} };
const dummyBudo = () => dummyBudoServer;
const dummyChokidarWatcher = { on: () => {} };
const dummyChokidar = { watch: () => dummyChokidarWatcher };
test('Prints `--help`', (assert) => {
assert.plan(5);
const crossSpawn = { sync: (command, args, config) => {
assert.is(command, 'man',
'spawns `man`'
);
assert.is(
args[0],
path.resolve(__dirname, 'manpages/elm-live.1'),
'with the right manpage'
);
assert.is(
config.stdio[0],
dummyConfig.inputStream,
'reads from `inputStream`'
);
assert.is(
config.stdio[1],
dummyConfig.outputStream,
'reads from `outputStream`'
);
return { error: null };
} };
const elmLive = proxyquire('./source/elm-live', { 'cross-spawn': crossSpawn });
const exitCode = elmLive(['--help'], dummyConfig);
assert.is(exitCode, 0,
'succeeds'
);
});
test((
'Falls back to plain text `--help` over stdout'
), (assert) => new Promise((resolve, reject) => {
assert.plan(4);
setTimeout(reject, 100);
const expectedHelpContent = (
`I’m dreaming
of a white Christmas
`
);
const hasbin = { sync: (command) => {
assert.is(command, 'man',
'checks if `man` is in the PATH'
);
return false;
} };
const fs = { readFileSync: (file, encoding) => {
const expectedLocation = 'manpages/elm-live.1.txt';
assert.is(file, path.resolve(__dirname, expectedLocation),
`reads the file at \`${expectedLocation}\``
);
if (encoding !== 'utf8') throw new Error('Bad encoding!');
return expectedHelpContent;
} };
const outputStream = qs((chunk) => {
assert.is(chunk, expectedHelpContent,
'prints the help text'
);
resolve();
});
const elmLive = proxyquire('./source/elm-live', {
hasbin, fs,
});
const exitCode = elmLive(['--help'], {
inputStream: devnull(), outputStream,
});
assert.is(exitCode, 0,
'succeeds'
);
}));
test((
'Throws any other `man` error'
), (assert) => {
assert.plan(2);
const dummyError = { code: 'unknown' };
const crossSpawn = { sync: (command) => {
assert.is(command, 'man',
'tries to spawn `man`'
);
return { error: dummyError };
} };
const elmLive = proxyquire('./source/elm-live', {
'cross-spawn': crossSpawn,
});
try {
elmLive(['--help'], dummyConfig);
} catch (error) {
assert.is(error, dummyError,
'throws the same error cross-spawn returns'
);
}
});
test((
'Shouts if `elm-make` can’t be found'
), (assert) => new Promise((resolve) => {
assert.plan(3);
const expectedMessage = new RegExp(
`^\nelm-live:
I can’t find the command elm-make!`
);
const crossSpawn = { sync: (command) => {
assert.is(command, 'elm-make',
'spawns `elm-make`'
);
return { error: { code: 'ENOENT' } };
} };
const elmLive = proxyquire('./source/elm-live', { 'cross-spawn': crossSpawn });
const exitCode = elmLive([], { outputStream: qs((chunk) => {
assert.truthy(
expectedMessage.test(naked(chunk)),
'prints an informative message'
);
resolve();
}), inputStream: devnull() });
assert.is(exitCode, 1,
'fails'
);
}));
test('Exits if `--no-recover`', (assert) => {
assert.plan(4);
const status = 59;
const crossSpawn = { sync: (command) => {
assert.is(command, 'elm-make',
'spawns `elm-make`'
);
return { status };
} };
const budo = () => {
assert.fail(
'doesn’t start the server until errors are fixed'
);
};
const elmLive = proxyquire('./source/elm-live', {
'cross-spawn': crossSpawn, budo, chokidar: dummyChokidar,
});
assert.is(
elmLive(['--no-recover'], dummyConfig),
status,
'exits with the same status as `elm-make`'
);
assert.is(
elmLive([], dummyConfig),
null,
'keeps running otherwise'
);
});
test('Informs of compile errors', (assert) => new Promise((resolve) => {
assert.plan(3);
const message = 'whatever';
const status = 9;
const crossSpawn = { sync: (command, _, options) => {
assert.is(command, 'elm-make',
'spawns `elm-make`'
);
options.stdio[1].write(message);
return { status };
} };
const elmLive = proxyquire('./source/elm-live', {
'cross-spawn': crossSpawn, budo: dummyBudo, chokidar: dummyChokidar,
});
let run = 0;
const outputStream = qs((chunk) => {
if (run === 0) assert.is(chunk, message,
'prints elm-make output first'
);
if (run === 1) resolve(assert.is(
naked(chunk),
(
`\nelm-live:
elm-make failed! You can find more info above. Keep calm and take your time
to fix your code. We’ll try to compile it again as soon as you change a file.
`
),
'prints a friendly message afterwards'
));
run++;
});
elmLive([], { outputStream, inputStream: devnull() });
}));
test('Prints any other `elm-make` error', (assert) => new Promise((resolve) => {
assert.plan(3);
const message = 'whatever';
const status = 9;
const crossSpawn = { sync: (command) => {
assert.is(command, 'elm-make',
'spawns `elm-make`'
);
return { status, error: { toString: () => message } };
} };
const elmLive = proxyquire('./source/elm-live', { 'cross-spawn': crossSpawn });
const exitCode = elmLive(['--no-recover'], { outputStream: qs((chunk) => {
assert.is(
naked(chunk),
(
`\nelm-live: Error while calling elm-make! This output may be helpful:
${message}
`
),
'prints the error’s output'
);
resolve();
}), inputStream: devnull() });
assert.is(exitCode, status,
'exits with whatever code `elm-make` returned'
);
}));
test('Passes correct args to `elm-make`', (assert) => {
assert.plan(2);
const elmLiveArgs = ['--port=77', '--no-recover'];
const otherArgs =
['--anything', 'whatever', 'with whitespace', '--beep=boop', '--no-flag'];
const crossSpawn = { sync: (command, args) => {
assert.is(command, 'elm-make',
'spawns `elm-make`'
);
assert.deepEqual(
args,
otherArgs,
'passes all not understood arguments'
);
// Kill after one attempt
return { status: 77, error: {} };
} };
const elmLive = proxyquire('./source/elm-live', { 'cross-spawn': crossSpawn });
elmLive(elmLiveArgs.concat(otherArgs), dummyConfig);
});
test('Disambiguates `elm-make` args with `--`', (assert) => {
assert.plan(2);
const elmMakeBefore =
['--anything', 'whatever', 'whatever 2'];
const elmLiveBefore =
['--open', '--no-recover'];
const elmMakeAfter =
['--port=77', '--beep=boop'];
const allArgs = [].concat(
elmMakeBefore,
elmLiveBefore,
['--'],
elmMakeAfter
);
const crossSpawn = { sync: (command, args) => {
assert.is(command, 'elm-make',
'spawns `elm-make`'
);
assert.deepEqual(
args,
elmMakeBefore.concat(elmMakeAfter),
'passes all not understood commands and all commands after the `--` ' +
'to elm-make'
);
// Kill after one attempt
return { status: 77, error: {} };
} };
const elmLive = proxyquire('./source/elm-live', { 'cross-spawn': crossSpawn });
elmLive(allArgs, dummyConfig);
});
test('Spawns `--path-to-elm-make` instead of `elm-make` if given', (assert) => {
assert.plan(1);
const pathToElmMake = '/path/to/any/binary';
const crossSpawn = { sync: (command) => {
assert.is(command, pathToElmMake);
// Kill after one attempt
return { status: 77, error: {} };
} };
const elmLive = proxyquire('./source/elm-live',
{ 'cross-spawn': crossSpawn }
);
elmLive([`--path-to-elm-make=${pathToElmMake}`], dummyConfig);
});
test('Redirects elm-make stdio', (assert) => new Promise((resolve) => {
assert.plan(4);
const elmLiveOut = (
`Hello there!
How’s it going?
`
);
const elmLiveErr = (
`Not too well, to be honest
`
);
const inputStream = {};
const crossSpawn = { sync: (command, _, options) => {
assert.is(command, 'elm-make',
'spawns `elm-make`'
);
assert.is(
options.stdio[0],
inputStream,
'takes stdin from the `inputStream`'
);
options.stdio[1].write(elmLiveOut);
options.stdio[2].write(elmLiveErr);
// Kill after one attempt
return { status: 77, error: {} };
} };
let run = 0;
const elmLive = proxyquire('./source/elm-live', { 'cross-spawn': crossSpawn });
elmLive(['--no-recover'], { inputStream, outputStream: qs((chunk) => {
if (run === 0) assert.is(
chunk,
elmLiveOut,
'directs stdout to the `outputStream`'
);
if (run === 1) resolve(assert.is(
chunk,
elmLiveErr,
'directs stderr to the `outputStream`'
));
run++;
}) });
}));
test('Starts budo and chokidar with correct config', (assert) => {
assert.plan(8);
const budo = (options) => {
assert.is(options.port, 8000,
'uses port 8000 by default (or the next available one)'
);
assert.is(options.host, 'localhost',
'uses the localhost interface by default'
);
assert.is(options.open, false,
'doesn’t `--open` the app by default'
);
assert.is(options.dir, '.',
'serves the current working directory by default'
);
assert.is(options.watchGlob, '**/*.{html,css,js}',
'reloads the app when an HTML, JS or CSS static file changes'
);
assert.is(options.pushstate, false,
'disables `--pushstate` by default'
);
assert.is(options.stream, dummyConfig.outputStream,
'directs all output to `outputStream`'
);
return dummyBudoServer;
};
const chokidar = {
watch: (glob) => {
assert.is(glob, '**/*.elm',
'watches all `*.elm` files in the current directory ' +
'and its subdirectories'
);
return dummyChokidarWatcher;
},
};
const elmLive = proxyquire('./source/elm-live', {
budo, chokidar, 'cross-spawn': dummyCrossSpawn,
});
elmLive([], dummyConfig);
});
test('`--open`s the default browser', (assert) => {
assert.plan(1);
const budo = (options) => {
assert.is(options.open, true,
'passes `--open` to budo'
);
return dummyBudoServer;
};
const elmLive = proxyquire('./source/elm-live', {
budo, chokidar: dummyChokidar, 'cross-spawn': dummyCrossSpawn,
});
elmLive(['--open'], dummyConfig);
});
test('Serves at the specified `--port`', (assert) => {
assert.plan(1);
const portNumber = 867;
const budo = (options) => {
assert.is(options.port, portNumber,
'passes `--port` to budo'
);
return dummyBudoServer;
};
const elmLive = proxyquire('./source/elm-live', {
budo, chokidar: dummyChokidar, 'cross-spawn': dummyCrossSpawn,
});
elmLive([`--port=${portNumber}`], dummyConfig);
});
test('Serves on the specified `--host`', (assert) => {
assert.plan(2);
const hostName1 = 'localhost';
const hostName2 = '127.0.0.1';
const budo = (options) => {
assert.true(options.host === hostName1 || options.host === hostName2,
'passes `--host` to budo'
);
return dummyBudoServer;
};
const elmLive = proxyquire('./source/elm-live', {
budo, chokidar: dummyChokidar, 'cross-spawn': dummyCrossSpawn,
});
elmLive([`--host=${hostName1}`], dummyConfig);
elmLive([`--host=${hostName2}`], dummyConfig);
});
test('Serves from the specified `--dir`', (assert) => {
assert.plan(2);
const dir = 'relative/or/absolute/path/';
const budo = (options) => {
assert.true(options.dir === dir,
'serves the right files'
);
assert.true(options.watchGlob === `${dir}**/*.{html,css,js}`,
'watches the right files'
);
return dummyBudoServer;
};
const elmLive = proxyquire('./source/elm-live', {
budo, chokidar: dummyChokidar, 'cross-spawn': dummyCrossSpawn,
});
elmLive([`--dir=${dir}`], dummyConfig);
});
test('`--pushstate to support client-side routing', (assert) => {
assert.plan(1);
const budo = (options) => {
assert.is(options.pushstate, true,
'passes `--pushstate` to budo'
);
return dummyBudoServer;
};
const elmLive = proxyquire('./source/elm-live', {
budo, chokidar: dummyChokidar, 'cross-spawn': dummyCrossSpawn,
});
elmLive(['--pushstate'], dummyConfig);
});
test((
'Watches all `**/*.elm` files in the current directory'
), (assert) => new Promise((resolve) => {
assert.plan(5);
const event = 'change';
const relativePath = path.join('ab', 'c.elm');
const absolutePath = path.resolve(process.cwd(), relativePath);
const watcherMock = {
on: (what, callback) => {
callback(event, absolutePath);
},
};
const chokidar = {
watch: (target, options) => {
assert.is(target, '**/*.elm',
'passes the right glob to chokidar'
);
assert.true(options.ignoreInitial, 'does not trigger events when starting watch');
return watcherMock;
},
};
let serverStarted = false;
const budo = () => {
serverStarted = true;
return dummyBudoServer;
};
let elmMakeRun = 0;
const success = { status: 0, error: {} };
const failure = { status: 77, error: {} };
const crossSpawn = { sync: (command) => {
if (command !== 'elm-make') return success;
elmMakeRun++;
if (elmMakeRun === 1) return failure;
assert.falsy(serverStarted,
'doesn’t start the server immediately if things go wrong'
);
assert.pass(
'retries spawning `elm-make` if things go wrong the first time'
);
return success;
} };
const elmLive = proxyquire('./source/elm-live', {
chokidar, budo, 'cross-spawn': crossSpawn,
});
let chunkNumber = 0;
elmLive([], { inputStream: devnull(), outputStream: qs((chunk) => {
chunkNumber++;
if (chunkNumber !== 3) return;
const expectedMessage = (
`\nelm-live:
You’ve changed \`${relativePath}\`. Rebuilding!
`
);
assert.is(naked(chunk), expectedMessage,
'prints a message when a file is changed'
);
resolve();
}) });
}));