From 25f2034ba95f4f42df4fedacfcfa1319bd2b6178 Mon Sep 17 00:00:00 2001 From: Larry Hastings Date: Sun, 23 Jul 2023 15:31:29 +0200 Subject: [PATCH] Improved error checking when parsing a Blurb. 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.) --- blurb/blurb.py | 54 ++++++++++++++++---------- blurb/tests/fail/invalid-gh-number.rst | 4 ++ blurb/tests/fail/invalid-section.rst | 4 ++ blurb/tests/fail/no-gh-number.rst | 4 ++ blurb/tests/fail/no-section.rst | 3 ++ 5 files changed, 49 insertions(+), 20 deletions(-) create mode 100644 blurb/tests/fail/invalid-gh-number.rst create mode 100644 blurb/tests/fail/invalid-section.rst create mode 100644 blurb/tests/fail/no-gh-number.rst create mode 100644 blurb/tests/fail/no-section.rst diff --git a/blurb/blurb.py b/blurb/blurb.py index 51f1087..7557c23 100755 --- a/blurb/blurb.py +++ b/blurb/blurb.py @@ -472,27 +472,34 @@ def finish_entry(): throw("Blurb 'body' can't start with " + repr(naughty_prefix) + "!") no_changes = metadata.get('no changes') - section = metadata.get('section') - - if not no_changes: - if not section: - throw("No 'section' specified. You must provide one!") - elif section not in sections: - throw("Invalid 'section'! You must use one of the predefined sections.") - - issue_number = None - - if metadata.get("gh-issue") is not None: - try: - issue_number = int(metadata.get('gh-issue')) - except (TypeError, ValueError): - throw("Invalid GitHub issue number! (" + repr(issue_number) + ")") - elif metadata.get("bpo") is not None: - try: - issue_number = int(metadata.get('bpo')) - except (TypeError, ValueError): - throw("Invalid bpo issue number! (" + repr(issue_number) + ")") + issue_keys = { + 'gh-issue': 'GitHub', + 'bpo': 'bpo', + } + for key, value in metadata.items(): + # Iterate over metadata items in order. + # We parsed the blurb file line by line, + # so we'll insert metadata keys in the + # order we see them. So if we issue the + # errors in the order we see the keys, + # we'll complain about the *first* error + # we see in the blurb file, which is a + # better user experience. + if key in issue_keys: + try: + int(value) + except (TypeError, ValueError): + throw(f"Invalid {issue_keys[key]} issue number! ({value!r})") + + if key == "section": + if no_changes: + continue + if value not in sections: + throw(f"Invalid section {value!r}! You must use one of the predefined sections.") + + if not 'section' in metadata: + throw("No 'section' specified. You must provide one!") self.append((metadata, text)) metadata = {} @@ -854,6 +861,13 @@ def test(*args): # unittest.main doesn't work because this isn't a module # so we'll do it ourselves + while not (os.path.isdir(".git") and os.path.isdir("blurb")): + old_dir = os.getcwd() + os.chdir("..") + if old_dir == os.getcwd(): + # we reached the root and never found it! + sys.exit("Error: Couldn't find the root of your blurb repo!") + print("-" * 79) for clsname, cls in sorted(globals().items()): diff --git a/blurb/tests/fail/invalid-gh-number.rst b/blurb/tests/fail/invalid-gh-number.rst new file mode 100644 index 0000000..6d60917 --- /dev/null +++ b/blurb/tests/fail/invalid-gh-number.rst @@ -0,0 +1,4 @@ +.. gh-issue: abcde +.. section: Library + +Things, stuff. \ No newline at end of file diff --git a/blurb/tests/fail/invalid-section.rst b/blurb/tests/fail/invalid-section.rst new file mode 100644 index 0000000..1c0af55 --- /dev/null +++ b/blurb/tests/fail/invalid-section.rst @@ -0,0 +1,4 @@ +.. gh-issue: 8675309 +.. section: Funky Kong + +This is an invalid blurb. Shockingly, "Funky Kong" is not a valid section name. \ No newline at end of file diff --git a/blurb/tests/fail/no-gh-number.rst b/blurb/tests/fail/no-gh-number.rst new file mode 100644 index 0000000..480fcbf --- /dev/null +++ b/blurb/tests/fail/no-gh-number.rst @@ -0,0 +1,4 @@ +.. gh-issue: +.. section: Library + +Things, stuff. \ No newline at end of file diff --git a/blurb/tests/fail/no-section.rst b/blurb/tests/fail/no-section.rst new file mode 100644 index 0000000..f6e06aa --- /dev/null +++ b/blurb/tests/fail/no-section.rst @@ -0,0 +1,3 @@ +.. gh-issue: 8675309 + +This is an invalid blurb. It doesn't have a "section". \ No newline at end of file pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy