From 30b56ce837cc3d442d55898e4e6f13fc9aad1584 Mon Sep 17 00:00:00 2001 From: Boni Garcia Date: Wed, 9 Aug 2023 10:31:15 +0200 Subject: [PATCH] [rust] Improve SM tests (WIP) --- rust/src/files.rs | 46 +++++++++++++++++++++++++---- rust/src/main.rs | 17 ++++++++++- rust/tests/browser_tests.rs | 2 +- rust/tests/chrome_download_tests.rs | 4 +-- 4 files changed, 59 insertions(+), 10 deletions(-) diff --git a/rust/src/files.rs b/rust/src/files.rs index 4061d2ab83c2c..53da506726175 100644 --- a/rust/src/files.rs +++ b/rust/src/files.rs @@ -197,8 +197,22 @@ pub fn unzip( .into()); } + log.debug(format!("**** Deleting {}", compressed_path.display())); remove_file(compressed_path)?; - copy_folder_content(tmp_path, final_path, single_file)?; + log.debug(format!( + "**** Copying {} to {} (single file {:?})", + tmp_path.display(), + final_path.display(), + single_file + )); + copy_folder_content( + tmp_path, + final_path, + single_file, + &compressed_path.to_path_buf(), + &log, + )?; + log.debug("**** Done"); Ok(()) } @@ -207,12 +221,17 @@ pub fn copy_folder_content( source: impl AsRef, destination: impl AsRef, single_file: Option, + avoid_path: &PathBuf, + log: &Logger, ) -> io::Result<()> { fs::create_dir_all(&destination)?; for dir_entry in fs::read_dir(source)? { let entry = dir_entry?; let file_type = entry.file_type()?; if file_type.is_file() { + if entry.path().eq(avoid_path) { + continue; + } let target_file_name = entry .file_name() .to_os_string() @@ -222,16 +241,31 @@ pub fn copy_folder_content( || (single_file.is_some() && single_file.clone().unwrap().eq(&target_file_name)) { let destination_path = destination.as_ref().join(entry.file_name()); + log.debug(format!( + "----> Copying {} to {}", + entry.path().display(), + destination_path.display() + )); if !destination_path.exists() { fs::copy(entry.path(), destination_path)?; } } } else if single_file.is_none() { - copy_folder_content( - entry.path(), - destination.as_ref().join(entry.file_name()), - single_file.clone(), - )?; + let destination_path = destination.as_ref().join(entry.file_name()); + log.debug(format!( + "----> Copying {} to {} (recursively)", + entry.path().display(), + destination_path.display() + )); + if !destination_path.exists() { + copy_folder_content( + entry.path(), + destination_path, + single_file.clone(), + avoid_path, + &log, + )?; + } } } Ok(()) diff --git a/rust/src/main.rs b/rust/src/main.rs index 45309fe0cb53d..d7e1893439fc1 100644 --- a/rust/src/main.rs +++ b/rust/src/main.rs @@ -15,6 +15,7 @@ // specific language governing permissions and limitations // under the License. +use std::error::Error; use std::process::exit; use clap::Parser; @@ -206,12 +207,26 @@ fn main() { log.warn(err.to_string()); flush_and_exit(OK, log); } else { - log.error(err.to_string()); + log_error(&log, err); flush_and_exit(DATAERR, log); } }); } +fn log_error(log: &Logger, err: Box) { + let error_message = err.to_string(); + let source = err.source(); + if source.is_some() { + log.error(format!( + "{}. Caused by: {}", + error_message, + source.unwrap().to_string() + )); + } else { + log.error(error_message); + } +} + fn flush_and_exit(code: i32, log: &Logger) -> ! { log.set_code(code); log.flush(); diff --git a/rust/tests/browser_tests.rs b/rust/tests/browser_tests.rs index 1d96737fb40c9..879274f4f770b 100644 --- a/rust/tests/browser_tests.rs +++ b/rust/tests/browser_tests.rs @@ -69,7 +69,7 @@ fn wrong_parameters_test( let mut cmd = Command::new(env!("CARGO_BIN_EXE_selenium-manager")); let assert_result = cmd .args([ - "--trace", + "--debug", "--browser", &browser, "--browser-version", diff --git a/rust/tests/chrome_download_tests.rs b/rust/tests/chrome_download_tests.rs index 76926f4b42296..bc2983de61d3d 100644 --- a/rust/tests/chrome_download_tests.rs +++ b/rust/tests/chrome_download_tests.rs @@ -31,7 +31,7 @@ fn chrome_latest_download_test() { "--force-browser-download", "--output", "json", - "--trace", + "--debug", ]) .assert() .success() @@ -53,7 +53,7 @@ fn chrome_version_download_test(#[case] browser_version: String) { &browser_version, "--output", "json", - "--trace", + "--debug", ]) .assert() .success()