-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvariables.rs
188 lines (163 loc) · 5.78 KB
/
variables.rs
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
use std::{env, io::Write};
use crate::models::ErrorKind;
/// List of variables
type VariablesList = Vec<(String, String)>;
/// Print all environment variables
pub fn print_env<W: Write>(format: &str, mut buffer: W) {
for (key, value) in get_variables() {
let entry = format.replace("{name}", &key).replace("{value}", &value);
writeln!(buffer, "{}", entry).expect("Failed to write to buffer");
}
}
/// Get list of environment variables with values
pub fn get_variables() -> VariablesList {
env::vars().collect()
}
/// Set variable with given key and value
pub fn set_variable(key: &str, value: &str, global: bool) -> Result<(), ErrorKind> {
if global {
if let Err(err) = globalenv::set_var(key, value) {
return Err(ErrorKind::CannotSetVariableGlobally(err.to_string()));
}
} else {
unsafe { env::set_var(key, value) };
}
Ok(())
}
/// Delete variable with given name
pub fn delete_variable(name: String, global: bool) -> Result<(), ErrorKind> {
if global {
if let Err(err) = globalenv::unset_var(&name) {
return Err(ErrorKind::CannotDeleteVariableGlobally(err.to_string()));
}
} else {
unsafe { env::remove_var(&name) };
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use std::env;
#[test]
fn test_get_variables_list() {
unsafe { env::set_var("TEST_GET_VARIABLES", "test_value") };
let list = get_variables();
assert!(list.contains(&("TEST_GET_VARIABLES".to_string(), "test_value".to_string())));
unsafe { env::remove_var("TEST_GET_VARIABLES") };
}
#[test]
fn test_set_variable_simple() {
let result = set_variable("TEST_VAR", "test_value", false);
assert!(result.is_ok());
assert_eq!(env::var("TEST_VAR").unwrap(), "test_value");
unsafe { env::remove_var("TEST_VAR") };
}
#[test]
fn test_print_env() {
unsafe { env::set_var("TEST_PRINT_VAR", "test_value") };
let mut buffer = vec![];
print_env("{name} = \"{value}\"", &mut buffer);
assert!(
String::from_utf8(buffer)
.unwrap()
.contains("TEST_PRINT_VAR = \"test_value\"")
);
unsafe { env::remove_var("TEST_PRINT_VAR") };
}
#[test]
fn test_delete_variable() {
unsafe { env::set_var("TEST_DELETE_VAR", "test_value") };
let result = delete_variable("TEST_DELETE_VAR".to_string(), false);
assert!(result.is_ok());
assert!(env::var("TEST_DELETE_VAR").is_err());
}
#[test]
fn test_set_variable_empty_value() {
let result = set_variable("TEST_EMPTY_VAR", "", false);
assert!(result.is_ok());
assert_eq!(env::var("TEST_EMPTY_VAR").unwrap(), "");
unsafe { env::remove_var("TEST_EMPTY_VAR") };
}
#[test]
fn test_print_env_format() {
// Set up test environment variables
unsafe { env::set_var("TEST_VAR_1", "value1") };
unsafe { env::set_var("TEST_VAR_2", "value2") };
let mut buffer = vec![];
print_env("{name} = \"{value}\"", &mut buffer);
assert!(
String::from_utf8(buffer.clone())
.unwrap()
.contains("TEST_VAR_1 = \"value1\"")
);
assert!(
String::from_utf8(buffer)
.unwrap()
.contains("TEST_VAR_2 = \"value2\"")
);
// Clean up
unsafe { env::remove_var("TEST_VAR_1") };
unsafe { env::remove_var("TEST_VAR_2") };
}
#[test]
fn test_print_env_empty_value() {
unsafe { env::set_var("TEST_EMPTY", "") };
let mut buffer = vec![];
print_env("{name} = \"{value}\"", &mut buffer);
assert!(
String::from_utf8(buffer)
.unwrap()
.contains("TEST_EMPTY = \"\"")
);
unsafe { env::remove_var("TEST_EMPTY") };
}
#[test]
fn test_print_env_special_characters() {
unsafe { env::set_var("TEST_SPECIAL", "value with spaces and $#@!") };
let mut buffer = vec![];
print_env("{name} = \"{value}\"", &mut buffer);
assert!(
String::from_utf8(buffer)
.unwrap()
.contains("TEST_SPECIAL = \"value with spaces and $#@!\"")
);
unsafe { env::remove_var("TEST_SPECIAL") };
}
#[test]
fn test_set_variable_global() {
let result = set_variable("TEST_GLOBAL_VAR", "test_value", true);
match result {
Ok(_) => {
assert_eq!(env::var("TEST_GLOBAL_VAR").unwrap(), "test_value");
delete_variable("TEST_GLOBAL_VAR".to_string(), true).unwrap();
}
Err(ErrorKind::CannotSetVariableGlobally(_)) => {
// Test passes if we get permission error on non-admin run
}
Err(e) => panic!("Unexpected error: {:?}", e),
}
}
#[test]
fn test_delete_variable_global() {
// First try to set a global variable
let set_result = set_variable("TEST_GLOBAL_DELETE", "test_value", true);
// Only test deletion if we could set the variable (i.e., we have admin rights)
if set_result.is_ok() {
let result = delete_variable("TEST_GLOBAL_DELETE".to_string(), true);
assert!(result.is_ok());
assert!(env::var("TEST_GLOBAL_DELETE").is_err());
}
}
#[test]
fn test_delete_nonexistent_variable_global() {
let result = delete_variable("NONEXISTENT_GLOBAL_VAR".to_string(), true);
match result {
Ok(_) => {}
Err(ErrorKind::CannotDeleteVariableGlobally(_)) => {
// Test passes if we get permission error on non-admin run
}
Err(e) => panic!("Unexpected error: {:?}", e),
}
}
}