Skip to content

Commit

Permalink
tests: fix environment not being replaced soon enough
Browse files Browse the repository at this point in the history
  • Loading branch information
onerandomusername committed Nov 21, 2021
1 parent dd2f16c commit 03fd57f
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions tests/modmail/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import pytest


_ORIG_ENVIRON = None


def pytest_report_header(config) -> str:
"""Pytest headers."""
return "package: modmail"
Expand All @@ -24,16 +27,16 @@ def _get_env():


def pytest_configure():
"""Check that the test specific env file exists, and cancel the run if it does not exist."""
"""Load the test specific environment file, exit if it does not exist."""
env = _get_env()
if not env.is_file():
pytest.exit(f"Testing specific {env} does not exist. Cancelling test run.", 2)
os.environ.clear()
dotenv.load_dotenv(_get_env(), override=True)


@pytest.fixture(autouse=True, scope="package")
def standardize_environment():
"""Clear environment variables except for the test.env file."""
env = _get_env()
with unittest.mock.patch.dict(os.environ, clear=True):
dotenv.load_dotenv(env)
yield
def pytest_unconfigure():
"""Reset os.environ to the original environment before the run."""
if _ORIG_ENVIRON is not None:
os.environ.clear()
os.environ.update(**_ORIG_ENVIRON)

0 comments on commit 03fd57f

Please sign in to comment.