tests: move test checkign for my_config handling to core/tests/test_config.py
allows to remove the hacky reset_modules thing from setup fixture
This commit is contained in:
parent
1215181af5
commit
2ff2dcfc00
2 changed files with 49 additions and 39 deletions
|
@ -3,6 +3,7 @@ Various tests that are checking behaviour of user config wrt to various things
|
|||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
@ -13,6 +14,10 @@ import my.config
|
|||
from my.core import notnone
|
||||
from my.demo import items, make_config
|
||||
|
||||
from .common import tmp_environ_set
|
||||
|
||||
# TODO would be nice to randomize test order here to catch various config issues
|
||||
|
||||
|
||||
# run the same test multiple times to make sure there are not issues with import order etc
|
||||
@pytest.mark.parametrize('run_id', ['1', '2'])
|
||||
|
@ -120,6 +125,48 @@ def do_transform(x):
|
|||
sys.modules.pop('external.submodule', None)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('run_id', ['1', '2'])
|
||||
def test_my_config_env_variable(tmp_path: Path, run_id: str) -> None:
|
||||
"""
|
||||
Tests handling of MY_CONFIG variable
|
||||
"""
|
||||
|
||||
# ugh. so by this point, my.config is already loaded (default stub), so we need to unload it
|
||||
sys.modules.pop('my.config', None)
|
||||
# but my.config itself relies on my.core.init hook, so unless it's reloaded too it wouldn't help
|
||||
sys.modules.pop('my.core', None)
|
||||
sys.modules.pop('my.core.init', None)
|
||||
# it's a bit of a mouthful of course, but in most cases MY_CONFIG would be set once
|
||||
# , and before hpi runs, so hopefully it's not a huge deal
|
||||
cfg_dir = tmp_path / 'my'
|
||||
cfg_file = cfg_dir / 'config.py'
|
||||
cfg_dir.mkdir()
|
||||
|
||||
cfg_file.write_text(
|
||||
f'''
|
||||
# print("IMPORTING CONFIG {run_id}")
|
||||
class demo:
|
||||
username = 'xxx_{run_id}'
|
||||
data_path = r'{tmp_path}{os.sep}*.json' # need raw string for windows...
|
||||
'''
|
||||
)
|
||||
|
||||
with tmp_environ_set('MY_CONFIG', str(tmp_path)):
|
||||
[item1, item2] = items()
|
||||
assert item1.username == f'xxx_{run_id}'
|
||||
assert item2.username == f'xxx_{run_id}'
|
||||
|
||||
# sigh.. so this is cached in sys.path
|
||||
# so it takes precedence later during next import, not giving the MY_CONFIG hook
|
||||
# (imported from builtin my.config) to kick in
|
||||
sys.path.remove(str(tmp_path))
|
||||
|
||||
# FIXME ideally this shouldn't be necessary?
|
||||
# remove this after we fixup my.tests.reddit and my.tests.commits
|
||||
# (they were failing ci when running all tests)
|
||||
sys.modules.pop('my.config', None)
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def prepare_data(tmp_path: Path):
|
||||
(tmp_path / 'data.json').write_text(
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
from pathlib import Path
|
||||
|
||||
|
||||
# TODO move this somewhere else -- there are more specific tests covering this now
|
||||
def test_dynamic_configuration(notes: Path) -> None:
|
||||
import pytz
|
||||
from types import SimpleNamespace as NS
|
||||
|
@ -26,42 +27,11 @@ def test_dynamic_configuration(notes: Path) -> None:
|
|||
import pytest
|
||||
|
||||
|
||||
def test_environment_variable(tmp_path: Path) -> None:
|
||||
cfg_dir = tmp_path / 'my'
|
||||
cfg_file = cfg_dir / 'config.py'
|
||||
cfg_dir.mkdir()
|
||||
cfg_file.write_text('''
|
||||
class feedly:
|
||||
pass
|
||||
class just_for_test:
|
||||
pass
|
||||
''')
|
||||
|
||||
import os
|
||||
oenv = dict(os.environ)
|
||||
try:
|
||||
os.environ['MY_CONFIG'] = str(tmp_path)
|
||||
# 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
|
||||
|
||||
|
||||
# TODO this test should probs be deprecated? it's more of a documentation?
|
||||
def test_user_config() -> None:
|
||||
from my.core.common import classproperty
|
||||
class user_config:
|
||||
|
@ -117,10 +87,3 @@ Some misc stuff
|
|||
yield ndir
|
||||
finally:
|
||||
pass
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def prepare():
|
||||
from my.tests.common import reset_modules
|
||||
reset_modules()
|
||||
yield
|
||||
|
|
Loading…
Add table
Reference in a new issue