Is there a way to inspect the dependencies of a ta...
# general
e
Is there a way to inspect the dependencies of a target while creating the target? I have this following applied to all my tests:
Copy code
__defaults__(
    {
        "python_tests": dict(
            extra_env_vars=parametrize(
                A_B=("FEATURE_A=true", "FEATURE_B=true"),
                A_noB=("FEATURE_A=true", "FEATURE_B=false"),
                noA_B=("FEATURE_A=false", "FEATURE_B=true"),
                noA_noB=("FEATURE_A=false", "FEATURE_B=false"),
            ),
        ),
    },
)
Because I want to run my tests against all the different combinations of feature flags. (Some tests are skipped based on the feature flags, some test alternate behaviours, and some are just to make sure that the code under test doesn't do anything different if feature flags are enabled). However, there is plenty of code where the feature flags don't matter. In fact, there are lots of files that do not even have `src/python/config/feature_flags.py`(single place where all env vars are imported into the application code) as a transitive dependency. Since they cannot be affected by feature flags, there is no need to run them multiple times. Is there any way I can do something like:
extra_env_vars=parametrize(...) if "//src/python/config/feature_flags.py" in target.dependencies else None
s
short answer is no. Dependency inference happens after all the targets are collected, and might be affected by the target values, so you can't look at the dependencies in your BUILD files
why are you trying to do this in pants and not in pytest fixtures?
you can parametrize your fixtures instead:
Copy code
import pytest


@pytest.fixture(params=[True, False], ids=["A", "noA"])
def feature_a(request):
    return request.param


@pytest.fixture(params=[True, False], ids=["B", "noB"])
def feature_b(request):
    return request.param


def test_something(feature_a, feature_b):
    assert True
Copy code
test_something.py::test_something[A-B] PASSED    
test_something.py::test_something[A-noB] PASSED  
test_something.py::test_something[noA-B] PASSED  
test_something.py::test_something[noA-noB] PASSED
e
short answer: 2 reasons: 1. Doing this in pytest requires being explicit about which tests need to check on the feature flags. Its great for the "alternative behaviour" tests, but its not too helpful for making sure no accidental regressions are introduced by inserting a feature flag somewhere we wouldn't expect. Theoretically, I'd like all tests run for each FF variant, and only skip those that can be explicitly proven (eg. by dep inference) to not require it. 2. We actually use our feature flags to configure api responses (FastAPI), by changing the definitions of some schema classes. This means we pretty much have to run tests of different feature flag assignments in different pytest invocations (else the modules would need to be reloaded to force the classes to be redefined under the new feature flagset)
s
1. Is it? You can enable autouse for parametrized fixtures 2. Can you change the models so that they use an enum? You can then run the requests against the same server
2. Or just spin up a second server with a different flag enabled
Copy code
import pytest


@pytest.fixture(params=[True, False], ids=["A", "noA"], autouse=True)
def feature_a(request):
    return request.param


@pytest.fixture(params=[True, False], ids=["B", "noB"], autouse=True)
def feature_b(request):
    return request.param


def test_something():
    assert True
Copy code
some_test.py::test_something[A-B] PASSED    
some_test.py::test_something[A-noB] PASSED  
some_test.py::test_something[noA-B] PASSED  
some_test.py::test_something[noA-noB] PASSED