use std::env;
use std::path::PathBuf;
pub fn expand_path(path: &str) -> PathBuf {
let expanded = shellexpand::tilde(path);
PathBuf::from(expanded.to_string())
}
pub fn get_path_entries() -> Vec<PathBuf> {
env::var_os("PATH")
.map(|paths| env::split_paths(&paths).collect())
.unwrap_or_default()
}
pub fn set_path_entries(entries: &[PathBuf]) {
if let Ok(new_path) = env::join_paths(entries) {
env::set_var("PATH", new_path);
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::commands::validator::is_valid_path_entry;
use std::env;
use tempfile::TempDir;
#[test]
fn test_expand_path() {
let home = dirs_next::home_dir().unwrap();
let expanded = expand_path("~/test");
assert_eq!(expanded, home.join("test"));
}
#[test]
fn test_is_valid_path_entry() {
let temp_dir = TempDir::new().unwrap();
assert!(is_valid_path_entry(temp_dir.path()));
let non_existent = temp_dir.path().join("non_existent");
assert!(!is_valid_path_entry(&non_existent));
}
#[test]
fn test_get_set_path_entries() {
let original_path = env::var("PATH").ok();
let test_paths = vec![PathBuf::from("/test/path1"), PathBuf::from("/test/path2")];
set_path_entries(&test_paths);
let current_paths = get_path_entries();
assert_eq!(current_paths, test_paths);
if let Some(path) = original_path {
env::set_var("PATH", path);
}
}
}