| 1 | use std::collections::HashMap; |
| 2 | use std::path::{Path, PathBuf}; |
| 3 | use std::process::Command; |
| 4 | |
| 5 | use crate::error::BrowserError; |
| 6 | |
| 7 | pub const PROFILE_NAME: &str = "rusty-teams-chrome"; |
| 8 | pub const DEFAULT_PORT: u16 = 9222; |
| 9 | const CHROME_RELATIVE: &str = r"Google\Chrome\Application\chrome.exe"; |
| 10 | const WINDOWS_VARIABLES: [&str; 4] = ["LOCALAPPDATA", "PROGRAMFILES", "PROGRAMFILES(X86)", "TEMP"]; |
| 11 | const LINUX_CHROME_NAMES: [&str; 5] = |
| 12 | ["google-chrome", "google-chrome-stable", "chromium", "chromium-browser", "chrome"]; |
| 13 | const MACOS_CHROME_NAMES: [&str; 3] = ["google-chrome", "chromium", "chrome"]; |
| 14 | const MACOS_CHROME_APP_PATHS: [&str; 2] = [ |
| 15 | "Google Chrome.app/Contents/MacOS/Google Chrome", |
| 16 | "Chromium.app/Contents/MacOS/Chromium", |
| 17 | ]; |
| 18 | |
| 19 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 20 | pub enum Platform { |
| 21 | Wsl, |
| 22 | Windows, |
| 23 | Linux, |
| 24 | Macos, |
| 25 | } |
| 26 | |
| 27 | impl Platform { |
| 28 | pub fn detect() -> Platform { |
| 29 | if cfg!(windows) { |
| 30 | return Platform::Windows; |
| 31 | } |
| 32 | if cfg!(target_os = "macos") { |
| 33 | return Platform::Macos; |
| 34 | } |
| 35 | let proc_version = std::fs::read_to_string("/proc/version").ok(); |
| 36 | Platform::from_proc_version(proc_version.as_deref()) |
| 37 | } |
| 38 | |
| 39 | pub fn from_proc_version(content: Option<&str>) -> Platform { |
| 40 | match content { |
| 41 | Some(text) if text.to_ascii_lowercase().contains("microsoft") => Platform::Wsl, |
| 42 | _ => Platform::Linux, |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | pub fn uses_unix_paths(self) -> bool { |
| 47 | matches!(self, Platform::Linux | Platform::Macos) |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | #[derive(Debug, Clone)] |
| 52 | pub(crate) enum ChromeSearch { |
| 53 | Files(Vec<PathBuf>), |
| 54 | Named(Vec<String>), |
| 55 | /// Absolute install paths first, then PATH names (macOS Application bundles + CLI). |
| 56 | FilesThenNamed { files: Vec<PathBuf>, names: Vec<String> }, |
| 57 | } |
| 58 | |
| 59 | #[derive(Debug, Clone)] |
| 60 | pub struct Config { |
| 61 | pub platform: Platform, |
| 62 | pub port: u16, |
| 63 | pub profile: String, |
| 64 | pub mode_file: PathBuf, |
| 65 | profile_parent: String, |
| 66 | mode_parent: PathBuf, |
| 67 | pub(crate) chrome_search: ChromeSearch, |
| 68 | } |
| 69 | |
| 70 | pub struct ConfigInputs<'a> { |
| 71 | pub windows_variables: HashMap<String, String>, |
| 72 | pub linux_data_dir: Option<PathBuf>, |
| 73 | pub home_dir: Option<PathBuf>, |
| 74 | pub to_local: &'a dyn Fn(&str) -> String, |
| 75 | } |
| 76 | |
| 77 | impl Config { |
| 78 | pub fn detect() -> Result<Config, BrowserError> { |
| 79 | let platform = Platform::detect(); |
| 80 | let windows_variables = match platform { |
| 81 | Platform::Linux | Platform::Macos => HashMap::new(), |
| 82 | Platform::Windows => WINDOWS_VARIABLES |
| 83 | .iter() |
| 84 | .filter_map(|name| std::env::var(name).ok().map(|value| ((*name).to_owned(), value))) |
| 85 | .collect(), |
| 86 | Platform::Wsl => wsl_windows_variables()?, |
| 87 | }; |
| 88 | let base = directories::BaseDirs::new(); |
| 89 | let linux_data_dir = base.as_ref().map(|dirs| dirs.data_dir().to_path_buf()); |
| 90 | let home_dir = base.as_ref().map(|dirs| dirs.home_dir().to_path_buf()); |
| 91 | let to_local = |windows_path: &str| match platform { |
| 92 | Platform::Wsl => wslpath(windows_path).unwrap_or_else(|| windows_path_to_wsl(windows_path)), |
| 93 | _ => windows_path.to_owned(), |
| 94 | }; |
| 95 | Config::resolve( |
| 96 | platform, |
| 97 | DEFAULT_PORT, |
| 98 | &ConfigInputs { windows_variables, linux_data_dir, home_dir, to_local: &to_local }, |
| 99 | ) |
| 100 | } |
| 101 | |
| 102 | pub fn resolve(platform: Platform, port: u16, inputs: &ConfigInputs) -> Result<Config, BrowserError> { |
| 103 | let to_local = inputs.to_local; |
| 104 | let missing = |name: &str| BrowserError::Environment(format!("%{name}% is not set")); |
| 105 | let config = match platform { |
| 106 | Platform::Linux => { |
| 107 | let data_dir = inputs |
| 108 | .linux_data_dir |
| 109 | .clone() |
| 110 | .ok_or_else(|| BrowserError::Environment("no XDG data directory".into()))?; |
| 111 | Config { |
| 112 | platform, |
| 113 | port, |
| 114 | // Built with the platform's '/' like with_profile_name, not the |
| 115 | // host's separator: the string is a Linux path whoever builds it. |
| 116 | profile: format!("{}/{}", path_text(&data_dir), PROFILE_NAME), |
| 117 | mode_file: data_dir.join(format!("{PROFILE_NAME}.mode")), |
| 118 | profile_parent: path_text(&data_dir), |
| 119 | mode_parent: data_dir, |
| 120 | chrome_search: ChromeSearch::Named( |
| 121 | LINUX_CHROME_NAMES.iter().map(|name| (*name).to_owned()).collect(), |
| 122 | ), |
| 123 | } |
| 124 | } |
| 125 | Platform::Macos => { |
| 126 | let data_dir = inputs.linux_data_dir.clone().ok_or_else(|| { |
| 127 | BrowserError::Environment("no Application Support directory".into()) |
| 128 | })?; |
| 129 | let mut files = Vec::new(); |
| 130 | for root in [PathBuf::from("/Applications")] |
| 131 | .into_iter() |
| 132 | .chain(inputs.home_dir.iter().map(|home| home.join("Applications"))) |
| 133 | { |
| 134 | for relative in MACOS_CHROME_APP_PATHS { |
| 135 | files.push(root.join(relative)); |
| 136 | } |
| 137 | } |
| 138 | Config { |
| 139 | platform, |
| 140 | port, |
| 141 | profile: format!("{}/{}", path_text(&data_dir), PROFILE_NAME), |
| 142 | mode_file: data_dir.join(format!("{PROFILE_NAME}.mode")), |
| 143 | profile_parent: path_text(&data_dir), |
| 144 | mode_parent: data_dir, |
| 145 | chrome_search: ChromeSearch::FilesThenNamed { |
| 146 | files, |
| 147 | names: MACOS_CHROME_NAMES.iter().map(|name| (*name).to_owned()).collect(), |
| 148 | }, |
| 149 | } |
| 150 | } |
| 151 | Platform::Windows | Platform::Wsl => { |
| 152 | let local = inputs.windows_variables.get("LOCALAPPDATA").ok_or_else(|| missing("LOCALAPPDATA"))?; |
| 153 | let candidates = ["PROGRAMFILES", "PROGRAMFILES(X86)", "LOCALAPPDATA"] |
| 154 | .iter() |
| 155 | .filter_map(|name| inputs.windows_variables.get(*name)) |
| 156 | .map(|root| PathBuf::from(to_local(&format!("{root}\\{CHROME_RELATIVE}")))) |
| 157 | .collect(); |
| 158 | Config { |
| 159 | platform, |
| 160 | port, |
| 161 | profile: format!("{local}\\{PROFILE_NAME}"), |
| 162 | mode_file: PathBuf::from(to_local(&format!("{local}\\{PROFILE_NAME}.mode"))), |
| 163 | profile_parent: local.clone(), |
| 164 | mode_parent: PathBuf::from(to_local(local)), |
| 165 | chrome_search: ChromeSearch::Files(candidates), |
| 166 | } |
| 167 | } |
| 168 | }; |
| 169 | Ok(config) |
| 170 | } |
| 171 | |
| 172 | pub fn with_port(mut self, port: u16) -> Config { |
| 173 | self.port = port; |
| 174 | self |
| 175 | } |
| 176 | |
| 177 | pub fn with_profile_name(mut self, name: &str) -> Config { |
| 178 | let separator = if self.platform.uses_unix_paths() { "/" } else { "\\" }; |
| 179 | self.profile = format!("{}{separator}{name}", self.profile_parent); |
| 180 | self.mode_file = self.mode_parent.join(format!("{name}.mode")); |
| 181 | self |
| 182 | } |
| 183 | |
| 184 | pub fn chrome_executable(&self) -> Result<PathBuf, BrowserError> { |
| 185 | let path_variable = std::env::var_os("PATH").unwrap_or_default(); |
| 186 | find_chrome(&self.chrome_search, &path_variable, &|path| path.is_file()) |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | fn path_text(path: &Path) -> String { |
| 191 | path.to_string_lossy().into_owned() |
| 192 | } |
| 193 | |
| 194 | fn find_named_chrome( |
| 195 | names: &[String], |
| 196 | path_variable: &std::ffi::OsStr, |
| 197 | exists: &dyn Fn(&Path) -> bool, |
| 198 | ) -> Option<PathBuf> { |
| 199 | std::env::split_paths(path_variable) |
| 200 | .flat_map(|directory| names.iter().map(move |name| directory.join(name))) |
| 201 | .find(|candidate| exists(candidate)) |
| 202 | } |
| 203 | |
| 204 | pub(crate) fn find_chrome( |
| 205 | search: &ChromeSearch, |
| 206 | path_variable: &std::ffi::OsStr, |
| 207 | exists: &dyn Fn(&Path) -> bool, |
| 208 | ) -> Result<PathBuf, BrowserError> { |
| 209 | match search { |
| 210 | ChromeSearch::Files(candidates) => candidates |
| 211 | .iter() |
| 212 | .find(|candidate| exists(candidate)) |
| 213 | .cloned() |
| 214 | .ok_or_else(|| BrowserError::ChromeNotFound("not under Program Files or LocalAppData".into())), |
| 215 | ChromeSearch::Named(names) => find_named_chrome(names, path_variable, exists) |
| 216 | .ok_or_else(|| BrowserError::ChromeNotFound(format!("none of {} on PATH", names.join(", ")))), |
| 217 | ChromeSearch::FilesThenNamed { files, names } => files |
| 218 | .iter() |
| 219 | .find(|candidate| exists(candidate)) |
| 220 | .cloned() |
| 221 | .or_else(|| find_named_chrome(names, path_variable, exists)) |
| 222 | .ok_or_else(|| { |
| 223 | BrowserError::ChromeNotFound( |
| 224 | "not under /Applications, ~/Applications, or PATH".into(), |
| 225 | ) |
| 226 | }), |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | pub fn windows_path_to_wsl(windows_path: &str) -> String { |
| 231 | match windows_path.split_once(':') { |
| 232 | Some((drive, rest)) if drive.len() == 1 => { |
| 233 | format!("/mnt/{}{}", drive.to_ascii_lowercase(), rest.replace('\\', "/")) |
| 234 | } |
| 235 | _ => windows_path.replace('\\', "/"), |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | fn wslpath(windows_path: &str) -> Option<String> { |
| 240 | let output = Command::new("wslpath").arg("-u").arg(windows_path).output().ok()?; |
| 241 | if !output.status.success() { |
| 242 | return None; |
| 243 | } |
| 244 | let text = String::from_utf8(output.stdout).ok()?; |
| 245 | let trimmed = text.trim(); |
| 246 | (!trimmed.is_empty()).then(|| trimmed.to_owned()) |
| 247 | } |
| 248 | |
| 249 | pub(crate) fn parse_windows_variables(output: &str) -> Option<HashMap<String, String>> { |
| 250 | let line = output.lines().map(str::trim).rfind(|line| !line.is_empty())?; |
| 251 | let values: Vec<&str> = line.split(';').collect(); |
| 252 | if values.len() != WINDOWS_VARIABLES.len() { |
| 253 | return None; |
| 254 | } |
| 255 | let map: HashMap<String, String> = WINDOWS_VARIABLES |
| 256 | .iter() |
| 257 | .zip(values) |
| 258 | .filter(|(name, value)| *value != format!("%{name}%")) |
| 259 | .map(|(name, value)| ((*name).to_owned(), value.to_owned())) |
| 260 | .collect(); |
| 261 | map.contains_key("LOCALAPPDATA").then_some(map) |
| 262 | } |
| 263 | |
| 264 | fn wsl_windows_variables() -> Result<HashMap<String, String>, BrowserError> { |
| 265 | let echo = WINDOWS_VARIABLES.iter().map(|name| format!("%{name}%")).collect::<Vec<_>>().join(";"); |
| 266 | let output = Command::new("/mnt/c/Windows/System32/cmd.exe") |
| 267 | .args(["/c", &format!("echo {echo}")]) |
| 268 | .current_dir("/mnt/c") |
| 269 | .output() |
| 270 | .map_err(|error| BrowserError::Environment(format!("cmd.exe: {error}")))?; |
| 271 | parse_windows_variables(&String::from_utf8_lossy(&output.stdout)) |
| 272 | .ok_or_else(|| BrowserError::Environment("cmd.exe returned no %LOCALAPPDATA%".into())) |
| 273 | } |
| 274 | |
| 275 | #[cfg(test)] |
| 276 | mod tests { |
| 277 | use super::*; |
| 278 | use std::ffi::OsString; |
| 279 | |
| 280 | fn windows_inputs(to_local: &dyn Fn(&str) -> String) -> ConfigInputs<'_> { |
| 281 | let variables = [ |
| 282 | ("LOCALAPPDATA", r"C:\Users\me\AppData\Local"), |
| 283 | ("PROGRAMFILES", r"C:\Program Files"), |
| 284 | ("PROGRAMFILES(X86)", r"C:\Program Files (x86)"), |
| 285 | ]; |
| 286 | ConfigInputs { |
| 287 | windows_variables: variables.iter().map(|(k, v)| ((*k).into(), (*v).into())).collect(), |
| 288 | linux_data_dir: None, |
| 289 | home_dir: None, |
| 290 | to_local, |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | #[test] |
| 295 | fn detects_platform_from_proc_version() { |
| 296 | let wsl = "Linux version 6.18.33.2-microsoft-standard-WSL2 (root@x)"; |
| 297 | assert_eq!(Platform::from_proc_version(Some(wsl)), Platform::Wsl); |
| 298 | assert_eq!(Platform::from_proc_version(Some("Linux version 6.8.0-generic")), Platform::Linux); |
| 299 | assert_eq!(Platform::from_proc_version(None), Platform::Linux); |
| 300 | } |
| 301 | |
| 302 | #[test] |
| 303 | fn converts_windows_paths() { |
| 304 | assert_eq!(windows_path_to_wsl(r"C:\Users\me\x.mode"), "/mnt/c/Users/me/x.mode"); |
| 305 | assert_eq!(windows_path_to_wsl(r"D:\a"), "/mnt/d/a"); |
| 306 | } |
| 307 | |
| 308 | #[test] |
| 309 | fn resolves_wsl_config() { |
| 310 | let to_local = |path: &str| windows_path_to_wsl(path); |
| 311 | let config = Config::resolve(Platform::Wsl, 9222, &windows_inputs(&to_local)).unwrap(); |
| 312 | assert_eq!(config.profile, r"C:\Users\me\AppData\Local\rusty-teams-chrome"); |
| 313 | assert_eq!( |
| 314 | config.mode_file, |
| 315 | PathBuf::from("/mnt/c/Users/me/AppData/Local/rusty-teams-chrome.mode") |
| 316 | ); |
| 317 | let ChromeSearch::Files(files) = &config.chrome_search else { panic!("expected files") }; |
| 318 | assert_eq!(files[0], PathBuf::from("/mnt/c/Program Files/Google/Chrome/Application/chrome.exe")); |
| 319 | assert_eq!(files.len(), 3); |
| 320 | } |
| 321 | |
| 322 | #[test] |
| 323 | fn resolves_windows_config_without_conversion() { |
| 324 | let to_local = |path: &str| path.to_owned(); |
| 325 | let config = Config::resolve(Platform::Windows, 9222, &windows_inputs(&to_local)).unwrap(); |
| 326 | assert_eq!(config.mode_file, PathBuf::from(r"C:\Users\me\AppData\Local\rusty-teams-chrome.mode")); |
| 327 | } |
| 328 | |
| 329 | #[test] |
| 330 | fn native_windows_uses_the_app_profile_and_chrome_install_dirs() { |
| 331 | let to_local = |path: &str| path.to_owned(); |
| 332 | let config = Config::resolve(Platform::Windows, 9222, &windows_inputs(&to_local)).unwrap(); |
| 333 | assert_eq!(config.profile, r"C:\Users\me\AppData\Local\rusty-teams-chrome"); |
| 334 | let ChromeSearch::Files(files) = &config.chrome_search else { panic!("expected files") }; |
| 335 | let expected = [ |
| 336 | r"C:\Program Files\Google\Chrome\Application\chrome.exe", |
| 337 | r"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe", |
| 338 | r"C:\Users\me\AppData\Local\Google\Chrome\Application\chrome.exe", |
| 339 | ]; |
| 340 | assert_eq!(files.iter().map(|file| file.to_string_lossy().into_owned()).collect::<Vec<_>>(), expected); |
| 341 | } |
| 342 | |
| 343 | #[test] |
| 344 | fn native_windows_with_only_localappdata_still_finds_a_per_user_chrome() { |
| 345 | let to_local = |path: &str| path.to_owned(); |
| 346 | let inputs = ConfigInputs { |
| 347 | windows_variables: [("LOCALAPPDATA".to_owned(), r"D:\L".to_owned())].into(), |
| 348 | linux_data_dir: None, |
| 349 | home_dir: None, |
| 350 | to_local: &to_local, |
| 351 | }; |
| 352 | let config = Config::resolve(Platform::Windows, 9222, &inputs).unwrap(); |
| 353 | let ChromeSearch::Files(files) = &config.chrome_search else { panic!("expected files") }; |
| 354 | assert_eq!(files, &[PathBuf::from(r"D:\L\Google\Chrome\Application\chrome.exe")]); |
| 355 | assert_eq!(config.with_profile_name("x").profile, r"D:\L\x"); |
| 356 | } |
| 357 | |
| 358 | #[test] |
| 359 | fn wsl_profile_stays_a_windows_path_while_files_are_mounted() { |
| 360 | let to_local = |path: &str| windows_path_to_wsl(path); |
| 361 | let config = Config::resolve(Platform::Wsl, 9222, &windows_inputs(&to_local)).unwrap(); |
| 362 | assert!(config.profile.starts_with(r"C:\")); |
| 363 | assert!(config.mode_file.starts_with("/mnt/c")); |
| 364 | } |
| 365 | |
| 366 | #[test] |
| 367 | fn resolves_linux_config_under_xdg_data_dir() { |
| 368 | let to_local = |path: &str| path.to_owned(); |
| 369 | let inputs = ConfigInputs { |
| 370 | windows_variables: HashMap::new(), |
| 371 | linux_data_dir: Some(PathBuf::from("/home/u/.local/share")), |
| 372 | home_dir: None, |
| 373 | to_local: &to_local, |
| 374 | }; |
| 375 | let config = Config::resolve(Platform::Linux, 9222, &inputs).unwrap(); |
| 376 | assert_eq!(config.profile, "/home/u/.local/share/rusty-teams-chrome"); |
| 377 | assert_eq!(config.mode_file, PathBuf::from("/home/u/.local/share/rusty-teams-chrome.mode")); |
| 378 | } |
| 379 | |
| 380 | #[test] |
| 381 | fn resolves_macos_config_under_application_support() { |
| 382 | let to_local = |path: &str| path.to_owned(); |
| 383 | let inputs = ConfigInputs { |
| 384 | windows_variables: HashMap::new(), |
| 385 | linux_data_dir: Some(PathBuf::from("/Users/joe/Library/Application Support")), |
| 386 | home_dir: Some(PathBuf::from("/Users/joe")), |
| 387 | to_local: &to_local, |
| 388 | }; |
| 389 | let config = Config::resolve(Platform::Macos, 9222, &inputs).unwrap(); |
| 390 | assert_eq!( |
| 391 | config.profile, |
| 392 | "/Users/joe/Library/Application Support/rusty-teams-chrome" |
| 393 | ); |
| 394 | assert_eq!( |
| 395 | config.mode_file, |
| 396 | PathBuf::from("/Users/joe/Library/Application Support/rusty-teams-chrome.mode") |
| 397 | ); |
| 398 | let ChromeSearch::FilesThenNamed { files, names } = &config.chrome_search else { |
| 399 | panic!("expected files then named") |
| 400 | }; |
| 401 | assert_eq!( |
| 402 | files[0], |
| 403 | PathBuf::from("/Applications/Google Chrome.app/Contents/MacOS/Google Chrome") |
| 404 | ); |
| 405 | assert_eq!( |
| 406 | files[2], |
| 407 | PathBuf::from("/Users/joe/Applications/Google Chrome.app/Contents/MacOS/Google Chrome") |
| 408 | ); |
| 409 | assert!(names.iter().any(|name| name == "google-chrome")); |
| 410 | assert_eq!(config.with_profile_name("x").profile, "/Users/joe/Library/Application Support/x"); |
| 411 | } |
| 412 | |
| 413 | #[test] |
| 414 | fn missing_localappdata_is_an_error() { |
| 415 | let to_local = |path: &str| path.to_owned(); |
| 416 | let inputs = ConfigInputs { |
| 417 | windows_variables: HashMap::new(), |
| 418 | linux_data_dir: None, |
| 419 | home_dir: None, |
| 420 | to_local: &to_local, |
| 421 | }; |
| 422 | assert!(Config::resolve(Platform::Windows, 9222, &inputs).is_err()); |
| 423 | } |
| 424 | |
| 425 | #[test] |
| 426 | fn finds_macos_chrome_under_applications_before_path() { |
| 427 | let search = ChromeSearch::FilesThenNamed { |
| 428 | files: vec![ |
| 429 | "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome".into(), |
| 430 | "/Users/joe/Applications/Chromium.app/Contents/MacOS/Chromium".into(), |
| 431 | ], |
| 432 | names: vec!["google-chrome".into()], |
| 433 | }; |
| 434 | let path = std::env::join_paths(["/usr/local/bin"]).unwrap(); |
| 435 | let found = find_chrome(&search, &path, &|candidate| { |
| 436 | candidate.ends_with("Google Chrome") || candidate.ends_with("google-chrome") |
| 437 | }) |
| 438 | .unwrap(); |
| 439 | assert_eq!( |
| 440 | found, |
| 441 | PathBuf::from("/Applications/Google Chrome.app/Contents/MacOS/Google Chrome") |
| 442 | ); |
| 443 | } |
| 444 | |
| 445 | #[test] |
| 446 | fn overrides_port_and_profile_name() { |
| 447 | let to_local = |path: &str| windows_path_to_wsl(path); |
| 448 | let config = Config::resolve(Platform::Wsl, 9222, &windows_inputs(&to_local)) |
| 449 | .unwrap() |
| 450 | .with_port(9333) |
| 451 | .with_profile_name("rusty-teams-chrome-test"); |
| 452 | assert_eq!(config.port, 9333); |
| 453 | assert_eq!(config.profile, r"C:\Users\me\AppData\Local\rusty-teams-chrome-test"); |
| 454 | assert!(config.mode_file.ends_with("rusty-teams-chrome-test.mode")); |
| 455 | } |
| 456 | |
| 457 | #[test] |
| 458 | fn parses_cmd_output_with_crlf_and_unset_variables() { |
| 459 | let output = "\r\nC:\\L;C:\\PF;%PROGRAMFILES(X86)%;C:\\T\r\n"; |
| 460 | let map = parse_windows_variables(output).unwrap(); |
| 461 | assert_eq!(map["LOCALAPPDATA"], r"C:\L"); |
| 462 | assert_eq!(map["TEMP"], r"C:\T"); |
| 463 | assert!(!map.contains_key("PROGRAMFILES(X86)")); |
| 464 | assert!(parse_windows_variables("%LOCALAPPDATA%;a;b;c").is_none()); |
| 465 | assert!(parse_windows_variables("garbage").is_none()); |
| 466 | } |
| 467 | |
| 468 | #[test] |
| 469 | fn finds_first_existing_candidate() { |
| 470 | let search = ChromeSearch::Files(vec!["/a/chrome.exe".into(), "/b/chrome.exe".into()]); |
| 471 | let found = find_chrome(&search, &OsString::new(), &|path| path.starts_with("/b")).unwrap(); |
| 472 | assert_eq!(found, PathBuf::from("/b/chrome.exe")); |
| 473 | assert!(find_chrome(&search, &OsString::new(), &|_| false).is_err()); |
| 474 | } |
| 475 | |
| 476 | #[test] |
| 477 | fn finds_named_chrome_on_path_in_name_priority_per_directory() { |
| 478 | let search = ChromeSearch::Named(vec!["google-chrome".into(), "chromium".into()]); |
| 479 | // join_paths picks the host's PATH separator so split_paths splits it here too. |
| 480 | let path = std::env::join_paths(["/usr/local/bin", "/usr/bin"]).unwrap(); |
| 481 | let found = find_chrome(&search, &path, &|candidate| { |
| 482 | candidate == Path::new("/usr/bin/chromium") || candidate == Path::new("/usr/bin/google-chrome") |
| 483 | }) |
| 484 | .unwrap(); |
| 485 | assert_eq!(found, PathBuf::from("/usr/bin/google-chrome")); |
| 486 | } |
| 487 | } |