diff --git a/src/blurb/blurb.py b/src/blurb/blurb.py index 0af3ea9..25c3b87 100755 --- a/src/blurb/blurb.py +++ b/src/blurb/blurb.py @@ -482,14 +482,14 @@ def finish_entry(): # we'll complain about the *first* error # we see in the blurb file, which is a # better user experience. - if key == "gh-issue" and int(value) < lowest_possible_gh_issue_number: - throw(f"The gh-issue number must be {lowest_possible_gh_issue_number} or above, not a PR number.") - if key in issue_keys: try: int(value) except (TypeError, ValueError): - throw(f"Invalid {issue_keys[key]} issue number! ({value!r})") + throw(f"Invalid {issue_keys[key]} number: {value!r}") + + if key == "gh-issue" and int(value) < lowest_possible_gh_issue_number: + throw(f"Invalid gh-issue number: {value!r} (must be >= {lowest_possible_gh_issue_number})") if key == "section": if no_changes: @@ -497,6 +497,9 @@ def finish_entry(): if value not in sections: throw(f"Invalid section {value!r}! You must use one of the predefined sections.") + if "gh-issue" not in metadata and "bpo" not in metadata: + throw("'gh-issue:' or 'bpo:' must be specified in the metadata!") + if not 'section' in metadata: throw("No 'section' specified. You must provide one!") diff --git a/tests/test_blurb.py b/tests/test_blurb.py index 9ff3a8d..cd8f20a 100644 --- a/tests/test_blurb.py +++ b/tests/test_blurb.py @@ -1,5 +1,4 @@ import pytest -from pyfakefs.fake_filesystem import FakeFilesystem from blurb import blurb @@ -188,3 +187,68 @@ def test_version(capfd): # Assert captured = capfd.readouterr() assert captured.out.startswith("blurb version ") + + +def test_parse(): + # Arrange + contents = ".. gh-issue: 123456\n.. section: IDLE\nHello world!" + blurbs = blurb.Blurbs() + + # Act + blurbs.parse(contents) + + # Assert + metadata, body = blurbs[0] + assert metadata["gh-issue"] == "123456" + assert metadata["section"] == "IDLE" + assert body == "Hello world!\n" + + +@pytest.mark.parametrize( + "contents, expected_error", + ( + ( + "", + r"Blurb 'body' text must not be empty!", + ), + ( + "gh-issue: Hello world!", + r"Blurb 'body' can't start with 'gh-'!", + ), + ( + ".. gh-issue: 1\n.. section: IDLE\nHello world!", + r"Invalid gh-issue number: '1' \(must be >= 32426\)", + ), + ( + ".. bpo: one-two\n.. section: IDLE\nHello world!", + r"Invalid bpo number: 'one-two'", + ), + ( + ".. gh-issue: one-two\n.. section: IDLE\nHello world!", + r"Invalid GitHub number: 'one-two'", + ), + ( + ".. gh-issue: 123456\n.. section: Funky Kong\nHello world!", + r"Invalid section 'Funky Kong'! You must use one of the predefined sections", + ), + ( + ".. gh-issue: 123456\nHello world!", + r"No 'section' specified. You must provide one!", + ), + ( + ".. gh-issue: 123456\n.. section: IDLE\n.. section: IDLE\nHello world!", + r"Blurb metadata sets 'section' twice!", + ), + ( + ".. section: IDLE\nHello world!", + r"'gh-issue:' or 'bpo:' must be specified in the metadata!", + ), + ), +) +def test_parse_no_body(contents, expected_error): + # Arrange + blurbs = blurb.Blurbs() + + # Act / Assert + with pytest.raises(blurb.BlurbError, match=expected_error): + blurbs.parse(contents)