From 01ce896ddf68ac5cedf9ce5e6d7782890dc08def Mon Sep 17 00:00:00 2001 From: Boni Garcia Date: Mon, 12 Aug 2024 16:36:42 +0200 Subject: [PATCH] [rust] Selenium Manager errors when browser-path is wrong (#13352) --- rust/src/config.rs | 2 ++ rust/src/lib.rs | 14 +++++++++++++ rust/src/main.rs | 41 ++++++++++++++++++++----------------- rust/tests/browser_tests.rs | 14 +++++++++++++ 4 files changed, 52 insertions(+), 19 deletions(-) diff --git a/rust/src/config.rs b/rust/src/config.rs index a3a2699bee6f1..7c57688b4acf2 100644 --- a/rust/src/config.rs +++ b/rust/src/config.rs @@ -42,6 +42,7 @@ pub const CACHE_PATH_KEY: &str = "cache-path"; pub struct ManagerConfig { pub cache_path: String, + pub fallback_driver_from_cache: bool, pub browser_version: String, pub driver_version: String, pub browser_path: String, @@ -97,6 +98,7 @@ impl ManagerConfig { ManagerConfig { cache_path, + fallback_driver_from_cache: true, browser_version: StringKey(vec!["browser-version", &browser_version_label], "") .get_value(), driver_version: StringKey(vec!["driver-version", &driver_version_label], "") diff --git a/rust/src/lib.rs b/rust/src/lib.rs index d7a0fc0878951..7d9bf60e5fbd3 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs @@ -1065,6 +1065,12 @@ pub trait SeleniumManager { if let Some(path) = self.detect_browser_path() { browser_path = path_to_string(&path); } + } else if !Path::new(&browser_path).exists() { + self.set_fallback_driver_from_cache(false); + return Err(anyhow!(format_one_arg( + "Browser path does not exist: {}", + &browser_path, + ))); } let escaped_browser_path = self.get_escaped_path(browser_path.to_string()); @@ -1466,6 +1472,14 @@ pub trait SeleniumManager { self.get_config_mut().avoid_stats = true; } } + + fn is_fallback_driver_from_cache(&self) -> bool { + self.get_config().fallback_driver_from_cache + } + + fn set_fallback_driver_from_cache(&mut self, fallback_driver_from_cache: bool) { + self.get_config_mut().fallback_driver_from_cache = fallback_driver_from_cache; + } } // ---------------------------------------------------------- diff --git a/rust/src/main.rs b/rust/src/main.rs index 4bf799e781396..537a0257ab152 100644 --- a/rust/src/main.rs +++ b/rust/src/main.rs @@ -243,25 +243,28 @@ fn main() { }) .unwrap_or_else(|err| { let log = selenium_manager.get_logger(); - if let Some(best_driver_from_cache) = - selenium_manager.find_best_driver_from_cache().unwrap() - { - log.debug_or_warn( - format!( - "There was an error managing {} ({}); using driver found in the cache", - selenium_manager.get_driver_name(), - err - ), - selenium_manager.is_offline(), - ); - log_driver_and_browser_path( - log, - &best_driver_from_cache, - &selenium_manager.get_browser_path_or_latest_from_cache(), - selenium_manager.get_receiver(), - ); - flush_and_exit(OK, log, Some(err)); - } else if selenium_manager.is_offline() { + if selenium_manager.is_fallback_driver_from_cache() { + if let Some(best_driver_from_cache) = + selenium_manager.find_best_driver_from_cache().unwrap() + { + log.debug_or_warn( + format!( + "There was an error managing {} ({}); using driver found in the cache", + selenium_manager.get_driver_name(), + err + ), + selenium_manager.is_offline(), + ); + log_driver_and_browser_path( + log, + &best_driver_from_cache, + &selenium_manager.get_browser_path_or_latest_from_cache(), + selenium_manager.get_receiver(), + ); + flush_and_exit(OK, log, Some(err)); + } + } + if selenium_manager.is_offline() { log.warn(&err); flush_and_exit(OK, log, Some(err)); } else { diff --git a/rust/tests/browser_tests.rs b/rust/tests/browser_tests.rs index 5254e3f0d277b..dd69101ee1fcf 100644 --- a/rust/tests/browser_tests.rs +++ b/rust/tests/browser_tests.rs @@ -151,3 +151,17 @@ fn browser_path_test(#[case] os: String, #[case] browser: String, #[case] browse assert!(!stdout.contains("WARN")); } } + +#[test] +fn invalid_browser_path_test() { + let mut cmd = get_selenium_manager(); + cmd.args([ + "--browser", + "chrome", + "--browser-path", + "/bad/path/google-chrome-wrong", + ]) + .assert() + .code(DATAERR) + .failure(); +}