Skip to content

Commit a0db0d9

Browse files
Improved error checking when parsing a Blurb. (#507)
We now: * Check the entries in metadata in order, so we complain about the *first* one that has an error, which is a more familiar user experience. * Have checks for: * Invalid issue number * Invalid section * Empty section * Completely missing section (There is no test for "missing issue number", because it's legal to have a Blurb with no issue number. "no changes" blurbs don't have an issue number. But we do now reliably test that, *if* the issue number is specified, it *is* correctly formatted.)
1 parent 9ea7fc6 commit a0db0d9

File tree

5 files changed

+49
-20
lines changed

5 files changed

+49
-20
lines changed

blurb/blurb.py

+34-20
Original file line numberDiff line numberDiff line change
@@ -472,27 +472,34 @@ def finish_entry():
472472
throw("Blurb 'body' can't start with " + repr(naughty_prefix) + "!")
473473

474474
no_changes = metadata.get('no changes')
475-
section = metadata.get('section')
476-
477-
if not no_changes:
478-
if not section:
479-
throw("No 'section' specified. You must provide one!")
480-
elif section not in sections:
481-
throw("Invalid 'section'! You must use one of the predefined sections.")
482-
483-
issue_number = None
484-
485-
if metadata.get("gh-issue") is not None:
486-
try:
487-
issue_number = int(metadata.get('gh-issue'))
488-
except (TypeError, ValueError):
489-
throw("Invalid GitHub issue number! (" + repr(issue_number) + ")")
490-
elif metadata.get("bpo") is not None:
491-
try:
492-
issue_number = int(metadata.get('bpo'))
493-
except (TypeError, ValueError):
494-
throw("Invalid bpo issue number! (" + repr(issue_number) + ")")
495475

476+
issue_keys = {
477+
'gh-issue': 'GitHub',
478+
'bpo': 'bpo',
479+
}
480+
for key, value in metadata.items():
481+
# Iterate over metadata items in order.
482+
# We parsed the blurb file line by line,
483+
# so we'll insert metadata keys in the
484+
# order we see them. So if we issue the
485+
# errors in the order we see the keys,
486+
# we'll complain about the *first* error
487+
# we see in the blurb file, which is a
488+
# better user experience.
489+
if key in issue_keys:
490+
try:
491+
int(value)
492+
except (TypeError, ValueError):
493+
throw(f"Invalid {issue_keys[key]} issue number! ({value!r})")
494+
495+
if key == "section":
496+
if no_changes:
497+
continue
498+
if value not in sections:
499+
throw(f"Invalid section {value!r}! You must use one of the predefined sections.")
500+
501+
if not 'section' in metadata:
502+
throw("No 'section' specified. You must provide one!")
496503

497504
self.append((metadata, text))
498505
metadata = {}
@@ -854,6 +861,13 @@ def test(*args):
854861
# unittest.main doesn't work because this isn't a module
855862
# so we'll do it ourselves
856863

864+
while not (os.path.isdir(".git") and os.path.isdir("blurb")):
865+
old_dir = os.getcwd()
866+
os.chdir("..")
867+
if old_dir == os.getcwd():
868+
# we reached the root and never found it!
869+
sys.exit("Error: Couldn't find the root of your blurb repo!")
870+
857871
print("-" * 79)
858872

859873
for clsname, cls in sorted(globals().items()):
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.. gh-issue: abcde
2+
.. section: Library
3+
4+
Things, stuff.

blurb/tests/fail/invalid-section.rst

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.. gh-issue: 8675309
2+
.. section: Funky Kong
3+
4+
This is an invalid blurb. Shockingly, "Funky Kong" is not a valid section name.

blurb/tests/fail/no-gh-number.rst

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.. gh-issue:
2+
.. section: Library
3+
4+
Things, stuff.

blurb/tests/fail/no-section.rst

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.. gh-issue: 8675309
2+
3+
This is an invalid blurb. It doesn't have a "section".

0 commit comments

Comments
 (0)