Skip to content

Commit 35e3a15

Browse files
fix: use OsString for EnvGuard and add remove() constructor
Addresses PR review feedback: - Store previous env var as OsString (via env::var_os) to correctly handle non-UTF-8 values instead of silently dropping them. - Add EnvGuard::remove() for tests that need to ensure a variable is unset, so test_envsubst_missing also gets proper cleanup. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent b3a030c commit 35e3a15

1 file changed

Lines changed: 14 additions & 3 deletions

File tree

src/render.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,20 +77,31 @@ pub fn template_render(input: &str) -> Result<String, String> {
7777
mod tests {
7878
use super::*;
7979

80+
use std::ffi::OsString;
81+
8082
struct EnvGuard {
8183
name: String,
82-
previous: Option<String>,
84+
previous: Option<OsString>,
8385
}
8486

8587
impl EnvGuard {
8688
fn set(name: &str, value: &str) -> Self {
87-
let previous = env::var(name).ok();
89+
let previous = env::var_os(name);
8890
env::set_var(name, value);
8991
Self {
9092
name: name.to_string(),
9193
previous,
9294
}
9395
}
96+
97+
fn remove(name: &str) -> Self {
98+
let previous = env::var_os(name);
99+
env::remove_var(name);
100+
Self {
101+
name: name.to_string(),
102+
previous,
103+
}
104+
}
94105
}
95106

96107
impl Drop for EnvGuard {
@@ -110,7 +121,7 @@ mod tests {
110121
}
111122
#[test]
112123
fn test_envsubst_missing() {
113-
env::remove_var("MISSING_VAR_XYZ");
124+
let _g = EnvGuard::remove("MISSING_VAR_XYZ");
114125
assert_eq!(envsubst("${MISSING_VAR_XYZ}"), "${MISSING_VAR_XYZ}");
115126
}
116127
#[test]

0 commit comments

Comments
 (0)