Skip to content

Commit 5c835f3

Browse files
Added tests for stub file, removed unnecessary branches
1 parent 195692e commit 5c835f3

File tree

6 files changed

+43
-12
lines changed

6 files changed

+43
-12
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "pyya"
3-
version = "0.1.12"
3+
version = "0.1.13"
44
description = "Convert YAML/TOML configuration files to Python objects"
55
readme = "README.md"
66
requires-python = ">=3.8"

pyya.egg-info/PKG-INFO

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Metadata-Version: 2.1
22
Name: pyya
3-
Version: 0.1.12
3+
Version: 0.1.13
44
Summary: Convert YAML/TOML configuration files to Python objects
55
Author-email: shadowy-pycoder <[email protected]>
66
Project-URL: Homepage, https://github.com/shadowy-pycoder/pyya

pyya.egg-info/SOURCES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ pyya.egg-info/dependency_links.txt
1010
pyya.egg-info/entry_points.txt
1111
pyya.egg-info/requires.txt
1212
pyya.egg-info/top_level.txt
13+
tests/test_common.py
1314
tests/test_toml.py
1415
tests/test_yaml.py

pyya/__init__.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,19 +73,13 @@ def _merge_configs(
7373
# is it fine to proccess already poped dicts on recursion?
7474
entry = _pop_ignored_keys(entry)
7575
if section not in _raw_data or _raw_data[section] is None:
76-
if section not in _raw_data:
77-
_raw_data[section] = entry
78-
logger.debug(f'section `{".".join(sections)}` with value `{entry}` taken from {default_config}')
79-
else:
80-
logger.debug(f'section `{".".join(sections)}` already exists in {config}, skipping')
76+
_raw_data[section] = entry
77+
logger.debug(f'section `{".".join(sections)}` with value `{entry}` taken from {default_config}')
8178
elif isinstance(entry, Dict):
8279
_merge_configs(_raw_data[section], entry, sections)
8380
# TODO: add support for merging lists
8481
else:
85-
if section not in _raw_data:
86-
_raw_data[section] = _raw_data.pop(section, None)
87-
else:
88-
logger.debug(f'section `{".".join(sections)}` already exists in {config}, skipping')
82+
logger.debug(f'section `{".".join(sections)}` already exists in {config}, skipping')
8983
sections.pop()
9084

9185
def _sanitize_section(section: str) -> str:

tests/test_common.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
default_config_path = Path(__file__).parent / 'testdata/default.config.yaml'
1010
config_wrong_ext = Path(__file__).parent / 'testdata/config.txt'
1111
config_corrupted = Path(__file__).parent / 'testdata/corrupted.yaml'
12-
1312
config_extra = Path(__file__).parent / 'testdata/extra.yaml'
1413
default_config_extra = Path(__file__).parent / 'testdata/default_extra.yaml'
14+
config_stub = Path(__file__).parent / 'testdata/config.pyi'
1515

1616

1717
def test_raise_err_default_file_not_found() -> None:
@@ -85,3 +85,37 @@ def test_raise_err_extra_sections_access() -> None:
8585
default_config=default_config_extra,
8686
)
8787
_ = config.database.garbage
88+
89+
90+
def test_generate_stub() -> None:
91+
assert not config_stub.exists()
92+
_ = pyya.init_config(
93+
config=config_stub,
94+
default_config=default_config_path,
95+
_generate_stub=True, # this private argument is used by CLI tool
96+
)
97+
assert config_stub.exists()
98+
config_stub.unlink()
99+
100+
101+
def test_generate_stub_exist() -> None:
102+
_ = pyya.init_config(
103+
config=config_stub,
104+
default_config=default_config_path,
105+
_generate_stub=True,
106+
)
107+
# NOTE: it would be better to use fixture here
108+
try:
109+
_ = pyya.init_config(
110+
config=config_stub,
111+
default_config=default_config_path,
112+
_generate_stub=True,
113+
)
114+
except pyya.PyyaError:
115+
pass
116+
except Exception:
117+
pytest.fail(reason='Wrong type of exception')
118+
else:
119+
pytest.fail(reason="Exception wasn't raised")
120+
finally:
121+
config_stub.unlink(missing_ok=True)

tests/testdata/default.config.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,5 @@ environments:
5757
level: warn
5858
cache:
5959
ttl_seconds: 600
60+
61+
flags: []

0 commit comments

Comments
 (0)