/
watch.dart
667 lines (543 loc) · 25.1 KB
/
watch.dart
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
// Copyright 2018 Google Inc. Use of this source code is governed by an
// MIT-style license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.
import 'dart:async';
import 'package:path/path.dart' as p;
import 'package:test/test.dart';
import 'package:test_descriptor/test_descriptor.dart' as d;
import 'package:test_process/test_process.dart';
import '../../utils.dart';
/// Defines test that are shared between the Dart and Node.js CLI test suites.
void sharedTests(Future<TestProcess> runSass(Iterable<String> arguments)) {
test("--poll may not be passed without --watch", () async {
var sass = await runSass(["--poll", "-"]);
await expectLater(
sass.stdout, emits("--poll may not be passed without --watch."));
expect(
sass.stdout, emitsThrough(contains("Print this usage information.")));
await sass.shouldExit(64);
});
for (var poll in [true, false]) {
Future<TestProcess> watch(Iterable<String> arguments) => runSass(
["--no-source-map", "--watch", ...arguments, if (poll) "--poll"]);
/// Returns a future that completes after a delay if [poll] is `true`.
///
/// Modifying a file very quickly after it was processed can go
/// unrecognized, especially on Windows where filesystem operations can have
/// very high delays.
Future<void> tickIfPoll() => poll ? tick : Future.value();
group("${poll ? 'with' : 'without'} --poll", () {
group("when started", () {
test("updates a CSS file whose source was modified", () async {
await d.file("out.css", "x {y: z}").create();
await tick;
await d.file("test.scss", "a {b: c}").create();
var sass = await watch(["test.scss:out.css"]);
await expectLater(
sass.stdout, emits('Compiled test.scss to out.css.'));
await expectLater(sass.stdout, _watchingForChanges);
await sass.kill();
await d
.file("out.css", equalsIgnoringWhitespace("a { b: c; }"))
.validate();
});
test("doesn't update a CSS file that wasn't modified", () async {
await d.file("test.scss", "a {b: c}").create();
await d.file("out.css", "x {y: z}").create();
var sass = await watch(["test.scss:out.css"]);
await expectLater(sass.stdout, _watchingForChanges);
await sass.kill();
await d.file("out.css", "x {y: z}").validate();
});
group("continues compiling after an error", () {
test("with --error-css", () async {
await d.file("test1.scss", "a {b: }").create();
await d.file("test2.scss", "x {y: z}").create();
var message = 'Error: Expected expression.';
var sass =
await watch(["test1.scss:out1.css", "test2.scss:out2.css"]);
await expectLater(sass.stderr, emits(message));
await expectLater(
sass.stderr, emitsThrough(contains('test1.scss 1:7')));
await expectLater(
sass.stdout, emitsThrough('Compiled test2.scss to out2.css.'));
await expectLater(sass.stdout, _watchingForChanges);
await sass.kill();
await d.file("out1.css", contains(message)).validate();
await d
.file("out2.css", equalsIgnoringWhitespace("x { y: z; }"))
.validate();
});
test("with --no-error-css", () async {
await d.file("test1.scss", "a {b: }").create();
await d.file("test2.scss", "x {y: z}").create();
var sass = await watch([
"--no-error-css",
"test1.scss:out1.css",
"test2.scss:out2.css"
]);
await expectLater(
sass.stderr, emits('Error: Expected expression.'));
await expectLater(
sass.stderr, emitsThrough(contains('test1.scss 1:7')));
await expectLater(
sass.stdout, emitsThrough('Compiled test2.scss to out2.css.'));
await expectLater(sass.stdout, _watchingForChanges);
await sass.kill();
await d.nothing("out1.css").validate();
await d
.file("out2.css", equalsIgnoringWhitespace("x { y: z; }"))
.validate();
});
});
test("stops compiling after an error with --stop-on-error", () async {
await d.file("test1.scss", "a {b: }").create();
await d.file("test2.scss", "x {y: z}").create();
var sass = await watch([
"--stop-on-error",
"test1.scss:out1.css",
"test2.scss:out2.css"
]);
var message = 'Error: Expected expression.';
await expectLater(
sass.stderr,
emitsInOrder([
message,
emitsThrough(contains('test1.scss 1:7')),
emitsDone
]));
await sass.shouldExit(65);
await d.file("out1.css", contains(message)).validate();
await d.nothing("out2.css").validate();
});
});
group("recompiles a watched file", () {
test("when it's modified", () async {
await d.file("test.scss", "a {b: c}").create();
var sass = await watch(["test.scss:out.css"]);
await expectLater(
sass.stdout, emits('Compiled test.scss to out.css.'));
await expectLater(sass.stdout, _watchingForChanges);
await tickIfPoll();
await d.file("test.scss", "x {y: z}").create();
await expectLater(
sass.stdout, emits('Compiled test.scss to out.css.'));
await sass.kill();
await d
.file("out.css", equalsIgnoringWhitespace("x { y: z; }"))
.validate();
});
test("when it's modified when watched from a directory", () async {
await d.dir("dir", [d.file("test.scss", "a {b: c}")]).create();
var sass = await watch(["dir:out"]);
await expectLater(
sass.stdout, emits(_compiled('dir/test.scss', 'out/test.css')));
await expectLater(sass.stdout, _watchingForChanges);
await tickIfPoll();
await d.dir("dir", [d.file("test.scss", "x {y: z}")]).create();
await expectLater(
sass.stdout, emits(_compiled('dir/test.scss', 'out/test.css')));
await sass.kill();
await d.dir("out", [
d.file("test.css", equalsIgnoringWhitespace("x { y: z; }"))
]).validate();
});
test("when its dependency is modified", () async {
await d.file("_other.scss", "a {b: c}").create();
await d.file("test.scss", "@import 'other'").create();
var sass = await watch(["test.scss:out.css"]);
await expectLater(
sass.stdout, emits('Compiled test.scss to out.css.'));
await expectLater(sass.stdout, _watchingForChanges);
await tickIfPoll();
await d.file("_other.scss", "x {y: z}").create();
await expectLater(
sass.stdout, emits('Compiled test.scss to out.css.'));
await sass.kill();
await d
.file("out.css", equalsIgnoringWhitespace("x { y: z; }"))
.validate();
});
test("when it's deleted and re-added", () async {
await d.file("test.scss", "a {b: c}").create();
var sass = await watch(["test.scss:out.css"]);
await expectLater(
sass.stdout, emits('Compiled test.scss to out.css.'));
await expectLater(sass.stdout, _watchingForChanges);
await tickIfPoll();
d.file("test.scss").io.deleteSync();
await expectLater(sass.stdout, emits('Deleted out.css.'));
// Windows gets confused at the OS level if we don't wait a bit here.
await tick;
await d.file("test.scss", "x {y: z}").create();
await expectLater(
sass.stdout, emits('Compiled test.scss to out.css.'));
await sass.kill();
await d
.file("out.css", equalsIgnoringWhitespace("x { y: z; }"))
.validate();
});
test("when it gets a parse error", () async {
await d.file("test.scss", "a {b: c}").create();
var sass = await watch(["test.scss:out.css"]);
await expectLater(
sass.stdout, emits('Compiled test.scss to out.css.'));
await expectLater(sass.stdout, _watchingForChanges);
await tickIfPoll();
var message = 'Error: Expected expression.';
await d.file("test.scss", "a {b: }").create();
await expectLater(sass.stderr, emits(message));
await expectLater(
sass.stderr, emitsThrough(contains('test.scss 1:7')));
await sass.kill();
await d.file("out.css", contains(message)).validate();
});
test("stops compiling after an error with --stop-on-error", () async {
await d.file("test.scss", "a {b: c}").create();
var sass = await watch(["--stop-on-error", "test.scss:out.css"]);
await expectLater(
sass.stdout, emits('Compiled test.scss to out.css.'));
await expectLater(sass.stdout, _watchingForChanges);
await tickIfPoll();
var message = 'Error: Expected expression.';
await d.file("test.scss", "a {b: }").create();
await expectLater(
sass.stderr,
emitsInOrder([
message,
emitsThrough(contains('test.scss 1:7')),
emitsDone
]));
await sass.shouldExit(65);
await d.file("out.css", contains(message)).validate();
});
group("when its dependency is deleted", () {
test("and updates the output", () async {
await d.file("_other.scss", "a {b: c}").create();
await d.file("test.scss", "@import 'other'").create();
var sass = await watch(["test.scss:out.css"]);
await expectLater(
sass.stdout, emits('Compiled test.scss to out.css.'));
await expectLater(sass.stdout, _watchingForChanges);
await tickIfPoll();
var message = "Error: Can't find stylesheet to import.";
d.file("_other.scss").io.deleteSync();
await expectLater(sass.stderr, emits(message));
await expectLater(
sass.stderr, emitsThrough(contains('test.scss 1:9')));
await sass.kill();
await d.file("out.css", contains(message)).validate();
});
test("but another is available", () async {
await d.file("_other.scss", "a {b: c}").create();
await d.file("test.scss", "@import 'other'").create();
await d.dir("dir", [d.file("_other.scss", "x {y: z}")]).create();
var sass = await watch(["-I", "dir", "test.scss:out.css"]);
await expectLater(
sass.stdout, emits('Compiled test.scss to out.css.'));
await expectLater(sass.stdout, _watchingForChanges);
await tickIfPoll();
d.file("_other.scss").io.deleteSync();
await expectLater(
sass.stdout, emits('Compiled test.scss to out.css.'));
await sass.kill();
await d
.file("out.css", equalsIgnoringWhitespace("x { y: z; }"))
.validate();
});
test("which resolves a conflict", () async {
await d.file("_other.scss", "a {b: c}").create();
await d.file("_other.sass", "x\n y: z").create();
await d.file("test.scss", "@import 'other'").create();
var sass = await watch(["test.scss:out.css"]);
await expectLater(sass.stderr,
emits("Error: It's not clear which file to import. Found:"));
await expectLater(sass.stdout, _watchingForChanges);
await tickIfPoll();
d.file("_other.sass").io.deleteSync();
await expectLater(
sass.stdout, emits('Compiled test.scss to out.css.'));
await sass.kill();
await d
.file("out.css", equalsIgnoringWhitespace("a { b: c; }"))
.validate();
});
});
group("when a dependency is added", () {
group("that was missing", () {
test("relative to the file", () async {
await d.file("test.scss", "@import 'other'").create();
var sass = await watch(["test.scss:out.css"]);
await expectLater(sass.stderr,
emits("Error: Can't find stylesheet to import."));
await expectLater(
sass.stderr, emitsThrough(contains("test.scss 1:9")));
await expectLater(sass.stdout, _watchingForChanges);
await tickIfPoll();
await d.file("_other.scss", "a {b: c}").create();
await expectLater(
sass.stdout, emits('Compiled test.scss to out.css.'));
await sass.kill();
await d
.file("out.css", equalsIgnoringWhitespace("a { b: c; }"))
.validate();
});
test("on a load path", () async {
await d.file("test.scss", "@import 'other'").create();
await d.dir("dir").create();
var sass = await watch(["-I", "dir", "test.scss:out.css"]);
await expectLater(sass.stderr,
emits("Error: Can't find stylesheet to import."));
await expectLater(
sass.stderr, emitsThrough(contains("test.scss 1:9")));
await expectLater(sass.stdout, _watchingForChanges);
await tickIfPoll();
await d.dir("dir", [d.file("_other.scss", "a {b: c}")]).create();
await expectLater(
sass.stdout, emits('Compiled test.scss to out.css.'));
await sass.kill();
await d
.file("out.css", equalsIgnoringWhitespace("a { b: c; }"))
.validate();
});
test("on a load path that was created", () async {
await d.dir(
"dir1", [d.file("test.scss", "@import 'other'")]).create();
var sass = await watch(["-I", "dir2", "dir1:out"]);
await expectLater(sass.stderr,
emits("Error: Can't find stylesheet to import."));
await expectLater(sass.stderr,
emitsThrough(contains("${p.join('dir1', 'test.scss')} 1:9")));
await expectLater(sass.stdout, _watchingForChanges);
await tickIfPoll();
await d.dir("dir2", [d.file("_other.scss", "a {b: c}")]).create();
await expectLater(sass.stdout,
emits(_compiled('dir1/test.scss', 'out/test.css')));
await sass.kill();
await d
.file("out/test.css", equalsIgnoringWhitespace("a { b: c; }"))
.validate();
});
});
test("that conflicts with the previous dependency", () async {
await d.file("_other.scss", "a {b: c}").create();
await d.file("test.scss", "@import 'other'").create();
var sass = await watch(["test.scss:out.css"]);
await expectLater(
sass.stdout, emits('Compiled test.scss to out.css.'));
await expectLater(sass.stdout, _watchingForChanges);
await tickIfPoll();
var message = "Error: It's not clear which file to import. Found:";
await d.file("_other.sass", "x\n y: z").create();
await expectLater(sass.stderr, emits(message));
await sass.kill();
await d.file("out.css", contains(message)).validate();
});
group("that overrides the previous dependency", () {
test("on an import path", () async {
await d.file("test.scss", "@import 'other'").create();
await d.dir("dir2", [d.file("_other.scss", "a {b: c}")]).create();
await d.dir("dir1").create();
var sass = await watch(
["-I", "dir1", "-I", "dir2", "test.scss:out.css"]);
await expectLater(
sass.stdout, emits('Compiled test.scss to out.css.'));
await expectLater(sass.stdout, _watchingForChanges);
await tickIfPoll();
await d.dir("dir1", [d.file("_other.scss", "x {y: z}")]).create();
await expectLater(
sass.stdout, emits('Compiled test.scss to out.css.'));
await sass.kill();
await d
.file("out.css", equalsIgnoringWhitespace("x { y: z; }"))
.validate();
});
test("because it's relative", () async {
await d.file("test.scss", "@import 'other'").create();
await d.dir("dir", [d.file("_other.scss", "a {b: c}")]).create();
var sass = await watch(["-I", "dir", "test.scss:out.css"]);
await expectLater(
sass.stdout, emits('Compiled test.scss to out.css.'));
await expectLater(sass.stdout, _watchingForChanges);
await tickIfPoll();
await d.file("_other.scss", "x {y: z}").create();
await expectLater(
sass.stdout, emits('Compiled test.scss to out.css.'));
await sass.kill();
await d
.file("out.css", equalsIgnoringWhitespace("x { y: z; }"))
.validate();
});
test("because it's not an index", () async {
await d.file("test.scss", "@import 'other'").create();
await d
.dir("other", [d.file("_index.scss", "a {b: c}")]).create();
var sass = await watch(["test.scss:out.css"]);
await expectLater(
sass.stdout, emits('Compiled test.scss to out.css.'));
await expectLater(sass.stdout, _watchingForChanges);
await tickIfPoll();
await d.file("_other.scss", "x {y: z}").create();
await expectLater(
sass.stdout, emits('Compiled test.scss to out.css.'));
await sass.kill();
await d
.file("out.css", equalsIgnoringWhitespace("x { y: z; }"))
.validate();
});
});
test("gracefully handles a parse error", () async {
await d.dir("dir").create();
var sass = await watch(["dir:out"]);
await expectLater(sass.stdout, _watchingForChanges);
await tickIfPoll();
await d.dir("dir", [d.file("test.scss", "a {b: }")]).create();
await expectLater(
sass.stderr, emits('Error: Expected expression.'));
await tickIfPoll();
await d.dir("dir", [d.file("test.scss", "a {b: c}")]).create();
await expectLater(
sass.stdout,
emits('Compiled ${p.join('dir', 'test.scss')} to '
'${p.join('out', 'test.css')}.'));
await sass.kill();
await d.dir("out", [
d.file("test.css", equalsIgnoringWhitespace("a { b: c; }"))
]).validate();
});
});
// Regression test for #806
test("with a .css extension", () async {
await d.file("test.css", "a {b: c}").create();
var sass = await watch(["test.css:out.css"]);
await expectLater(
sass.stdout, emits('Compiled test.css to out.css.'));
await expectLater(sass.stdout, _watchingForChanges);
await tickIfPoll();
await d.file("test.css", "x {y: z}").create();
await expectLater(
sass.stdout, emits('Compiled test.css to out.css.'));
await sass.kill();
await d
.file("out.css", equalsIgnoringWhitespace("x { y: z; }"))
.validate();
});
});
group("doesn't recompile the watched file", () {
test("when an unrelated file is modified", () async {
await d.dir("dir", [
d.file("test1.scss", "a {b: c}"),
d.file("test2.scss", "a {b: c}")
]).create();
var sass = await watch(["dir:out"]);
await expectLater(
sass.stdout,
emitsInAnyOrder([
_compiled('dir/test1.scss', 'out/test1.css'),
_compiled('dir/test2.scss', 'out/test2.css')
]));
await expectLater(sass.stdout, _watchingForChanges);
await tickIfPoll();
await d.dir("dir", [d.file("test2.scss", "x {y: z}")]).create();
await expectLater(
sass.stdout, emits(_compiled('dir/test2.scss', 'out/test2.css')));
expect(sass.stdout,
neverEmits(_compiled('dir/test1.scss', 'out/test1.css')));
await tick;
await sass.kill();
});
test(
"when a potential dependency that's not actually imported is added",
() async {
await d.file("test.scss", "@import 'other'").create();
await d.file("_other.scss", "a {b: c}").create();
await d.dir("dir").create();
var sass = await watch(["-I", "dir", "test.scss:out.css"]);
await expectLater(
sass.stdout, emits('Compiled test.scss to out.css.'));
await expectLater(sass.stdout, _watchingForChanges);
await tickIfPoll();
await d.dir("dir", [d.file("_other.scss", "a {b: c}")]).create();
expect(sass.stdout, neverEmits('Compiled test.scss to out.css.'));
await tick;
await sass.kill();
await d
.file("out.css", equalsIgnoringWhitespace("a { b: c; }"))
.validate();
});
});
group("deletes the CSS", () {
test("when a file is deleted", () async {
await d.file("test.scss", "a {b: c}").create();
var sass = await watch(["test.scss:out.css"]);
await expectLater(
sass.stdout, emits('Compiled test.scss to out.css.'));
await expectLater(sass.stdout, _watchingForChanges);
await tickIfPoll();
d.file("test.scss").io.deleteSync();
await expectLater(sass.stdout, emits('Deleted out.css.'));
await sass.kill();
await d.nothing("out.css").validate();
});
test("when a file is deleted within a directory", () async {
await d.dir("dir", [d.file("test.scss", "a {b: c}")]).create();
var sass = await watch(["dir:out"]);
await expectLater(
sass.stdout, emits(_compiled('dir/test.scss', 'out/test.css')));
await expectLater(sass.stdout, _watchingForChanges);
await tickIfPoll();
d.file("dir/test.scss").io.deleteSync();
await expectLater(
sass.stdout, emits('Deleted ${p.join('out', 'test.css')}.'));
await sass.kill();
await d.dir("dir", [d.nothing("out.css")]).validate();
});
});
test("creates a new CSS file when a Sass file is added", () async {
await d.dir("dir").create();
var sass = await watch(["dir:out"]);
await expectLater(sass.stdout, _watchingForChanges);
await tickIfPoll();
await d.dir("dir", [d.file("test.scss", "a {b: c}")]).create();
await expectLater(
sass.stdout, emits(_compiled('dir/test.scss', 'out/test.css')));
await sass.kill();
await d.dir("out", [
d.file("test.css", equalsIgnoringWhitespace("a { b: c; }"))
]).validate();
});
test("doesn't create a new CSS file when a partial is added", () async {
await d.dir("dir").create();
var sass = await watch(["dir:out"]);
await expectLater(sass.stdout, _watchingForChanges);
await tickIfPoll();
await d.dir("dir", [d.file("_test.scss", "a {b: c}")]).create();
expect(sass.stdout,
neverEmits(_compiled('dir/test.scss', 'out/test.css')));
await tick;
await sass.kill();
await d.nothing("out/test.scss").validate();
});
group("doesn't allow", () {
test("--stdin", () async {
var sass = await watch(["--stdin", "test.scss"]);
expect(sass.stdout, emits('--watch is not allowed with --stdin.'));
await sass.shouldExit(64);
});
test("printing to stderr", () async {
var sass = await watch(["test.scss"]);
expect(sass.stdout,
emits('--watch is not allowed when printing to stdout.'));
await sass.shouldExit(64);
});
});
});
}
}
/// Returns the message that Sass prints indicating that [from] was compiled to
/// [to], with path separators normalized for the current operating system.
String _compiled(String from, String to) =>
'Compiled ${p.normalize(from)} to ${p.normalize(to)}.';
/// Matches the output that indicates that Sass is watching for changes.
final _watchingForChanges =
emitsInOrder(["Sass is watching for changes. Press Ctrl-C to stop.", ""]);