Skip to content

Commit 7de6dec

Browse files
authored
[rust] Selenium Manager processes PATH (#11597)
* [rust] Selenium Manager processes PATH * [rust] Fix CLI test (when driver is not in the cache but in the PATH)
1 parent 0e5bb5b commit 7de6dec

File tree

6 files changed

+97
-25
lines changed

6 files changed

+97
-25
lines changed

rust/src/chrome.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -139,14 +139,11 @@ impl SeleniumManager for ChromeManager {
139139
} else {
140140
commands = vec![self.format_one_arg(WMIC_COMMAND, browser_path)];
141141
}
142-
let (shell, flag, args) = if WINDOWS.is(self.get_os()) {
143-
("cmd", "/C", commands)
142+
let (shell, flag) = self.get_shell_command();
143+
let args = if WINDOWS.is(self.get_os()) {
144+
commands
144145
} else {
145-
(
146-
"sh",
147-
"-c",
148-
vec![self.format_one_arg(DASH_DASH_VERSION, browser_path)],
149-
)
146+
vec![self.format_one_arg(DASH_DASH_VERSION, browser_path)]
150147
};
151148
self.detect_browser_version(shell, flag, args)
152149
}

rust/src/edge.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -139,14 +139,11 @@ impl SeleniumManager for EdgeManager {
139139
} else {
140140
commands = vec![self.format_one_arg(WMIC_COMMAND, browser_path)];
141141
}
142-
let (shell, flag, args) = if WINDOWS.is(self.get_os()) {
143-
("cmd", "/C", commands)
142+
let (shell, flag) = self.get_shell_command();
143+
let args = if WINDOWS.is(self.get_os()) {
144+
commands
144145
} else {
145-
(
146-
"sh",
147-
"-c",
148-
vec![self.format_one_arg(DASH_DASH_VERSION, browser_path)],
149-
)
146+
vec![self.format_one_arg(DASH_DASH_VERSION, browser_path)]
150147
};
151148
self.detect_browser_version(shell, flag, args)
152149
}

rust/src/files.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,14 @@ pub fn parse_version(version_text: String) -> Result<String, Box<dyn Error>> {
183183
if version_text.to_ascii_lowercase().contains("error") {
184184
return Err(PARSE_ERROR.into());
185185
}
186-
let re = Regex::new(r"[^\d^.]").unwrap();
187-
Ok(re.replace_all(&version_text, "").to_string())
186+
let mut parsed_version = "".to_string();
187+
let re_numbers_dots = Regex::new(r"[^\d^.]")?;
188+
let re_versions = Regex::new(r"(?:(\d+)\.)?(?:(\d+)\.)?(?:(\d+)\.\d+)")?;
189+
for token in version_text.split(' ') {
190+
parsed_version = re_numbers_dots.replace_all(token, "").to_string();
191+
if re_versions.is_match(parsed_version.as_str()) {
192+
break;
193+
}
194+
}
195+
Ok(parsed_version)
188196
}

rust/src/firefox.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -129,14 +129,11 @@ impl SeleniumManager for FirefoxManager {
129129
} else {
130130
commands = vec![self.format_one_arg(WMIC_COMMAND, browser_path)];
131131
}
132-
let (shell, flag, args) = if WINDOWS.is(self.get_os()) {
133-
("cmd", "/C", commands)
132+
let (shell, flag) = self.get_shell_command();
133+
let args = if WINDOWS.is(self.get_os()) {
134+
commands
134135
} else {
135-
(
136-
"sh",
137-
"-c",
138-
vec![self.format_one_arg(DASH_VERSION, browser_path)],
139-
)
136+
vec![self.format_one_arg(DASH_VERSION, browser_path)]
140137
};
141138
self.detect_browser_version(shell, flag, args)
142139
}

rust/src/lib.rs

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use crate::firefox::FirefoxManager;
2222
use crate::iexplorer::IExplorerManager;
2323
use std::fs;
2424

25+
use crate::config::OS::WINDOWS;
2526
use crate::config::{str_to_os, ManagerConfig};
2627
use reqwest::{Client, ClientBuilder, Proxy};
2728
use std::collections::HashMap;
@@ -62,6 +63,8 @@ pub const ENV_PROGRAM_FILES: &str = "PROGRAMFILES";
6263
pub const ENV_PROGRAM_FILES_X86: &str = "PROGRAMFILES(X86)";
6364
pub const ENV_LOCALAPPDATA: &str = "LOCALAPPDATA";
6465
pub const FALLBACK_RETRIES: u32 = 5;
66+
pub const WHERE_COMMAND: &str = "where {}";
67+
pub const WHICH_COMMAND: &str = "which {}";
6568

6669
pub trait SeleniumManager {
6770
// ----------------------------------------------------------
@@ -217,6 +220,45 @@ pub trait SeleniumManager {
217220
Ok(driver_version)
218221
}
219222

223+
fn find_driver_in_path(&self) -> (Option<String>, Option<String>) {
224+
let (shell, flag) = self.get_shell_command();
225+
match self.run_shell_command(
226+
shell,
227+
flag,
228+
self.format_one_arg(DASH_DASH_VERSION, self.get_driver_name()),
229+
) {
230+
Ok(output) => {
231+
let parsed_version = parse_version(output).unwrap_or_default();
232+
if !parsed_version.is_empty() {
233+
let which_command = if WINDOWS.is(self.get_os()) {
234+
WHERE_COMMAND
235+
} else {
236+
WHICH_COMMAND
237+
};
238+
let driver_path = match self.run_shell_command(
239+
shell,
240+
flag,
241+
self.format_one_arg(which_command, self.get_driver_name()),
242+
) {
243+
Ok(path) => Some(path),
244+
Err(_) => None,
245+
};
246+
return (Some(parsed_version), driver_path);
247+
}
248+
(None, None)
249+
}
250+
Err(_) => (None, None),
251+
}
252+
}
253+
254+
fn get_shell_command(&self) -> (&str, &str) {
255+
if WINDOWS.is(self.get_os()) {
256+
("cmd", "/C")
257+
} else {
258+
("sh", "-c")
259+
}
260+
}
261+
220262
fn is_browser_version_unstable(&self) -> bool {
221263
let browser_version = self.get_browser_version();
222264
browser_version.eq_ignore_ascii_case(BETA)
@@ -231,6 +273,27 @@ pub trait SeleniumManager {
231273
self.set_driver_version(driver_version);
232274
}
233275

276+
let (in_path_driver_version, in_path_driver_path) = self.find_driver_in_path();
277+
if let (Some(found_driver_version), Some(found_driver_path)) =
278+
(in_path_driver_version, in_path_driver_path)
279+
{
280+
if found_driver_version.eq(self.get_driver_version()) {
281+
self.get_logger().debug(format!(
282+
"Found {} {} in PATH: {}",
283+
self.get_driver_name(),
284+
found_driver_version,
285+
found_driver_path
286+
));
287+
return Ok(PathBuf::from(found_driver_path));
288+
} else {
289+
self.get_logger().warn(format!(
290+
"Incompatible release of {} (version {}) detected in PATH: {}",
291+
self.get_driver_name(),
292+
found_driver_version,
293+
found_driver_path
294+
));
295+
}
296+
}
234297
let driver_path = self.get_driver_path_in_cache();
235298
if driver_path.exists() {
236299
self.get_logger().debug(format!(
@@ -255,7 +318,10 @@ pub trait SeleniumManager {
255318
let output = Command::new(command).args([flag, args.as_str()]).output()?;
256319
self.get_logger().debug(format!("{:?}", output));
257320

258-
Ok(String::from_utf8_lossy(&output.stdout).to_string())
321+
Ok(
322+
strip_trailing_newline(String::from_utf8_lossy(&output.stdout).to_string().as_str())
323+
.to_string(),
324+
)
259325
}
260326

261327
fn get_major_version(&self, full_version: &str) -> Result<String, Box<dyn Error>> {
@@ -454,3 +520,10 @@ pub fn http_client_builder() -> ClientBuilder {
454520
.danger_accept_invalid_certs(true)
455521
.use_rustls_tls()
456522
}
523+
524+
fn strip_trailing_newline(input: &str) -> &str {
525+
input
526+
.strip_suffix("\r\n")
527+
.or_else(|| input.strip_suffix('\n'))
528+
.unwrap_or(input)
529+
}

rust/tests/cli_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ fn ok_test(
5555
println!("{}", output);
5656

5757
assert!(output.contains(&driver_name));
58-
if !browser_version.is_empty() {
58+
if !browser_version.is_empty() && output.contains("cache") {
5959
assert!(output.contains(&driver_version));
6060
}
6161
}

0 commit comments

Comments
 (0)