Skip to content

Commit df5dbb9

Browse files
tests: fix environment not being replaced soon enough (#126)
* tests: fix environment not being replaced soon enough * fix(tests): only clear modmail specific environment variables * chore: remove unneccessary bug comment Co-authored-by: Shivansh-007 <[email protected]> Co-authored-by: Shivansh-007 <[email protected]>
1 parent 8ba34ba commit df5dbb9

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

tests/modmail/conftest.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
_ORIG_ENVIRON = None
1010

11+
_modmail_env_prefix = "MODMAIL_"
12+
1113

1214
def pytest_report_header(config) -> str:
1315
"""Pytest headers."""
@@ -26,17 +28,35 @@ def _get_env():
2628
return pathlib.Path(__file__).parent / "test.env"
2729

2830

31+
def _get_env_vars() -> dict:
32+
result = {}
33+
for key, value in os.environ.items():
34+
# not using upper() since the config system is case sensitive
35+
if key.startswith(_modmail_env_prefix):
36+
result[key] = value
37+
return result
38+
39+
2940
def pytest_configure():
3041
"""Load the test specific environment file, exit if it does not exist."""
42+
global _ORIG_ENVIRON
3143
env = _get_env()
3244
if not env.is_file():
3345
pytest.exit(f"Testing specific {env} does not exist. Cancelling test run.", 2)
34-
os.environ.clear()
46+
_ORIG_ENVIRON = _get_env_vars()
47+
for key in _ORIG_ENVIRON:
48+
del os.environ[key]
49+
3550
dotenv.load_dotenv(_get_env(), override=True)
3651

3752

3853
def pytest_unconfigure():
3954
"""Reset os.environ to the original environment before the run."""
40-
if _ORIG_ENVIRON is not None:
41-
os.environ.clear()
42-
os.environ.update(**_ORIG_ENVIRON)
55+
global _ORIG_ENVIRON
56+
if _ORIG_ENVIRON is None:
57+
return
58+
59+
for key in _get_env_vars():
60+
del os.environ[key]
61+
os.environ.update(**_ORIG_ENVIRON)
62+
_ORIG_ENVIRON = None

0 commit comments

Comments
 (0)