conftest: cleanup environment cleaner (#31486)

* clean env

* no self
pull/31488/head
Justin Newberry 1 year ago committed by GitHub
parent 663f7017f2
commit 0cb206cb95
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 47
      conftest.py

@ -1,3 +1,4 @@
import contextlib
import gc import gc
import os import os
import pytest import pytest
@ -25,42 +26,42 @@ def pytest_runtest_call(item):
yield yield
@pytest.fixture(scope="function", autouse=True) @contextlib.contextmanager
def openpilot_function_fixture(request): def clean_env():
starting_env = dict(os.environ) starting_env = dict(os.environ)
yield
os.environ.clear()
os.environ.update(starting_env)
random.seed(0)
# setup a clean environment for each test @pytest.fixture(scope="function", autouse=True)
with OpenpilotPrefix(shared_download_cache=request.node.get_closest_marker("shared_download_cache") is not None) as prefix: def openpilot_function_fixture(request):
prefix = os.environ["OPENPILOT_PREFIX"] random.seed(0)
yield with clean_env():
# setup a clean environment for each test
with OpenpilotPrefix(shared_download_cache=request.node.get_closest_marker("shared_download_cache") is not None) as prefix:
prefix = os.environ["OPENPILOT_PREFIX"]
# ensure the test doesn't change the prefix yield
assert "OPENPILOT_PREFIX" in os.environ and prefix == os.environ["OPENPILOT_PREFIX"]
os.environ.clear() # ensure the test doesn't change the prefix
os.environ.update(starting_env) assert "OPENPILOT_PREFIX" in os.environ and prefix == os.environ["OPENPILOT_PREFIX"]
# cleanup any started processes # cleanup any started processes
manager.manager_cleanup() manager.manager_cleanup()
# some processes disable gc for performance, re-enable here # some processes disable gc for performance, re-enable here
if not gc.isenabled(): if not gc.isenabled():
gc.enable() gc.enable()
gc.collect() gc.collect()
# If you use setUpClass, the environment variables won't be cleared properly, # If you use setUpClass, the environment variables won't be cleared properly,
# so we need to hook both the function and class pytest fixtures # so we need to hook both the function and class pytest fixtures
@pytest.fixture(scope="class", autouse=True) @pytest.fixture(scope="class", autouse=True)
def openpilot_class_fixture(): def openpilot_class_fixture():
starting_env = dict(os.environ) with clean_env():
yield
yield
os.environ.clear()
os.environ.update(starting_env)
@pytest.fixture(scope="function") @pytest.fixture(scope="function")

Loading…
Cancel
Save