Skip to content

Commit

Permalink
[rust] Improve SM tests (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
bonigarcia committed Aug 9, 2023
1 parent 889df2d commit 7f4e07b
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 12 deletions.
42 changes: 34 additions & 8 deletions rust/src/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,16 @@ 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_file, &log)?;
log.debug("**** Done");

Ok(())
}
Expand All @@ -207,6 +215,8 @@ pub fn copy_folder_content(
source: impl AsRef<Path>,
destination: impl AsRef<Path>,
single_file: Option<String>,
avoid_file: &str,
log: &Logger,
) -> io::Result<()> {
fs::create_dir_all(&destination)?;
for dir_entry in fs::read_dir(source)? {
Expand All @@ -218,20 +228,36 @@ pub fn copy_folder_content(
.to_os_string()
.into_string()
.unwrap_or_default();
if single_file.is_none()
|| (single_file.is_some() && single_file.clone().unwrap().eq(&target_file_name))
if (single_file.is_none()
|| (single_file.is_some() && single_file.clone().unwrap().eq(&target_file_name)))
&& !target_file_name.eq(avoid_file)
{
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_file,
&log,
)?;
}
}
}
Ok(())
Expand Down
17 changes: 16 additions & 1 deletion rust/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// specific language governing permissions and limitations
// under the License.

use std::error::Error;
use std::process::exit;

use clap::Parser;
Expand Down Expand Up @@ -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<dyn Error>) {
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();
Expand Down
2 changes: 1 addition & 1 deletion rust/tests/browser_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions rust/tests/chrome_download_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn chrome_latest_download_test() {
"--force-browser-download",
"--output",
"json",
"--trace",
"--debug",
])
.assert()
.success()
Expand All @@ -53,7 +53,7 @@ fn chrome_version_download_test(#[case] browser_version: String) {
&browser_version,
"--output",
"json",
"--trace",
"--debug",
])
.assert()
.success()
Expand Down

0 comments on commit 7f4e07b

Please sign in to comment.