diff --git a/my/core/core_config.py b/my/core/core_config.py index 5c7843d..c4f8ec0 100644 --- a/my/core/core_config.py +++ b/my/core/core_config.py @@ -5,15 +5,19 @@ import re from typing import Sequence, Optional from .common import PathIsh +from . import warnings try: - # FIXME support legacy 'common'? from my.config import core as user_config # type: ignore[attr-defined] except Exception as e: - # make it defensive, because it's pretty commonly used and would be annoying if it breaks hpi doctor etc. - # this way it'll at least use the defaults - # TODO add high warning - user_config = object # type: ignore[assignment, misc] + try: + from my.config import common as user_config # type: ignore[attr-defined, assignment, misc] + warnings.high("'common' config section is deprecated. Please rename it to 'core'.") + except Exception as e2: + # make it defensive, because it's pretty commonly used and would be annoying if it breaks hpi doctor etc. + # this way it'll at least use the defaults + # todo actually not sure if needs a warning? Perhaps it's okay without it, because the defaults are reasonable enough + user_config = object # type: ignore[assignment, misc] from dataclasses import dataclass @@ -55,13 +59,55 @@ class Config(user_config): return not matches(disabled) else: if disabled is None: - # only enabled the specifid modules + # only enable the specified modules return matches(enabled) else: # ok, this means the config is inconsistent. better fallback onto the 'enable everything', then the user will notice? - # todo add medium warning? + warnings.medium("Both 'enabled_modules' and 'disabled_modules' are set in the config. Please only use one of them.") return True from .cfg import make_config config = make_config(Config) + + +### tests start + +def test_active_modules() -> None: + # todo maybe have this decorator for the whole of my.config? + from contextlib import contextmanager as ctx + @ctx + def reset(): + from .cfg import override_config + with override_config(config) as cc: + cc.enabled_modules = None + cc.disabled_modules = None + yield cc + + with reset() as cc: + assert cc.is_module_active('my.whatever') + assert cc.is_module_active('my.core' ) + assert cc.is_module_active('my.body.exercise') + + with reset() as cc: + cc.disabled_modules = ['my.body.*'] + assert cc.is_module_active('my.whatever') + assert cc.is_module_active('my.core' ) + assert not cc.is_module_active('my.body.exercise') + + with reset() as cc: + cc.enabled_modules = ['my.whatever'] + assert cc.is_module_active('my.whatever') + assert not cc.is_module_active('my.core' ) + assert not cc.is_module_active('my.body.exercise') + + with reset() as cc: + # if both are set, enable all + cc.disabled_modules = ['my.body.*'] + cc.enabled_modules = ['my.whatever'] + assert cc.is_module_active('my.whatever') + assert cc.is_module_active('my.core' ) + assert cc.is_module_active('my.body.exercise') + # todo suppress warnings during the tests? + +### tests end diff --git a/tests/core.py b/tests/core.py new file mode 100644 index 0000000..0997527 --- /dev/null +++ b/tests/core.py @@ -0,0 +1,14 @@ +''' +NOTE: Sigh. it's nice to be able to define the tests next to the source code (so it serves as documentation). +However, if you run 'pytest --pyargs my.core', it detects 'core' package name (because there is no my/__init__.py) +(see https://docs.pytest.org/en/latest/goodpractices.html#tests-as-part-of-application-code) + +This results in relative imports failing (e.g. from ..kython import...). + +By using this helper file, pytest can detect the package name properly. A bit meh, but perhaps after kython is moved into the core, +we can run against the tests in my.core directly. + +''' + +from my.core.core_config import * +from my.core.error import * diff --git a/tests/misc.py b/tests/misc.py index 43ec617..869c276 100644 --- a/tests/misc.py +++ b/tests/misc.py @@ -46,10 +46,6 @@ def prepare(tmp_path: Path): pass -# meh. otherwise was struggling to run directly against my.core.error... -from my.core.error import * - - from typing import Iterable, List import warnings from my.core import warn_if_empty diff --git a/tox.ini b/tox.ini index ecd9679..f88bd3c 100644 --- a/tox.ini +++ b/tox.ini @@ -1,6 +1,6 @@ [tox] minversion = 3.5 -envlist = py3,mypy,mypy-modules # pylint +envlist = py3,mypy,mypy-modules # TODO ugh. unclear how to reuse setup.cfg deps in tox [testenv] @@ -13,7 +13,7 @@ commands = # todo these are probably not necessary anymore? python3 -c 'from my.config import stub as config; print(config.key)' python3 -c 'import my.config; import my.config.repos' # shouldn't fail at least - python3 -m pytest tests/misc.py tests/get_files.py tests/config.py::test_set_repo tests/config.py::test_environment_variable tests/demo.py + python3 -m pytest tests/core.py tests/misc.py tests/get_files.py tests/config.py::test_set_repo tests/config.py::test_environment_variable tests/demo.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 e.g. under CI, rely on installing @@ -48,15 +48,3 @@ skip_install = true commands = pip install -e .[testing] .[optional] ./lint - - -# TODO fix it -# [testenv:pylint] -# # TODO wtf???? -# changedir = {toxworkdir}/{envname}/../.. -# skip_install = true -# commands = -# pip install -e .[testing] - # for now ignore import errors until I figure out how to import everything for CI checking.. - # TODO FIXME ugh. fix later, after properly switched to my.config - # python -m pylint -E -d import-error my