From a056044d9c20c174e5c04804eb30a446132be60a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Boni=20Garc=C3=ADa?= Date: Wed, 28 Aug 2024 11:16:32 +0200 Subject: [PATCH] [rust] Include arguments for skipping drivers and browsers in path (#14444) * [rust] Include arguments for skipping drivers and browsers in path * [rust] Update skip logic with the current codebase --------- Co-authored-by: Diego Molina --- rust/src/config.rs | 4 +++ rust/src/lib.rs | 82 +++++++++++++++++++++++++++++++++------------- rust/src/main.rs | 10 ++++++ 3 files changed, 74 insertions(+), 22 deletions(-) diff --git a/rust/src/config.rs b/rust/src/config.rs index a3a2699bee6f1..1b940fcbfa727 100644 --- a/rust/src/config.rs +++ b/rust/src/config.rs @@ -58,6 +58,8 @@ pub struct ManagerConfig { pub language_binding: String, pub selenium_version: String, pub avoid_stats: bool, + pub skip_driver_in_path: bool, + pub skip_browser_in_path: bool, } impl ManagerConfig { @@ -117,6 +119,8 @@ impl ManagerConfig { language_binding: StringKey(vec!["language-binding"], "").get_value(), selenium_version: StringKey(vec!["selenium-version"], "").get_value(), avoid_stats: BooleanKey("avoid-stats", false).get_value(), + skip_driver_in_path: BooleanKey("skip-driver-in-path", false).get_value(), + skip_browser_in_path: BooleanKey("skip-browser-in-path", false).get_value(), } } } diff --git a/rust/src/lib.rs b/rust/src/lib.rs index d7a0fc0878951..ab5c01b10a21e 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs @@ -386,7 +386,16 @@ pub trait SeleniumManager { // Check browser in PATH let browser_in_path = self.find_browser_in_path(); if let Some(path) = &browser_in_path { - self.set_browser_path(path_to_string(path)); + if self.is_skip_browser_in_path() { + self.get_logger().debug(format!( + "Skipping {} in path: {}", + self.get_browser_name(), + path.display() + )); + return None; + } else { + self.set_browser_path(path_to_string(path)); + } } browser_in_path } @@ -756,32 +765,41 @@ pub trait SeleniumManager { // Use driver in PATH when the user has not specified any browser version if use_driver_in_path { - let version = driver_in_path_version.unwrap(); let path = driver_in_path.unwrap(); - let major_version = self.get_major_version(&version)?; - // Display warning if the discovered driver version is not the same as the driver in PATH - if !self.get_driver_version().is_empty() - && (self.is_firefox() && !version.eq(self.get_driver_version())) - || (!self.is_firefox() && !major_version.eq(&self.get_major_browser_version())) - { - self.get_logger().warn(format!( - "The {} version ({}) detected in PATH at {} might not be compatible with \ - the detected {} version ({}); currently, {} {} is recommended for {} {}.*, \ - so it is advised to delete the driver in PATH and retry", - self.get_driver_name(), - &version, - path, - self.get_browser_name(), - self.get_browser_version(), + if self.is_skip_driver_in_path() { + self.get_logger().debug(format!( + "Skipping {} in path: {}", self.get_driver_name(), - self.get_driver_version(), - self.get_browser_name(), - self.get_major_browser_version() + path )); + } else { + let version = driver_in_path_version.unwrap(); + let major_version = self.get_major_version(&version)?; + + // Display warning if the discovered driver version is not the same as the driver in PATH + if !self.get_driver_version().is_empty() + && (self.is_firefox() && !version.eq(self.get_driver_version())) + || (!self.is_firefox() && !major_version.eq(&self.get_major_browser_version())) + { + self.get_logger().warn(format!( + "The {} version ({}) detected in PATH at {} might not be compatible with \ + the detected {} version ({}); currently, {} {} is recommended for {} {}.*, \ + so it is advised to delete the driver in PATH and retry", + self.get_driver_name(), + &version, + path, + self.get_browser_name(), + self.get_browser_version(), + self.get_driver_name(), + self.get_driver_version(), + self.get_browser_name(), + self.get_major_browser_version() + )); + } + self.set_driver_version(version.to_string()); + return Ok(PathBuf::from(path)); } - self.set_driver_version(version.to_string()); - return Ok(PathBuf::from(path)); } // If driver was not in the PATH, try to find it in the cache @@ -1413,6 +1431,26 @@ pub trait SeleniumManager { } } + fn is_skip_driver_in_path(&self) -> bool { + self.get_config().skip_driver_in_path + } + + fn set_skip_driver_in_path(&mut self, skip_driver_in_path: bool) { + if skip_driver_in_path { + self.get_config_mut().skip_driver_in_path = true; + } + } + + fn is_skip_browser_in_path(&self) -> bool { + self.get_config().skip_browser_in_path + } + + fn set_skip_browser_in_path(&mut self, skip_browser_in_path: bool) { + if skip_browser_in_path { + self.get_config_mut().skip_browser_in_path = true; + } + } + fn get_cache_path(&self) -> Result, Error> { let path = Path::new(&self.get_config().cache_path); match create_path_if_not_exists(path) { diff --git a/rust/src/main.rs b/rust/src/main.rs index 5b42caa1e77df..3e53a7a8da012 100644 --- a/rust/src/main.rs +++ b/rust/src/main.rs @@ -147,6 +147,14 @@ struct Cli { /// Avoid sends usage statistics to plausible.io #[clap(long)] avoid_stats: bool, + + /// Not using drivers found in the PATH + #[clap(long)] + skip_driver_in_path: bool, + + /// Not using browsers found in the PATH + #[clap(long)] + skip_browser_in_path: bool, } fn main() { @@ -218,6 +226,8 @@ fn main() { let selenium_version = sm_version.strip_prefix(SM_BETA_LABEL).unwrap_or(sm_version); selenium_manager.set_selenium_version(selenium_version.to_string()); selenium_manager.set_avoid_stats(cli.avoid_stats); + selenium_manager.set_skip_driver_in_path(cli.skip_driver_in_path); + selenium_manager.set_skip_browser_in_path(cli.skip_browser_in_path); if cli.clear_cache || BooleanKey("clear-cache", false).get_value() { clear_cache(selenium_manager.get_logger(), &cache_path);