2021-06-27 13:27:36 -04:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
2021-08-18 09:35:24 -04:00
|
|
|
use flaky_test::flaky_test;
|
2021-09-23 15:12:22 -04:00
|
|
|
use std::fs::write;
|
2021-06-27 13:27:36 -04:00
|
|
|
use std::io::BufRead;
|
|
|
|
use tempfile::TempDir;
|
|
|
|
use test_util as util;
|
|
|
|
|
2021-09-23 15:12:22 -04:00
|
|
|
macro_rules! assert_contains {
|
|
|
|
($string:expr, $($test:expr),+) => {
|
|
|
|
let string = $string; // This might be a function call or something
|
|
|
|
if !($(string.contains($test))||+) {
|
|
|
|
panic!("{:?} does not contain any of {:?}", string, [$($test),+]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-27 13:27:36 -04:00
|
|
|
// Helper function to skip watcher output that contains "Restarting"
|
|
|
|
// phrase.
|
|
|
|
fn skip_restarting_line(
|
|
|
|
mut stderr_lines: impl Iterator<Item = String>,
|
|
|
|
) -> String {
|
|
|
|
loop {
|
|
|
|
let msg = stderr_lines.next().unwrap();
|
|
|
|
if !msg.contains("Restarting") {
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-05 17:07:38 -04:00
|
|
|
fn read_all_lints(stderr_lines: &mut impl Iterator<Item = String>) -> String {
|
|
|
|
let mut str = String::new();
|
|
|
|
for t in stderr_lines {
|
|
|
|
let t = util::strip_ansi_codes(&t);
|
|
|
|
if t.starts_with("Watcher File change detected") {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if t.starts_with("Watcher") {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if t.starts_with('(') {
|
|
|
|
str.push_str(&t);
|
|
|
|
str.push('\n');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
str
|
|
|
|
}
|
|
|
|
|
2021-09-23 15:12:22 -04:00
|
|
|
fn wait_for(s: &str, lines: &mut impl Iterator<Item = String>) {
|
2021-06-27 13:27:36 -04:00
|
|
|
loop {
|
2021-09-23 15:12:22 -04:00
|
|
|
let msg = lines.next().unwrap();
|
|
|
|
if msg.contains(s) {
|
2021-06-27 13:27:36 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-23 15:12:22 -04:00
|
|
|
fn check_alive_then_kill(mut child: std::process::Child) {
|
|
|
|
assert!(child.try_wait().unwrap().is_none());
|
|
|
|
child.kill().unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn child_lines(
|
|
|
|
child: &mut std::process::Child,
|
|
|
|
) -> (impl Iterator<Item = String>, impl Iterator<Item = String>) {
|
|
|
|
let stdout_lines = std::io::BufReader::new(child.stdout.take().unwrap())
|
|
|
|
.lines()
|
|
|
|
.map(|r| r.unwrap());
|
|
|
|
let stderr_lines = std::io::BufReader::new(child.stderr.take().unwrap())
|
|
|
|
.lines()
|
|
|
|
.map(|r| r.unwrap());
|
|
|
|
(stdout_lines, stderr_lines)
|
2021-06-27 13:27:36 -04:00
|
|
|
}
|
|
|
|
|
2021-10-05 17:07:38 -04:00
|
|
|
#[test]
|
|
|
|
fn lint_watch_test() {
|
|
|
|
let t = TempDir::new().expect("tempdir fail");
|
|
|
|
let badly_linted_original =
|
|
|
|
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_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");
|
|
|
|
let badly_linted_output =
|
|
|
|
util::testdata_path().join("lint/watch/badly_linted.js.out");
|
|
|
|
|
|
|
|
std::fs::copy(&badly_linted_original, &badly_linted)
|
|
|
|
.expect("Failed to copy file");
|
|
|
|
|
|
|
|
let mut child = util::deno_cmd()
|
|
|
|
.current_dir(util::testdata_path())
|
|
|
|
.arg("lint")
|
|
|
|
.arg(&badly_linted)
|
|
|
|
.arg("--watch")
|
|
|
|
.arg("--unstable")
|
|
|
|
.stdout(std::process::Stdio::piped())
|
|
|
|
.stderr(std::process::Stdio::piped())
|
|
|
|
.spawn()
|
|
|
|
.expect("Failed to spawn script");
|
|
|
|
let mut stderr = child.stderr.as_mut().unwrap();
|
|
|
|
let mut stderr_lines = std::io::BufReader::new(&mut stderr)
|
|
|
|
.lines()
|
|
|
|
.map(|r| r.unwrap());
|
|
|
|
|
|
|
|
// TODO(lucacasonato): remove this timeout. It seems to be needed on Linux.
|
|
|
|
std::thread::sleep(std::time::Duration::from_secs(1));
|
|
|
|
|
|
|
|
let mut output = read_all_lints(&mut stderr_lines);
|
|
|
|
let expected = std::fs::read_to_string(badly_linted_output).unwrap();
|
|
|
|
assert_eq!(expected, output);
|
|
|
|
|
|
|
|
// Change content of the file again to be badly-linted1
|
|
|
|
std::fs::copy(&badly_linted_fixed1, &badly_linted)
|
|
|
|
.expect("Failed to copy file");
|
|
|
|
std::thread::sleep(std::time::Duration::from_secs(1));
|
|
|
|
|
|
|
|
output = read_all_lints(&mut stderr_lines);
|
|
|
|
let expected = std::fs::read_to_string(badly_linted_fixed1_output).unwrap();
|
|
|
|
assert_eq!(expected, output);
|
|
|
|
|
|
|
|
// Change content of the file again to be badly-linted1
|
|
|
|
std::fs::copy(&badly_linted_fixed2, &badly_linted)
|
|
|
|
.expect("Failed to copy file");
|
|
|
|
std::thread::sleep(std::time::Duration::from_secs(1));
|
|
|
|
|
|
|
|
output = read_all_lints(&mut stderr_lines);
|
|
|
|
let expected = std::fs::read_to_string(badly_linted_fixed2_output).unwrap();
|
|
|
|
assert_eq!(expected, output);
|
|
|
|
|
|
|
|
// the watcher process is still alive
|
|
|
|
assert!(child.try_wait().unwrap().is_none());
|
|
|
|
|
|
|
|
child.kill().unwrap();
|
|
|
|
drop(t);
|
|
|
|
}
|
|
|
|
|
2021-06-27 13:27:36 -04:00
|
|
|
#[test]
|
|
|
|
fn fmt_watch_test() {
|
2021-09-23 15:12:22 -04:00
|
|
|
let t = TempDir::new().unwrap();
|
2021-08-11 10:20:47 -04:00
|
|
|
let fixed = util::testdata_path().join("badly_formatted_fixed.js");
|
2021-06-27 13:27:36 -04:00
|
|
|
let badly_formatted_original =
|
2021-08-11 10:20:47 -04:00
|
|
|
util::testdata_path().join("badly_formatted.mjs");
|
2021-06-27 13:27:36 -04:00
|
|
|
let badly_formatted = t.path().join("badly_formatted.js");
|
2021-09-23 15:12:22 -04:00
|
|
|
std::fs::copy(&badly_formatted_original, &badly_formatted).unwrap();
|
2021-06-27 13:27:36 -04:00
|
|
|
|
|
|
|
let mut child = util::deno_cmd()
|
2021-08-11 10:20:47 -04:00
|
|
|
.current_dir(util::testdata_path())
|
2021-06-27 13:27:36 -04:00
|
|
|
.arg("fmt")
|
|
|
|
.arg(&badly_formatted)
|
|
|
|
.arg("--watch")
|
|
|
|
.arg("--unstable")
|
|
|
|
.stdout(std::process::Stdio::piped())
|
|
|
|
.stderr(std::process::Stdio::piped())
|
|
|
|
.spawn()
|
2021-09-23 15:12:22 -04:00
|
|
|
.unwrap();
|
|
|
|
let (_stdout_lines, stderr_lines) = child_lines(&mut child);
|
2021-06-27 13:27:36 -04:00
|
|
|
|
|
|
|
// TODO(lucacasonato): remove this timeout. It seems to be needed on Linux.
|
|
|
|
std::thread::sleep(std::time::Duration::from_secs(1));
|
|
|
|
|
|
|
|
assert!(skip_restarting_line(stderr_lines).contains("badly_formatted.js"));
|
|
|
|
|
|
|
|
let expected = std::fs::read_to_string(fixed.clone()).unwrap();
|
|
|
|
let actual = std::fs::read_to_string(badly_formatted.clone()).unwrap();
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
|
|
|
|
// Change content of the file again to be badly formatted
|
2021-09-23 15:12:22 -04:00
|
|
|
std::fs::copy(&badly_formatted_original, &badly_formatted).unwrap();
|
2021-06-27 13:27:36 -04:00
|
|
|
std::thread::sleep(std::time::Duration::from_secs(1));
|
|
|
|
|
|
|
|
// Check if file has been automatically formatted by watcher
|
|
|
|
let expected = std::fs::read_to_string(fixed).unwrap();
|
|
|
|
let actual = std::fs::read_to_string(badly_formatted).unwrap();
|
|
|
|
assert_eq!(expected, actual);
|
2021-09-23 15:12:22 -04:00
|
|
|
check_alive_then_kill(child);
|
2021-06-27 13:27:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn bundle_js_watch() {
|
|
|
|
use std::path::PathBuf;
|
|
|
|
// Test strategy extends this of test bundle_js by adding watcher
|
2021-09-23 15:12:22 -04:00
|
|
|
let t = TempDir::new().unwrap();
|
2021-06-27 13:27:36 -04:00
|
|
|
let file_to_watch = t.path().join("file_to_watch.js");
|
2021-09-23 15:12:22 -04:00
|
|
|
write(&file_to_watch, "console.log('Hello world');").unwrap();
|
2021-06-27 13:27:36 -04:00
|
|
|
assert!(file_to_watch.is_file());
|
2021-09-23 15:12:22 -04:00
|
|
|
let t = TempDir::new().unwrap();
|
2021-06-27 13:27:36 -04:00
|
|
|
let bundle = t.path().join("mod6.bundle.js");
|
|
|
|
let mut deno = util::deno_cmd()
|
2021-08-11 10:20:47 -04:00
|
|
|
.current_dir(util::testdata_path())
|
2021-06-27 13:27:36 -04:00
|
|
|
.arg("bundle")
|
|
|
|
.arg(&file_to_watch)
|
|
|
|
.arg(&bundle)
|
|
|
|
.arg("--watch")
|
|
|
|
.arg("--unstable")
|
|
|
|
.env("NO_COLOR", "1")
|
|
|
|
.stdout(std::process::Stdio::piped())
|
|
|
|
.stderr(std::process::Stdio::piped())
|
|
|
|
.spawn()
|
2021-09-23 15:12:22 -04:00
|
|
|
.unwrap();
|
2021-06-27 13:27:36 -04:00
|
|
|
|
2021-09-23 15:12:22 -04:00
|
|
|
let (_stdout_lines, mut stderr_lines) = child_lines(&mut deno);
|
2021-06-27 13:27:36 -04:00
|
|
|
|
|
|
|
std::thread::sleep(std::time::Duration::from_secs(1));
|
2021-09-23 15:12:22 -04:00
|
|
|
assert_contains!(stderr_lines.next().unwrap(), "file_to_watch.js");
|
|
|
|
assert_contains!(stderr_lines.next().unwrap(), "mod6.bundle.js");
|
2021-06-27 13:27:36 -04:00
|
|
|
let file = PathBuf::from(&bundle);
|
|
|
|
assert!(file.is_file());
|
2021-09-23 15:12:22 -04:00
|
|
|
wait_for("Bundle finished", &mut stderr_lines);
|
2021-06-27 13:27:36 -04:00
|
|
|
|
2021-09-23 15:12:22 -04:00
|
|
|
write(&file_to_watch, "console.log('Hello world2');").unwrap();
|
2021-06-27 13:27:36 -04:00
|
|
|
std::thread::sleep(std::time::Duration::from_secs(1));
|
2021-09-23 15:12:22 -04:00
|
|
|
assert_contains!(stderr_lines.next().unwrap(), "File change detected!");
|
|
|
|
assert_contains!(stderr_lines.next().unwrap(), "file_to_watch.js");
|
|
|
|
assert_contains!(stderr_lines.next().unwrap(), "mod6.bundle.js");
|
2021-06-27 13:27:36 -04:00
|
|
|
let file = PathBuf::from(&bundle);
|
|
|
|
assert!(file.is_file());
|
2021-09-23 15:12:22 -04:00
|
|
|
wait_for("Bundle finished", &mut stderr_lines);
|
2021-06-27 13:27:36 -04:00
|
|
|
|
|
|
|
// Confirm that the watcher keeps on working even if the file is updated and has invalid syntax
|
2021-09-23 15:12:22 -04:00
|
|
|
write(&file_to_watch, "syntax error ^^").unwrap();
|
2021-06-27 13:27:36 -04:00
|
|
|
std::thread::sleep(std::time::Duration::from_secs(1));
|
2021-09-23 15:12:22 -04:00
|
|
|
assert_contains!(stderr_lines.next().unwrap(), "File change detected!");
|
|
|
|
assert_contains!(stderr_lines.next().unwrap(), "error: ");
|
|
|
|
wait_for("Bundle failed", &mut stderr_lines);
|
|
|
|
check_alive_then_kill(deno);
|
2021-06-27 13:27:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Confirm that the watcher continues to work even if module resolution fails at the *first* attempt
|
|
|
|
#[test]
|
|
|
|
fn bundle_watch_not_exit() {
|
2021-09-23 15:12:22 -04:00
|
|
|
let t = TempDir::new().unwrap();
|
2021-06-27 13:27:36 -04:00
|
|
|
let file_to_watch = t.path().join("file_to_watch.js");
|
2021-09-23 15:12:22 -04:00
|
|
|
write(&file_to_watch, "syntax error ^^").unwrap();
|
2021-06-27 13:27:36 -04:00
|
|
|
let target_file = t.path().join("target.js");
|
|
|
|
|
|
|
|
let mut deno = util::deno_cmd()
|
2021-08-11 10:20:47 -04:00
|
|
|
.current_dir(util::testdata_path())
|
2021-06-27 13:27:36 -04:00
|
|
|
.arg("bundle")
|
|
|
|
.arg(&file_to_watch)
|
|
|
|
.arg(&target_file)
|
|
|
|
.arg("--watch")
|
|
|
|
.arg("--unstable")
|
|
|
|
.env("NO_COLOR", "1")
|
|
|
|
.stdout(std::process::Stdio::piped())
|
|
|
|
.stderr(std::process::Stdio::piped())
|
|
|
|
.spawn()
|
2021-09-23 15:12:22 -04:00
|
|
|
.unwrap();
|
|
|
|
let (_stdout_lines, mut stderr_lines) = child_lines(&mut deno);
|
2021-06-27 13:27:36 -04:00
|
|
|
|
|
|
|
std::thread::sleep(std::time::Duration::from_secs(1));
|
2021-09-23 15:12:22 -04:00
|
|
|
assert_contains!(stderr_lines.next().unwrap(), "error:");
|
|
|
|
assert_contains!(stderr_lines.next().unwrap(), "Bundle failed");
|
2021-06-27 13:27:36 -04:00
|
|
|
// the target file hasn't been created yet
|
|
|
|
assert!(!target_file.is_file());
|
|
|
|
|
|
|
|
// Make sure the watcher actually restarts and works fine with the proper syntax
|
2021-09-23 15:12:22 -04:00
|
|
|
write(&file_to_watch, "console.log(42);").unwrap();
|
2021-06-27 13:27:36 -04:00
|
|
|
std::thread::sleep(std::time::Duration::from_secs(1));
|
2021-09-23 15:12:22 -04:00
|
|
|
assert_contains!(stderr_lines.next().unwrap(), "File change detected!");
|
|
|
|
assert_contains!(stderr_lines.next().unwrap(), "file_to_watch.js");
|
|
|
|
assert_contains!(stderr_lines.next().unwrap(), "target.js");
|
|
|
|
wait_for("Bundle finished", &mut stderr_lines);
|
2021-06-27 13:27:36 -04:00
|
|
|
// bundled file is created
|
|
|
|
assert!(target_file.is_file());
|
2021-09-23 15:12:22 -04:00
|
|
|
check_alive_then_kill(deno);
|
2021-06-27 13:27:36 -04:00
|
|
|
}
|
|
|
|
|
2021-08-12 07:51:27 -04:00
|
|
|
#[flaky_test::flaky_test]
|
2021-06-27 13:27:36 -04:00
|
|
|
fn run_watch() {
|
2021-09-23 15:12:22 -04:00
|
|
|
let t = TempDir::new().unwrap();
|
2021-06-27 13:27:36 -04:00
|
|
|
let file_to_watch = t.path().join("file_to_watch.js");
|
2021-09-23 15:12:22 -04:00
|
|
|
write(&file_to_watch, "console.log('Hello world');").unwrap();
|
2021-06-27 13:27:36 -04:00
|
|
|
|
|
|
|
let mut child = util::deno_cmd()
|
2021-08-11 10:20:47 -04:00
|
|
|
.current_dir(util::testdata_path())
|
2021-06-27 13:27:36 -04:00
|
|
|
.arg("run")
|
|
|
|
.arg("--watch")
|
|
|
|
.arg("--unstable")
|
|
|
|
.arg(&file_to_watch)
|
|
|
|
.env("NO_COLOR", "1")
|
|
|
|
.stdout(std::process::Stdio::piped())
|
|
|
|
.stderr(std::process::Stdio::piped())
|
|
|
|
.spawn()
|
2021-09-23 15:12:22 -04:00
|
|
|
.unwrap();
|
|
|
|
let (mut stdout_lines, mut stderr_lines) = child_lines(&mut child);
|
2021-06-27 13:27:36 -04:00
|
|
|
|
2021-09-23 15:12:22 -04:00
|
|
|
assert_contains!(stdout_lines.next().unwrap(), "Hello world");
|
|
|
|
wait_for("Process finished", &mut stderr_lines);
|
2021-06-27 13:27:36 -04:00
|
|
|
|
|
|
|
// TODO(lucacasonato): remove this timeout. It seems to be needed on Linux.
|
|
|
|
std::thread::sleep(std::time::Duration::from_secs(1));
|
|
|
|
|
|
|
|
// Change content of the file
|
2021-09-23 15:12:22 -04:00
|
|
|
write(&file_to_watch, "console.log('Hello world2');").unwrap();
|
2021-06-27 13:27:36 -04:00
|
|
|
// Events from the file watcher is "debounced", so we need to wait for the next execution to start
|
|
|
|
std::thread::sleep(std::time::Duration::from_secs(1));
|
|
|
|
|
2021-09-23 15:12:22 -04:00
|
|
|
assert_contains!(stderr_lines.next().unwrap(), "Restarting");
|
|
|
|
assert_contains!(stdout_lines.next().unwrap(), "Hello world2");
|
|
|
|
wait_for("Process finished", &mut stderr_lines);
|
2021-06-27 13:27:36 -04:00
|
|
|
|
|
|
|
// Add dependency
|
|
|
|
let another_file = t.path().join("another_file.js");
|
2021-09-23 15:12:22 -04:00
|
|
|
write(&another_file, "export const foo = 0;").unwrap();
|
|
|
|
write(
|
2021-06-27 13:27:36 -04:00
|
|
|
&file_to_watch,
|
|
|
|
"import { foo } from './another_file.js'; console.log(foo);",
|
|
|
|
)
|
2021-09-23 15:12:22 -04:00
|
|
|
.unwrap();
|
2021-06-27 13:27:36 -04:00
|
|
|
std::thread::sleep(std::time::Duration::from_secs(1));
|
2021-09-23 15:12:22 -04:00
|
|
|
assert_contains!(stderr_lines.next().unwrap(), "Restarting");
|
|
|
|
assert_contains!(stdout_lines.next().unwrap(), '0');
|
|
|
|
wait_for("Process finished", &mut stderr_lines);
|
2021-06-27 13:27:36 -04:00
|
|
|
|
|
|
|
// Confirm that restarting occurs when a new file is updated
|
2021-09-23 15:12:22 -04:00
|
|
|
write(&another_file, "export const foo = 42;").unwrap();
|
2021-06-27 13:27:36 -04:00
|
|
|
std::thread::sleep(std::time::Duration::from_secs(1));
|
2021-09-23 15:12:22 -04:00
|
|
|
assert_contains!(stderr_lines.next().unwrap(), "Restarting");
|
|
|
|
assert_contains!(stdout_lines.next().unwrap(), "42");
|
|
|
|
wait_for("Process finished", &mut stderr_lines);
|
2021-06-27 13:27:36 -04:00
|
|
|
|
|
|
|
// Confirm that the watcher keeps on working even if the file is updated and has invalid syntax
|
2021-09-23 15:12:22 -04:00
|
|
|
write(&file_to_watch, "syntax error ^^").unwrap();
|
2021-06-27 13:27:36 -04:00
|
|
|
std::thread::sleep(std::time::Duration::from_secs(1));
|
2021-09-23 15:12:22 -04:00
|
|
|
assert_contains!(stderr_lines.next().unwrap(), "Restarting");
|
|
|
|
assert_contains!(stderr_lines.next().unwrap(), "error:");
|
|
|
|
wait_for("Process failed", &mut stderr_lines);
|
2021-06-27 13:27:36 -04:00
|
|
|
|
|
|
|
// Then restore the file
|
2021-09-23 15:12:22 -04:00
|
|
|
write(
|
2021-06-27 13:27:36 -04:00
|
|
|
&file_to_watch,
|
|
|
|
"import { foo } from './another_file.js'; console.log(foo);",
|
|
|
|
)
|
2021-09-23 15:12:22 -04:00
|
|
|
.unwrap();
|
2021-06-27 13:27:36 -04:00
|
|
|
std::thread::sleep(std::time::Duration::from_secs(1));
|
2021-09-23 15:12:22 -04:00
|
|
|
assert_contains!(stderr_lines.next().unwrap(), "Restarting");
|
|
|
|
assert_contains!(stdout_lines.next().unwrap(), "42");
|
|
|
|
wait_for("Process finished", &mut stderr_lines);
|
2021-06-27 13:27:36 -04:00
|
|
|
|
|
|
|
// Update the content of the imported file with invalid syntax
|
2021-09-23 15:12:22 -04:00
|
|
|
write(&another_file, "syntax error ^^").unwrap();
|
2021-06-27 13:27:36 -04:00
|
|
|
std::thread::sleep(std::time::Duration::from_secs(1));
|
2021-09-23 15:12:22 -04:00
|
|
|
assert_contains!(stderr_lines.next().unwrap(), "Restarting");
|
|
|
|
assert_contains!(stderr_lines.next().unwrap(), "error:");
|
|
|
|
wait_for("Process failed", &mut stderr_lines);
|
2021-06-27 13:27:36 -04:00
|
|
|
|
|
|
|
// Modify the imported file and make sure that restarting occurs
|
2021-09-23 15:12:22 -04:00
|
|
|
write(&another_file, "export const foo = 'modified!';").unwrap();
|
2021-06-27 13:27:36 -04:00
|
|
|
std::thread::sleep(std::time::Duration::from_secs(1));
|
2021-09-23 15:12:22 -04:00
|
|
|
assert_contains!(stderr_lines.next().unwrap(), "Restarting");
|
|
|
|
assert_contains!(stdout_lines.next().unwrap(), "modified!");
|
|
|
|
wait_for("Process finished", &mut stderr_lines);
|
|
|
|
check_alive_then_kill(child);
|
2021-06-27 13:27:36 -04:00
|
|
|
}
|
|
|
|
|
2021-08-24 16:34:09 -04:00
|
|
|
#[test]
|
|
|
|
fn run_watch_load_unload_events() {
|
2021-09-23 15:12:22 -04:00
|
|
|
let t = TempDir::new().unwrap();
|
2021-08-24 16:34:09 -04:00
|
|
|
let file_to_watch = t.path().join("file_to_watch.js");
|
2021-09-23 15:12:22 -04:00
|
|
|
write(
|
2021-08-24 16:34:09 -04:00
|
|
|
&file_to_watch,
|
|
|
|
r#"
|
|
|
|
setInterval(() => {}, 0);
|
|
|
|
window.addEventListener("load", () => {
|
|
|
|
console.log("load");
|
|
|
|
});
|
|
|
|
|
|
|
|
window.addEventListener("unload", () => {
|
|
|
|
console.log("unload");
|
|
|
|
});
|
|
|
|
"#,
|
|
|
|
)
|
2021-09-23 15:12:22 -04:00
|
|
|
.unwrap();
|
2021-08-24 16:34:09 -04:00
|
|
|
|
|
|
|
let mut child = util::deno_cmd()
|
|
|
|
.current_dir(util::testdata_path())
|
|
|
|
.arg("run")
|
|
|
|
.arg("--watch")
|
|
|
|
.arg("--unstable")
|
|
|
|
.arg(&file_to_watch)
|
|
|
|
.env("NO_COLOR", "1")
|
|
|
|
.stdout(std::process::Stdio::piped())
|
|
|
|
.stderr(std::process::Stdio::piped())
|
|
|
|
.spawn()
|
2021-09-23 15:12:22 -04:00
|
|
|
.unwrap();
|
|
|
|
let (mut stdout_lines, mut stderr_lines) = child_lines(&mut child);
|
2021-08-24 16:34:09 -04:00
|
|
|
|
|
|
|
// Wait for the first load event to fire
|
2021-09-23 15:12:22 -04:00
|
|
|
assert_contains!(stdout_lines.next().unwrap(), "load");
|
2021-08-24 16:34:09 -04:00
|
|
|
|
|
|
|
// Change content of the file, this time without an interval to keep it alive.
|
2021-09-23 15:12:22 -04:00
|
|
|
write(
|
2021-08-24 16:34:09 -04:00
|
|
|
&file_to_watch,
|
|
|
|
r#"
|
|
|
|
window.addEventListener("load", () => {
|
|
|
|
console.log("load");
|
|
|
|
});
|
|
|
|
|
|
|
|
window.addEventListener("unload", () => {
|
|
|
|
console.log("unload");
|
|
|
|
});
|
|
|
|
"#,
|
|
|
|
)
|
2021-09-23 15:12:22 -04:00
|
|
|
.unwrap();
|
2021-08-24 16:34:09 -04:00
|
|
|
|
|
|
|
// Events from the file watcher is "debounced", so we need to wait for the next execution to start
|
|
|
|
std::thread::sleep(std::time::Duration::from_secs(1));
|
|
|
|
|
|
|
|
// Wait for the restart
|
2021-09-23 15:12:22 -04:00
|
|
|
assert_contains!(stderr_lines.next().unwrap(), "Restarting");
|
2021-08-24 16:34:09 -04:00
|
|
|
|
|
|
|
// Confirm that the unload event was dispatched from the first run
|
2021-09-23 15:12:22 -04:00
|
|
|
assert_contains!(stdout_lines.next().unwrap(), "unload");
|
2021-08-24 16:34:09 -04:00
|
|
|
|
|
|
|
// Followed by the load event of the second run
|
2021-09-23 15:12:22 -04:00
|
|
|
assert_contains!(stdout_lines.next().unwrap(), "load");
|
2021-08-24 16:34:09 -04:00
|
|
|
|
|
|
|
// Which is then unloaded as there is nothing keeping it alive.
|
2021-09-23 15:12:22 -04:00
|
|
|
assert_contains!(stdout_lines.next().unwrap(), "unload");
|
|
|
|
check_alive_then_kill(child);
|
2021-08-24 16:34:09 -04:00
|
|
|
}
|
|
|
|
|
2021-06-27 13:27:36 -04:00
|
|
|
/// Confirm that the watcher continues to work even if module resolution fails at the *first* attempt
|
|
|
|
#[test]
|
|
|
|
fn run_watch_not_exit() {
|
2021-09-23 15:12:22 -04:00
|
|
|
let t = TempDir::new().unwrap();
|
2021-06-27 13:27:36 -04:00
|
|
|
let file_to_watch = t.path().join("file_to_watch.js");
|
2021-09-23 15:12:22 -04:00
|
|
|
write(&file_to_watch, "syntax error ^^").unwrap();
|
2021-06-27 13:27:36 -04:00
|
|
|
|
|
|
|
let mut child = util::deno_cmd()
|
2021-08-11 10:20:47 -04:00
|
|
|
.current_dir(util::testdata_path())
|
2021-06-27 13:27:36 -04:00
|
|
|
.arg("run")
|
|
|
|
.arg("--watch")
|
|
|
|
.arg("--unstable")
|
|
|
|
.arg(&file_to_watch)
|
|
|
|
.env("NO_COLOR", "1")
|
|
|
|
.stdout(std::process::Stdio::piped())
|
|
|
|
.stderr(std::process::Stdio::piped())
|
|
|
|
.spawn()
|
2021-09-23 15:12:22 -04:00
|
|
|
.unwrap();
|
|
|
|
let (mut stdout_lines, mut stderr_lines) = child_lines(&mut child);
|
2021-06-27 13:27:36 -04:00
|
|
|
|
|
|
|
std::thread::sleep(std::time::Duration::from_secs(1));
|
2021-09-23 15:12:22 -04:00
|
|
|
assert_contains!(stderr_lines.next().unwrap(), "error:");
|
|
|
|
assert_contains!(stderr_lines.next().unwrap(), "Process failed");
|
2021-06-27 13:27:36 -04:00
|
|
|
|
|
|
|
// Make sure the watcher actually restarts and works fine with the proper syntax
|
2021-09-23 15:12:22 -04:00
|
|
|
write(&file_to_watch, "console.log(42);").unwrap();
|
2021-06-27 13:27:36 -04:00
|
|
|
std::thread::sleep(std::time::Duration::from_secs(1));
|
2021-09-23 15:12:22 -04:00
|
|
|
assert_contains!(stderr_lines.next().unwrap(), "Restarting");
|
|
|
|
assert_contains!(stdout_lines.next().unwrap(), "42");
|
|
|
|
wait_for("Process finished", &mut stderr_lines);
|
|
|
|
check_alive_then_kill(child);
|
2021-06-27 13:27:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
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);
|
2021-09-23 15:12:22 -04:00
|
|
|
write(&absolute_path, filecontent).unwrap();
|
2021-06-27 13:27:36 -04:00
|
|
|
let relative_path = absolute_path
|
2021-08-11 10:20:47 -04:00
|
|
|
.strip_prefix(util::testdata_path())
|
2021-09-23 15:12:22 -04:00
|
|
|
.unwrap()
|
2021-06-27 13:27:36 -04:00
|
|
|
.to_owned();
|
|
|
|
assert!(relative_path.is_relative());
|
|
|
|
relative_path
|
|
|
|
}
|
2021-09-23 15:12:22 -04:00
|
|
|
let temp_directory = TempDir::new_in(util::testdata_path()).unwrap();
|
2021-06-27 13:27:36 -04:00
|
|
|
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()
|
2021-08-11 10:20:47 -04:00
|
|
|
.current_dir(util::testdata_path())
|
2021-06-27 13:27:36 -04:00
|
|
|
.arg("run")
|
|
|
|
.arg("--unstable")
|
|
|
|
.arg("--watch")
|
|
|
|
.arg("--import-map")
|
|
|
|
.arg(&import_map_path)
|
|
|
|
.arg(&file_to_watch)
|
|
|
|
.env("NO_COLOR", "1")
|
|
|
|
.stdout(std::process::Stdio::piped())
|
|
|
|
.stderr(std::process::Stdio::piped())
|
|
|
|
.spawn()
|
2021-09-23 15:12:22 -04:00
|
|
|
.unwrap();
|
|
|
|
let (mut stdout_lines, mut stderr_lines) = child_lines(&mut child);
|
2021-06-27 13:27:36 -04:00
|
|
|
|
2021-09-23 15:12:22 -04:00
|
|
|
assert_contains!(stderr_lines.next().unwrap(), "Process finished");
|
|
|
|
assert_contains!(stdout_lines.next().unwrap(), "Hello world");
|
2021-06-27 13:27:36 -04:00
|
|
|
|
2021-09-23 15:12:22 -04:00
|
|
|
check_alive_then_kill(child);
|
2021-06-27 13:27:36 -04:00
|
|
|
}
|
|
|
|
|
2021-08-18 09:35:24 -04:00
|
|
|
#[flaky_test]
|
2021-06-27 13:27:36 -04:00
|
|
|
fn test_watch() {
|
2021-09-23 15:12:22 -04:00
|
|
|
let t = TempDir::new().unwrap();
|
2021-06-27 13:27:36 -04:00
|
|
|
|
|
|
|
let mut child = util::deno_cmd()
|
2021-08-11 10:20:47 -04:00
|
|
|
.current_dir(util::testdata_path())
|
2021-06-27 13:27:36 -04:00
|
|
|
.arg("test")
|
|
|
|
.arg("--watch")
|
|
|
|
.arg("--unstable")
|
|
|
|
.arg("--no-check")
|
|
|
|
.arg(&t.path())
|
|
|
|
.env("NO_COLOR", "1")
|
|
|
|
.stdout(std::process::Stdio::piped())
|
|
|
|
.stderr(std::process::Stdio::piped())
|
|
|
|
.spawn()
|
2021-09-23 15:12:22 -04:00
|
|
|
.unwrap();
|
|
|
|
let (mut stdout_lines, mut stderr_lines) = child_lines(&mut child);
|
2021-06-27 13:27:36 -04:00
|
|
|
|
2021-08-18 09:35:24 -04:00
|
|
|
assert_eq!(stdout_lines.next().unwrap(), "");
|
2021-06-27 13:27:36 -04:00
|
|
|
assert_contains!(
|
|
|
|
stdout_lines.next().unwrap(),
|
2021-08-18 09:35:24 -04:00
|
|
|
"0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out"
|
2021-06-27 13:27:36 -04:00
|
|
|
);
|
2021-09-23 15:12:22 -04:00
|
|
|
wait_for("Test finished", &mut stderr_lines);
|
2021-06-27 13:27:36 -04:00
|
|
|
|
|
|
|
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");
|
2021-09-23 15:12:22 -04:00
|
|
|
write(&foo_file, "export default function foo() { 1 + 1 }").unwrap();
|
|
|
|
write(&bar_file, "export default function bar() { 2 + 2 }").unwrap();
|
|
|
|
write(
|
2021-06-27 13:27:36 -04:00
|
|
|
&foo_test,
|
|
|
|
"import foo from './foo.js'; Deno.test('foo', foo);",
|
|
|
|
)
|
2021-09-23 15:12:22 -04:00
|
|
|
.unwrap();
|
|
|
|
write(
|
2021-06-27 13:27:36 -04:00
|
|
|
&bar_test,
|
|
|
|
"import bar from './bar.js'; Deno.test('bar', bar);",
|
|
|
|
)
|
2021-09-23 15:12:22 -04:00
|
|
|
.unwrap();
|
2021-06-27 13:27:36 -04:00
|
|
|
|
2021-08-18 09:35:24 -04:00
|
|
|
assert_eq!(stdout_lines.next().unwrap(), "");
|
2021-06-27 13:27:36 -04:00
|
|
|
assert_contains!(stdout_lines.next().unwrap(), "running 1 test");
|
|
|
|
assert_contains!(stdout_lines.next().unwrap(), "foo", "bar");
|
|
|
|
assert_contains!(stdout_lines.next().unwrap(), "running 1 test");
|
|
|
|
assert_contains!(stdout_lines.next().unwrap(), "foo", "bar");
|
|
|
|
stdout_lines.next();
|
|
|
|
stdout_lines.next();
|
|
|
|
stdout_lines.next();
|
2021-09-23 15:12:22 -04:00
|
|
|
wait_for("Test finished", &mut stderr_lines);
|
2021-06-27 13:27:36 -04:00
|
|
|
|
|
|
|
// Change content of the file
|
2021-09-23 15:12:22 -04:00
|
|
|
write(
|
2021-06-27 13:27:36 -04:00
|
|
|
&foo_test,
|
|
|
|
"import foo from './foo.js'; Deno.test('foobar', foo);",
|
|
|
|
)
|
2021-09-23 15:12:22 -04:00
|
|
|
.unwrap();
|
2021-06-27 13:27:36 -04:00
|
|
|
|
|
|
|
assert_contains!(stderr_lines.next().unwrap(), "Restarting");
|
|
|
|
assert_contains!(stdout_lines.next().unwrap(), "running 1 test");
|
|
|
|
assert_contains!(stdout_lines.next().unwrap(), "foobar");
|
|
|
|
stdout_lines.next();
|
|
|
|
stdout_lines.next();
|
|
|
|
stdout_lines.next();
|
2021-09-23 15:12:22 -04:00
|
|
|
wait_for("Test finished", &mut stderr_lines);
|
2021-06-27 13:27:36 -04:00
|
|
|
|
|
|
|
// Add test
|
|
|
|
let another_test = t.path().join("new_test.js");
|
2021-09-23 15:12:22 -04:00
|
|
|
write(&another_test, "Deno.test('another one', () => 3 + 3)").unwrap();
|
2021-06-27 13:27:36 -04:00
|
|
|
assert_contains!(stderr_lines.next().unwrap(), "Restarting");
|
|
|
|
assert_contains!(stdout_lines.next().unwrap(), "running 1 test");
|
|
|
|
assert_contains!(stdout_lines.next().unwrap(), "another one");
|
|
|
|
stdout_lines.next();
|
|
|
|
stdout_lines.next();
|
|
|
|
stdout_lines.next();
|
2021-09-23 15:12:22 -04:00
|
|
|
wait_for("Test finished", &mut stderr_lines);
|
2021-06-27 13:27:36 -04:00
|
|
|
|
|
|
|
// Confirm that restarting occurs when a new file is updated
|
2021-09-23 15:12:22 -04:00
|
|
|
write(&another_test, "Deno.test('another one', () => 3 + 3); Deno.test('another another one', () => 4 + 4)")
|
|
|
|
.unwrap();
|
2021-06-27 13:27:36 -04:00
|
|
|
assert_contains!(stderr_lines.next().unwrap(), "Restarting");
|
|
|
|
assert_contains!(stdout_lines.next().unwrap(), "running 2 tests");
|
|
|
|
assert_contains!(stdout_lines.next().unwrap(), "another one");
|
|
|
|
assert_contains!(stdout_lines.next().unwrap(), "another another one");
|
|
|
|
stdout_lines.next();
|
|
|
|
stdout_lines.next();
|
|
|
|
stdout_lines.next();
|
2021-09-23 15:12:22 -04:00
|
|
|
wait_for("Test finished", &mut stderr_lines);
|
2021-06-27 13:27:36 -04:00
|
|
|
|
|
|
|
// Confirm that the watcher keeps on working even if the file is updated and has invalid syntax
|
2021-09-23 15:12:22 -04:00
|
|
|
write(&another_test, "syntax error ^^").unwrap();
|
2021-06-27 13:27:36 -04:00
|
|
|
assert_contains!(stderr_lines.next().unwrap(), "Restarting");
|
|
|
|
assert_contains!(stderr_lines.next().unwrap(), "error:");
|
|
|
|
assert_contains!(stderr_lines.next().unwrap(), "Test failed");
|
|
|
|
|
|
|
|
// Then restore the file
|
2021-09-23 15:12:22 -04:00
|
|
|
write(&another_test, "Deno.test('another one', () => 3 + 3)").unwrap();
|
2021-06-27 13:27:36 -04:00
|
|
|
assert_contains!(stderr_lines.next().unwrap(), "Restarting");
|
|
|
|
assert_contains!(stdout_lines.next().unwrap(), "running 1 test");
|
|
|
|
assert_contains!(stdout_lines.next().unwrap(), "another one");
|
|
|
|
stdout_lines.next();
|
|
|
|
stdout_lines.next();
|
|
|
|
stdout_lines.next();
|
2021-09-23 15:12:22 -04:00
|
|
|
wait_for("Test finished", &mut stderr_lines);
|
2021-06-27 13:27:36 -04:00
|
|
|
|
|
|
|
// Confirm that the watcher keeps on working even if the file is updated and the test fails
|
|
|
|
// This also confirms that it restarts when dependencies change
|
2021-09-23 15:12:22 -04:00
|
|
|
write(
|
2021-06-27 13:27:36 -04:00
|
|
|
&foo_file,
|
|
|
|
"export default function foo() { throw new Error('Whoops!'); }",
|
|
|
|
)
|
2021-09-23 15:12:22 -04:00
|
|
|
.unwrap();
|
2021-06-27 13:27:36 -04:00
|
|
|
assert_contains!(stderr_lines.next().unwrap(), "Restarting");
|
|
|
|
assert_contains!(stdout_lines.next().unwrap(), "running 1 test");
|
|
|
|
assert_contains!(stdout_lines.next().unwrap(), "FAILED");
|
2021-09-23 15:12:22 -04:00
|
|
|
wait_for("test result", &mut stdout_lines);
|
2021-06-27 13:27:36 -04:00
|
|
|
stdout_lines.next();
|
2021-09-23 15:12:22 -04:00
|
|
|
wait_for("Test finished", &mut stderr_lines);
|
2021-06-27 13:27:36 -04:00
|
|
|
|
|
|
|
// Then restore the file
|
2021-09-23 15:12:22 -04:00
|
|
|
write(&foo_file, "export default function foo() { 1 + 1 }").unwrap();
|
2021-06-27 13:27:36 -04:00
|
|
|
assert_contains!(stderr_lines.next().unwrap(), "Restarting");
|
|
|
|
assert_contains!(stdout_lines.next().unwrap(), "running 1 test");
|
|
|
|
assert_contains!(stdout_lines.next().unwrap(), "foo");
|
|
|
|
stdout_lines.next();
|
|
|
|
stdout_lines.next();
|
|
|
|
stdout_lines.next();
|
2021-09-23 15:12:22 -04:00
|
|
|
wait_for("Test finished", &mut stderr_lines);
|
2021-06-27 13:27:36 -04:00
|
|
|
|
|
|
|
// Test that circular dependencies work fine
|
2021-09-23 15:12:22 -04:00
|
|
|
write(
|
2021-06-27 13:27:36 -04:00
|
|
|
&foo_file,
|
|
|
|
"import './bar.js'; export default function foo() { 1 + 1 }",
|
|
|
|
)
|
2021-09-23 15:12:22 -04:00
|
|
|
.unwrap();
|
|
|
|
write(
|
2021-06-27 13:27:36 -04:00
|
|
|
&bar_file,
|
|
|
|
"import './foo.js'; export default function bar() { 2 + 2 }",
|
|
|
|
)
|
2021-09-23 15:12:22 -04:00
|
|
|
.unwrap();
|
|
|
|
check_alive_then_kill(child);
|
2021-06-27 13:27:36 -04:00
|
|
|
}
|
2021-07-09 19:15:15 -04:00
|
|
|
|
2021-08-18 09:35:24 -04:00
|
|
|
#[flaky_test]
|
2021-07-09 19:15:15 -04:00
|
|
|
fn test_watch_doc() {
|
2021-09-23 15:12:22 -04:00
|
|
|
let t = TempDir::new().unwrap();
|
2021-07-09 19:15:15 -04:00
|
|
|
|
|
|
|
let mut child = util::deno_cmd()
|
2021-08-11 10:20:47 -04:00
|
|
|
.current_dir(util::testdata_path())
|
2021-07-09 19:15:15 -04:00
|
|
|
.arg("test")
|
|
|
|
.arg("--watch")
|
|
|
|
.arg("--doc")
|
|
|
|
.arg("--unstable")
|
|
|
|
.arg(&t.path())
|
|
|
|
.env("NO_COLOR", "1")
|
|
|
|
.stdout(std::process::Stdio::piped())
|
|
|
|
.stderr(std::process::Stdio::piped())
|
|
|
|
.spawn()
|
2021-09-23 15:12:22 -04:00
|
|
|
.unwrap();
|
|
|
|
let (mut stdout_lines, mut stderr_lines) = child_lines(&mut child);
|
2021-07-09 19:15:15 -04:00
|
|
|
|
2021-08-18 09:35:24 -04:00
|
|
|
assert_eq!(stdout_lines.next().unwrap(), "");
|
2021-07-09 19:15:15 -04:00
|
|
|
assert_contains!(
|
|
|
|
stdout_lines.next().unwrap(),
|
2021-08-18 09:35:24 -04:00
|
|
|
"0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out"
|
2021-07-09 19:15:15 -04:00
|
|
|
);
|
2021-09-23 15:12:22 -04:00
|
|
|
wait_for("Test finished", &mut stderr_lines);
|
2021-07-09 19:15:15 -04:00
|
|
|
|
|
|
|
let foo_file = t.path().join("foo.ts");
|
2021-09-23 15:12:22 -04:00
|
|
|
write(
|
2021-07-09 19:15:15 -04:00
|
|
|
&foo_file,
|
|
|
|
r#"
|
|
|
|
export default function foo() {}
|
|
|
|
"#,
|
|
|
|
)
|
2021-09-23 15:12:22 -04:00
|
|
|
.unwrap();
|
2021-07-09 19:15:15 -04:00
|
|
|
|
2021-09-23 15:12:22 -04:00
|
|
|
write(
|
2021-07-09 19:15:15 -04:00
|
|
|
&foo_file,
|
|
|
|
r#"
|
|
|
|
/**
|
|
|
|
* ```ts
|
|
|
|
* import foo from "./foo.ts";
|
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
export default function foo() {}
|
|
|
|
"#,
|
|
|
|
)
|
2021-09-23 15:12:22 -04:00
|
|
|
.unwrap();
|
2021-07-09 19:15:15 -04:00
|
|
|
|
|
|
|
// We only need to scan for a Check file://.../foo.ts$3-6 line that
|
|
|
|
// corresponds to the documentation block being type-checked.
|
|
|
|
assert_contains!(skip_restarting_line(stderr_lines), "foo.ts$3-6");
|
2021-09-23 15:12:22 -04:00
|
|
|
check_alive_then_kill(child);
|
2021-07-09 19:15:15 -04:00
|
|
|
}
|