-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathenvironment_locations.rs
More file actions
108 lines (93 loc) · 3.45 KB
/
environment_locations.rs
File metadata and controls
108 lines (93 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
use crate::env_variables::EnvVariables;
use lazy_static::lazy_static;
use regex::Regex;
use std::path::PathBuf;
lazy_static! {
static ref PYTHON_VERSION: Regex =
Regex::new(r"/(\d+\.\d+\.\d+)/").expect("error parsing Version regex for Homebrew");
}
// fn get_homebrew_prefix_env_var(env_vars: &EnvVariables) -> Option<PathBuf> {
// if let Some(homebrew_prefix) = &env_vars.homebrew_prefix {
// let homebrew_prefix_bin = PathBuf::from(homebrew_prefix).join("bin");
// if fs::metadata(&homebrew_prefix_bin).is_ok() {
// return Some(homebrew_prefix_bin);
// }
// }
// None
// }
pub fn get_homebrew_prefix_bin(env_vars: &EnvVariables) -> Vec<PathBuf> {
// Homebrew install folders documented here https://docs.brew.sh/Installation
// /opt/homebrew for Apple Silicon,
// /usr/local for macOS Intel
// /home/linuxbrew/.linuxbrew for Linux
// If user has rosetta enabled, then its possible we have homebrew installed via rosetta as well as apple silicon
// I.e. we can have multiple home brews on the same machine, hence search all,
let mut homebrew_prefixes = [
"/home/linuxbrew/.linuxbrew/bin",
"/opt/homebrew/bin",
"/usr/local/bin",
]
.iter()
.map(PathBuf::from)
.filter(|p| p.exists())
.collect::<Vec<PathBuf>>();
// Check the environment variables
if let Some(homebrew_prefix) = &env_vars.homebrew_prefix {
let homebrew_prefix_bin = PathBuf::from(homebrew_prefix).join("bin");
if homebrew_prefix_bin.exists() && !homebrew_prefixes.contains(&homebrew_prefix_bin) {
homebrew_prefixes.push(homebrew_prefix_bin);
}
}
homebrew_prefixes
}
#[cfg(test)]
mod tests {
use super::*;
use std::{
fs,
time::{SystemTime, UNIX_EPOCH},
};
fn create_unique_prefix(name: &str) -> PathBuf {
let unique = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_nanos();
std::env::temp_dir().join(format!(
"pet-homebrew-{name}-{}-{unique}",
std::process::id()
))
}
#[test]
fn homebrew_prefix_bin_uses_existing_homebrew_prefix_env_var() {
let homebrew_prefix = create_unique_prefix("prefix");
let homebrew_bin = homebrew_prefix.join("bin");
fs::create_dir_all(&homebrew_bin).unwrap();
let env_vars = EnvVariables {
home: None,
root: None,
path: None,
homebrew_prefix: Some(homebrew_prefix.to_string_lossy().to_string()),
known_global_search_locations: vec![],
};
let prefix_bins = get_homebrew_prefix_bin(&env_vars);
assert!(prefix_bins.contains(&homebrew_bin));
fs::remove_dir_all(homebrew_prefix).unwrap();
}
#[test]
fn homebrew_prefix_bin_ignores_missing_homebrew_prefix_env_var() {
let missing_homebrew_prefix = create_unique_prefix("missing-prefix");
let env_vars = EnvVariables {
home: None,
root: None,
path: None,
homebrew_prefix: Some(missing_homebrew_prefix.to_string_lossy().to_string()),
known_global_search_locations: vec![],
};
let prefix_bins = get_homebrew_prefix_bin(&env_vars);
assert!(!prefix_bins
.iter()
.any(|path| path == &missing_homebrew_prefix.join("bin")));
}
}