From 85a2943435d645c0b45e27e4f0312b5434e1fb65 Mon Sep 17 00:00:00 2001 From: Zheyu Zhang Date: Sat, 30 Oct 2021 15:59:53 +0800 Subject: fix(cli): lint/format all discoverd files on each change (#12518) --- cli/tests/integration/watcher_tests.rs | 100 ++++++++++++++++++++++++++++++++- 1 file changed, 98 insertions(+), 2 deletions(-) (limited to 'cli/tests') diff --git a/cli/tests/integration/watcher_tests.rs b/cli/tests/integration/watcher_tests.rs index feda1bac7..ce7848527 100644 --- a/cli/tests/integration/watcher_tests.rs +++ b/cli/tests/integration/watcher_tests.rs @@ -55,6 +55,10 @@ fn wait_for(s: &str, lines: &mut impl Iterator) { } } +fn read_line(s: &str, lines: &mut impl Iterator) -> String { + lines.find(|m| m.contains(s)).unwrap() +} + fn check_alive_then_kill(mut child: std::process::Child) { assert!(child.try_wait().unwrap().is_none()); child.kill().unwrap(); @@ -77,6 +81,8 @@ 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_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 = @@ -86,8 +92,6 @@ fn lint_watch_test() { 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"); @@ -139,6 +143,54 @@ fn lint_watch_test() { drop(t); } +#[test] +fn lint_all_files_on_each_change_test() { + let t = TempDir::new().expect("tempdir fail"); + 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"); + std::fs::copy(&badly_linted_fixed0, &badly_linted_1) + .expect("Failed to copy file"); + std::fs::copy(&badly_linted_fixed1, &badly_linted_2) + .expect("Failed to copy file"); + + let mut child = util::deno_cmd() + .current_dir(util::testdata_path()) + .arg("lint") + .arg(&t.path()) + .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()); + + std::thread::sleep(std::time::Duration::from_secs(1)); + + assert_contains!(read_line("Checked", &mut stderr_lines), "Checked 2 files"); + + std::fs::copy(&badly_linted_fixed2, &badly_linted_2) + .expect("Failed to copy file"); + std::thread::sleep(std::time::Duration::from_secs(1)); + + assert_contains!(read_line("Checked", &mut stderr_lines), "Checked 2 files"); + + assert!(child.try_wait().unwrap().is_none()); + + child.kill().unwrap(); + drop(t); +} + #[test] fn fmt_watch_test() { let t = TempDir::new().unwrap(); @@ -180,6 +232,50 @@ fn fmt_watch_test() { check_alive_then_kill(child); } +#[test] +fn fmt_check_all_files_on_each_change_test() { + let t = TempDir::new().unwrap(); + let badly_formatted_original = + util::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"); + std::fs::copy(&badly_formatted_original, &badly_formatted_1).unwrap(); + std::fs::copy(&badly_formatted_original, &badly_formatted_2).unwrap(); + + let mut child = util::deno_cmd() + .current_dir(util::testdata_path()) + .arg("fmt") + .arg(&t.path()) + .arg("--watch") + .arg("--check") + .arg("--unstable") + .stdout(std::process::Stdio::piped()) + .stderr(std::process::Stdio::piped()) + .spawn() + .unwrap(); + let (_stdout_lines, mut stderr_lines) = child_lines(&mut child); + + // TODO(lucacasonato): remove this timeout. It seems to be needed on Linux. + std::thread::sleep(std::time::Duration::from_secs(1)); + + assert_contains!( + read_line("error", &mut stderr_lines), + "Found 2 not formatted files in 2 files" + ); + + // Change content of the file again to be badly formatted + std::fs::copy(&badly_formatted_original, &badly_formatted_1).unwrap(); + + std::thread::sleep(std::time::Duration::from_secs(1)); + + assert_contains!( + read_line("error", &mut stderr_lines), + "Found 2 not formatted files in 2 files" + ); + + check_alive_then_kill(child); +} + #[test] fn bundle_js_watch() { use std::path::PathBuf; -- cgit v1.2.3