Running Pants 2.16.0 on Ubuntu 22.04. I have a tes...
# general
a
Running Pants 2.16.0 on Ubuntu 22.04. I have a test that sets an environment variable using
os.environ["VAR_NAME"] = "some value"
. This isn't reflected when the test is ran, even when
VAR_NAME
is included without a value in
extra_env_vars
in the BUILD file for the tests. Is this intended behavior?
1
l
Morten, where is the
os.environ["VAR_NAME"] = "some value"
occurring? Could you provide some more details? If you are running a python process and it sets the os.environ and then reads it later, I can't imagine that pants can get in the way of that.
a
Copy code
def test_example():
    os.environ["VAR_NAME"] = "<https://test.com>"
    settings = Settings()
    assert settings.enable_sink is True
Here is the test. The build file looks like this:
Copy code
python_tests(
    name="tests",
    extra_env_vars=[
        "VAR_NAME",
    ]
)
l
Isn't that setting so you can transmit VAR_NAME from the caller into the test. Like
Copy code
def test_example():
   assert os.environ['VAR_NAME'] == 'good'
and you can run
$ VAR_NAME=good pants test path/to/the/test.py
Where is the issue happening. Does
Settings()
read the
os.environ
?
I don't think that pants prevents the tests from setting any environment variable that it wants, but I may be mistaken ...
a
Yes, the call to
Settings()
which internally calls
pydantic_settings.BaseSettings
reads the environment variables 🙂
Could be something unrelated to Pants, I'll try running it with just pytest.
l
Yes. It is possible that on import pydantic_settings has already read the os.environ and doesn't observe you updating it.
+1 to running it with plain pytest.
You can also see for yourself by reading os.environ in the test in pants that it does get updated there.
You can also run the debugging functionality and it should jump out. https://www.pantsbuild.org/docs/python-test-goal#debugging-tests
a
Jeez, it was 100% my own fault. I was running
settings = Settings()
in the file I was importing from. This means that when I ran my test this would be run twice, the first time before my monkeypatch had a chance to take effect, and then again in my test, now with patched values.
Thanks for the swift help, Gautham!
(Once again proving to me that this type of singleton is the root of all evil)
l
Man, np, this happens all the time. Def done it myself
👍 1