core: add tests for core_config

This commit is contained in:
Dima Gerasimov 2020-09-29 11:01:51 +01:00 committed by karlicoss
parent 70c801f692
commit c79ffb50f6
4 changed files with 69 additions and 25 deletions

View file

@ -5,15 +5,19 @@ import re
from typing import Sequence, Optional from typing import Sequence, Optional
from .common import PathIsh from .common import PathIsh
from . import warnings
try: try:
# FIXME support legacy 'common'?
from my.config import core as user_config # type: ignore[attr-defined] from my.config import core as user_config # type: ignore[attr-defined]
except Exception as e: except Exception as e:
# make it defensive, because it's pretty commonly used and would be annoying if it breaks hpi doctor etc. try:
# this way it'll at least use the defaults from my.config import common as user_config # type: ignore[attr-defined, assignment, misc]
# TODO add high warning warnings.high("'common' config section is deprecated. Please rename it to 'core'.")
user_config = object # type: ignore[assignment, misc] 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 from dataclasses import dataclass
@ -55,13 +59,55 @@ class Config(user_config):
return not matches(disabled) return not matches(disabled)
else: else:
if disabled is None: if disabled is None:
# only enabled the specifid modules # only enable the specified modules
return matches(enabled) return matches(enabled)
else: else:
# ok, this means the config is inconsistent. better fallback onto the 'enable everything', then the user will notice? # 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 return True
from .cfg import make_config from .cfg import make_config
config = make_config(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

14
tests/core.py Normal file
View file

@ -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 *

View file

@ -46,10 +46,6 @@ def prepare(tmp_path: Path):
pass pass
# meh. otherwise was struggling to run directly against my.core.error...
from my.core.error import *
from typing import Iterable, List from typing import Iterable, List
import warnings import warnings
from my.core import warn_if_empty from my.core import warn_if_empty

16
tox.ini
View file

@ -1,6 +1,6 @@
[tox] [tox]
minversion = 3.5 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 # TODO ugh. unclear how to reuse setup.cfg deps in tox
[testenv] [testenv]
@ -13,7 +13,7 @@ commands =
# todo these are probably not necessary anymore? # todo these are probably not necessary anymore?
python3 -c 'from my.config import stub as config; print(config.key)' 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 -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 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
@ -48,15 +48,3 @@ skip_install = true
commands = commands =
pip install -e .[testing] .[optional] pip install -e .[testing] .[optional]
./lint ./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