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

feat: add envExists cheatcode to check if a environment variable exists #7744

Merged
merged 1 commit into from
Apr 20, 2024

Conversation

0xtekgrinder
Copy link
Contributor

Motivation

Inside some of my tests I need to have a pretty advanced environment configuration with potential forks yet I cannot just do it in a clean way as if I load a environment variable that doesn't exists it will reverts. So currently I am using a vm.ffi to bypass this problem and use bash instead to check if the environment variable exists.

Solution

Add a cheatcode that can tell if a environment variable exists or not

Copy link
Member

@DaniPopes DaniPopes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense! cc @mattsse @klkvr @mds1

impl Cheatcode for envExistsCall {
fn apply(&self, _state: &mut Cheatcodes) -> Result {
let Self { name } = self;
Ok(env::var(name).is_ok().abi_encode())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that env::var returns Err if it exists but is not valid unicode (see fn get_env). This is probably fine since we error on that as well in getEnv

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should I handle this error to still returns true or not then ?

Copy link
Member

@mattsse mattsse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reasonable

needs forge-std followup foundry-rs/forge-std#545

@mattsse mattsse merged commit db74e6e into foundry-rs:master Apr 20, 2024
19 checks passed
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

Successfully merging this pull request may close these issues.

3 participants