Builtin safe support for setting/clearing env vars?
One thing I've been using rusty-fork for is to test behavior connected to environment variables. (Thanks!) For example:
rusty_fork_test! {
#[test]
fn resolve_auto_not_on_github() {
env::remove_var("GITHUB_ACTION");
assert_eq!(AnnotationKind::Auto.resolve(), AnnotationKind::None);
}
#[test]
fn resolve_auto_github_isolated() {
env::set_var("GITHUB_ACTION", "true");
assert_eq!(AnnotationKind::Auto.resolve(), AnnotationKind::GitHub);
}
}
Rust's env::set_var and remove_var are now unsafe in edition 2024, because the C API fundamentally has no way to avoid races when multiple threads are in play. https://doc.rust-lang.org/std/env/fn.set_var.html On the other hand it is safe to change environment variables when spawning a child process through https://doc.rust-lang.org/nightly/std/process/struct.Command.html#method.env.
Although it's quite possible to have the child code make an unsafe call, and although this will likely be safe in practice as it should have a single thread, it would be nice to avoid the need to have the unsafe code.
I wonder what you think about adding some option to rusty_fork_test! to specify that some variables are set or erased, and then using that when spawning the child?