diff options
Diffstat (limited to 'cli/tests/integration/watcher_tests.rs')
-rw-r--r-- | cli/tests/integration/watcher_tests.rs | 115 |
1 files changed, 50 insertions, 65 deletions
diff --git a/cli/tests/integration/watcher_tests.rs b/cli/tests/integration/watcher_tests.rs index 42ca4b520..c9a5632c0 100644 --- a/cli/tests/integration/watcher_tests.rs +++ b/cli/tests/integration/watcher_tests.rs @@ -18,7 +18,7 @@ macro_rules! assert_contains { // Helper function to skip watcher output that contains "Restarting" // phrase. fn skip_restarting_line( - mut stderr_lines: impl Iterator<Item = String>, + stderr_lines: &mut impl Iterator<Item = String>, ) -> String { loop { let msg = stderr_lines.next().unwrap(); @@ -69,10 +69,18 @@ fn child_lines( ) -> (impl Iterator<Item = String>, impl Iterator<Item = String>) { let stdout_lines = std::io::BufReader::new(child.stdout.take().unwrap()) .lines() - .map(|r| r.unwrap()); + .map(|r| { + let line = r.unwrap(); + eprintln!("STDOUT: {}", line); + line + }); let stderr_lines = std::io::BufReader::new(child.stderr.take().unwrap()) .lines() - .map(|r| r.unwrap()); + .map(|r| { + let line = r.unwrap(); + eprintln!("STERR: {}", line); + line + }); (stdout_lines, stderr_lines) } @@ -106,13 +114,7 @@ fn lint_watch_test() { .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 (_stdout_lines, mut stderr_lines) = child_lines(&mut child); let mut output = read_all_lints(&mut stderr_lines); let expected = std::fs::read_to_string(badly_linted_output).unwrap(); @@ -130,7 +132,6 @@ fn lint_watch_test() { // 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(); @@ -172,13 +173,7 @@ fn lint_watch_without_args_test() { .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 (_stdout_lines, mut stderr_lines) = child_lines(&mut child); let mut output = read_all_lints(&mut stderr_lines); let expected = std::fs::read_to_string(badly_linted_output).unwrap(); @@ -187,7 +182,6 @@ fn lint_watch_without_args_test() { // 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(); @@ -236,18 +230,12 @@ fn lint_all_files_on_each_change_test() { .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)); + let (_stdout_lines, mut stderr_lines) = child_lines(&mut child); 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"); @@ -276,12 +264,13 @@ fn fmt_watch_test() { .stderr(std::process::Stdio::piped()) .spawn() .unwrap(); - let (_stdout_lines, 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)); + let (_stdout_lines, mut stderr_lines) = child_lines(&mut child); - assert!(skip_restarting_line(stderr_lines).contains("badly_formatted.js")); + assert_contains!( + skip_restarting_line(&mut stderr_lines), + "badly_formatted.js" + ); + assert_contains!(read_line("Checked", &mut stderr_lines), "Checked 1 file"); let expected = std::fs::read_to_string(fixed.clone()).unwrap(); let actual = std::fs::read_to_string(badly_formatted.clone()).unwrap(); @@ -289,7 +278,12 @@ fn fmt_watch_test() { // Change content of the file again to be badly formatted std::fs::copy(&badly_formatted_original, &badly_formatted).unwrap(); - std::thread::sleep(std::time::Duration::from_secs(1)); + + assert_contains!( + skip_restarting_line(&mut stderr_lines), + "badly_formatted.js" + ); + assert_contains!(read_line("Checked", &mut stderr_lines), "Checked 1 file"); // Check if file has been automatically formatted by watcher let expected = std::fs::read_to_string(fixed).unwrap(); @@ -316,12 +310,13 @@ fn fmt_watch_without_args_test() { .stderr(std::process::Stdio::piped()) .spawn() .unwrap(); - let (_stdout_lines, 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)); + let (_stdout_lines, mut stderr_lines) = child_lines(&mut child); - assert!(skip_restarting_line(stderr_lines).contains("badly_formatted.js")); + assert_contains!( + skip_restarting_line(&mut stderr_lines), + "badly_formatted.js" + ); + assert_contains!(read_line("Checked", &mut stderr_lines), "Checked 1 file"); let expected = std::fs::read_to_string(fixed.clone()).unwrap(); let actual = std::fs::read_to_string(badly_formatted.clone()).unwrap(); @@ -329,7 +324,11 @@ fn fmt_watch_without_args_test() { // Change content of the file again to be badly formatted std::fs::copy(&badly_formatted_original, &badly_formatted).unwrap(); - std::thread::sleep(std::time::Duration::from_secs(1)); + assert_contains!( + skip_restarting_line(&mut stderr_lines), + "badly_formatted.js" + ); + assert_contains!(read_line("Checked", &mut stderr_lines), "Checked 1 file"); // Check if file has been automatically formatted by watcher let expected = std::fs::read_to_string(fixed).unwrap(); @@ -361,9 +360,6 @@ fn fmt_check_all_files_on_each_change_test() { .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" @@ -372,8 +368,6 @@ fn fmt_check_all_files_on_each_change_test() { // 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" @@ -407,7 +401,6 @@ fn bundle_js_watch() { let (_stdout_lines, mut stderr_lines) = child_lines(&mut deno); - std::thread::sleep(std::time::Duration::from_secs(1)); assert_contains!(stderr_lines.next().unwrap(), "Check"); assert_contains!(stderr_lines.next().unwrap(), "file_to_watch.js"); assert_contains!(stderr_lines.next().unwrap(), "mod6.bundle.js"); @@ -416,7 +409,7 @@ fn bundle_js_watch() { wait_for("Bundle finished", &mut stderr_lines); write(&file_to_watch, "console.log('Hello world2');").unwrap(); - std::thread::sleep(std::time::Duration::from_secs(1)); + assert_contains!(stderr_lines.next().unwrap(), "Check"); assert_contains!(stderr_lines.next().unwrap(), "File change detected!"); assert_contains!(stderr_lines.next().unwrap(), "file_to_watch.js"); @@ -427,7 +420,7 @@ fn bundle_js_watch() { // Confirm that the watcher keeps on working even if the file is updated and has invalid syntax write(&file_to_watch, "syntax error ^^").unwrap(); - std::thread::sleep(std::time::Duration::from_secs(1)); + assert_contains!(stderr_lines.next().unwrap(), "File change detected!"); assert_contains!(stderr_lines.next().unwrap(), "error: "); wait_for("Bundle failed", &mut stderr_lines); @@ -456,7 +449,6 @@ fn bundle_watch_not_exit() { .unwrap(); let (_stdout_lines, mut stderr_lines) = child_lines(&mut deno); - std::thread::sleep(std::time::Duration::from_secs(1)); assert_contains!(stderr_lines.next().unwrap(), "error:"); assert_contains!(stderr_lines.next().unwrap(), "Bundle failed"); // the target file hasn't been created yet @@ -464,12 +456,14 @@ fn bundle_watch_not_exit() { // Make sure the watcher actually restarts and works fine with the proper syntax write(&file_to_watch, "console.log(42);").unwrap(); - std::thread::sleep(std::time::Duration::from_secs(1)); + assert_contains!(stderr_lines.next().unwrap(), "Check"); 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); + // bundled file is created assert!(target_file.is_file()); check_alive_then_kill(deno); @@ -497,13 +491,8 @@ fn run_watch() { assert_contains!(stdout_lines.next().unwrap(), "Hello world"); wait_for("Process finished", &mut stderr_lines); - // 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 write(&file_to_watch, "console.log('Hello world2');").unwrap(); - // 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)); assert_contains!(stderr_lines.next().unwrap(), "Restarting"); assert_contains!(stdout_lines.next().unwrap(), "Hello world2"); @@ -517,21 +506,21 @@ fn run_watch() { "import { foo } from './another_file.js'; console.log(foo);", ) .unwrap(); - std::thread::sleep(std::time::Duration::from_secs(1)); + assert_contains!(stderr_lines.next().unwrap(), "Restarting"); assert_contains!(stdout_lines.next().unwrap(), '0'); wait_for("Process finished", &mut stderr_lines); // Confirm that restarting occurs when a new file is updated write(&another_file, "export const foo = 42;").unwrap(); - std::thread::sleep(std::time::Duration::from_secs(1)); + assert_contains!(stderr_lines.next().unwrap(), "Restarting"); assert_contains!(stdout_lines.next().unwrap(), "42"); wait_for("Process finished", &mut stderr_lines); // Confirm that the watcher keeps on working even if the file is updated and has invalid syntax write(&file_to_watch, "syntax error ^^").unwrap(); - std::thread::sleep(std::time::Duration::from_secs(1)); + assert_contains!(stderr_lines.next().unwrap(), "Restarting"); assert_contains!(stderr_lines.next().unwrap(), "error:"); wait_for("Process failed", &mut stderr_lines); @@ -542,21 +531,21 @@ fn run_watch() { "import { foo } from './another_file.js'; console.log(foo);", ) .unwrap(); - std::thread::sleep(std::time::Duration::from_secs(1)); + assert_contains!(stderr_lines.next().unwrap(), "Restarting"); assert_contains!(stdout_lines.next().unwrap(), "42"); wait_for("Process finished", &mut stderr_lines); // Update the content of the imported file with invalid syntax write(&another_file, "syntax error ^^").unwrap(); - std::thread::sleep(std::time::Duration::from_secs(1)); + assert_contains!(stderr_lines.next().unwrap(), "Restarting"); assert_contains!(stderr_lines.next().unwrap(), "error:"); wait_for("Process failed", &mut stderr_lines); // Modify the imported file and make sure that restarting occurs write(&another_file, "export const foo = 'modified!';").unwrap(); - std::thread::sleep(std::time::Duration::from_secs(1)); + assert_contains!(stderr_lines.next().unwrap(), "Restarting"); assert_contains!(stdout_lines.next().unwrap(), "modified!"); wait_for("Process finished", &mut stderr_lines); @@ -613,9 +602,6 @@ fn run_watch_load_unload_events() { ) .unwrap(); - // 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 assert_contains!(stderr_lines.next().unwrap(), "Restarting"); @@ -650,13 +636,12 @@ fn run_watch_not_exit() { .unwrap(); let (mut stdout_lines, mut stderr_lines) = child_lines(&mut child); - std::thread::sleep(std::time::Duration::from_secs(1)); assert_contains!(stderr_lines.next().unwrap(), "error:"); assert_contains!(stderr_lines.next().unwrap(), "Process failed"); // Make sure the watcher actually restarts and works fine with the proper syntax write(&file_to_watch, "console.log(42);").unwrap(); - std::thread::sleep(std::time::Duration::from_secs(1)); + assert_contains!(stderr_lines.next().unwrap(), "Restarting"); assert_contains!(stdout_lines.next().unwrap(), "42"); wait_for("Process finished", &mut stderr_lines); @@ -905,6 +890,6 @@ fn test_watch_doc() { // 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"); + assert_contains!(skip_restarting_line(&mut stderr_lines), "foo.ts$3-6"); check_alive_then_kill(child); } |