| 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 FIREFOX_PROFILE_NAME: &str = "rusty-teams-firefox"; |
| 9 | pub const DEFAULT_PORT: u16 = 9222; |
| 10 | const CHROME_RELATIVE: &str = r"Google\Chrome\Application\chrome.exe"; |
| 11 | const WINDOWS_VARIABLES: [&str; 4] = ["LOCALAPPDATA", "PROGRAMFILES", "PROGRAMFILES(X86)", "TEMP"]; |
| 12 | pub(crate) const LINUX_CHROME_NAMES: &[&str] = &[ |
| 13 | "google-chrome", |
| 14 | "google-chrome-stable", |
| 15 | "chromium", |
| 16 | "chromium-browser", |
| 17 | "chrome", |
| 18 | "microsoft-edge", |
| 19 | "microsoft-edge-stable", |
| 20 | "microsoft-edge-beta", |
| 21 | "microsoft-edge-dev", |
| 22 | "brave", |
| 23 | "brave-browser", |
| 24 | "brave-browser-stable", |
| 25 | "brave-browser-beta", |
| 26 | "brave-browser-nightly", |
| 27 | "helium", |
| 28 | "vivaldi", |
| 29 | "vivaldi-stable", |
| 30 | "vivaldi-snapshot", |
| 31 | ]; |
| 32 | const MACOS_CHROME_NAMES: [&str; 3] = ["google-chrome", "chromium", "chrome"]; |
| 33 | /// Chromium-based browsers in order of preference; each one gets its own profile, so the user's |
| 34 | /// everyday browser stays untouched. |
| 35 | pub(crate) const MACOS_CHROME_APP_PATHS: [&str; 6] = [ |
| 36 | "Google Chrome.app/Contents/MacOS/Google Chrome", |
| 37 | "Chromium.app/Contents/MacOS/Chromium", |
| 38 | "Microsoft Edge.app/Contents/MacOS/Microsoft Edge", |
| 39 | "Brave Browser.app/Contents/MacOS/Brave Browser", |
| 40 | "Helium.app/Contents/MacOS/Helium", |
| 41 | "Vivaldi.app/Contents/MacOS/Vivaldi", |
| 42 | ]; |
| 43 | /// Firefox-based browsers, tried only when no Chromium browser is installed. |
| 44 | pub(crate) const MACOS_FIREFOX_APP_PATHS: [&str; 4] = [ |
| 45 | "Firefox.app/Contents/MacOS/firefox", |
| 46 | "Zen.app/Contents/MacOS/zen", |
| 47 | "LibreWolf.app/Contents/MacOS/librewolf", |
| 48 | "Floorp.app/Contents/MacOS/floorp", |
| 49 | ]; |
| 50 | pub(crate) const LINUX_FIREFOX_NAMES: [&str; 5] = |
| 51 | ["firefox", "zen-browser", "zen", "librewolf", "floorp"]; |
| 52 | |
| 53 | /// The protocol family of the sign-in browser: Chromium speaks CDP, Firefox WebDriver BiDi. |
| 54 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 55 | pub enum Engine { |
| 56 | Chromium, |
| 57 | Firefox, |
| 58 | } |
| 59 | |
| 60 | impl Engine { |
| 61 | pub fn of_executable(path: &Path) -> Engine { |
| 62 | if crate::process::is_firefox_image(&path.to_string_lossy()) { |
| 63 | Engine::Firefox |
| 64 | } else { |
| 65 | Engine::Chromium |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 71 | pub enum Platform { |
| 72 | Wsl, |
| 73 | Windows, |
| 74 | Linux, |
| 75 | Macos, |
| 76 | } |
| 77 | |
| 78 | impl Platform { |
| 79 | pub fn detect() -> Platform { |
| 80 | if cfg!(windows) { |
| 81 | return Platform::Windows; |
| 82 | } |
| 83 | if cfg!(target_os = "macos") { |
| 84 | return Platform::Macos; |
| 85 | } |
| 86 | let proc_version = std::fs::read_to_string("/proc/version").ok(); |
| 87 | Platform::from_proc_version(proc_version.as_deref()) |
| 88 | } |
| 89 | |
| 90 | pub fn from_proc_version(content: Option<&str>) -> Platform { |
| 91 | match content { |
| 92 | Some(text) if text.to_ascii_lowercase().contains("microsoft") => Platform::Wsl, |
| 93 | _ => Platform::Linux, |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | pub fn uses_unix_paths(self) -> bool { |
| 98 | matches!(self, Platform::Linux | Platform::Macos) |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | #[derive(Debug, Clone)] |
| 103 | pub(crate) enum BrowserSearch { |
| 104 | Files(Vec<PathBuf>), |
| 105 | Named(Vec<String>), |
| 106 | /// Absolute install paths first, then PATH names (macOS Application bundles + CLI). |
| 107 | FilesThenNamed { |
| 108 | files: Vec<PathBuf>, |
| 109 | names: Vec<String>, |
| 110 | }, |
| 111 | } |
| 112 | |
| 113 | #[derive(Debug, Clone)] |
| 114 | pub struct Config { |
| 115 | pub platform: Platform, |
| 116 | pub engine: Engine, |
| 117 | pub port: u16, |
| 118 | pub profile: String, |
| 119 | pub mode_file: PathBuf, |
| 120 | profile_parent: String, |
| 121 | mode_parent: PathBuf, |
| 122 | pub(crate) search: BrowserSearch, |
| 123 | } |
| 124 | |
| 125 | pub struct ConfigInputs<'a> { |
| 126 | pub windows_variables: HashMap<String, String>, |
| 127 | pub linux_data_dir: Option<PathBuf>, |
| 128 | pub home_dir: Option<PathBuf>, |
| 129 | pub to_local: &'a dyn Fn(&str) -> String, |
| 130 | } |
| 131 | |
| 132 | impl Config { |
| 133 | pub fn detect() -> Result<Config, BrowserError> { |
| 134 | let platform = Platform::detect(); |
| 135 | let windows_variables = match platform { |
| 136 | Platform::Linux | Platform::Macos => HashMap::new(), |
| 137 | Platform::Windows => WINDOWS_VARIABLES |
| 138 | .iter() |
| 139 | .filter_map(|name| { |
| 140 | std::env::var(name) |
| 141 | .ok() |
| 142 | .map(|value| ((*name).to_owned(), value)) |
| 143 | }) |
| 144 | .collect(), |
| 145 | Platform::Wsl => wsl_windows_variables()?, |
| 146 | }; |
| 147 | let base = directories::BaseDirs::new(); |
| 148 | let linux_data_dir = base.as_ref().map(|dirs| dirs.data_dir().to_path_buf()); |
| 149 | let home_dir = base.as_ref().map(|dirs| dirs.home_dir().to_path_buf()); |
| 150 | let to_local = |windows_path: &str| match platform { |
| 151 | Platform::Wsl => { |
| 152 | wslpath(windows_path).unwrap_or_else(|| windows_path_to_wsl(windows_path)) |
| 153 | } |
| 154 | _ => windows_path.to_owned(), |
| 155 | }; |
| 156 | let config = Config::resolve( |
| 157 | platform, |
| 158 | DEFAULT_PORT, |
| 159 | &ConfigInputs { |
| 160 | windows_variables, |
| 161 | linux_data_dir, |
| 162 | home_dir: home_dir.clone(), |
| 163 | to_local: &to_local, |
| 164 | }, |
| 165 | )?; |
| 166 | let path_variable = crate::host::path_variable(); |
| 167 | let chosen = crate::sign_in_choice::chosen_executable(platform, home_dir.as_deref()); |
| 168 | Ok(config.select_browser( |
| 169 | chosen, |
| 170 | home_dir.as_deref(), |
| 171 | &path_variable, |
| 172 | &crate::host::is_file, |
| 173 | )) |
| 174 | } |
| 175 | |
| 176 | /// Uses `chosen` as the sign-in browser, else the first Chromium browser, else the first |
| 177 | /// Firefox-based one on Linux and macOS. A Firefox browser gets its own profile. |
| 178 | pub(crate) fn select_browser( |
| 179 | mut self, |
| 180 | chosen: Option<PathBuf>, |
| 181 | home_dir: Option<&Path>, |
| 182 | path_variable: &std::ffi::OsStr, |
| 183 | exists: &dyn Fn(&Path) -> bool, |
| 184 | ) -> Config { |
| 185 | if let Some(path) = chosen { |
| 186 | self.engine = Engine::of_executable(&path); |
| 187 | self.search = BrowserSearch::Files(vec![path]); |
| 188 | } else if find_browser(&self.search, path_variable, exists).is_err() |
| 189 | && let Some(search) = firefox_search(self.platform, home_dir) |
| 190 | && find_browser(&search, path_variable, exists).is_ok() |
| 191 | { |
| 192 | self.engine = Engine::Firefox; |
| 193 | self.search = search; |
| 194 | } |
| 195 | if self.engine == Engine::Firefox { |
| 196 | self = self.with_profile_name(FIREFOX_PROFILE_NAME); |
| 197 | } |
| 198 | self |
| 199 | } |
| 200 | |
| 201 | pub fn resolve( |
| 202 | platform: Platform, |
| 203 | port: u16, |
| 204 | inputs: &ConfigInputs, |
| 205 | ) -> Result<Config, BrowserError> { |
| 206 | let to_local = inputs.to_local; |
| 207 | let missing = |name: &str| BrowserError::Environment(format!("%{name}% is not set")); |
| 208 | let config = match platform { |
| 209 | Platform::Linux => { |
| 210 | let data_dir = inputs |
| 211 | .linux_data_dir |
| 212 | .clone() |
| 213 | .ok_or_else(|| BrowserError::Environment("no XDG data directory".into()))?; |
| 214 | Config { |
| 215 | platform, |
| 216 | engine: Engine::Chromium, |
| 217 | port, |
| 218 | // Built with the platform's '/' like with_profile_name, not the |
| 219 | // host's separator: the string is a Linux path whoever builds it. |
| 220 | profile: format!("{}/{}", path_text(&data_dir), PROFILE_NAME), |
| 221 | mode_file: data_dir.join(format!("{PROFILE_NAME}.mode")), |
| 222 | profile_parent: path_text(&data_dir), |
| 223 | mode_parent: data_dir, |
| 224 | search: BrowserSearch::Named( |
| 225 | LINUX_CHROME_NAMES |
| 226 | .iter() |
| 227 | .map(|name| (*name).to_owned()) |
| 228 | .collect(), |
| 229 | ), |
| 230 | } |
| 231 | } |
| 232 | Platform::Macos => { |
| 233 | let data_dir = inputs.linux_data_dir.clone().ok_or_else(|| { |
| 234 | BrowserError::Environment("no Application Support directory".into()) |
| 235 | })?; |
| 236 | let files = application_roots(inputs.home_dir.as_deref()) |
| 237 | .flat_map(|root| MACOS_CHROME_APP_PATHS.map(|relative| root.join(relative))) |
| 238 | .collect(); |
| 239 | Config { |
| 240 | platform, |
| 241 | engine: Engine::Chromium, |
| 242 | port, |
| 243 | profile: format!("{}/{}", path_text(&data_dir), PROFILE_NAME), |
| 244 | mode_file: data_dir.join(format!("{PROFILE_NAME}.mode")), |
| 245 | profile_parent: path_text(&data_dir), |
| 246 | mode_parent: data_dir, |
| 247 | search: BrowserSearch::FilesThenNamed { |
| 248 | files, |
| 249 | names: MACOS_CHROME_NAMES |
| 250 | .iter() |
| 251 | .map(|name| (*name).to_owned()) |
| 252 | .collect(), |
| 253 | }, |
| 254 | } |
| 255 | } |
| 256 | Platform::Windows | Platform::Wsl => { |
| 257 | let local = inputs |
| 258 | .windows_variables |
| 259 | .get("LOCALAPPDATA") |
| 260 | .ok_or_else(|| missing("LOCALAPPDATA"))?; |
| 261 | let candidates = ["PROGRAMFILES", "PROGRAMFILES(X86)", "LOCALAPPDATA"] |
| 262 | .iter() |
| 263 | .filter_map(|name| inputs.windows_variables.get(*name)) |
| 264 | .map(|root| PathBuf::from(to_local(&format!("{root}\\{CHROME_RELATIVE}")))) |
| 265 | .collect(); |
| 266 | Config { |
| 267 | platform, |
| 268 | engine: Engine::Chromium, |
| 269 | port, |
| 270 | profile: format!("{local}\\{PROFILE_NAME}"), |
| 271 | mode_file: PathBuf::from(to_local(&format!("{local}\\{PROFILE_NAME}.mode"))), |
| 272 | profile_parent: local.clone(), |
| 273 | mode_parent: PathBuf::from(to_local(local)), |
| 274 | search: BrowserSearch::Files(candidates), |
| 275 | } |
| 276 | } |
| 277 | }; |
| 278 | Ok(config) |
| 279 | } |
| 280 | |
| 281 | pub fn with_port(mut self, port: u16) -> Config { |
| 282 | self.port = port; |
| 283 | self |
| 284 | } |
| 285 | |
| 286 | pub fn with_profile_name(mut self, name: &str) -> Config { |
| 287 | let separator = if self.platform.uses_unix_paths() { |
| 288 | "/" |
| 289 | } else { |
| 290 | "\\" |
| 291 | }; |
| 292 | self.profile = format!("{}{separator}{name}", self.profile_parent); |
| 293 | self.mode_file = self.mode_parent.join(format!("{name}.mode")); |
| 294 | self |
| 295 | } |
| 296 | |
| 297 | pub fn executable(&self) -> Result<PathBuf, BrowserError> { |
| 298 | find_browser( |
| 299 | &self.search, |
| 300 | &crate::host::path_variable(), |
| 301 | &crate::host::is_file, |
| 302 | ) |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | fn firefox_search(platform: Platform, home_dir: Option<&Path>) -> Option<BrowserSearch> { |
| 307 | let names = || LINUX_FIREFOX_NAMES.iter().map(|name| (*name).to_owned()); |
| 308 | match platform { |
| 309 | Platform::Linux => Some(BrowserSearch::Named(names().collect())), |
| 310 | Platform::Macos => Some(BrowserSearch::FilesThenNamed { |
| 311 | files: application_roots(home_dir) |
| 312 | .flat_map(|root| MACOS_FIREFOX_APP_PATHS.map(|relative| root.join(relative))) |
| 313 | .collect(), |
| 314 | names: names().collect(), |
| 315 | }), |
| 316 | Platform::Windows | Platform::Wsl => None, |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | pub(crate) fn application_roots(home_dir: Option<&Path>) -> impl Iterator<Item = PathBuf> { |
| 321 | std::iter::once(PathBuf::from("/Applications")) |
| 322 | .chain(home_dir.map(|home| home.join("Applications"))) |
| 323 | } |
| 324 | |
| 325 | fn path_text(path: &Path) -> String { |
| 326 | path.to_string_lossy().into_owned() |
| 327 | } |
| 328 | |
| 329 | fn find_named_chrome( |
| 330 | names: &[String], |
| 331 | path_variable: &std::ffi::OsStr, |
| 332 | exists: &dyn Fn(&Path) -> bool, |
| 333 | ) -> Option<PathBuf> { |
| 334 | std::env::split_paths(path_variable) |
| 335 | .flat_map(|directory| names.iter().map(move |name| directory.join(name))) |
| 336 | .find(|candidate| exists(candidate)) |
| 337 | } |
| 338 | |
| 339 | pub(crate) fn find_browser( |
| 340 | search: &BrowserSearch, |
| 341 | path_variable: &std::ffi::OsStr, |
| 342 | exists: &dyn Fn(&Path) -> bool, |
| 343 | ) -> Result<PathBuf, BrowserError> { |
| 344 | match search { |
| 345 | BrowserSearch::Files(candidates) => candidates |
| 346 | .iter() |
| 347 | .find(|candidate| exists(candidate)) |
| 348 | .cloned() |
| 349 | .ok_or_else(|| { |
| 350 | BrowserError::ChromeNotFound("not under Program Files or LocalAppData".into()) |
| 351 | }), |
| 352 | BrowserSearch::Named(names) => { |
| 353 | find_named_chrome(names, path_variable, exists).ok_or_else(|| { |
| 354 | BrowserError::ChromeNotFound(format!("none of {} on PATH", names.join(", "))) |
| 355 | }) |
| 356 | } |
| 357 | BrowserSearch::FilesThenNamed { files, names } => files |
| 358 | .iter() |
| 359 | .find(|candidate| exists(candidate)) |
| 360 | .cloned() |
| 361 | .or_else(|| find_named_chrome(names, path_variable, exists)) |
| 362 | .ok_or_else(|| { |
| 363 | BrowserError::ChromeNotFound( |
| 364 | "no Chromium or Firefox browser under /Applications, ~/Applications, or PATH" |
| 365 | .into(), |
| 366 | ) |
| 367 | }), |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | pub fn windows_path_to_wsl(windows_path: &str) -> String { |
| 372 | match windows_path.split_once(':') { |
| 373 | Some((drive, rest)) if drive.len() == 1 => { |
| 374 | format!( |
| 375 | "/mnt/{}{}", |
| 376 | drive.to_ascii_lowercase(), |
| 377 | rest.replace('\\', "/") |
| 378 | ) |
| 379 | } |
| 380 | _ => windows_path.replace('\\', "/"), |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | fn wslpath(windows_path: &str) -> Option<String> { |
| 385 | let output = Command::new("wslpath") |
| 386 | .arg("-u") |
| 387 | .arg(windows_path) |
| 388 | .output() |
| 389 | .ok()?; |
| 390 | if !output.status.success() { |
| 391 | return None; |
| 392 | } |
| 393 | let text = String::from_utf8(output.stdout).ok()?; |
| 394 | let trimmed = text.trim(); |
| 395 | (!trimmed.is_empty()).then(|| trimmed.to_owned()) |
| 396 | } |
| 397 | |
| 398 | pub(crate) fn parse_windows_variables(output: &str) -> Option<HashMap<String, String>> { |
| 399 | let line = output |
| 400 | .lines() |
| 401 | .map(str::trim) |
| 402 | .rfind(|line| !line.is_empty())?; |
| 403 | let values: Vec<&str> = line.split(';').collect(); |
| 404 | if values.len() != WINDOWS_VARIABLES.len() { |
| 405 | return None; |
| 406 | } |
| 407 | let map: HashMap<String, String> = WINDOWS_VARIABLES |
| 408 | .iter() |
| 409 | .zip(values) |
| 410 | .filter(|(name, value)| *value != format!("%{name}%")) |
| 411 | .map(|(name, value)| ((*name).to_owned(), value.to_owned())) |
| 412 | .collect(); |
| 413 | map.contains_key("LOCALAPPDATA").then_some(map) |
| 414 | } |
| 415 | |
| 416 | fn wsl_windows_variables() -> Result<HashMap<String, String>, BrowserError> { |
| 417 | let echo = WINDOWS_VARIABLES |
| 418 | .iter() |
| 419 | .map(|name| format!("%{name}%")) |
| 420 | .collect::<Vec<_>>() |
| 421 | .join(";"); |
| 422 | let output = Command::new("/mnt/c/Windows/System32/cmd.exe") |
| 423 | .args(["/c", &format!("echo {echo}")]) |
| 424 | .current_dir("/mnt/c") |
| 425 | .output() |
| 426 | .map_err(|error| BrowserError::Environment(format!("cmd.exe: {error}")))?; |
| 427 | parse_windows_variables(&String::from_utf8_lossy(&output.stdout)) |
| 428 | .ok_or_else(|| BrowserError::Environment("cmd.exe returned no %LOCALAPPDATA%".into())) |
| 429 | } |
| 430 | |
| 431 | #[cfg(test)] |
| 432 | mod tests { |
| 433 | use super::*; |
| 434 | use std::ffi::OsString; |
| 435 | |
| 436 | fn windows_inputs(to_local: &dyn Fn(&str) -> String) -> ConfigInputs<'_> { |
| 437 | let variables = [ |
| 438 | ("LOCALAPPDATA", r"C:\Users\me\AppData\Local"), |
| 439 | ("PROGRAMFILES", r"C:\Program Files"), |
| 440 | ("PROGRAMFILES(X86)", r"C:\Program Files (x86)"), |
| 441 | ]; |
| 442 | ConfigInputs { |
| 443 | windows_variables: variables |
| 444 | .iter() |
| 445 | .map(|(k, v)| ((*k).into(), (*v).into())) |
| 446 | .collect(), |
| 447 | linux_data_dir: None, |
| 448 | home_dir: None, |
| 449 | to_local, |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | #[test] |
| 454 | fn detects_platform_from_proc_version() { |
| 455 | let wsl = "Linux version 6.18.33.2-microsoft-standard-WSL2 (root@x)"; |
| 456 | assert_eq!(Platform::from_proc_version(Some(wsl)), Platform::Wsl); |
| 457 | assert_eq!( |
| 458 | Platform::from_proc_version(Some("Linux version 6.8.0-generic")), |
| 459 | Platform::Linux |
| 460 | ); |
| 461 | assert_eq!(Platform::from_proc_version(None), Platform::Linux); |
| 462 | } |
| 463 | |
| 464 | #[test] |
| 465 | fn converts_windows_paths() { |
| 466 | assert_eq!( |
| 467 | windows_path_to_wsl(r"C:\Users\me\x.mode"), |
| 468 | "/mnt/c/Users/me/x.mode" |
| 469 | ); |
| 470 | assert_eq!(windows_path_to_wsl(r"D:\a"), "/mnt/d/a"); |
| 471 | } |
| 472 | |
| 473 | #[test] |
| 474 | fn resolves_wsl_config() { |
| 475 | let to_local = |path: &str| windows_path_to_wsl(path); |
| 476 | let config = Config::resolve(Platform::Wsl, 9222, &windows_inputs(&to_local)).unwrap(); |
| 477 | assert_eq!( |
| 478 | config.profile, |
| 479 | r"C:\Users\me\AppData\Local\rusty-teams-chrome" |
| 480 | ); |
| 481 | assert_eq!( |
| 482 | config.mode_file, |
| 483 | PathBuf::from("/mnt/c/Users/me/AppData/Local/rusty-teams-chrome.mode") |
| 484 | ); |
| 485 | let BrowserSearch::Files(files) = &config.search else { |
| 486 | panic!("expected files") |
| 487 | }; |
| 488 | assert_eq!( |
| 489 | files[0], |
| 490 | PathBuf::from("/mnt/c/Program Files/Google/Chrome/Application/chrome.exe") |
| 491 | ); |
| 492 | assert_eq!(files.len(), 3); |
| 493 | } |
| 494 | |
| 495 | #[test] |
| 496 | fn resolves_windows_config_without_conversion() { |
| 497 | let to_local = |path: &str| path.to_owned(); |
| 498 | let config = Config::resolve(Platform::Windows, 9222, &windows_inputs(&to_local)).unwrap(); |
| 499 | assert_eq!( |
| 500 | config.mode_file, |
| 501 | PathBuf::from(r"C:\Users\me\AppData\Local\rusty-teams-chrome.mode") |
| 502 | ); |
| 503 | } |
| 504 | |
| 505 | #[test] |
| 506 | fn native_windows_uses_the_app_profile_and_chrome_install_dirs() { |
| 507 | let to_local = |path: &str| path.to_owned(); |
| 508 | let config = Config::resolve(Platform::Windows, 9222, &windows_inputs(&to_local)).unwrap(); |
| 509 | assert_eq!( |
| 510 | config.profile, |
| 511 | r"C:\Users\me\AppData\Local\rusty-teams-chrome" |
| 512 | ); |
| 513 | let BrowserSearch::Files(files) = &config.search else { |
| 514 | panic!("expected files") |
| 515 | }; |
| 516 | let expected = [ |
| 517 | r"C:\Program Files\Google\Chrome\Application\chrome.exe", |
| 518 | r"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe", |
| 519 | r"C:\Users\me\AppData\Local\Google\Chrome\Application\chrome.exe", |
| 520 | ]; |
| 521 | assert_eq!( |
| 522 | files |
| 523 | .iter() |
| 524 | .map(|file| file.to_string_lossy().into_owned()) |
| 525 | .collect::<Vec<_>>(), |
| 526 | expected |
| 527 | ); |
| 528 | } |
| 529 | |
| 530 | #[test] |
| 531 | fn native_windows_with_only_localappdata_still_finds_a_per_user_chrome() { |
| 532 | let to_local = |path: &str| path.to_owned(); |
| 533 | let inputs = ConfigInputs { |
| 534 | windows_variables: [("LOCALAPPDATA".to_owned(), r"D:\L".to_owned())].into(), |
| 535 | linux_data_dir: None, |
| 536 | home_dir: None, |
| 537 | to_local: &to_local, |
| 538 | }; |
| 539 | let config = Config::resolve(Platform::Windows, 9222, &inputs).unwrap(); |
| 540 | let BrowserSearch::Files(files) = &config.search else { |
| 541 | panic!("expected files") |
| 542 | }; |
| 543 | assert_eq!( |
| 544 | files, |
| 545 | &[PathBuf::from(r"D:\L\Google\Chrome\Application\chrome.exe")] |
| 546 | ); |
| 547 | assert_eq!(config.with_profile_name("x").profile, r"D:\L\x"); |
| 548 | } |
| 549 | |
| 550 | #[test] |
| 551 | fn wsl_profile_stays_a_windows_path_while_files_are_mounted() { |
| 552 | let to_local = |path: &str| windows_path_to_wsl(path); |
| 553 | let config = Config::resolve(Platform::Wsl, 9222, &windows_inputs(&to_local)).unwrap(); |
| 554 | assert!(config.profile.starts_with(r"C:\")); |
| 555 | assert!(config.mode_file.starts_with("/mnt/c")); |
| 556 | } |
| 557 | |
| 558 | #[test] |
| 559 | fn resolves_linux_config_under_xdg_data_dir() { |
| 560 | let to_local = |path: &str| path.to_owned(); |
| 561 | let inputs = ConfigInputs { |
| 562 | windows_variables: HashMap::new(), |
| 563 | linux_data_dir: Some(PathBuf::from("/home/u/.local/share")), |
| 564 | home_dir: None, |
| 565 | to_local: &to_local, |
| 566 | }; |
| 567 | let config = Config::resolve(Platform::Linux, 9222, &inputs).unwrap(); |
| 568 | assert_eq!(config.profile, "/home/u/.local/share/rusty-teams-chrome"); |
| 569 | assert_eq!( |
| 570 | config.mode_file, |
| 571 | PathBuf::from("/home/u/.local/share/rusty-teams-chrome.mode") |
| 572 | ); |
| 573 | } |
| 574 | |
| 575 | #[test] |
| 576 | fn resolves_macos_config_under_application_support() { |
| 577 | let to_local = |path: &str| path.to_owned(); |
| 578 | let inputs = ConfigInputs { |
| 579 | windows_variables: HashMap::new(), |
| 580 | linux_data_dir: Some(PathBuf::from("/Users/joe/Library/Application Support")), |
| 581 | home_dir: Some(PathBuf::from("/Users/joe")), |
| 582 | to_local: &to_local, |
| 583 | }; |
| 584 | let config = Config::resolve(Platform::Macos, 9222, &inputs).unwrap(); |
| 585 | assert_eq!( |
| 586 | config.profile, |
| 587 | "/Users/joe/Library/Application Support/rusty-teams-chrome" |
| 588 | ); |
| 589 | assert_eq!( |
| 590 | config.mode_file, |
| 591 | PathBuf::from("/Users/joe/Library/Application Support/rusty-teams-chrome.mode") |
| 592 | ); |
| 593 | let BrowserSearch::FilesThenNamed { files, names } = &config.search else { |
| 594 | panic!("expected files then named") |
| 595 | }; |
| 596 | assert_eq!( |
| 597 | files[0], |
| 598 | PathBuf::from("/Applications/Google Chrome.app/Contents/MacOS/Google Chrome") |
| 599 | ); |
| 600 | assert_eq!( |
| 601 | files[MACOS_CHROME_APP_PATHS.len()], |
| 602 | PathBuf::from("/Users/joe/Applications/Google Chrome.app/Contents/MacOS/Google Chrome") |
| 603 | ); |
| 604 | assert!(files.contains(&PathBuf::from( |
| 605 | "/Applications/Helium.app/Contents/MacOS/Helium" |
| 606 | ))); |
| 607 | assert!(names.iter().any(|name| name == "google-chrome")); |
| 608 | assert_eq!( |
| 609 | config.with_profile_name("x").profile, |
| 610 | "/Users/joe/Library/Application Support/x" |
| 611 | ); |
| 612 | } |
| 613 | |
| 614 | #[test] |
| 615 | fn missing_localappdata_is_an_error() { |
| 616 | let to_local = |path: &str| path.to_owned(); |
| 617 | let inputs = ConfigInputs { |
| 618 | windows_variables: HashMap::new(), |
| 619 | linux_data_dir: None, |
| 620 | home_dir: None, |
| 621 | to_local: &to_local, |
| 622 | }; |
| 623 | assert!(Config::resolve(Platform::Windows, 9222, &inputs).is_err()); |
| 624 | } |
| 625 | |
| 626 | #[test] |
| 627 | fn finds_macos_chrome_under_applications_before_path() { |
| 628 | let search = BrowserSearch::FilesThenNamed { |
| 629 | files: vec![ |
| 630 | "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome".into(), |
| 631 | "/Users/joe/Applications/Chromium.app/Contents/MacOS/Chromium".into(), |
| 632 | ], |
| 633 | names: vec!["google-chrome".into()], |
| 634 | }; |
| 635 | let path = std::env::join_paths(["/usr/local/bin"]).unwrap(); |
| 636 | let found = find_browser(&search, &path, &|candidate| { |
| 637 | candidate.ends_with("Google Chrome") || candidate.ends_with("google-chrome") |
| 638 | }) |
| 639 | .unwrap(); |
| 640 | assert_eq!( |
| 641 | found, |
| 642 | PathBuf::from("/Applications/Google Chrome.app/Contents/MacOS/Google Chrome") |
| 643 | ); |
| 644 | } |
| 645 | |
| 646 | #[test] |
| 647 | fn overrides_port_and_profile_name() { |
| 648 | let to_local = |path: &str| windows_path_to_wsl(path); |
| 649 | let config = Config::resolve(Platform::Wsl, 9222, &windows_inputs(&to_local)) |
| 650 | .unwrap() |
| 651 | .with_port(9333) |
| 652 | .with_profile_name("rusty-teams-chrome-test"); |
| 653 | assert_eq!(config.port, 9333); |
| 654 | assert_eq!( |
| 655 | config.profile, |
| 656 | r"C:\Users\me\AppData\Local\rusty-teams-chrome-test" |
| 657 | ); |
| 658 | assert!(config.mode_file.ends_with("rusty-teams-chrome-test.mode")); |
| 659 | } |
| 660 | |
| 661 | #[test] |
| 662 | fn parses_cmd_output_with_crlf_and_unset_variables() { |
| 663 | let output = "\r\nC:\\L;C:\\PF;%PROGRAMFILES(X86)%;C:\\T\r\n"; |
| 664 | let map = parse_windows_variables(output).unwrap(); |
| 665 | assert_eq!(map["LOCALAPPDATA"], r"C:\L"); |
| 666 | assert_eq!(map["TEMP"], r"C:\T"); |
| 667 | assert!(!map.contains_key("PROGRAMFILES(X86)")); |
| 668 | assert!(parse_windows_variables("%LOCALAPPDATA%;a;b;c").is_none()); |
| 669 | assert!(parse_windows_variables("garbage").is_none()); |
| 670 | } |
| 671 | |
| 672 | #[test] |
| 673 | fn finds_first_existing_candidate() { |
| 674 | let search = BrowserSearch::Files(vec!["/a/chrome.exe".into(), "/b/chrome.exe".into()]); |
| 675 | let found = |
| 676 | find_browser(&search, &OsString::new(), &|path| path.starts_with("/b")).unwrap(); |
| 677 | assert_eq!(found, PathBuf::from("/b/chrome.exe")); |
| 678 | assert!(find_browser(&search, &OsString::new(), &|_| false).is_err()); |
| 679 | } |
| 680 | |
| 681 | #[test] |
| 682 | fn finds_named_chrome_on_path_in_name_priority_per_directory() { |
| 683 | let search = BrowserSearch::Named(vec!["google-chrome".into(), "chromium".into()]); |
| 684 | // join_paths picks the host's PATH separator so split_paths splits it here too. |
| 685 | let path = std::env::join_paths(["/usr/local/bin", "/usr/bin"]).unwrap(); |
| 686 | let found = find_browser(&search, &path, &|candidate| { |
| 687 | candidate == Path::new("/usr/bin/chromium") |
| 688 | || candidate == Path::new("/usr/bin/google-chrome") |
| 689 | }) |
| 690 | .unwrap(); |
| 691 | assert_eq!(found, PathBuf::from("/usr/bin/google-chrome")); |
| 692 | } |
| 693 | |
| 694 | fn macos_config() -> Config { |
| 695 | let to_local = |path: &str| path.to_owned(); |
| 696 | let inputs = ConfigInputs { |
| 697 | windows_variables: HashMap::new(), |
| 698 | linux_data_dir: Some(PathBuf::from("/Users/joe/Library/Application Support")), |
| 699 | home_dir: Some(PathBuf::from("/Users/joe")), |
| 700 | to_local: &to_local, |
| 701 | }; |
| 702 | Config::resolve(Platform::Macos, 9222, &inputs).unwrap() |
| 703 | } |
| 704 | |
| 705 | fn select(installed: &[&str], chosen: Option<&str>) -> Config { |
| 706 | macos_config().select_browser( |
| 707 | chosen.map(PathBuf::from), |
| 708 | Some(Path::new("/Users/joe")), |
| 709 | &OsString::new(), |
| 710 | &|path| { |
| 711 | installed |
| 712 | .iter() |
| 713 | .any(|installed| path == Path::new(installed)) |
| 714 | }, |
| 715 | ) |
| 716 | } |
| 717 | |
| 718 | #[test] |
| 719 | fn a_chromium_browser_wins_over_firefox() { |
| 720 | let config = select( |
| 721 | &[ |
| 722 | "/Applications/Helium.app/Contents/MacOS/Helium", |
| 723 | "/Applications/Zen.app/Contents/MacOS/zen", |
| 724 | ], |
| 725 | None, |
| 726 | ); |
| 727 | assert_eq!(config.engine, Engine::Chromium); |
| 728 | assert!(config.profile.ends_with(PROFILE_NAME)); |
| 729 | } |
| 730 | |
| 731 | #[test] |
| 732 | fn firefox_is_the_fallback_with_its_own_profile() { |
| 733 | let zen = "/Users/joe/Applications/Zen.app/Contents/MacOS/zen"; |
| 734 | let config = select(&[zen], None); |
| 735 | assert_eq!(config.engine, Engine::Firefox); |
| 736 | assert_eq!( |
| 737 | config.profile, |
| 738 | "/Users/joe/Library/Application Support/rusty-teams-firefox" |
| 739 | ); |
| 740 | let found = find_browser(&config.search, &OsString::new(), &|path| { |
| 741 | path == Path::new(zen) |
| 742 | }); |
| 743 | assert_eq!(found.unwrap(), PathBuf::from(zen)); |
| 744 | } |
| 745 | |
| 746 | #[test] |
| 747 | fn the_chosen_browser_sets_the_engine() { |
| 748 | let helium = "/Applications/Helium.app/Contents/MacOS/Helium"; |
| 749 | let firefox = select(&[helium], Some("/opt/zen/zen")); |
| 750 | assert_eq!(firefox.engine, Engine::Firefox); |
| 751 | let BrowserSearch::Files(files) = &firefox.search else { |
| 752 | panic!("expected the chosen path") |
| 753 | }; |
| 754 | assert_eq!(files, &[PathBuf::from("/opt/zen/zen")]); |
| 755 | assert_eq!(select(&[], Some(helium)).engine, Engine::Chromium); |
| 756 | } |
| 757 | } |