forked from denoland/deno
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathwatcher_tests.rs
More file actions
2814 lines (2441 loc) · 80.1 KB
/
watcher_tests.rs
File metadata and controls
2814 lines (2441 loc) · 80.1 KB
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
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2018-2026 the Deno authors. MIT license.
use test_util as util;
use test_util::TempDir;
use test_util::assert_contains;
use test_util::env_vars_for_npm_tests;
use test_util::eprintln;
use test_util::http_server;
use test_util::test;
use tokio::io::AsyncBufReadExt;
use util::DenoChild;
use util::assert_not_contains;
/// Logs to stderr every time next_line() is called
struct LoggingLines<R>
where
R: tokio::io::AsyncBufRead + Unpin,
{
pub lines: tokio::io::Lines<R>,
pub stream_name: String,
}
impl<R> LoggingLines<R>
where
R: tokio::io::AsyncBufRead + Unpin,
{
pub async fn next_line(&mut self) -> tokio::io::Result<Option<String>> {
let line = self.lines.next_line().await;
eprintln!(
"{}: {}",
self.stream_name,
line.as_ref().unwrap().clone().unwrap()
);
line
}
}
// Helper function to skip watcher output that contains "Restarting"
// phrase.
async fn skip_restarting_line<R>(stderr_lines: &mut LoggingLines<R>) -> String
where
R: tokio::io::AsyncBufRead + Unpin,
{
loop {
let msg = next_line(stderr_lines).await.unwrap();
if !msg.contains("Restarting") {
return msg;
}
}
}
async fn read_all_lints<R>(stderr_lines: &mut LoggingLines<R>) -> String
where
R: tokio::io::AsyncBufRead + Unpin,
{
let mut str = String::new();
while let Some(t) = next_line(stderr_lines).await {
let t = util::strip_ansi_codes(&t);
if t.starts_with("Watcher Restarting! File change detected") {
continue;
}
if t.starts_with("Watcher") {
break;
}
if t.starts_with("error[") {
str.push_str(&t);
str.push('\n');
}
}
str
}
async fn next_line<R>(lines: &mut LoggingLines<R>) -> Option<String>
where
R: tokio::io::AsyncBufRead + Unpin,
{
let timeout = tokio::time::Duration::from_secs(60);
tokio::time::timeout(timeout, lines.next_line())
.await
.unwrap_or_else(|_| {
panic!(
"Output did not contain a new line after {} seconds",
timeout.as_secs()
)
})
.unwrap()
}
/// Returns the matched line or None if there are no more lines in this stream
async fn wait_for<R>(
condition: impl Fn(&str) -> bool,
lines: &mut LoggingLines<R>,
) -> Option<String>
where
R: tokio::io::AsyncBufRead + Unpin,
{
while let Some(line) = lines.next_line().await.unwrap() {
if condition(line.as_str()) {
return Some(line);
}
}
None
}
async fn wait_contains<R>(s: &str, lines: &mut LoggingLines<R>) -> String
where
R: tokio::io::AsyncBufRead + Unpin,
{
let timeout = tokio::time::Duration::from_secs(60);
tokio::time::timeout(timeout, wait_for(|line| line.contains(s), lines))
.await
.unwrap_or_else(|_| {
panic!(
"Output did not contain \"{}\" after {} seconds",
s,
timeout.as_secs()
)
})
.unwrap_or_else(|| panic!("Output ended without containing \"{}\"", s))
}
/// Before test cases touch files, they need to wait for the watcher to be
/// ready. Waiting for subcommand output is insufficient.
/// The file watcher takes a moment to start watching files due to
/// asynchronicity. It is possible for the watched subcommand to finish before
/// any files are being watched.
/// deno must be running with --log-level=debug
/// file_name should be the file name and, optionally, extension. file_name
/// may not be a full path, as it is not portable.
async fn wait_for_watcher<R>(
file_name: &str,
stderr_lines: &mut LoggingLines<R>,
) -> String
where
R: tokio::io::AsyncBufRead + Unpin,
{
let timeout = tokio::time::Duration::from_secs(60);
tokio::time::timeout(
timeout,
wait_for(
|line| line.contains("Watching paths") && line.contains(file_name),
stderr_lines,
),
)
.await
.unwrap_or_else(|_| {
panic!(
"Watcher did not start for file \"{}\" after {} seconds",
file_name,
timeout.as_secs()
)
})
.unwrap_or_else(|| {
panic!(
"Output ended without before the watcher started watching file \"{}\"",
file_name
)
})
}
fn check_alive_then_kill(mut child: DenoChild) {
assert!(child.try_wait().unwrap().is_none());
child.kill().unwrap();
}
fn child_lines(
child: &mut std::process::Child,
) -> (
LoggingLines<tokio::io::BufReader<tokio::process::ChildStdout>>,
LoggingLines<tokio::io::BufReader<tokio::process::ChildStderr>>,
) {
let stdout_lines = LoggingLines {
lines: tokio::io::BufReader::new(
tokio::process::ChildStdout::from_std(child.stdout.take().unwrap())
.unwrap(),
)
.lines(),
stream_name: "STDOUT".to_string(),
};
let stderr_lines = LoggingLines {
lines: tokio::io::BufReader::new(
tokio::process::ChildStderr::from_std(child.stderr.take().unwrap())
.unwrap(),
)
.lines(),
stream_name: "STDERR".to_string(),
};
(stdout_lines, stderr_lines)
}
#[test(flaky)]
async fn lint_watch_test() {
let t = TempDir::new();
let badly_linted_original =
util::testdata_path().join("lint/watch/badly_linted.js");
let badly_linted_output =
util::testdata_path().join("lint/watch/badly_linted.js.out");
let badly_linted_fixed1 =
util::testdata_path().join("lint/watch/badly_linted_fixed1.js");
let badly_linted_fixed1_output =
util::testdata_path().join("lint/watch/badly_linted_fixed1.js.out");
let badly_linted_fixed2 =
util::testdata_path().join("lint/watch/badly_linted_fixed2.js");
let badly_linted_fixed2_output =
util::testdata_path().join("lint/watch/badly_linted_fixed2.js.out");
let badly_linted = t.path().join("badly_linted.js");
badly_linted_original.copy(&badly_linted);
let mut child = util::deno_cmd()
.current_dir(t.path())
.arg("lint")
.arg(&badly_linted)
.arg("--watch")
.piped_output()
.spawn()
.unwrap();
let (_stdout_lines, mut stderr_lines) = child_lines(&mut child);
let next_line = next_line(&mut stderr_lines).await.unwrap();
assert_contains!(&next_line, "Lint started");
let mut output = read_all_lints(&mut stderr_lines).await;
let expected = badly_linted_output.read_to_string();
assert_eq!(output, expected);
// Change content of the file again to be badly-linted
badly_linted_fixed1.copy(&badly_linted);
output = read_all_lints(&mut stderr_lines).await;
let expected = badly_linted_fixed1_output.read_to_string();
assert_eq!(output, expected);
// Change content of the file again to be badly-linted
badly_linted_fixed2.copy(&badly_linted);
output = read_all_lints(&mut stderr_lines).await;
let expected = badly_linted_fixed2_output.read_to_string();
assert_eq!(output, expected);
// the watcher process is still alive
assert!(child.try_wait().unwrap().is_none());
child.kill().unwrap();
}
#[test(flaky)]
async fn lint_watch_without_args_test() {
let t = TempDir::new();
let badly_linted_original =
util::testdata_path().join("lint/watch/badly_linted.js");
let badly_linted_output =
util::testdata_path().join("lint/watch/badly_linted.js.out");
let badly_linted_fixed1 =
util::testdata_path().join("lint/watch/badly_linted_fixed1.js");
let badly_linted_fixed1_output =
util::testdata_path().join("lint/watch/badly_linted_fixed1.js.out");
let badly_linted_fixed2 =
util::testdata_path().join("lint/watch/badly_linted_fixed2.js");
let badly_linted_fixed2_output =
util::testdata_path().join("lint/watch/badly_linted_fixed2.js.out");
let badly_linted = t.path().join("badly_linted.js");
badly_linted_original.copy(&badly_linted);
let mut child = util::deno_cmd()
.current_dir(t.path())
.arg("lint")
.arg("--watch")
.piped_output()
.spawn()
.unwrap();
let (_stdout_lines, mut stderr_lines) = child_lines(&mut child);
let next_line = next_line(&mut stderr_lines).await.unwrap();
assert_contains!(&next_line, "Lint started");
let mut output = read_all_lints(&mut stderr_lines).await;
let expected = badly_linted_output.read_to_string();
assert_eq!(output, expected);
// Change content of the file again to be badly-linted
badly_linted_fixed1.copy(&badly_linted);
output = read_all_lints(&mut stderr_lines).await;
let expected = badly_linted_fixed1_output.read_to_string();
assert_eq!(output, expected);
// Change content of the file again to be badly-linted
badly_linted_fixed2.copy(&badly_linted);
output = read_all_lints(&mut stderr_lines).await;
let expected = badly_linted_fixed2_output.read_to_string();
assert_eq!(output, expected);
// the watcher process is still alive
assert!(child.try_wait().unwrap().is_none());
child.kill().unwrap();
drop(t);
}
#[test(flaky)]
async fn lint_all_files_on_each_change_test() {
let t = TempDir::new();
let badly_linted_fixed0 =
util::testdata_path().join("lint/watch/badly_linted.js");
let badly_linted_fixed1 =
util::testdata_path().join("lint/watch/badly_linted_fixed1.js");
let badly_linted_fixed2 =
util::testdata_path().join("lint/watch/badly_linted_fixed2.js");
let badly_linted_1 = t.path().join("badly_linted_1.js");
let badly_linted_2 = t.path().join("badly_linted_2.js");
badly_linted_fixed0.copy(&badly_linted_1);
badly_linted_fixed1.copy(&badly_linted_2);
let mut child = util::deno_cmd()
.current_dir(t.path())
.arg("lint")
.arg(t.path())
.arg("--watch")
.piped_output()
.spawn()
.unwrap();
let (_stdout_lines, mut stderr_lines) = child_lines(&mut child);
assert_contains!(
wait_contains("Checked", &mut stderr_lines).await,
"Checked 2 files"
);
badly_linted_fixed2.copy(&badly_linted_2);
assert_contains!(
wait_contains("Checked", &mut stderr_lines).await,
"Checked 2 files"
);
assert!(child.try_wait().unwrap().is_none());
child.kill().unwrap();
drop(t);
}
#[test(flaky)]
async fn fmt_watch_test() {
let fmt_testdata_path = util::testdata_path().join("fmt");
let t = TempDir::new();
let fixed = fmt_testdata_path.join("badly_formatted_fixed.js");
let badly_formatted_original = fmt_testdata_path.join("badly_formatted.mjs");
let badly_formatted = t.path().join("badly_formatted.js");
badly_formatted_original.copy(&badly_formatted);
let mut child = util::deno_cmd()
.current_dir(t.path())
.arg("fmt")
.arg(&badly_formatted)
.arg("--watch")
.piped_output()
.spawn()
.unwrap();
let (_stdout_lines, mut stderr_lines) = child_lines(&mut child);
let next_line = next_line(&mut stderr_lines).await.unwrap();
assert_contains!(&next_line, "Fmt started");
assert_contains!(
skip_restarting_line(&mut stderr_lines).await,
"badly_formatted.js"
);
assert_contains!(
wait_contains("Checked", &mut stderr_lines).await,
"Checked 1 file"
);
wait_contains("Fmt finished", &mut stderr_lines).await;
let expected = fixed.read_to_string();
let actual = badly_formatted.read_to_string();
assert_eq!(actual, expected);
// Change content of the file again to be badly formatted
badly_formatted_original.copy(&badly_formatted);
assert_contains!(
skip_restarting_line(&mut stderr_lines).await,
"badly_formatted.js"
);
assert_contains!(
wait_contains("Checked", &mut stderr_lines).await,
"Checked 1 file"
);
wait_contains("Fmt finished", &mut stderr_lines).await;
// Check if file has been automatically formatted by watcher
let expected = fixed.read_to_string();
let actual = badly_formatted.read_to_string();
assert_eq!(actual, expected);
check_alive_then_kill(child);
}
#[test(flaky)]
async fn fmt_watch_without_args_test() {
let fmt_testdata_path = util::testdata_path().join("fmt");
let t = TempDir::new();
let fixed = fmt_testdata_path.join("badly_formatted_fixed.js");
let badly_formatted_original = fmt_testdata_path.join("badly_formatted.mjs");
let badly_formatted = t.path().join("badly_formatted.js");
badly_formatted_original.copy(&badly_formatted);
let mut child = util::deno_cmd()
.current_dir(t.path())
.arg("fmt")
.arg("--watch")
.arg(".")
.piped_output()
.spawn()
.unwrap();
let (_stdout_lines, mut stderr_lines) = child_lines(&mut child);
let next_line = next_line(&mut stderr_lines).await.unwrap();
assert_contains!(&next_line, "Fmt started");
assert_contains!(
skip_restarting_line(&mut stderr_lines).await,
"badly_formatted.js"
);
assert_contains!(
wait_contains("Checked", &mut stderr_lines).await,
"Checked 1 file"
);
wait_contains("Fmt finished.", &mut stderr_lines).await;
let expected = fixed.read_to_string();
let actual = badly_formatted.read_to_string();
assert_eq!(actual, expected);
// Change content of the file again to be badly formatted
badly_formatted_original.copy(&badly_formatted);
assert_contains!(
skip_restarting_line(&mut stderr_lines).await,
"badly_formatted.js"
);
assert_contains!(
wait_contains("Checked", &mut stderr_lines).await,
"Checked 1 file"
);
// Check if file has been automatically formatted by watcher
let expected = fixed.read_to_string();
let actual = badly_formatted.read_to_string();
assert_eq!(actual, expected);
check_alive_then_kill(child);
}
#[test(flaky)]
async fn fmt_check_all_files_on_each_change_test() {
let t = TempDir::new();
let fmt_testdata_path = util::testdata_path().join("fmt");
let badly_formatted_original = fmt_testdata_path.join("badly_formatted.mjs");
let badly_formatted_1 = t.path().join("badly_formatted_1.js");
let badly_formatted_2 = t.path().join("badly_formatted_2.js");
badly_formatted_original.copy(&badly_formatted_1);
badly_formatted_original.copy(&badly_formatted_2);
let mut child = util::deno_cmd()
.current_dir(t.path())
.arg("fmt")
.arg(t.path())
.arg("--watch")
.arg("--check")
.piped_output()
.spawn()
.unwrap();
let (_stdout_lines, mut stderr_lines) = child_lines(&mut child);
assert_contains!(
wait_contains("error", &mut stderr_lines).await,
"Found 2 not formatted files in 2 files"
);
wait_contains("Fmt failed.", &mut stderr_lines).await;
// Change content of the file again to be badly formatted
badly_formatted_original.copy(&badly_formatted_1);
assert_contains!(
wait_contains("error", &mut stderr_lines).await,
"Found 2 not formatted files in 2 files"
);
check_alive_then_kill(child);
}
#[test(flaky)]
async fn run_watch_no_dynamic() {
let t = TempDir::new();
let file_to_watch = t.path().join("file_to_watch.js");
file_to_watch.write("console.log('Hello world');");
let mut child = util::deno_cmd()
.current_dir(t.path())
.arg("run")
.arg("--watch")
.arg("-L")
.arg("debug")
.arg(&file_to_watch)
.env("NO_COLOR", "1")
.piped_output()
.spawn()
.unwrap();
let (mut stdout_lines, mut stderr_lines) = child_lines(&mut child);
wait_contains("Hello world", &mut stdout_lines).await;
wait_for_watcher("file_to_watch.js", &mut stderr_lines).await;
// Change content of the file
file_to_watch.write("console.log('Hello world2');");
wait_contains("Restarting", &mut stderr_lines).await;
wait_contains("Hello world2", &mut stdout_lines).await;
wait_for_watcher("file_to_watch.js", &mut stderr_lines).await;
// Add dependency
let another_file = t.path().join("another_file.js");
another_file.write("export const foo = 0;");
file_to_watch
.write("import { foo } from './another_file.js'; console.log(foo);");
wait_contains("Restarting", &mut stderr_lines).await;
wait_contains("0", &mut stdout_lines).await;
wait_for_watcher("another_file.js", &mut stderr_lines).await;
// Confirm that restarting occurs when a new file is updated
another_file.write("export const foo = 42;");
wait_contains("Restarting", &mut stderr_lines).await;
wait_contains("42", &mut stdout_lines).await;
wait_for_watcher("file_to_watch.js", &mut stderr_lines).await;
// Confirm that the watcher keeps on working even if the file is updated and has invalid syntax
file_to_watch.write("syntax error ^^");
wait_contains("Restarting", &mut stderr_lines).await;
wait_contains("error:", &mut stderr_lines).await;
wait_for_watcher("file_to_watch.js", &mut stderr_lines).await;
// Then restore the file
file_to_watch
.write("import { foo } from './another_file.js'; console.log(foo);");
wait_contains("Restarting", &mut stderr_lines).await;
wait_contains("42", &mut stdout_lines).await;
wait_for_watcher("another_file.js", &mut stderr_lines).await;
// Update the content of the imported file with invalid syntax
another_file.write("syntax error ^^");
wait_contains("Restarting", &mut stderr_lines).await;
wait_contains("error:", &mut stderr_lines).await;
wait_for_watcher("another_file.js", &mut stderr_lines).await;
// Modify the imported file and make sure that restarting occurs
another_file.write("export const foo = 'modified!';");
wait_contains("Restarting", &mut stderr_lines).await;
wait_contains("modified!", &mut stdout_lines).await;
wait_contains("Watching paths", &mut stderr_lines).await;
check_alive_then_kill(child);
}
#[test(flaky)]
async fn serve_watch_all() {
let t = TempDir::new();
let main_file_to_watch = t.path().join("main_file_to_watch.js");
main_file_to_watch.write(
"export default {
fetch(_request) {
return new Response(\"aaaaaaqqq!\");
},
};",
);
let another_file = t.path().join("another_file.js");
another_file.write("");
let mut child = util::deno_cmd()
.current_dir(t.path())
.arg("serve")
.arg(format!("--watch={another_file}"))
.arg("-L")
.arg("debug")
.arg(&main_file_to_watch)
.env("NO_COLOR", "1")
.piped_output()
.spawn()
.unwrap();
let (mut stdout_lines, mut stderr_lines) = child_lines(&mut child);
wait_for_watcher("main_file_to_watch.js", &mut stderr_lines).await;
// Change content of the file
main_file_to_watch.write(
"export default {
fetch(_request) {
return new Response(\"aaaaaaqqq123!\");
},
};",
);
wait_contains("Restarting", &mut stderr_lines).await;
wait_for_watcher("main_file_to_watch.js", &mut stderr_lines).await;
another_file.write("export const foo = 0;");
// Confirm that the added file is watched as well
wait_contains("Restarting", &mut stderr_lines).await;
wait_for_watcher("main_file_to_watch.js", &mut stderr_lines).await;
main_file_to_watch
.write("import { foo } from './another_file.js'; console.log(foo);");
wait_contains("Restarting", &mut stderr_lines).await;
wait_for_watcher("main_file_to_watch.js", &mut stderr_lines).await;
wait_contains("0", &mut stdout_lines).await;
another_file.write("export const foo = 42;");
wait_contains("Restarting", &mut stderr_lines).await;
wait_for_watcher("main_file_to_watch.js", &mut stderr_lines).await;
wait_contains("42", &mut stdout_lines).await;
// Confirm that watch continues even with wrong syntax error
another_file.write("syntax error ^^");
wait_contains("Restarting", &mut stderr_lines).await;
wait_contains("error:", &mut stderr_lines).await;
wait_for_watcher("main_file_to_watch.js", &mut stderr_lines).await;
main_file_to_watch.write(
"export default {
fetch(_request) {
return new Response(\"aaaaaaqqq!\");
},
};",
);
wait_contains("Restarting", &mut stderr_lines).await;
wait_for_watcher("main_file_to_watch.js", &mut stderr_lines).await;
check_alive_then_kill(child);
}
#[test(flaky)]
async fn run_watch_npm_specifier() {
let _g = util::http_server();
let t = TempDir::new();
let file_to_watch = t.path().join("file_to_watch.txt");
file_to_watch.write("Hello world");
let mut child = util::deno_cmd()
.current_dir(t.path())
.envs(env_vars_for_npm_tests())
.arg("run")
.arg("--watch=file_to_watch.txt")
.arg("-L")
.arg("debug")
.arg("npm:@denotest/bin/cli-cjs")
.arg("Hello world")
.env("NO_COLOR", "1")
.piped_output()
.spawn()
.unwrap();
let (mut stdout_lines, mut stderr_lines) = child_lines(&mut child);
wait_contains("Hello world", &mut stdout_lines).await;
wait_for_watcher("file_to_watch.txt", &mut stderr_lines).await;
// Change content of the file
file_to_watch.write("Hello world2");
wait_contains("Restarting", &mut stderr_lines).await;
wait_contains("Hello world", &mut stdout_lines).await;
wait_for_watcher("file_to_watch.txt", &mut stderr_lines).await;
check_alive_then_kill(child);
}
// TODO(bartlomieju): this test became flaky on macOS runner; it is unclear
// if that's because of a bug in code or the runner itself. We should reenable
// it once we upgrade to XL runners for macOS.
#[cfg(not(target_os = "macos"))]
#[test(flaky)]
async fn run_watch_external_watch_files() {
let t = TempDir::new();
let file_to_watch = t.path().join("file_to_watch.js");
file_to_watch.write("console.log('Hello world');");
let external_file_to_watch = t.path().join("external_file_to_watch.txt");
external_file_to_watch.write("Hello world");
let mut watch_arg = "--watch=".to_owned();
let external_file_to_watch_str = external_file_to_watch.to_string();
watch_arg.push_str(&external_file_to_watch_str);
let mut child = util::deno_cmd()
.current_dir(t.path())
.arg("run")
.arg(watch_arg)
.arg("-L")
.arg("debug")
.arg(&file_to_watch)
.env("NO_COLOR", "1")
.piped_output()
.spawn()
.unwrap();
let (mut stdout_lines, mut stderr_lines) = child_lines(&mut child);
wait_contains("Process started", &mut stderr_lines).await;
wait_contains("Hello world", &mut stdout_lines).await;
wait_for_watcher("external_file_to_watch.txt", &mut stderr_lines).await;
// Change content of the external file
external_file_to_watch.write("Hello world2");
wait_contains("Restarting", &mut stderr_lines).await;
wait_contains("Process finished", &mut stderr_lines).await;
// Again (https://github.com/denoland/deno/issues/17584)
external_file_to_watch.write("Hello world3");
wait_contains("Restarting", &mut stderr_lines).await;
wait_contains("Process finished", &mut stderr_lines).await;
check_alive_then_kill(child);
}
#[test(flaky)]
async fn run_watch_load_unload_events() {
let t = TempDir::new();
let file_to_watch = t.path().join("file_to_watch.js");
file_to_watch.write(
r#"
setInterval(() => {}, 0);
globalThis.addEventListener("load", () => {
console.log("load");
});
globalThis.addEventListener("unload", () => {
console.log("unload");
});
"#,
);
let mut child = util::deno_cmd()
.current_dir(t.path())
.arg("run")
.arg("--watch")
.arg("-L")
.arg("debug")
.arg(&file_to_watch)
.env("NO_COLOR", "1")
.piped_output()
.spawn()
.unwrap();
let (mut stdout_lines, mut stderr_lines) = child_lines(&mut child);
// Wait for the first load event to fire
wait_contains("load", &mut stdout_lines).await;
wait_for_watcher("file_to_watch.js", &mut stderr_lines).await;
// Change content of the file, this time without an interval to keep it alive.
file_to_watch.write(
r#"
globalThis.addEventListener("load", () => {
console.log("load");
});
globalThis.addEventListener("unload", () => {
console.log("unload");
});
"#,
);
// Wait for the restart
wait_contains("Restarting", &mut stderr_lines).await;
// Confirm that the unload event was dispatched from the first run
wait_contains("unload", &mut stdout_lines).await;
// Followed by the load event of the second run
wait_contains("load", &mut stdout_lines).await;
// Which is then unloaded as there is nothing keeping it alive.
wait_contains("unload", &mut stdout_lines).await;
check_alive_then_kill(child);
}
/// Confirm that the watcher continues to work even if module resolution fails at the *first* attempt
#[test(flaky)]
async fn run_watch_not_exit() {
let t = TempDir::new();
let file_to_watch = t.path().join("file_to_watch.js");
file_to_watch.write("syntax error ^^");
let mut child = util::deno_cmd()
.current_dir(t.path())
.arg("run")
.arg("--watch")
.arg("-L")
.arg("debug")
.arg(&file_to_watch)
.env("NO_COLOR", "1")
.piped_output()
.spawn()
.unwrap();
let (mut stdout_lines, mut stderr_lines) = child_lines(&mut child);
wait_contains("Process started", &mut stderr_lines).await;
wait_contains("error:", &mut stderr_lines).await;
wait_for_watcher("file_to_watch.js", &mut stderr_lines).await;
// Make sure the watcher actually restarts and works fine with the proper syntax
file_to_watch.write("console.log(42);");
wait_contains("Restarting", &mut stderr_lines).await;
wait_contains("42", &mut stdout_lines).await;
wait_contains("Process finished", &mut stderr_lines).await;
check_alive_then_kill(child);
}
#[test(flaky)]
async fn run_watch_with_import_map_and_relative_paths() {
fn create_relative_tmp_file(
directory: &TempDir,
filename: &'static str,
filecontent: &'static str,
) -> std::path::PathBuf {
let absolute_path = directory.path().join(filename);
absolute_path.write(filecontent);
let relative_path = absolute_path
.as_path()
.strip_prefix(directory.path())
.unwrap()
.to_owned();
assert!(relative_path.is_relative());
relative_path
}
let temp_directory = TempDir::new();
let file_to_watch = create_relative_tmp_file(
&temp_directory,
"file_to_watch.js",
"console.log('Hello world');",
);
let import_map_path = create_relative_tmp_file(
&temp_directory,
"import_map.json",
"{\"imports\": {}}",
);
let mut child = util::deno_cmd()
.current_dir(temp_directory.path())
.arg("run")
.arg("--watch")
.arg("--import-map")
.arg(&import_map_path)
.arg(&file_to_watch)
.env("NO_COLOR", "1")
.piped_output()
.spawn()
.unwrap();
let (mut stdout_lines, mut stderr_lines) = child_lines(&mut child);
let line = next_line(&mut stderr_lines).await.unwrap();
assert_contains!(&line, "Process started");
assert_contains!(
next_line(&mut stderr_lines).await.unwrap(),
"Process finished"
);
assert_contains!(next_line(&mut stdout_lines).await.unwrap(), "Hello world");
check_alive_then_kill(child);
}
#[test(flaky)]
async fn run_watch_with_ext_flag() {
let t = TempDir::new();
let file_to_watch = t.path().join("file_to_watch");
file_to_watch.write("interface I{}; console.log(42);");
let mut child = util::deno_cmd()
.current_dir(t.path())
.arg("run")
.arg("--watch")
.arg("--log-level")
.arg("debug")
.arg("--ext")
.arg("ts")
.arg(&file_to_watch)
.env("NO_COLOR", "1")
.piped_output()
.spawn()
.unwrap();
let (mut stdout_lines, mut stderr_lines) = child_lines(&mut child);
wait_contains("42", &mut stdout_lines).await;
// Make sure the watcher actually restarts and works fine with the proper language
wait_for_watcher("file_to_watch", &mut stderr_lines).await;
wait_contains("Process finished", &mut stderr_lines).await;
file_to_watch.write("type Bear = 'polar' | 'grizzly'; console.log(123);");
wait_contains("Restarting!", &mut stderr_lines).await;
wait_contains("123", &mut stdout_lines).await;
wait_contains("Process finished", &mut stderr_lines).await;
check_alive_then_kill(child);
}
#[test(flaky)]
async fn run_watch_error_messages() {
let t = TempDir::new();
let file_to_watch = t.path().join("file_to_watch.js");
file_to_watch
.write("throw SyntaxError(`outer`, {cause: TypeError(`inner`)})");
let mut child = util::deno_cmd()
.current_dir(t.path())
.arg("run")
.arg("--watch")
.arg(&file_to_watch)
.env("NO_COLOR", "1")
.piped_output()
.spawn()
.unwrap();
let (_, mut stderr_lines) = child_lines(&mut child);
wait_contains("Process started", &mut stderr_lines).await;
wait_contains(
"error: Uncaught (in promise) SyntaxError: outer",
&mut stderr_lines,
)
.await;
wait_contains("Caused by: TypeError: inner", &mut stderr_lines).await;
wait_contains("Process failed", &mut stderr_lines).await;
check_alive_then_kill(child);
}
#[test(flaky)]
async fn test_watch_basic() {
let t = TempDir::new();
let mut child = util::deno_cmd()
.current_dir(t.path())
.arg("test")
.arg("--watch")
.arg("--no-check")
.arg(t.path())
.env("NO_COLOR", "1")
.piped_output()
.spawn()
.unwrap();
let (mut stdout_lines, mut stderr_lines) = child_lines(&mut child);
assert_eq!(next_line(&mut stdout_lines).await.unwrap(), "");
assert_contains!(
next_line(&mut stdout_lines).await.unwrap(),
"0 passed | 0 failed"
);
wait_contains("Test finished", &mut stderr_lines).await;
let foo_file = t.path().join("foo.js");
let bar_file = t.path().join("bar.js");
let foo_test = t.path().join("foo_test.js");
let bar_test = t.path().join("bar_test.js");
foo_file.write("export default function foo() { 1 + 1 }");
bar_file.write("export default function bar() { 2 + 2 }");
foo_test.write("import foo from './foo.js'; Deno.test('foo', foo);");
bar_test.write("import bar from './bar.js'; Deno.test('bar', bar);");
assert_eq!(next_line(&mut stdout_lines).await.unwrap(), "");
assert_contains!(
next_line(&mut stdout_lines).await.unwrap(),
"running 1 test"
);
assert_contains!(next_line(&mut stdout_lines).await.unwrap(), "foo", "bar");
assert_contains!(
next_line(&mut stdout_lines).await.unwrap(),
"running 1 test"
);
assert_contains!(next_line(&mut stdout_lines).await.unwrap(), "foo", "bar");
next_line(&mut stdout_lines).await;
next_line(&mut stdout_lines).await;
next_line(&mut stdout_lines).await;
wait_contains("Test finished", &mut stderr_lines).await;
// Change content of the file
foo_test.write("import foo from './foo.js'; Deno.test('foobar', foo);");
assert_contains!(next_line(&mut stderr_lines).await.unwrap(), "Restarting");
assert_contains!(
next_line(&mut stdout_lines).await.unwrap(),
"running 1 test"
);
assert_contains!(next_line(&mut stdout_lines).await.unwrap(), "foobar");
next_line(&mut stdout_lines).await;
next_line(&mut stdout_lines).await;