Skip to content

Commit 2c45d7c

Browse files
committed
[rust] Improve logic to locate wmic.exe in Windows
1 parent d743a2c commit 2c45d7c

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

.bazelrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ test --test_env=JRUBY_OPTS="--dev"
9393
test:windows --test_env=LOCALAPPDATA
9494
test:windows --test_env=PROGRAMFILES="C:\\Program Files"
9595
test:windows --test_env=PROGRAMFILES(X86)="C:\\Program Files (x86)"
96+
test:windows --test_env=SYSTEMROOT="C:\\Windows"
9697

9798
test --test_timeout=1800
9899

rust/src/config.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use crate::config::OS::{LINUX, MACOS, WINDOWS};
1919
use crate::shell::run_shell_command_by_os;
2020
use crate::{
21-
default_cache_folder, format_one_arg, path_to_string, Command, REQUEST_TIMEOUT_SEC,
21+
default_cache_folder, format_one_arg, get_wmic, path_to_string, Command, REQUEST_TIMEOUT_SEC,
2222
UNAME_COMMAND,
2323
};
2424
use crate::{ARCH_AMD64, ARCH_ARM64, ARCH_X86, TTL_SEC, WMIC_COMMAND_OS};
@@ -66,7 +66,7 @@ impl ManagerConfig {
6666

6767
let self_os = OS;
6868
let self_arch = if WINDOWS.is(self_os) {
69-
let wmic_command = Command::new_single(WMIC_COMMAND_OS.to_string());
69+
let wmic_command = Command::new_single(format_one_arg(WMIC_COMMAND_OS, &get_wmic()));
7070
let wmic_output = run_shell_command_by_os(self_os, wmic_command).unwrap_or_default();
7171
if wmic_output.contains("32") {
7272
ARCH_X86.to_string()

rust/src/lib.rs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,10 @@ pub const DEV: &str = "dev";
7474
pub const CANARY: &str = "canary";
7575
pub const NIGHTLY: &str = "nightly";
7676
pub const ESR: &str = "esr";
77-
pub const WMIC_COMMAND: &str = r#"wmic datafile where name='{}' get Version /value"#;
78-
pub const WMIC_COMMAND_OS: &str = r#"wmic os get osarchitecture"#;
77+
pub const WMIC: &str = "wmic";
78+
pub const WMIC_DEFAULT_PATH: &str = r#"{}\System32\Wbem\WMIC.exe"#;
79+
pub const WMIC_COMMAND: &str = r#"{} datafile where name='{}' get Version /value"#;
80+
pub const WMIC_COMMAND_OS: &str = r#"{} os get osarchitecture"#;
7981
pub const REG_VERSION_ARG: &str = "version";
8082
pub const REG_CURRENT_VERSION_ARG: &str = "CurrentVersion";
8183
pub const REG_PV_ARG: &str = "pv";
@@ -93,6 +95,7 @@ pub const SINGLE_QUOTE: &str = "'";
9395
pub const ENV_PROGRAM_FILES: &str = "PROGRAMFILES";
9496
pub const ENV_PROGRAM_FILES_X86: &str = "PROGRAMFILES(X86)";
9597
pub const ENV_LOCALAPPDATA: &str = "LOCALAPPDATA";
98+
pub const ENV_SYSTEMROOT: &str = "SYSTEMROOT";
9699
pub const ENV_X86: &str = " (x86)";
97100
pub const ARCH_X86: &str = "x86";
98101
pub const ARCH_AMD64: &str = "amd64";
@@ -1073,8 +1076,11 @@ pub trait SeleniumManager {
10731076

10741077
if WINDOWS.is(self.get_os()) {
10751078
if !escaped_browser_path.is_empty() {
1076-
let wmic_command =
1077-
Command::new_single(format_one_arg(WMIC_COMMAND, &escaped_browser_path));
1079+
let wmic_command = Command::new_single(format_two_args(
1080+
WMIC_COMMAND,
1081+
&get_wmic(),
1082+
&escaped_browser_path,
1083+
));
10781084
commands.push(wmic_command);
10791085
}
10801086
if !self.is_browser_version_unstable() {
@@ -1549,6 +1555,19 @@ pub fn format_three_args(string: &str, arg1: &str, arg2: &str, arg3: &str) -> St
15491555
.replacen("{}", arg3, 1)
15501556
}
15511557

1558+
pub fn get_wmic() -> String {
1559+
let system_root = env::var(ENV_SYSTEMROOT).unwrap_or_default();
1560+
let wmic_default_path = format_one_arg(WMIC_DEFAULT_PATH, &system_root);
1561+
let wmic_path = Path::new(&wmic_default_path);
1562+
if !wmic_path.exists() {
1563+
return match which(WMIC) {
1564+
Ok(path) => path_to_string(&path),
1565+
Err(_) => WMIC.to_string(),
1566+
};
1567+
}
1568+
path_to_string(wmic_path)
1569+
}
1570+
15521571
// ----------------------------------------------------------
15531572
// Private functions
15541573
// ----------------------------------------------------------

0 commit comments

Comments
 (0)