Is there a way to just pass all environment variab...
# general
t
Is there a way to just pass all environment variables to a test? https://www.pantsbuild.org/docs/python-test-goal#setting-environment-variables Aside the fact that it seems not recommended
As far as I understand I would need to set the envs globally in
pants.toml
with
extra_env_vars
under
[test]
. These will be passed to all tests in the monorepo. If I define
extra_env_vars
in a specfic
python_tests
target, they will be merged. Can I achieve the following? Pass a specific set on envs to a specific app in the monorepo, e.g.
serviceA
and then for individual tests add some envs to the generally defined ones for the app.
h
Hmm. When you say "pass to an app", do you mean when running the app with
./pants run
? Or when running "the app's tests" (however we define those)?
That's an interesting one
t
As far as I can tell all envs will be pass to
./pants run
anyway. I mean
./pants test serviceA::
. Let us assume all tests need the envs
DB_HOST
and
DB_PORT
, but a specific test also needs the env
BUCKET_NAME
. Can this be addressed somehow?
h
As you suggested, if you put `DB_HOST`/`DB_PORT` in
extra_env_vars
they will go to all tests, and adding
BUCKET_NAME
to
extra_env_vars
on a specific test target will add it for the tests on that target.
If you want to add for a specific test file within that target, you can use
overrides
One sec let me dig up an example
Copy code
python_tests(
    name="serviceA_tests",
    overrides={
        "specific_test.py": {
            "extra_env_vars": ["BUCKET_NAME"]
        }
    },
)
But this still has you adding `DB_HOST`/`DB_PORT` to every test in the repo, which it sounds like is not what you want?
To do this in a more fine-grained way, you can do:
Copy code
python_tests(
    name="serviceA_tests",
    extra_env_vars=["DB_HOST", "DB_PORT"],
    overrides={
        "specific_test.py": {
            "extra_env_vars": ["DB_HOST", "DB_PORT","BUCKET_NAME"]
        }
    },
)
If you have multiple test targets under serviceA you could probably streamline this with a macro
t
Yeah
overrides
does not seem to address the issue. I was trying the new
__defaults__
feature, but there is no way to add an env to a specific test while keeping all default values. Or am I missing something there?
h
There is not AFAIK, you'd have to repeat the default vars in the
overrides
As per my example above
But possibly a macro can streamline this
👀 1