use std::collections::HashMap;
use std::env;
pub fn var(key: &str) -> Result<String, String> {
env::var(key).map_err(|e| e.to_string())
}
pub fn set_var(key: &str, value: &str) -> Result<(), String> {
env::set_var(key, value);
Ok(())
}
pub fn remove_var(key: &str) -> Result<(), String> {
env::remove_var(key);
Ok(())
}
pub fn vars() -> Result<HashMap<String, String>, String> {
Ok(env::vars().collect())
}
pub fn current_dir() -> Result<String, String> {
env::current_dir()
.map(|p| p.to_string_lossy().to_string())
.map_err(|e| e.to_string())
}
pub fn set_current_dir(path: &str) -> Result<(), String> {
env::set_current_dir(path).map_err(|e| e.to_string())
}
pub fn args() -> Result<Vec<String>, String> {
Ok(env::args().collect())
}
pub fn temp_dir() -> Result<String, String> {
Ok(env::temp_dir().to_string_lossy().to_string())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_var_existing() {
assert!(var("PATH").is_ok());
let path = var("PATH").expect("operation should succeed in test");
assert!(!path.is_empty());
}
#[test]
fn test_var_nonexistent() {
assert!(var("RUCHY_TEST_NONEXISTENT_VAR_XYZ").is_err());
}
#[test]
fn test_set_and_get_var() {
set_var("RUCHY_TEST_VAR", "test_value").expect("operation should succeed in test");
assert_eq!(
var("RUCHY_TEST_VAR").expect("operation should succeed in test"),
"test_value"
);
remove_var("RUCHY_TEST_VAR").expect("operation should succeed in test");
}
#[test]
fn test_set_var_overwrite() {
set_var("RUCHY_TEST_VAR2", "value1").expect("operation should succeed in test");
assert_eq!(
var("RUCHY_TEST_VAR2").expect("operation should succeed in test"),
"value1"
);
set_var("RUCHY_TEST_VAR2", "value2").expect("operation should succeed in test");
assert_eq!(
var("RUCHY_TEST_VAR2").expect("operation should succeed in test"),
"value2"
);
remove_var("RUCHY_TEST_VAR2").expect("operation should succeed in test");
}
#[test]
fn test_remove_var() {
set_var("RUCHY_TEST_VAR3", "value").expect("operation should succeed in test");
assert!(var("RUCHY_TEST_VAR3").is_ok());
remove_var("RUCHY_TEST_VAR3").expect("operation should succeed in test");
assert!(var("RUCHY_TEST_VAR3").is_err());
}
#[test]
fn test_remove_nonexistent_var() {
assert!(remove_var("RUCHY_TEST_NONEXISTENT").is_ok());
}
#[test]
fn test_vars() {
let all_vars = vars().expect("operation should succeed in test");
assert!(!all_vars.is_empty());
assert!(all_vars.contains_key("PATH"));
}
#[test]
fn test_vars_includes_set() {
set_var("RUCHY_TEST_VAR4", "test").expect("operation should succeed in test");
let all_vars = vars().expect("operation should succeed in test");
assert_eq!(all_vars.get("RUCHY_TEST_VAR4"), Some(&"test".to_string()));
remove_var("RUCHY_TEST_VAR4").expect("operation should succeed in test");
}
#[test]
fn test_current_dir() {
let dir = current_dir().expect("operation should succeed in test");
assert!(!dir.is_empty());
assert!(std::path::Path::new(&dir).exists());
}
#[test]
fn test_args() {
let args_list = args().expect("operation should succeed in test");
assert!(!args_list.is_empty()); }
#[test]
fn test_temp_dir() {
let temp = temp_dir().expect("operation should succeed in test");
assert!(!temp.is_empty());
assert!(std::path::Path::new(&temp).exists());
}
#[test]
fn test_env_workflow() {
let key = "RUCHY_WORKFLOW_TEST";
set_var(key, "value1").expect("operation should succeed in test");
assert_eq!(
var(key).expect("operation should succeed in test"),
"value1"
);
set_var(key, "value2").expect("operation should succeed in test");
assert_eq!(
var(key).expect("operation should succeed in test"),
"value2"
);
let all_vars = vars().expect("operation should succeed in test");
assert_eq!(all_vars.get(key), Some(&"value2".to_string()));
remove_var(key).expect("operation should succeed in test");
assert!(var(key).is_err());
}
#[test]
fn test_special_characters_in_values() {
let key = "RUCHY_SPECIAL_TEST";
set_var(key, "value with spaces").expect("operation should succeed in test");
assert_eq!(
var(key).expect("operation should succeed in test"),
"value with spaces"
);
set_var(key, "value=with=equals").expect("operation should succeed in test");
assert_eq!(
var(key).expect("operation should succeed in test"),
"value=with=equals"
);
set_var(key, "value:with:colons").expect("operation should succeed in test");
assert_eq!(
var(key).expect("operation should succeed in test"),
"value:with:colons"
);
remove_var(key).expect("operation should succeed in test");
}
#[test]
fn test_empty_value() {
let key = "RUCHY_EMPTY_TEST";
set_var(key, "").expect("operation should succeed in test");
assert_eq!(var(key).expect("operation should succeed in test"), "");
remove_var(key).expect("operation should succeed in test");
}
}