Skip to content

Validate gh-issue is int before checking range, and that gh-issue or bpo exists #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you trying to get us sued by Nintendo?!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

),
(
".. 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)