| 1 | //! How *this* binary was installed, and therefore which command updates it. |
| 2 | //! |
| 3 | //! `codewhale update` replaces the running executable in place. That is the |
| 4 | //! right thing for a binary the user downloaded from GitHub Releases, and the |
| 5 | //! wrong thing for one a package manager owns: overwriting Homebrew's Cellar |
| 6 | //! binary or npm's `node_modules` payload leaves the manager's metadata |
| 7 | //! describing a version that is no longer on disk, and the next |
| 8 | //! `brew upgrade` / `npm install -g` silently reverts the user. |
| 9 | //! |
| 10 | //! So before we tell anyone to run anything, we work out who owns the file. |
| 11 | //! Detection is path-based where managers use distinct prefixes. Omarchy's |
| 12 | //! AUR packages live in the ordinary `/usr/bin` prefix, so that case also |
| 13 | //! consults the local pacman ownership database. No network access is needed. |
| 14 | //! |
| 15 | //! This is the one ownership check: `codewhale update`, `doctor`, `uninstall`, |
| 16 | //! rollback and automatic updates all ask [`InstallMethod::detect`]. Do not add |
| 17 | //! a second path list beside it. |
| 18 | |
| 19 | use std::path::{Path, PathBuf}; |
| 20 | #[cfg(target_os = "linux")] |
| 21 | use std::process::{Command, Stdio}; |
| 22 | |
| 23 | /// Environment variable that overrides install-method detection. |
| 24 | /// |
| 25 | /// Accepts `npm`, `homebrew` (or `brew`), `cargo`, `omarchy`, `scoop`, |
| 26 | /// `winget`, and `binary`. |
| 27 | /// Anything else is ignored and detection falls back to automatic detection. |
| 28 | /// Packagers who relocate the binary somewhere the heuristics cannot read — |
| 29 | /// and users debugging a wrong guess — set this. Recognized managed paths |
| 30 | /// take precedence; `binary` cannot authorize overwriting a package-owned file. |
| 31 | pub const INSTALL_METHOD_ENV: &str = "CODEWHALE_INSTALL_METHOD"; |
| 32 | |
| 33 | /// Shared migration instructions for every runtime update surface. The new |
| 34 | /// directory avoids guessing ownership or overwriting a mixed installation. |
| 35 | pub const GITHUB_MIGRATION_HELP: &str = r#"Install the official GitHub release into a fresh user directory (macOS/Linux): |
| 36 | mkdir -p "$HOME/.local" |
| 37 | codewhale_install_dir="$(mktemp -d "$HOME/.local/codewhale-release.XXXXXX")" |
| 38 | curl -fsSL https://codewhale.net/install.sh | CODEWHALE_INSTALL_DIR="$codewhale_install_dir" sh |
| 39 | "$codewhale_install_dir/codewhale" --version |
| 40 | export PATH="$codewhale_install_dir:$PATH" |
| 41 | hash -r |
| 42 | command -v codewhale codew |
| 43 | Future updates: "$codewhale_install_dir/codewhale" update |
| 44 | Keep the chosen PATH directory in your shell profile after verifying it. |
| 45 | Windows: https://github.com/codewhale-hq/CodeWhale/releases/latest |
| 46 | PATH and migration: https://github.com/codewhale-hq/CodeWhale/blob/main/docs/INSTALL.md"#; |
| 47 | |
| 48 | pub const DESKTOP_APP_UPDATE: &str = "Codewhale > Check for Updates"; |
| 49 | |
| 50 | pub const DESKTOP_APP_UPDATE_HELP: &str = "The Codewhale app updates this copy together with the app. Use Codewhale > Check for Updates. `codewhale update` never writes inside the app."; |
| 51 | |
| 52 | /// The package manager (if any) that owns the running executable. |
| 53 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 54 | pub enum InstallMethod { |
| 55 | /// Global npm install — the `codewhale` package under `node_modules`. |
| 56 | Npm, |
| 57 | /// Homebrew — a binary under a `Cellar` or `linuxbrew` prefix. |
| 58 | Homebrew, |
| 59 | /// `cargo install` — a binary under `~/.cargo/bin`. |
| 60 | Cargo, |
| 61 | /// An AUR/pacman package installed on Omarchy. |
| 62 | Omarchy, |
| 63 | /// Scoop — a binary under a `scoop/apps` prefix. |
| 64 | Scoop, |
| 65 | /// A WinGet portable package under `Microsoft/WinGet/Packages`. |
| 66 | WinGet, |
| 67 | DesktopApp, |
| 68 | /// A system or distribution directory (`/usr/bin`, the Nix and Guix |
| 69 | /// stores, Windows system folders, Chocolatey) whose owning package |
| 70 | /// manager is not known. |
| 71 | System, |
| 72 | /// The Windows setup installer: `<root>\bin\codewhale.exe` beside its |
| 73 | /// `Uninstall.exe`. `codewhale update` replaces the binary in place; the |
| 74 | /// installer removes it. |
| 75 | WindowsInstaller, |
| 76 | /// A self-updating layout in a folder this account cannot write. |
| 77 | ReadOnly, |
| 78 | /// A release binary the user placed on disk themselves. The default. |
| 79 | Binary, |
| 80 | } |
| 81 | |
| 82 | impl InstallMethod { |
| 83 | /// Detect from an executable path, honouring [`INSTALL_METHOD_ENV`]. |
| 84 | /// |
| 85 | /// `std::env::current_exe()` follows symlinks on Linux but not on macOS, |
| 86 | /// where a Homebrew launch reports `/opt/homebrew/bin/codewhale` instead |
| 87 | /// of the `Cellar` file it points at. When the path as given looks |
| 88 | /// self-updatable, the symlink target is checked too, and a package |
| 89 | /// manager that owns the real file wins. |
| 90 | #[must_use] |
| 91 | pub fn detect(exe: &Path) -> Self { |
| 92 | let detected = Self::owner(exe); |
| 93 | // An override can identify a relocated package, but cannot authorize |
| 94 | // replacing a file that a known package manager owns. An unknown |
| 95 | // system directory may be named by it, but only as another manager. |
| 96 | match std::env::var(INSTALL_METHOD_ENV) |
| 97 | .ok() |
| 98 | .and_then(|raw| Self::from_token(&raw)) |
| 99 | { |
| 100 | Some(forced) |
| 101 | if detected.supports_self_update() |
| 102 | || (detected == Self::System && !forced.supports_self_update()) => |
| 103 | { |
| 104 | forced |
| 105 | } |
| 106 | _ => detected, |
| 107 | } |
| 108 | .unless_unwritable(exe) |
| 109 | } |
| 110 | |
| 111 | /// A copy that would update itself, but whose folder this account cannot |
| 112 | /// write, cannot apply an update: say so before offering or staging one. |
| 113 | fn unless_unwritable(self, exe: &Path) -> Self { |
| 114 | let Some(dir) = exe.parent().filter(|dir| dir.is_dir()) else { |
| 115 | return self; |
| 116 | }; |
| 117 | if self.supports_self_update() |
| 118 | && tempfile::Builder::new() |
| 119 | .prefix(".codewhale-write-probe") |
| 120 | .tempfile_in(dir) |
| 121 | .is_err() |
| 122 | { |
| 123 | return Self::ReadOnly; |
| 124 | } |
| 125 | self |
| 126 | } |
| 127 | |
| 128 | /// Who owns `exe` on disk, ignoring [`INSTALL_METHOD_ENV`]: the answer for |
| 129 | /// a file that is not the running executable. |
| 130 | #[must_use] |
| 131 | pub fn owner(exe: &Path) -> Self { |
| 132 | let mut detected = Self::detect_with_omarchy_probe(exe, omarchy_package_owns); |
| 133 | if detected.supports_self_update() |
| 134 | && let Ok(real) = std::fs::canonicalize(exe) |
| 135 | && real != exe |
| 136 | { |
| 137 | let owner = Self::detect_with_omarchy_probe(&real, omarchy_package_owns); |
| 138 | if !owner.supports_self_update() { |
| 139 | detected = owner; |
| 140 | } |
| 141 | } |
| 142 | detected |
| 143 | } |
| 144 | |
| 145 | fn detect_with_omarchy_probe(exe: &Path, owns_package: impl FnOnce(&Path) -> bool) -> Self { |
| 146 | let mut path_method = Self::from_path(exe); |
| 147 | let unlisted = path_method == Self::Cargo |
| 148 | && exe |
| 149 | .file_name() |
| 150 | .and_then(|name| name.to_str()) |
| 151 | .is_some_and(|name| crate::installs::cargo_record_lists(exe, name) == Some(false)); |
| 152 | if unlisted { |
| 153 | path_method = Self::Binary; |
| 154 | } |
| 155 | match path_method { |
| 156 | Self::Binary if windows_installer_uninstaller(exe).is_some() => { |
| 157 | return Self::WindowsInstaller; |
| 158 | } |
| 159 | Self::Binary if !unlisted && crate::installs::cargo_owns_a_command_beside(exe) => { |
| 160 | return Self::Cargo; |
| 161 | } |
| 162 | Self::Binary | Self::System => {} |
| 163 | _ => return path_method, |
| 164 | } |
| 165 | if owns_package(exe) { |
| 166 | Self::Omarchy |
| 167 | } else { |
| 168 | path_method |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | /// Path-only detection, with no environment lookup. Split out from |
| 173 | /// [`detect`](Self::detect) so tests can exercise the heuristics without |
| 174 | /// mutating process-global state. |
| 175 | #[must_use] |
| 176 | pub fn from_path(exe: &Path) -> Self { |
| 177 | let components: Vec<String> = exe |
| 178 | .components() |
| 179 | .filter_map(|c| c.as_os_str().to_str()) |
| 180 | .map(str::to_ascii_lowercase) |
| 181 | .collect(); |
| 182 | |
| 183 | let has = |name: &str| components.iter().any(|c| c == name); |
| 184 | |
| 185 | if components.windows(3).any(|parts| { |
| 186 | parts[0].ends_with(".app") && parts[1] == "contents" && parts[2] == "macos" |
| 187 | }) || components |
| 188 | .windows(2) |
| 189 | .any(|pair| pair[0] == "gpui" && pair[1] == "engine") |
| 190 | { |
| 191 | return Self::DesktopApp; |
| 192 | } |
| 193 | |
| 194 | // npm is checked first: a `node_modules` install *inside* a Homebrew |
| 195 | // or Termux prefix is still npm's to update. |
| 196 | if has("node_modules") { |
| 197 | return Self::Npm; |
| 198 | } |
| 199 | if has("cellar") || has(".linuxbrew") || has("linuxbrew") { |
| 200 | return Self::Homebrew; |
| 201 | } |
| 202 | let pair = |first: &str, second: &str| { |
| 203 | components |
| 204 | .windows(2) |
| 205 | .any(|pair| pair[0] == first && pair[1] == second) |
| 206 | }; |
| 207 | if pair("scoop", "apps") { |
| 208 | return Self::Scoop; |
| 209 | } |
| 210 | if pair("winget", "packages") || pair("winget", "links") { |
| 211 | return Self::WinGet; |
| 212 | } |
| 213 | // `.cargo/bin/codewhale` — require the pair so an unrelated `bin` |
| 214 | // directory does not read as a Cargo install. |
| 215 | if components |
| 216 | .windows(2) |
| 217 | .any(|pair| pair[0] == ".cargo" && pair[1] == "bin") |
| 218 | { |
| 219 | return Self::Cargo; |
| 220 | } |
| 221 | let system_prefix = [ |
| 222 | "/usr/bin", |
| 223 | "/usr/sbin", |
| 224 | "/bin", |
| 225 | "/sbin", |
| 226 | "/nix/store", |
| 227 | "/gnu/store", |
| 228 | ] |
| 229 | .iter() |
| 230 | .any(|prefix| exe.starts_with(prefix)); |
| 231 | if system_prefix |
| 232 | || has("windows") |
| 233 | || has("windowsapps") |
| 234 | || has("scoop") |
| 235 | || has("chocolatey") |
| 236 | { |
| 237 | return Self::System; |
| 238 | } |
| 239 | Self::Binary |
| 240 | } |
| 241 | |
| 242 | fn from_token(raw: &str) -> Option<Self> { |
| 243 | match raw.trim().to_ascii_lowercase().as_str() { |
| 244 | "npm" => Some(Self::Npm), |
| 245 | "homebrew" | "brew" => Some(Self::Homebrew), |
| 246 | "cargo" => Some(Self::Cargo), |
| 247 | "omarchy" => Some(Self::Omarchy), |
| 248 | "scoop" => Some(Self::Scoop), |
| 249 | "winget" => Some(Self::WinGet), |
| 250 | "desktop" | "app" => Some(Self::DesktopApp), |
| 251 | "system" => Some(Self::System), |
| 252 | "binary" | "release" => Some(Self::Binary), |
| 253 | _ => None, |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | /// The exact shell command that updates this install. |
| 258 | /// |
| 259 | /// Homebrew's primary formula is `codewhale`. Existing Cellar paths |
| 260 | /// under the legacy `deepseek-tui` name still detect as Homebrew; those |
| 261 | /// installs can keep using `brew upgrade deepseek-tui` during the |
| 262 | /// overlap window, but new notices name the Codewhale formula. |
| 263 | #[must_use] |
| 264 | pub fn update_command(self) -> &'static str { |
| 265 | match self { |
| 266 | Self::Npm => "npm install -g codewhale@latest", |
| 267 | Self::Homebrew => "brew upgrade codewhale", |
| 268 | Self::Cargo => "cargo install codewhale-cli --locked --force", |
| 269 | Self::Omarchy => "omarchy update", |
| 270 | Self::Scoop => "scoop update codewhale", |
| 271 | Self::WinGet => "winget upgrade --id HunterBown.CodeWhale", |
| 272 | Self::DesktopApp => DESKTOP_APP_UPDATE, |
| 273 | Self::System => "the package manager that installed it", |
| 274 | Self::ReadOnly => "the installer, into a folder you own", |
| 275 | Self::WindowsInstaller | Self::Binary => "codewhale update", |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | #[must_use] |
| 280 | pub fn uninstall_command(self) -> &'static str { |
| 281 | match self { |
| 282 | Self::Npm => "npm uninstall -g codewhale", |
| 283 | Self::Homebrew => { |
| 284 | "brew uninstall codewhale (use the owning formula name for a legacy formula)" |
| 285 | } |
| 286 | Self::Cargo => "cargo uninstall codewhale-cli --root <the owning Cargo install root>", |
| 287 | Self::Omarchy => { |
| 288 | "pacman -Qo <the executable path>, then remove that package with your package manager" |
| 289 | } |
| 290 | Self::Scoop => "scoop uninstall codewhale", |
| 291 | Self::WinGet => "winget uninstall --id HunterBown.CodeWhale", |
| 292 | Self::DesktopApp => "remove the Codewhale app; this Engine is part of it", |
| 293 | Self::System => "remove it with the package manager that installed it", |
| 294 | Self::ReadOnly => "remove it as the account that owns its folder", |
| 295 | Self::WindowsInstaller => "Settings > Apps > CodeWhale > Uninstall", |
| 296 | Self::Binary => "codewhale uninstall --yes", |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | /// Whether `codewhale update` may replace this binary in place. |
| 301 | /// |
| 302 | /// False for every package-managed install: see the module docs for why |
| 303 | /// overwriting a managed binary is worse than doing nothing. |
| 304 | #[must_use] |
| 305 | pub fn supports_self_update(self) -> bool { |
| 306 | matches!(self, Self::Binary | Self::WindowsInstaller) |
| 307 | } |
| 308 | |
| 309 | /// Short human label, for messages that name the owner of the install. |
| 310 | #[must_use] |
| 311 | pub fn label(self) -> &'static str { |
| 312 | match self { |
| 313 | Self::Npm => "npm", |
| 314 | Self::Homebrew => "Homebrew", |
| 315 | Self::Cargo => "Cargo", |
| 316 | Self::Omarchy => "Omarchy", |
| 317 | Self::Scoop => "Scoop", |
| 318 | Self::WinGet => "WinGet", |
| 319 | Self::DesktopApp => "the Codewhale app", |
| 320 | Self::System => "a system package manager", |
| 321 | Self::ReadOnly => "a folder this account cannot write", |
| 322 | Self::WindowsInstaller => "the Codewhale Windows installer", |
| 323 | Self::Binary => "release binary", |
| 324 | } |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | /// `<root>\Uninstall.exe` when `exe` is `<root>\bin\<name>` from the Windows |
| 329 | /// setup installer, identified by the files only that installer writes. |
| 330 | pub(crate) fn windows_installer_uninstaller(exe: &Path) -> Option<PathBuf> { |
| 331 | let bin = exe.parent()?; |
| 332 | if !bin |
| 333 | .file_name() |
| 334 | .is_some_and(|name| name.eq_ignore_ascii_case("bin")) |
| 335 | { |
| 336 | return None; |
| 337 | } |
| 338 | let root = bin.parent()?; |
| 339 | let uninstaller = root.join("Uninstall.exe"); |
| 340 | [&uninstaller, &root.join("update-user-path.ps1")] |
| 341 | .iter() |
| 342 | .all(|path| std::fs::symlink_metadata(path).is_ok_and(|meta| meta.is_file())) |
| 343 | .then_some(uninstaller) |
| 344 | } |
| 345 | |
| 346 | #[cfg(target_os = "linux")] |
| 347 | fn omarchy_package_owns(exe: &Path) -> bool { |
| 348 | if !Path::new("/usr/share/omarchy/version").is_file() { |
| 349 | return false; |
| 350 | } |
| 351 | |
| 352 | Command::new("pacman") |
| 353 | .args(["-Qo"]) |
| 354 | .arg(exe) |
| 355 | .stdin(Stdio::null()) |
| 356 | .stdout(Stdio::null()) |
| 357 | .stderr(Stdio::null()) |
| 358 | .status() |
| 359 | .is_ok_and(|status| status.success()) |
| 360 | } |
| 361 | |
| 362 | #[cfg(not(target_os = "linux"))] |
| 363 | fn omarchy_package_owns(_exe: &Path) -> bool { |
| 364 | false |
| 365 | } |
| 366 | |
| 367 | /// Detect the install method for the currently running executable. |
| 368 | /// |
| 369 | /// Returns [`InstallMethod::Binary`] when the executable path cannot be |
| 370 | /// resolved — the conservative answer, because it is the one that tells the |
| 371 | /// user to run our own updater rather than a package manager command that may |
| 372 | /// not apply to them. |
| 373 | #[must_use] |
| 374 | pub fn current_install_method() -> InstallMethod { |
| 375 | match std::env::current_exe() { |
| 376 | Ok(exe) => InstallMethod::detect(&exe), |
| 377 | Err(_) => InstallMethod::Binary, |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | #[cfg(test)] |
| 382 | mod tests { |
| 383 | use std::path::PathBuf; |
| 384 | |
| 385 | use super::*; |
| 386 | |
| 387 | #[test] |
| 388 | fn npm_global_install_is_detected_from_node_modules() { |
| 389 | let exe = PathBuf::from("/usr/local/lib/node_modules/codewhale/bin/codewhale"); |
| 390 | assert_eq!(InstallMethod::from_path(&exe), InstallMethod::Npm); |
| 391 | assert_eq!( |
| 392 | InstallMethod::Npm.update_command(), |
| 393 | "npm install -g codewhale@latest" |
| 394 | ); |
| 395 | assert!(!InstallMethod::Npm.supports_self_update()); |
| 396 | } |
| 397 | |
| 398 | #[test] |
| 399 | fn homebrew_install_is_detected_from_cellar_on_both_prefixes() { |
| 400 | for exe in [ |
| 401 | "/opt/homebrew/Cellar/codewhale/0.9.8/bin/codewhale", |
| 402 | "/usr/local/Cellar/codewhale/0.9.8/bin/codewhale", |
| 403 | "/home/linuxbrew/.linuxbrew/Cellar/codewhale/0.9.8/bin/codewhale", |
| 404 | "/opt/homebrew/Cellar/deepseek-tui/0.9.4/bin/codewhale", |
| 405 | "/usr/local/Cellar/deepseek-tui/0.9.4/bin/codewhale", |
| 406 | "/home/linuxbrew/.linuxbrew/Cellar/deepseek-tui/0.9.4/bin/codewhale", |
| 407 | ] { |
| 408 | assert_eq!( |
| 409 | InstallMethod::from_path(&PathBuf::from(exe)), |
| 410 | InstallMethod::Homebrew, |
| 411 | "{exe} should read as Homebrew" |
| 412 | ); |
| 413 | } |
| 414 | assert_eq!( |
| 415 | InstallMethod::Homebrew.update_command(), |
| 416 | "brew upgrade codewhale" |
| 417 | ); |
| 418 | assert!(!InstallMethod::Homebrew.supports_self_update()); |
| 419 | } |
| 420 | |
| 421 | /// macOS reports the launcher symlink from `current_exe()`, so detection |
| 422 | /// must follow it into `Cellar` before allowing an in-place update. |
| 423 | #[cfg(unix)] |
| 424 | #[test] |
| 425 | fn homebrew_symlink_launcher_is_detected_through_its_target() { |
| 426 | let root = tempfile::tempdir().unwrap(); |
| 427 | let cellar_bin = root.path().join("Cellar/codewhale/0.10.0/bin"); |
| 428 | std::fs::create_dir_all(&cellar_bin).unwrap(); |
| 429 | let real = cellar_bin.join("codewhale"); |
| 430 | std::fs::write(&real, b"binary").unwrap(); |
| 431 | let bin = root.path().join("bin"); |
| 432 | std::fs::create_dir_all(&bin).unwrap(); |
| 433 | let launcher = bin.join("codewhale"); |
| 434 | std::os::unix::fs::symlink(&real, &launcher).unwrap(); |
| 435 | |
| 436 | assert_eq!(InstallMethod::from_path(&launcher), InstallMethod::Binary); |
| 437 | assert_eq!(InstallMethod::detect(&launcher), InstallMethod::Homebrew); |
| 438 | |
| 439 | // A plain release binary with no package owner stays self-updatable. |
| 440 | let plain = bin.join("codew"); |
| 441 | std::fs::write(&plain, b"binary").unwrap(); |
| 442 | assert!(InstallMethod::detect(&plain).supports_self_update()); |
| 443 | } |
| 444 | |
| 445 | #[test] |
| 446 | fn cargo_install_requires_the_cargo_bin_pair() { |
| 447 | assert_eq!( |
| 448 | InstallMethod::from_path(&PathBuf::from("/home/u/.cargo/bin/codewhale")), |
| 449 | InstallMethod::Cargo |
| 450 | ); |
| 451 | // A bare `bin` directory is not a Cargo install. |
| 452 | assert_eq!( |
| 453 | InstallMethod::from_path(&PathBuf::from("/home/u/bin/codewhale")), |
| 454 | InstallMethod::Binary |
| 455 | ); |
| 456 | assert!(!InstallMethod::Cargo.supports_self_update()); |
| 457 | let root = tempfile::tempdir().unwrap(); |
| 458 | let bin = root.path().join("bin"); |
| 459 | std::fs::create_dir(&bin).unwrap(); |
| 460 | std::fs::write( |
| 461 | root.path().join(".crates.toml"), |
| 462 | b"[v1]\n\"other 0.1.0 (path+file:///src/other)\" = [\"other-bin\"]\n", |
| 463 | ) |
| 464 | .unwrap(); |
| 465 | assert_eq!( |
| 466 | InstallMethod::detect(&bin.join("codewhale")), |
| 467 | InstallMethod::Binary |
| 468 | ); |
| 469 | let record = format!( |
| 470 | r#"{{"installs":{{"codewhale-cli 0.10.3":{{"bins":["{}"]}}}}}}"#, |
| 471 | crate::installs::RECEIPT_COMMANDS[0] |
| 472 | ); |
| 473 | std::fs::write(root.path().join(".crates2.json"), record).unwrap(); |
| 474 | for command in ["codewhale", "codew"] { |
| 475 | assert_eq!( |
| 476 | InstallMethod::detect(&bin.join(command)), |
| 477 | InstallMethod::Cargo, |
| 478 | "{command}" |
| 479 | ); |
| 480 | } |
| 481 | std::fs::remove_file(root.path().join(".crates2.json")).unwrap(); |
| 482 | std::fs::remove_file(root.path().join(".crates.toml")).unwrap(); |
| 483 | std::fs::create_dir(root.path().join(".crates.toml")).unwrap(); |
| 484 | assert_eq!( |
| 485 | InstallMethod::detect(&bin.join("codewhale")), |
| 486 | InstallMethod::Cargo |
| 487 | ); |
| 488 | } |
| 489 | |
| 490 | #[test] |
| 491 | fn npm_wins_over_an_enclosing_manager_prefix() { |
| 492 | // npm installed under a Homebrew-managed node prefix is still npm's. |
| 493 | let exe = PathBuf::from("/opt/homebrew/lib/node_modules/codewhale/bin/codewhale"); |
| 494 | assert_eq!(InstallMethod::from_path(&exe), InstallMethod::Npm); |
| 495 | } |
| 496 | |
| 497 | #[test] |
| 498 | fn system_directories_are_owned_but_user_release_paths_self_update() { |
| 499 | for path in [ |
| 500 | "/usr/bin/codewhale", |
| 501 | "/usr/sbin/codewhale", |
| 502 | "/bin/codewhale", |
| 503 | "/nix/store/pkg/bin/codewhale", |
| 504 | "/gnu/store/pkg/bin/codewhale", |
| 505 | "/Users/u/scoop/shims/codewhale.exe", |
| 506 | "/Windows/System32/codewhale.exe", |
| 507 | ] { |
| 508 | assert_eq!( |
| 509 | InstallMethod::from_path(Path::new(path)), |
| 510 | InstallMethod::System, |
| 511 | "{path}" |
| 512 | ); |
| 513 | } |
| 514 | for path in [ |
| 515 | "/usr/local/bin/codewhale", |
| 516 | "/home/u/.local/bin/codewhale", |
| 517 | "/data/data/com.termux/files/usr/bin/codewhale", |
| 518 | ] { |
| 519 | assert_eq!( |
| 520 | InstallMethod::from_path(Path::new(path)), |
| 521 | InstallMethod::Binary, |
| 522 | "{path}" |
| 523 | ); |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | #[test] |
| 528 | fn omarchy_probe_only_claims_package_owned_plain_paths() { |
| 529 | let managed = PathBuf::from("/usr/bin/codewhale"); |
| 530 | assert_eq!( |
| 531 | InstallMethod::detect_with_omarchy_probe(&managed, |_| true), |
| 532 | InstallMethod::Omarchy |
| 533 | ); |
| 534 | assert_eq!( |
| 535 | InstallMethod::detect_with_omarchy_probe(&managed, |_| false), |
| 536 | InstallMethod::System |
| 537 | ); |
| 538 | |
| 539 | let npm = PathBuf::from("/usr/lib/node_modules/codewhale/bin/codewhale"); |
| 540 | assert_eq!( |
| 541 | InstallMethod::detect_with_omarchy_probe(&npm, |_| { |
| 542 | panic!("a path-owned install must not query pacman") |
| 543 | }), |
| 544 | InstallMethod::Npm |
| 545 | ); |
| 546 | } |
| 547 | |
| 548 | #[test] |
| 549 | fn termux_and_plain_release_binaries_self_update() { |
| 550 | for exe in [ |
| 551 | "/data/data/com.termux/files/usr/bin/codewhale", |
| 552 | "/usr/local/bin/codewhale", |
| 553 | "/home/u/Downloads/codewhale", |
| 554 | ] { |
| 555 | let method = InstallMethod::from_path(&PathBuf::from(exe)); |
| 556 | assert_eq!(method, InstallMethod::Binary, "{exe} should self-update"); |
| 557 | assert!(method.supports_self_update()); |
| 558 | assert_eq!(method.update_command(), "codewhale update"); |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | #[test] |
| 563 | fn env_tokens_map_to_methods_and_junk_is_ignored() { |
| 564 | assert_eq!(InstallMethod::from_token("npm"), Some(InstallMethod::Npm)); |
| 565 | assert_eq!( |
| 566 | InstallMethod::from_token(" BREW "), |
| 567 | Some(InstallMethod::Homebrew) |
| 568 | ); |
| 569 | assert_eq!( |
| 570 | InstallMethod::from_token("homebrew"), |
| 571 | Some(InstallMethod::Homebrew) |
| 572 | ); |
| 573 | assert_eq!( |
| 574 | InstallMethod::from_token("cargo"), |
| 575 | Some(InstallMethod::Cargo) |
| 576 | ); |
| 577 | assert_eq!( |
| 578 | InstallMethod::from_token("omarchy"), |
| 579 | Some(InstallMethod::Omarchy) |
| 580 | ); |
| 581 | assert_eq!( |
| 582 | InstallMethod::from_token("binary"), |
| 583 | Some(InstallMethod::Binary) |
| 584 | ); |
| 585 | assert_eq!(InstallMethod::from_token("apt"), None); |
| 586 | } |
| 587 | |
| 588 | #[test] |
| 589 | fn omarchy_install_is_package_managed() { |
| 590 | assert_eq!(InstallMethod::Omarchy.update_command(), "omarchy update"); |
| 591 | assert_eq!(InstallMethod::Omarchy.label(), "Omarchy"); |
| 592 | assert!(!InstallMethod::Omarchy.supports_self_update()); |
| 593 | } |
| 594 | } |
| 595 |