From c7aba8a8b00b51d8eab3c2db339a5b5167028ae5 Mon Sep 17 00:00:00 2001 From: cristianoliveira Date: Sun, 30 Jun 2024 11:40:51 +0200 Subject: [PATCH] refact: removes sleep in the tests --- tests/common/lib.rs | 15 +++++----- tests/common/macros.rs | 7 +++-- ...watcher_does_not_die_with_failing_tasks.rs | 8 +++--- tests/watching_configured_rules.rs | 28 ++++++++----------- 4 files changed, 26 insertions(+), 32 deletions(-) diff --git a/tests/common/lib.rs b/tests/common/lib.rs index e936c13..e40dc6c 100644 --- a/tests/common/lib.rs +++ b/tests/common/lib.rs @@ -10,7 +10,7 @@ use std::{ }; pub struct Options { - pub log_file: &'static str, + pub output_file: &'static str, pub example_file: &'static str, } @@ -22,24 +22,23 @@ where // check if the file exists if so fail assert!( - !std::path::Path::new(&dir.join(opts.log_file)).exists(), + !std::path::Path::new(&dir.join(opts.output_file)).exists(), "the log file already exists, make sure to give an unique log file to avoid multiple writes to same file: {}", - dir.join(opts.log_file).display() + dir.join(opts.output_file).display() ); defer!({ - std::fs::remove_file(dir.join(opts.log_file)).expect("failed to remove test output file") + std::fs::remove_file(dir.join(opts.output_file)).expect("failed to remove file after running test"); }); let bin_path = dir.join("target/debug/fzz"); - let _ = std::fs::remove_file(dir.join(opts.log_file)); - let output_log = File::create(dir.join(opts.log_file)).expect("error log file"); + let output_file = File::create(dir.join(opts.output_file)).expect("error log file"); handler( Command::new(bin_path) .arg("-c") .arg(dir.join(opts.example_file)) - .stdout(Stdio::from(output_log)), - File::open(dir.join(opts.log_file)).expect("failed to open file"), + .stdout(Stdio::from(output_file)), + File::open(dir.join(opts.output_file)).expect("failed to open file"), ); } diff --git a/tests/common/macros.rs b/tests/common/macros.rs index 1a0d790..9ab39c9 100644 --- a/tests/common/macros.rs +++ b/tests/common/macros.rs @@ -26,7 +26,7 @@ macro_rules! wait_until { if result { break; } - sleep(Duration::from_millis(100)); + std::thread::sleep(std::time::Duration::from_millis(100)); } assert!($e, $($arg)+); @@ -38,7 +38,8 @@ macro_rules! wait_until { if result { break; } - sleep(Duration::from_millis(100)); + + std::thread::sleep(std::time::Duration::from_millis(100)); } }; } @@ -46,7 +47,7 @@ macro_rules! wait_until { #[macro_export] macro_rules! write_to_file { ($file:expr) => { - let mut file = File::create($file).expect("failed to open file"); + let mut file = std::fs::File::create($file).expect("failed to open file"); file.write_all(b"test_content\n") .expect("failed to write to file"); }; diff --git a/tests/watcher_does_not_die_with_failing_tasks.rs b/tests/watcher_does_not_die_with_failing_tasks.rs index 8bba72e..b3b6d76 100644 --- a/tests/watcher_does_not_die_with_failing_tasks.rs +++ b/tests/watcher_does_not_die_with_failing_tasks.rs @@ -9,9 +9,9 @@ fn test_it_watches_a_list_of_tasks_and_do_not_panic() { setup::with_example( setup::Options { example_file: "examples/list-of-watches.yml", - log_file: "test_it_watches_a_list_of_tasks_and_do_not_panic.log", + output_file: "test_it_watches_a_list_of_tasks_and_do_not_panic.log", }, - |fzz_cmd, mut output_log| { + |fzz_cmd, mut output_file| { let mut output = String::new(); let mut child = fzz_cmd.spawn().expect("failed to spawn sub process"); defer!({ @@ -19,7 +19,7 @@ fn test_it_watches_a_list_of_tasks_and_do_not_panic() { }); wait_until!({ - output_log + output_file .read_to_string(&mut output) .expect("failed to read test output file"); @@ -31,7 +31,7 @@ fn test_it_watches_a_list_of_tasks_and_do_not_panic() { write_to_file!("examples/workdir/trigger-watcher.txt"); wait_until!({ - output_log + output_file .read_to_string(&mut output) .expect("failed to read test output file"); diff --git a/tests/watching_configured_rules.rs b/tests/watching_configured_rules.rs index 3084f9f..d121a82 100644 --- a/tests/watching_configured_rules.rs +++ b/tests/watching_configured_rules.rs @@ -1,9 +1,4 @@ -use std::io::prelude::*; -use std::{ - fs::File, - thread::sleep, - time::Duration, -}; +use std::io::prelude::*; #[path = "./common/lib.rs"] mod setup; @@ -12,10 +7,10 @@ mod setup; fn test_it_is_not_triggered_by_ignored_files() { setup::with_example( setup::Options { - log_file: "test_it_is_not_triggered_by_ignored_files.log", + output_file: "test_it_is_not_triggered_by_ignored_files.log", example_file: "examples/simple-case.yml", }, - |fzz_cmd, mut log| { + |fzz_cmd, mut output_log| { let mut child = fzz_cmd .arg("-V") .arg("-t") @@ -31,7 +26,8 @@ fn test_it_is_not_triggered_by_ignored_files() { wait_until!( { - log.read_to_string(&mut output) + output_log + .read_to_string(&mut output) .expect("failed to read from file"); output.contains("Funzzy verbose") && output.contains("Watching...") @@ -44,11 +40,9 @@ fn test_it_is_not_triggered_by_ignored_files() { write_to_file!("examples/workdir/ignored/modifyme.txt"); - sleep(Duration::from_secs(2)); - wait_until!({ - sleep(Duration::from_millis(100)); - log.read_to_string(&mut output) + output_log + .read_to_string(&mut output) .expect("failed to read from file"); output.contains("Funzzy verbose: Events Ok") @@ -77,9 +71,9 @@ fn test_it_watch_files_and_execute_configured_commands() { setup::with_example( setup::Options { example_file: "examples/simple-case.yml", - log_file: "test_it_watch_files_and_execute_configured_commands.log", + output_file: "test_it_watch_files_and_execute_configured_commands.log", }, - |fzz_cmd, mut logger| { + |fzz_cmd, mut output_log| { let mut child = fzz_cmd.spawn().expect("failed to spawn process"); let mut output = String::new(); defer!({ @@ -87,7 +81,7 @@ fn test_it_watch_files_and_execute_configured_commands() { }); wait_until!({ - logger + output_log .read_to_string(&mut output) .expect("failed to read from file"); @@ -100,7 +94,7 @@ fn test_it_watch_files_and_execute_configured_commands() { wait_until!( { - logger + output_log .read_to_string(&mut output) .expect("failed to read from file");