CI: better cleanup for modules in between tests
This commit is contained in:
parent
94ace823e0
commit
ddbb2e5f23
5 changed files with 41 additions and 12 deletions
|
@ -11,6 +11,7 @@ A hook to insert user's config directory into Python's search path.
|
||||||
from types import ModuleType
|
from types import ModuleType
|
||||||
|
|
||||||
# TODO not ideal to keep it here, but this should really be a leaf in the import tree
|
# 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:
|
def assign_module(parent: str, name: str, module: ModuleType) -> None:
|
||||||
import sys
|
import sys
|
||||||
import importlib
|
import importlib
|
||||||
|
|
|
@ -7,3 +7,14 @@ V = 'HPI_TESTS_KARLICOSS'
|
||||||
skip_if_not_karlicoss = pytest.mark.skipif(
|
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.',
|
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]
|
||||||
|
|
|
@ -40,13 +40,30 @@ def test_environment_variable(tmp_path: Path) -> None:
|
||||||
cfg_file.write_text('''
|
cfg_file.write_text('''
|
||||||
class feedly:
|
class feedly:
|
||||||
pass
|
pass
|
||||||
|
class just_for_test:
|
||||||
|
pass
|
||||||
''')
|
''')
|
||||||
|
|
||||||
import os
|
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.config as c
|
||||||
import my.rss.feedly
|
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
|
from dataclasses import dataclass
|
||||||
|
@ -110,12 +127,7 @@ Some misc stuff
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(autouse=True)
|
@pytest.fixture(autouse=True)
|
||||||
def reset_config():
|
def prepare():
|
||||||
# otherwise tests impact each other because of the cached my. modules...
|
from .common import reset_modules
|
||||||
# hacky, but does the trick?
|
reset_modules()
|
||||||
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]
|
|
||||||
yield
|
yield
|
||||||
|
|
|
@ -20,6 +20,9 @@ def test() -> None:
|
||||||
|
|
||||||
@pytest.fixture(autouse=True)
|
@pytest.fixture(autouse=True)
|
||||||
def prepare(tmp_path: Path):
|
def prepare(tmp_path: Path):
|
||||||
|
from .common import reset_modules
|
||||||
|
reset_modules()
|
||||||
|
|
||||||
user_config = _prepare_google_config(tmp_path)
|
user_config = _prepare_google_config(tmp_path)
|
||||||
|
|
||||||
import my.core.cfg as C
|
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:
|
with zipfile.ZipFile(tmp_path / 'takeout.zip', 'w') as zf:
|
||||||
zf.writestr('Takeout/Location History/Location History.json', track.read_bytes())
|
zf.writestr('Takeout/Location History/Location History.json', track.read_bytes())
|
||||||
|
|
||||||
from my.cfg import config
|
|
||||||
class google_config:
|
class google_config:
|
||||||
takeout_path = tmp_path
|
takeout_path = tmp_path
|
||||||
return google_config
|
return google_config
|
||||||
|
|
|
@ -77,6 +77,9 @@ def D(dstr: str) -> datetime:
|
||||||
# TODO copy pasted from location.py, need to extract some common provider
|
# TODO copy pasted from location.py, need to extract some common provider
|
||||||
@pytest.fixture(autouse=True)
|
@pytest.fixture(autouse=True)
|
||||||
def prepare(tmp_path: Path):
|
def prepare(tmp_path: Path):
|
||||||
|
from .common import reset_modules
|
||||||
|
reset_modules()
|
||||||
|
|
||||||
LTZ._FASTER = True
|
LTZ._FASTER = True
|
||||||
|
|
||||||
from .location import _prepare_google_config
|
from .location import _prepare_google_config
|
||||||
|
|
Loading…
Add table
Reference in a new issue