Skip to content

Commit b9df9b8

Browse files
committed
Separate error types on config parsing
Distinguish between missing and malformed; raise different errors
1 parent 2f3dff4 commit b9df9b8

5 files changed

Lines changed: 48 additions & 13 deletions

File tree

autorelease/scripts/bump_dev_version.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
from packaging.version import Version
77
import requests
88
from autorelease.utils import split_setup_cfg_path
9-
from autorelease.version import get_setup_cfg, get_setup_name, get_setup_version
9+
from autorelease.version import (
10+
ConfigParserError, get_setup_cfg, get_setup_name, get_setup_version
11+
)
1012

1113
def get_latest_pypi(package, index="https://test.pypi.org/pypi"):
1214
url = "/".join([index, package, 'json'])
@@ -56,7 +58,12 @@ def shared_parser():
5658

5759
def get_version_info(conf_name, index):
5860
directory, filename = split_setup_cfg_path(conf_name)
59-
conf = get_setup_cfg(directory=directory, filename=filename)
61+
try:
62+
conf = get_setup_cfg(directory=directory, filename=filename)
63+
except ConfigParserError as exc:
64+
raise RuntimeError(
65+
f"Unable to parse setup config: {conf_name}"
66+
) from exc
6067
if conf is None:
6168
raise RuntimeError(f"Unable to find setup config: {conf_name}")
6269

autorelease/scripts/cli.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
from autorelease.scripts.vendor import vendor_actions
66
from autorelease.scripts.check import run_checks
77
from autorelease.utils import split_setup_cfg_path
8-
from autorelease.version import get_setup_cfg, get_setup_name, get_setup_version
8+
from autorelease.version import (
9+
ConfigParserError, get_setup_cfg, get_setup_name, get_setup_version
10+
)
911
# from autorelease import ReleaseNoteWriter
1012
from autorelease.gh_api4.notes4 import NotesWriter, prs_since_latest_release
1113

@@ -89,7 +91,12 @@ def metadata():
8991
help="setup.cfg file to use")
9092
def metadata_version(conf):
9193
directory, filename = split_setup_cfg_path(conf)
92-
setup_cfg = get_setup_cfg(directory=directory, filename=filename)
94+
try:
95+
setup_cfg = get_setup_cfg(directory=directory, filename=filename)
96+
except ConfigParserError as exc:
97+
raise click.ClickException(
98+
f"Unable to parse setup config: {conf}"
99+
) from exc
93100
value = get_setup_version(setup_cfg, default_version=None)
94101
field = "version"
95102

@@ -105,7 +112,12 @@ def metadata_version(conf):
105112
help="setup.cfg file to use")
106113
def metadata_name(conf):
107114
directory, filename = split_setup_cfg_path(conf)
108-
setup_cfg = get_setup_cfg(directory=directory, filename=filename)
115+
try:
116+
setup_cfg = get_setup_cfg(directory=directory, filename=filename)
117+
except ConfigParserError as exc:
118+
raise click.ClickException(
119+
f"Unable to parse setup config: {conf}"
120+
) from exc
109121
value = get_setup_name(setup_cfg, default_name=None)
110122
field = "name"
111123

autorelease/tests/test_bump_dev_version.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,17 @@ def unexpected_get_setup_cfg(*args, **kwargs):
5757
assert package == "mypkg"
5858
assert v_setup == "1.2.3.dev0"
5959
assert v_pypi == "1.2.2"
60+
61+
62+
def test_get_version_info_malformed_cfg(tmp_path):
63+
setup_cfg = tmp_path / "setup.cfg"
64+
setup_cfg.write_text(
65+
"[metadata\n"
66+
"name = badpkg\n"
67+
"version = 0.0.0\n"
68+
)
69+
70+
with pytest.raises(RuntimeError, match="Unable to parse setup config"):
71+
bump_mod.get_version_info(
72+
str(setup_cfg), "https://example.invalid/pypi"
73+
)

autorelease/tests/test_version.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55

66

77
from autorelease.version import (
8-
_find_rel_path_for_file, get_setup_cfg, get_setup_name, get_setup_version
8+
ConfigParserError, _find_rel_path_for_file, get_setup_cfg,
9+
get_setup_name, get_setup_version
910
)
1011

1112
@pytest.mark.parametrize("depth, result", [
@@ -86,6 +87,5 @@ def test_get_setup_name_and_version_malformed_cfg(tmp_path):
8687
"version = 0.0.0\n"
8788
)
8889

89-
conf = get_setup_cfg(str(tmp_path), "setup.cfg")
90-
assert get_setup_name(conf, default_name="default-name") == "default-name"
91-
assert get_setup_version(conf, default_version="0.0.0") == "0.0.0"
90+
with pytest.raises(ConfigParserError):
91+
get_setup_cfg(str(tmp_path), "setup.cfg")

autorelease/version.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ def get_setup_cfg(directory, filename="setup.cfg"):
102102
directory for setup.cfg, relative to cwd; default '.'
103103
filename : str
104104
filename for setup.cfg; default 'setup.cfg'
105+
106+
Raises
107+
------
108+
ConfigParserError
109+
if setup.cfg exists but cannot be parsed
105110
"""
106111
if isinstance(directory, int):
107112
rel_path = _find_rel_path_for_file(directory, filename)
@@ -113,10 +118,7 @@ def get_setup_cfg(directory, filename="setup.cfg"):
113118
conf = None
114119
if os.path.exists(setup_cfg):
115120
conf = ConfigParser()
116-
try:
117-
conf.read(setup_cfg)
118-
except ConfigParserError:
119-
conf = None
121+
conf.read(setup_cfg)
120122

121123
return conf
122124

0 commit comments

Comments
 (0)