Skip to content

Commit 9bbebcc

Browse files
committed
tests: increase coverage
1 parent ec0fc90 commit 9bbebcc

3 files changed

Lines changed: 29 additions & 14 deletions

File tree

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ repos:
3030
name: pytest
3131
language: python
3232
entry: env PYTHONPATH=. pytest
33-
args: [--cov=envwrap]
33+
args: [--cov=envwrap, -vv]
3434
types: [python]
3535
pass_filenames: false
3636
additional_dependencies:

envwrap/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,9 @@ def envwrap(name: str, app: str = "", types: dict = None, is_method=False):
158158

159159
def wrap(func):
160160
params = signature(func).parameters
161-
env_overrides = get_defaults(name, app, func.__name__)
162-
# ignore unknown env vars
163-
overrides = {k: v for k, v in env_overrides.items() if k in params}
161+
defaults = get_defaults(name, app, func.__name__)
162+
# ignore unknown params
163+
overrides = {k: v for k, v in defaults.items() if k in params}
164164
log.debug("Loaded overrides for %s: %s", func.__name__, overrides)
165165
# infer overrides' `type`s
166166
for k in overrides:

tests/test_envwrap.py

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,29 +43,42 @@ def set_env():
4343
get_defaults.cache_clear()
4444

4545

46-
def funcname(a=1, b=2, c=3, d=4, e=5, f=6):
46+
def funcname(a: int = None, b=2, c=3, d=4, e=5, f=6):
4747
return {'a': a, 'b': b, 'c': c, 'd': d, 'e': e, 'f': f}
4848

4949

5050
def test_env():
51-
f = envwrap('envwrap', 'testenv')(funcname)
52-
assert f(c=99) == {'a': 1, 'b': 42, 'c': 99, 'd': 360, 'e': 101, 'f': 404}
53-
f = envwrap('envwrap')(funcname)
54-
assert f(c=99) == {'a': 1, 'b': 42, 'c': 99, 'd': 4, 'e': 101, 'f': 6}
51+
wrapped = envwrap('envwrap', 'testenv')(funcname)
52+
assert wrapped(c=99) == {'a': None, 'b': 42, 'c': 99, 'd': 360, 'e': 101, 'f': 404}
53+
wrapped = envwrap('envwrap')(funcname)
54+
assert wrapped(c=99) == {'a': None, 'b': 42, 'c': 99, 'd': 4, 'e': 101, 'f': 6}
5555

5656

5757
@pytest.mark.parametrize('ext', ['toml', 'yaml', 'yml', 'json', 'ini', 'cfg'])
58-
def test_conf(tmp_path, ext):
58+
@pytest.mark.parametrize('base', ['cfgwrap', 'testcfg'])
59+
def test_conf(tmp_path, base, ext):
5960
if version_info < (3, 9) and ext in ('ini', 'cfg'):
6061
pytest.skip("configparser dict merging requires python>=3.9")
6162
config = {
62-
'testcfg': {'b': 43, 'c': 1338, 'd': 361, 'funcname': {'f': 405}}, 'funcname': {'e': 102}}
63-
write_config(tmp_path / f"cfgwrap.{ext}", config)
63+
'testcfg': {'b': 43, 'c': 1338, 'd': 361,
64+
'funcname': {'f': 405}}, 'funcname': {'e': 102, 'a': 0},
65+
'cfgwrap': {'b': -1, 'e': -2, 'f': -3, 'funcname': {'e': -4}}}
66+
write_config(tmp_path / f"{base}.{ext}", config)
6467
pwd = os.curdir
6568
os.chdir(tmp_path)
6669
try:
67-
f = envwrap('cfgwrap', 'testcfg')(funcname)
68-
assert f(c=98) == {'a': 1, 'b': 43, 'c': 98, 'd': 361, 'e': 102, 'f': 405}
70+
wrapped = envwrap('cfgwrap', 'testcfg')(funcname)
71+
if base == 'cfgwrap':
72+
assert wrapped(c=98) == {'a': 0, 'b': 43, 'c': 98, 'd': 361, 'e': 102, 'f': 405}
73+
else:
74+
assert wrapped(c=98) == {'a': None, 'b': 2, 'c': 98, 'd': 4, 'e': 5, 'f': 6}
75+
assert int(get_defaults(base, 'testcfg', 'funcname')['a']) == 0
76+
assert int(get_defaults(base, 'testcfg', 'funcname')['f']) == 405
77+
assert int(get_defaults(base, 'testcfg', 'miss-n/a')['d']) == 361
78+
assert int(get_defaults(base, 'cfgwrap', 'funcname')['b']) == -1
79+
assert int(get_defaults(base, 'cfgwrap', 'miss-n/a')['e']) == -2
80+
assert int(get_defaults(base, 'cfgwrap', 'funcname')['f']) == -3
81+
assert int(get_defaults(base, 'cfgwrap', 'funcname')['e']) == -4
6982
finally:
7083
os.chdir(pwd)
7184

@@ -80,6 +93,8 @@ def test_pyproject(tmp_path):
8093
assert get_defaults(tool, '', '')[key] == 99
8194

8295
assert get_defaults('coverage', '', 'report')['show_missing'] is True
96+
assert get_defaults('coverage', 'report', '')['show_missing'] is True
97+
assert get_defaults('coverage', 'report', 'show_missing')['report']['show_missing'] is True
8398
finally:
8499
os.chdir(pwd)
85100

0 commit comments

Comments
 (0)