CI: better cleanup for modules in between tests

This commit is contained in:
Dima Gerasimov 2021-02-19 00:09:08 +00:00 committed by karlicoss
parent 94ace823e0
commit ddbb2e5f23
5 changed files with 41 additions and 12 deletions

View file

@ -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

View file

@ -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]

View file

@ -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

View file

@ -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

View file

@ -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