Skip to content

Builtin safe support for setting/clearing env vars? #16

Description

@sourcefrog

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?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions