Skip to content

Commit

Permalink
[rust] Include arguments for skipping drivers and browsers in path (#…
Browse files Browse the repository at this point in the history
…14444)

* [rust] Include arguments for skipping drivers and browsers in path

* [rust] Update skip logic with the current codebase

---------

Co-authored-by: Diego Molina <diemol@users.noreply.github.com>
  • Loading branch information
bonigarcia and diemol authored Aug 28, 2024
1 parent cdc57b2 commit a056044
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 22 deletions.
4 changes: 4 additions & 0 deletions rust/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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(),
}
}
}
Expand Down
82 changes: 60 additions & 22 deletions rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<Option<PathBuf>, Error> {
let path = Path::new(&self.get_config().cache_path);
match create_path_if_not_exists(path) {
Expand Down
10 changes: 10 additions & 0 deletions rust/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit a056044

Please sign in to comment.