|
| 1 | +use std::{ |
| 2 | + ffi::OsStr, |
| 3 | + path::{Path, PathBuf}, |
| 4 | +}; |
| 5 | + |
| 6 | +/// Represents how prek was installed on the system. |
| 7 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 8 | +pub(crate) enum InstallSource { |
| 9 | + Homebrew, |
| 10 | + Cargo, |
| 11 | +} |
| 12 | + |
| 13 | +impl InstallSource { |
| 14 | + /// Detect the install source from a given path. |
| 15 | + fn from_path(path: &Path) -> Option<Self> { |
| 16 | + let canonical = path.canonicalize().unwrap_or_else(|_| PathBuf::from(path)); |
| 17 | + let components: Vec<_> = canonical |
| 18 | + .components() |
| 19 | + .map(|c| c.as_os_str().to_owned()) |
| 20 | + .collect(); |
| 21 | + |
| 22 | + // Check for Homebrew Cellar installation: .../Cellar/prek/... |
| 23 | + let cellar = OsStr::new("Cellar"); |
| 24 | + let prek = OsStr::new("prek"); |
| 25 | + if components |
| 26 | + .windows(2) |
| 27 | + .any(|w| w[0] == cellar && w[1] == prek) |
| 28 | + { |
| 29 | + return Some(Self::Homebrew); |
| 30 | + } |
| 31 | + |
| 32 | + // Check for cargo bin installation: .../.cargo/bin/... |
| 33 | + let cargo = OsStr::new(".cargo"); |
| 34 | + let bin = OsStr::new("bin"); |
| 35 | + if components.windows(2).any(|w| w[0] == cargo && w[1] == bin) { |
| 36 | + return Some(Self::Cargo); |
| 37 | + } |
| 38 | + |
| 39 | + None |
| 40 | + } |
| 41 | + |
| 42 | + /// Detect the install source from the current executable path. |
| 43 | + pub(crate) fn detect() -> Option<Self> { |
| 44 | + Self::from_path(&std::env::current_exe().ok()?) |
| 45 | + } |
| 46 | + |
| 47 | + /// Returns a human-readable description of the install source. |
| 48 | + pub(crate) fn description(self) -> &'static str { |
| 49 | + match self { |
| 50 | + Self::Homebrew => "Homebrew", |
| 51 | + Self::Cargo => "cargo", |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + /// Returns the command to update prek for this install source. |
| 56 | + pub(crate) fn update_instructions(self) -> &'static str { |
| 57 | + match self { |
| 58 | + Self::Homebrew => "brew update && brew upgrade prek", |
| 59 | + Self::Cargo => "cargo install prek", |
| 60 | + } |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +#[cfg(test)] |
| 65 | +mod tests { |
| 66 | + use super::*; |
| 67 | + |
| 68 | + #[test] |
| 69 | + fn detects_homebrew_cellar_arm() { |
| 70 | + assert_eq!( |
| 71 | + InstallSource::from_path(Path::new("/opt/homebrew/Cellar/prek/0.3.1/bin/prek")), |
| 72 | + Some(InstallSource::Homebrew) |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + #[test] |
| 77 | + fn detects_homebrew_cellar_intel() { |
| 78 | + assert_eq!( |
| 79 | + InstallSource::from_path(Path::new("/usr/local/Cellar/prek/0.3.1/bin/prek")), |
| 80 | + Some(InstallSource::Homebrew) |
| 81 | + ); |
| 82 | + } |
| 83 | + |
| 84 | + #[test] |
| 85 | + fn detects_cargo_bin_macos() { |
| 86 | + assert_eq!( |
| 87 | + InstallSource::from_path(Path::new("/Users/user/.cargo/bin/prek")), |
| 88 | + Some(InstallSource::Cargo) |
| 89 | + ); |
| 90 | + } |
| 91 | + |
| 92 | + #[test] |
| 93 | + fn detects_cargo_bin_linux() { |
| 94 | + assert_eq!( |
| 95 | + InstallSource::from_path(Path::new("/home/user/.cargo/bin/prek")), |
| 96 | + Some(InstallSource::Cargo) |
| 97 | + ); |
| 98 | + } |
| 99 | + |
| 100 | + #[test] |
| 101 | + fn returns_none_for_unknown_unix_path() { |
| 102 | + assert_eq!( |
| 103 | + InstallSource::from_path(Path::new("/usr/local/bin/prek")), |
| 104 | + None |
| 105 | + ); |
| 106 | + } |
| 107 | + |
| 108 | + #[test] |
| 109 | + fn does_not_match_other_cellar_formula() { |
| 110 | + assert_eq!( |
| 111 | + InstallSource::from_path(Path::new("/opt/homebrew/Cellar/other/0.1.0/bin/prek")), |
| 112 | + None |
| 113 | + ); |
| 114 | + } |
| 115 | + |
| 116 | + #[test] |
| 117 | + #[cfg(windows)] |
| 118 | + fn detects_cargo_bin_windows() { |
| 119 | + assert_eq!( |
| 120 | + InstallSource::from_path(Path::new(r"C:\Users\user\.cargo\bin\prek.exe")), |
| 121 | + Some(InstallSource::Cargo) |
| 122 | + ); |
| 123 | + } |
| 124 | + |
| 125 | + #[test] |
| 126 | + #[cfg(windows)] |
| 127 | + fn returns_none_for_unknown_windows_path() { |
| 128 | + assert_eq!( |
| 129 | + InstallSource::from_path(Path::new(r"C:\Program Files\prek\prek.exe")), |
| 130 | + None |
| 131 | + ); |
| 132 | + } |
| 133 | +} |
0 commit comments