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
/* //! Helper functions for testing the zfish library
use std::io::{self, Read, Write};
use std::env;
/// Temporarily set an environment variable for the duration of a function
pub fn with_env_var<F, T>(name: &str, value: Option<&str>, f: F) -> T
where
F: FnOnce() -> T,
{
let original = env::var(name).ok();
match value {
Some(val) => unsafe { env::set_var(name, val) },
None => unsafe { env::remove_var(name) },
};
let result = f();
match original {
Some(val) => unsafe { env::set_var(name, val) },
None => unsafe { env::remove_var(name) },
}
result
}
/// Capture stdout during a function's execution
pub fn capture_stdout<F>(f: F) -> io::Result<String>
where
F: FnOnce() -> io::Result<()>
{
// This is a simplified stub - in real code, you would need to
// redirect stdout which is more complex and platform-specific
// For now, we just return a placeholder
f()?;
Ok("Output captured (placeholder)".to_string())
}
use zfish::style::{Color, Style};
#[test]
fn test_basic_coloring() {
let expected_with_color = "\x1b[32mSuccess\x1b[0m";
let expected_without_color = "Success";
// Force color support off
helpers::with_env_var("NO_COLOR", Some("1"), || {
let green_text = Color::Green.paint("Success");
assert_eq!(format!("{}", green_text), expected_without_color);
});
// Force color support on (remove NO_COLOR, set COLORTERM)
helpers::with_env_var("NO_COLOR", None, || {
helpers::with_env_var("COLORTERM", Some("1"), || {
let green_text = Color::Green.paint("Success");
assert_eq!(format!("{}", green_text), expected_with_color);
});
});
}
#[test]
fn test_styling_combinations() {
let expected = "\x1b[31;1mError\x1b[0m";
helpers::with_env_var("COLORTERM", Some("1"), || {
helpers::with_env_var("NO_COLOR", None, || {
let styled_text = Color::Red.paint("Error").style(Style::Bold);
assert_eq!(format!("{}", styled_text), expected);
});
});
} */