Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A flag to unset all pre-existing variables #24

Open
tabokie opened this issue Aug 14, 2024 · 4 comments
Open

A flag to unset all pre-existing variables #24

tabokie opened this issue Aug 14, 2024 · 4 comments

Comments

@tabokie
Copy link

tabokie commented Aug 14, 2024

It would make tests more reproducible.

@vmx
Copy link
Owner

vmx commented Aug 14, 2024

That's a good idea. Though I don't think we need a new flag, this can be done already today with something along the lines of:

let vars_to_unset = std::env::vars().map(|(key, _value)| (key, None)).collect();
temp_env::with_vars(
    [
        vars_to_unset,
        vec![("MY_VAR".to_string(), Some("my_value"))],
    ]
    .concat(),
    || {
        println!("vmx: env without pre-existing vars: {:?}", std::env::vars());
    },
);

Does that solve your problem?

@tabokie
Copy link
Author

tabokie commented Aug 24, 2024

Calling std::env::vars() directly will again be interfered by other test cases. E.g. test A unsets some variables, test B collects vars_to_unset missing those variables, then test A finishes and test B enters temp_env.

But I agree it'll work if everyone is purely adding additional variables instead of unsetting some.

@vmx
Copy link
Owner

vmx commented Aug 28, 2024

Calling std::env::vars() directly will again be interfered by other test cases. E.g. test A unsets some variables, test B collects vars_to_unset missing those variables, then test A finishes and test B enters temp_env.

If I understand it correctly, you would set the vars_to_unset once (with a OnceCell` or so) and the other tests would re-use them.

@tabokie
Copy link
Author

tabokie commented Aug 29, 2024

But that would force every tests to run the setup even if they don't necessarily need a clean environment. Any one test missing the setup && unset some variables in temp_env will cause the problem I described earlier.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants