-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Add EnvExists cheatcode #3732
Comments
I can see how having a a workaround for this is something like: try vm.envBool("MYVAR") returns (bool myVar) {
if (myVar) {...};
} catch {
} |
Partly related to foundry-rs/forge-std#226, where we discuss how I've used the try/catch workaround in the past, and try/catch was introduced in solidity 0.6.2, so we can implement that workaround in forge-std too. Perhaps something like |
@mattsse Oh, for some reason I remember try/catch not working for me before, but maybe I'm just confusing it with smth else (I definitely saw But in this case of ENVs - it works! So yes, as @mds1 says - we can go with I just have several questions I want to discuss before I start:
|
sgtm, let's add that |
May I suggest a different implementation? Instead of Example: The existing Just a suggestion that would fit better my use case, feel free to ignore it. I'll be happy if this is implemented in any way. |
this is pretty useful and could be added easily, don't think we'd run into issues with solidity here. But I think |
This sounds amazing! I like that. |
+1 to @alcueca's suggestion, came to this issue to make the same comment in favor of the solution in foundry-rs/forge-std#237 Thanks for implementing @vicnaum! |
Oh wait, I've meant implementing that in Solidity, cause I don't know any Rust (yet). Now I see those are all native vm.cheatcodes (not solidity helpers like the JSON case) - so I won't be able to overload them in Rust, sorry :-/ Anyways, when I looked at what's available, I have concerns about these array with delimiter functions, and especially envString one: foundry/testdata/cheats/Cheats.sol Line 39 in 54ec8e3
It's (string,string) - so it will clash with overloaded |
Good point, what if we call all the new functions here
|
Should we also modify |
Yep! |
We have |
Component
Forge
Describe the feature you would like
Proposing to add a cheatcode like
envExists(string calldata key) returns (bool)
that will returntrue
if the ENV key exists, andfalse
if it doesn't exist.Reasoning:
Right now there is no way to check if a specific ENV variable was set, and the whole script just reverts. This doesn't allow to have optional ENV keys, and requires setting all ENVs explicitly, even if they're optional for some test cases.
Examples:
FORK
key to test on a fork VS testing on a local deployment. Currently theFORK=
ENV variable should be present for the tests to run at all, even if the FORK is not used. Things get complicated if there are many such optional variables.MNEMONIC
orPRIVATE_KEY
variables - there is no sense to have both of them - can use either. But with current behavior they both should be set (one left as an empty string) for the script to not revert.Additional context
No response
The text was updated successfully, but these errors were encountered: