Extra test for MY_CONFIG variable; fix order import for stub/dynamic config

This commit is contained in:
Dima Gerasimov 2020-05-05 08:18:08 +01:00 committed by karlicoss
parent 636060db57
commit 6d1fba2171
4 changed files with 61 additions and 30 deletions

View file

@ -43,14 +43,16 @@ def setup_config():
return return
mpath = str(mycfg_dir) mpath = str(mycfg_dir)
if mpath not in sys.path: # NOTE: we _really_ want to have mpath in front there, to shadow my.config stub within this packages
sys.path.insert(0, mpath) # hopefully it doesn't cause any issues
sys.path.insert(0, mpath)
# remove the stub and insert reimport hte 'real' config # remove the stub and insert reimport hte 'real' config
if 'my.config' in sys.modules: if 'my.config' in sys.modules:
# TODO FIXME make sure this method isn't called twice... # TODO FIXME make sure this method isn't called twice...
del sys.modules['my.config'] del sys.modules['my.config']
try: try:
# todo import_from instead?? dunno
import my.config import my.config
except ImportError as ex: except ImportError as ex:
# just in case... who knows what crazy setup users have in mind. # just in case... who knows what crazy setup users have in mind.

View file

@ -31,33 +31,7 @@ def test_dynamic_configuration(notes: Path) -> None:
0.0, 0.0,
] ]
import pytest # type: ignore import pytest # type: ignore
@pytest.fixture
def notes(tmp_path: Path):
ndir = tmp_path / 'notes'
ndir.mkdir()
logs = ndir / 'logs.org'
logs.write_text('''
#+TITLE: Stuff I'm logging
* Weight (org-capture) :weight:
** [2020-05-01 Fri 09:00] 62
** 63
this should be ignored, got no timestmap
** [2020-05-03 Sun 08:00] 61
** [2020-05-04 Mon 10:00] 62
''')
misc = ndir / 'misc.org'
misc.write_text('''
Some misc stuff
* unrelated note :weight:whatever:
''')
try:
yield ndir
finally:
pass
def test_set_repo(tmp_path: Path) -> None: def test_set_repo(tmp_path: Path) -> None:
@ -86,3 +60,58 @@ DAL = None
# should succeed now! # should succeed now!
import my.hypothesis import my.hypothesis
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
''')
import os
os.environ['MY_CONFIG'] = str(tmp_path)
# should not raise at least
import my.feedly
@pytest.fixture
def notes(tmp_path: Path):
ndir = tmp_path / 'notes'
ndir.mkdir()
logs = ndir / 'logs.org'
logs.write_text('''
#+TITLE: Stuff I'm logging
* Weight (org-capture) :weight:
** [2020-05-01 Fri 09:00] 62
** 63
this should be ignored, got no timestmap
** [2020-05-03 Sun 08:00] 61
** [2020-05-04 Mon 10:00] 62
''')
misc = ndir / 'misc.org'
misc.write_text('''
Some misc stuff
* unrelated note :weight:whatever:
''')
try:
yield ndir
finally:
pass
@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]
yield

View file

@ -72,7 +72,7 @@ def test_explicit_glob():
assert get_files('/tmp/hpi_test', glob='file_*.zip') == expected assert get_files('/tmp/hpi_test', glob='file_*.zip') == expected
def test_implicit_blog(): def test_implicit_glob():
''' '''
Asterisc in the path results in globing too. Asterisc in the path results in globing too.
''' '''

View file

@ -13,7 +13,7 @@ commands =
# python -m pytest {posargs} # python -m pytest {posargs}
python3 -c 'import my.init; from my.config import stub as config; print(config.key)' python3 -c 'import my.init; from my.config import stub as config; print(config.key)'
python3 -c 'import my.init; import my.config; import my.config.repos' # shouldn't fail at least python3 -c 'import my.init; import my.config; import my.config.repos' # shouldn't fail at least
python3 -m pytest tests/misc.py tests/get_files.py python3 -m pytest tests/misc.py tests/get_files.py tests/config.py::test_set_repo tests/config.py::test_environment_variable
# TODO add; once I figure out porg depdencency?? tests/config.py # TODO add; once I figure out porg depdencency?? tests/config.py
# TODO run demo.py? just make sure with_my is a bit cleverer? # TODO run demo.py? just make sure with_my is a bit cleverer?
# TODO e.g. under CI, rely on installing # TODO e.g. under CI, rely on installing