From ddbb2e5f23678c2ccd36fe47366035ec860aae3d Mon Sep 17 00:00:00 2001 From: Dima Gerasimov Date: Fri, 19 Feb 2021 00:09:08 +0000 Subject: [PATCH] CI: better cleanup for modules in between tests --- my/core/init.py | 1 + tests/common.py | 11 +++++++++++ tests/config.py | 34 +++++++++++++++++++++++----------- tests/location.py | 4 +++- tests/tz.py | 3 +++ 5 files changed, 41 insertions(+), 12 deletions(-) diff --git a/my/core/init.py b/my/core/init.py index 55224a8..3c607c6 100644 --- a/my/core/init.py +++ b/my/core/init.py @@ -11,6 +11,7 @@ A hook to insert user's config directory into Python's search path. from types import ModuleType # TODO not ideal to keep it here, but this should really be a leaf in the import tree +# TODO maybe I don't even need it anymore? def assign_module(parent: str, name: str, module: ModuleType) -> None: import sys import importlib diff --git a/tests/common.py b/tests/common.py index 23c1795..f8fc5de 100644 --- a/tests/common.py +++ b/tests/common.py @@ -7,3 +7,14 @@ V = 'HPI_TESTS_KARLICOSS' skip_if_not_karlicoss = pytest.mark.skipif( V not in os.environ, reason=f'test only works on @karlicoss data for now. Set evn variable {V}=true to override.', ) + +def reset_modules() -> None: + ''' + A hack to 'unload' HPI modules, otherwise some modules might cache the config + TODO: a bit crap, need a better way.. + ''' + import sys + import re + to_unload = [m for m in sys.modules if re.match(r'my[.]?', m)] + for m in to_unload: + del sys.modules[m] diff --git a/tests/config.py b/tests/config.py index fdcdebf..e9b791c 100644 --- a/tests/config.py +++ b/tests/config.py @@ -40,13 +40,30 @@ def test_environment_variable(tmp_path: Path) -> None: cfg_file.write_text(''' class feedly: pass +class just_for_test: + pass ''') import os - os.environ['MY_CONFIG'] = str(tmp_path) + oenv = dict(os.environ) + try: + os.environ['MY_CONFIG'] = str(tmp_path) + # should not raise at least + import my.rss.feedly - # should not raise at least - import my.rss.feedly + import my.config as c + assert hasattr(c, 'just_for_test') + finally: + os.environ.clear() + os.environ.update(oenv) + + import sys + # TODO wtf??? doesn't work without unlink... is it caching something? + cfg_file.unlink() + del sys.modules['my.config'] # meh.. + + import my.config as c + assert not hasattr(c, 'just_for_test') from dataclasses import dataclass @@ -110,12 +127,7 @@ Some misc stuff @pytest.fixture(autouse=True) -def reset_config(): - # otherwise tests impact each other because of the cached my. modules... - # hacky, but does the trick? - import sys - import re - to_unload = [m for m in sys.modules if re.match(r'my[.]?', m)] - for m in to_unload: - del sys.modules[m] +def prepare(): + from .common import reset_modules + reset_modules() yield diff --git a/tests/location.py b/tests/location.py index c62f733..8840901 100644 --- a/tests/location.py +++ b/tests/location.py @@ -20,6 +20,9 @@ def test() -> None: @pytest.fixture(autouse=True) def prepare(tmp_path: Path): + from .common import reset_modules + reset_modules() + user_config = _prepare_google_config(tmp_path) import my.core.cfg as C @@ -39,7 +42,6 @@ def _prepare_google_config(tmp_path: Path): with zipfile.ZipFile(tmp_path / 'takeout.zip', 'w') as zf: zf.writestr('Takeout/Location History/Location History.json', track.read_bytes()) - from my.cfg import config class google_config: takeout_path = tmp_path return google_config diff --git a/tests/tz.py b/tests/tz.py index e668656..cb8c513 100644 --- a/tests/tz.py +++ b/tests/tz.py @@ -77,6 +77,9 @@ def D(dstr: str) -> datetime: # TODO copy pasted from location.py, need to extract some common provider @pytest.fixture(autouse=True) def prepare(tmp_path: Path): + from .common import reset_modules + reset_modules() + LTZ._FASTER = True from .location import _prepare_google_config