From 6d1fba21710bee8e7390d6f31926cd9ff00c1320 Mon Sep 17 00:00:00 2001 From: Dima Gerasimov Date: Tue, 5 May 2020 08:18:08 +0100 Subject: [PATCH] Extra test for MY_CONFIG variable; fix order import for stub/dynamic config --- my/init.py | 6 ++-- tests/config.py | 81 +++++++++++++++++++++++++++++++--------------- tests/get_files.py | 2 +- tox.ini | 2 +- 4 files changed, 61 insertions(+), 30 deletions(-) diff --git a/my/init.py b/my/init.py index 8c54cf2..54686c3 100644 --- a/my/init.py +++ b/my/init.py @@ -43,14 +43,16 @@ def setup_config(): return mpath = str(mycfg_dir) - if mpath not in sys.path: - sys.path.insert(0, mpath) + # NOTE: we _really_ want to have mpath in front there, to shadow my.config stub within this packages + # hopefully it doesn't cause any issues + sys.path.insert(0, mpath) # remove the stub and insert reimport hte 'real' config if 'my.config' in sys.modules: # TODO FIXME make sure this method isn't called twice... del sys.modules['my.config'] try: + # todo import_from instead?? dunno import my.config except ImportError as ex: # just in case... who knows what crazy setup users have in mind. diff --git a/tests/config.py b/tests/config.py index 31e6f59..2cee194 100644 --- a/tests/config.py +++ b/tests/config.py @@ -31,33 +31,7 @@ def test_dynamic_configuration(notes: Path) -> None: 0.0, ] - 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: @@ -86,3 +60,58 @@ DAL = None # should succeed now! 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 diff --git a/tests/get_files.py b/tests/get_files.py index 2d4abcf..a3fd5a9 100644 --- a/tests/get_files.py +++ b/tests/get_files.py @@ -72,7 +72,7 @@ def test_explicit_glob(): 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. ''' diff --git a/tox.ini b/tox.ini index 52549a8..db24138 100644 --- a/tox.ini +++ b/tox.ini @@ -13,7 +13,7 @@ commands = # python -m pytest {posargs} 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 -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 run demo.py? just make sure with_my is a bit cleverer? # TODO e.g. under CI, rely on installing