foundry icon indicating copy to clipboard operation
foundry copied to clipboard

Isolating setEnv to a single test case

Open kopy-kat opened this issue 1 year ago • 2 comments

Component

Forge

Describe the feature you would like

Related to https://github.com/foundry-rs/foundry/issues/2349 which was closed as won't fix. The main problem is around setEnv setting the env variable for the entire test suite currently running. While this makes sense in some cases (eg forks) it doesnt make sense in others. Specifically, here's an example of a problem I'm running into:

We have a testing harness that abstracts some testing away from the dev (in this case account abstraction related). As part of this harness, there are some env flags a user can set in order to modify test behavior or print a gas report. However, it is basically not possible to write unit tests for this harness, since if a single test sets the env var then all tests will be executed with that var set.

Additional context

Example (this will revert on some runs):

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.23;

import { Test } from "forge-std/Test.sol";

contract EnvFoundryTest is Test {
    function testEnv() public {
        vm.setEnv("SIMULATE", "true");
        assertEq(vm.envOr("SIMULATE", false), true);

        vm.setEnv("SIMULATE", "false");
        assertEq(vm.envOr("SIMULATE", false), false);
    }

    function testEnv2() public {
        assertEq(vm.envOr("SIMULATE", false), false);
    }
}

kopy-kat avatar Feb 02 '24 15:02 kopy-kat

tbh I dont have a specific feature request but wanted to find out whether this is still a wont fix or whether there could be a possible feature that helps with this issue

kopy-kat avatar Feb 02 '24 15:02 kopy-kat

tbh I don't have a specific feature request but wanted to find out whether this is still a wont fix or whether there could be a possible feature that helps with this issue

simple create a separate cheatcode to isolate env to each test case, maybe call setEnvAll and the other setEnv

malik672 avatar Feb 03 '24 13:02 malik672

Hi @kopy-kat,

Since it has been a while since this ticket was opened I'm curious whether you've been able to find a workaround?

The requested feature makes sense to me but this seems like an inherent limitation to environment variables being flat and applied globally

It is also not practical to introduce an entirely new set of "local" environment variables with cheatcodes: https://github.com/foundry-rs/forge-std/blob/07263d193d621c4b2b0ce8b4d54af58f6957d97d/src/Vm.sol#L246-L402 or modify the existing behavior of environment variables.

cc @mattsse per your comment: https://github.com/foundry-rs/foundry/issues/2349#issuecomment-1204099138 is this still a won't fix?

zerosnacks avatar Jul 10 '24 15:07 zerosnacks

I was looking for this feature in the context of building a foundry-based dev kit and using env vars to run tests in certain modes. The workaround is using both env vars and storage vars to also be able to just run any specific test in a certain mode. Tbh the workaround is not that bad its just not very clean.

kopy-kat avatar Jul 12 '24 21:07 kopy-kat

I think this is something we can reconsider when thinking about integration points for extensions / plugins for Foundry https://github.com/foundry-rs/foundry/issues/8266

For now however I'll mark it as not planned and close the ticket as there is no immediate actionable point

Again thanks for your suggestion, if you have any ideas / requirements regarding extensions feel free to comment on that ticket

zerosnacks avatar Jul 12 '24 21:07 zerosnacks