Skip to content

Commit

Permalink
[rust] Refactor logic for running shell commands in Selenium Manager
Browse files Browse the repository at this point in the history
  • Loading branch information
bonigarcia committed Feb 2, 2023
1 parent 23782b3 commit 764aff6
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 54 deletions.
11 changes: 4 additions & 7 deletions rust/src/chrome.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,10 @@ impl SeleniumManager for ChromeManager {
} else {
commands = vec![self.format_one_arg(WMIC_COMMAND, browser_path)];
}
let (shell, flag) = self.get_shell_command();
let args = if WINDOWS.is(self.get_os()) {
commands
} else {
vec![self.format_one_arg(DASH_DASH_VERSION, browser_path)]
};
self.detect_browser_version(shell, flag, args)
if !WINDOWS.is(self.get_os()) {
commands = vec![self.format_one_arg(DASH_DASH_VERSION, browser_path)]
}
self.detect_browser_version(commands)
}

fn get_driver_name(&self) -> &str {
Expand Down
11 changes: 4 additions & 7 deletions rust/src/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,10 @@ impl SeleniumManager for EdgeManager {
} else {
commands = vec![self.format_one_arg(WMIC_COMMAND, browser_path)];
}
let (shell, flag) = self.get_shell_command();
let args = if WINDOWS.is(self.get_os()) {
commands
} else {
vec![self.format_one_arg(DASH_DASH_VERSION, browser_path)]
};
self.detect_browser_version(shell, flag, args)
if !WINDOWS.is(self.get_os()) {
commands = vec![self.format_one_arg(DASH_DASH_VERSION, browser_path)]
}
self.detect_browser_version(commands)
}

fn get_driver_name(&self) -> &str {
Expand Down
13 changes: 5 additions & 8 deletions rust/src/firefox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ impl SeleniumManager for FirefoxManager {
}

fn discover_browser_version(&self) -> Option<String> {
let commands;
let mut commands;
let mut browser_path = self.get_browser_path();
if browser_path.is_empty() {
match self.detect_browser_path() {
Expand All @@ -129,13 +129,10 @@ impl SeleniumManager for FirefoxManager {
} else {
commands = vec![self.format_one_arg(WMIC_COMMAND, browser_path)];
}
let (shell, flag) = self.get_shell_command();
let args = if WINDOWS.is(self.get_os()) {
commands
} else {
vec![self.format_one_arg(DASH_VERSION, browser_path)]
};
self.detect_browser_version(shell, flag, args)
if !WINDOWS.is(self.get_os()) {
commands = vec![self.format_one_arg(DASH_VERSION, browser_path)]
}
self.detect_browser_version(commands)
}

fn get_driver_name(&self) -> &str {
Expand Down
57 changes: 25 additions & 32 deletions rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ pub trait SeleniumManager {
.cloned()
}

fn detect_browser_version(&self, shell: &str, flag: &str, args: Vec<String>) -> Option<String> {
fn detect_browser_version(&self, commands: Vec<String>) -> Option<String> {
let mut metadata = get_metadata(self.get_logger());
let browser_name = &self.get_browser_name();

Expand All @@ -141,8 +141,8 @@ pub trait SeleniumManager {
browser_name
));
let mut browser_version = "".to_string();
for arg in args.iter() {
let output = match self.run_shell_command(shell, flag, arg.to_string()) {
for command in commands.iter() {
let output = match self.run_shell_command_with_log(command.to_string()) {
Ok(out) => out,
Err(_e) => continue,
};
Expand Down Expand Up @@ -221,10 +221,7 @@ pub trait SeleniumManager {
}

fn find_driver_in_path(&self) -> (Option<String>, Option<String>) {
let (shell, flag) = self.get_shell_command();
match self.run_shell_command(
shell,
flag,
match self.run_shell_command_with_log(
self.format_one_arg(DASH_DASH_VERSION, self.get_driver_name()),
) {
Ok(output) => {
Expand All @@ -235,9 +232,7 @@ pub trait SeleniumManager {
} else {
WHICH_COMMAND
};
let driver_path = match self.run_shell_command(
shell,
flag,
let driver_path = match self.run_shell_command_with_log(
self.format_one_arg(which_command, self.get_driver_name()),
) {
Ok(path) => Some(path),
Expand All @@ -251,14 +246,6 @@ pub trait SeleniumManager {
}
}

fn get_shell_command(&self) -> (&str, &str) {
if WINDOWS.is(self.get_os()) {
("cmd", "/C")
} else {
("sh", "-c")
}
}

fn is_browser_version_unstable(&self) -> bool {
let browser_version = self.get_browser_version();
browser_version.eq_ignore_ascii_case(BETA)
Expand Down Expand Up @@ -307,21 +294,12 @@ pub trait SeleniumManager {
Ok(driver_path)
}

fn run_shell_command(
&self,
command: &str,
flag: &str,
args: String,
) -> Result<String, Box<dyn Error>> {
fn run_shell_command_with_log(&self, command: String) -> Result<String, Box<dyn Error>> {
self.get_logger()
.debug(format!("Running {} command: {:?}", command, args));
let output = Command::new(command).args([flag, args.as_str()]).output()?;
self.get_logger().debug(format!("{:?}", output));

Ok(
strip_trailing_newline(String::from_utf8_lossy(&output.stdout).to_string().as_str())
.to_string(),
)
.debug(format!("Running command: {:?}", command));
let output = run_shell_command(self.get_os(), command)?;
self.get_logger().debug(format!("Output: {:?}", output));
Ok(output)
}

fn get_major_version(&self, full_version: &str) -> Result<String, Box<dyn Error>> {
Expand Down Expand Up @@ -527,3 +505,18 @@ fn strip_trailing_newline(input: &str) -> &str {
.or_else(|| input.strip_suffix('\n'))
.unwrap_or(input)
}

pub fn run_shell_command(os: &str, command: String) -> Result<String, Box<dyn Error>> {
let (shell, flag) = if WINDOWS.is(os) {
("cmd", "/C")
} else {
("sh", "-c")
};
let output = Command::new(shell)
.args([flag, command.as_str()])
.output()?;
Ok(
strip_trailing_newline(String::from_utf8_lossy(&output.stdout).to_string().as_str())
.to_string(),
)
}

0 comments on commit 764aff6

Please sign in to comment.