Skip to content

Commit

Permalink
Validate gh-issue is int before checking range, and that gh-issue or …
Browse files Browse the repository at this point in the history
…bpo exists (#35)
  • Loading branch information
hugovk authored Nov 10, 2024
2 parents 2a19feb + 60f10af commit 255d115
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 5 deletions.
11 changes: 7 additions & 4 deletions src/blurb/blurb.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,21 +482,24 @@ 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:
continue
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!")

Expand Down
66 changes: 65 additions & 1 deletion tests/test_blurb.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import pytest
from pyfakefs.fake_filesystem import FakeFilesystem

from blurb import blurb

Expand Down Expand Up @@ -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)

0 comments on commit 255d115

Please sign in to comment.