From 4cb4857c8cb98df1a93fc8b9074401c0ef818d47 Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Date: Sat, 9 Nov 2024 18:49:58 +0200 Subject: [PATCH 1/6] Unit test Blurbs.parse --- tests/test_blurb.py | 57 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/tests/test_blurb.py b/tests/test_blurb.py index 9ff3a8d..09eb12f 100644 --- a/tests/test_blurb.py +++ b/tests/test_blurb.py @@ -188,3 +188,60 @@ 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"The gh-issue number must be 32426 or above, not a PR number", + ), + ( + "..bpo: one-two\n..section: IDLE\nHello world!", + r"Invalid bpo issue 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!", + ), + ), +) +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) From 8e590fcd3f5abac027ddef48417dc071efcb09b3 Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Date: Sat, 9 Nov 2024 18:58:14 +0200 Subject: [PATCH 2/6] Check gh-issue- is int before checking range for better error Invalid GitHub issue number! ('one-two') instead of: ValueError: invalid literal for int() with base 10: 'one-two' --- src/blurb/blurb.py | 6 +++--- tests/test_blurb.py | 4 ++++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/blurb/blurb.py b/src/blurb/blurb.py index 0af3ea9..ec0e73d 100755 --- a/src/blurb/blurb.py +++ b/src/blurb/blurb.py @@ -482,15 +482,15 @@ 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})") + 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 == "section": if no_changes: continue diff --git a/tests/test_blurb.py b/tests/test_blurb.py index 09eb12f..7e898fa 100644 --- a/tests/test_blurb.py +++ b/tests/test_blurb.py @@ -224,6 +224,10 @@ def test_parse(): "..bpo: one-two\n..section: IDLE\nHello world!", r"Invalid bpo issue number! \('one-two'\)", ), + ( + "..gh-issue: one-two\n..section: IDLE\nHello world!", + r"Invalid GitHub issue 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", From 657a617f61845c964c51ce6570c474fb09c7b97a Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Date: Sat, 9 Nov 2024 19:40:56 +0200 Subject: [PATCH 3/6] Add spaces --- tests/test_blurb.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/test_blurb.py b/tests/test_blurb.py index 7e898fa..1ac7b02 100644 --- a/tests/test_blurb.py +++ b/tests/test_blurb.py @@ -217,23 +217,23 @@ def test_parse(): r"Blurb 'body' can't start with 'gh-'!", ), ( - "..gh-issue: 1\n..section: IDLE\nHello world!", + ".. gh-issue: 1\n.. section: IDLE\nHello world!", r"The gh-issue number must be 32426 or above, not a PR number", ), ( - "..bpo: one-two\n..section: IDLE\nHello world!", + ".. bpo: one-two\n.. section: IDLE\nHello world!", r"Invalid bpo issue number! \('one-two'\)", ), ( - "..gh-issue: one-two\n..section: IDLE\nHello world!", + ".. gh-issue: one-two\n.. section: IDLE\nHello world!", r"Invalid GitHub issue number! \('one-two'\)", ), ( - "..gh-issue: 123456\n..section: Funky Kong\nHello world!", + ".. 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!", + ".. gh-issue: 123456\nHello world!", r"No 'section' specified. You must provide one!", ), ( From 04b6913c6624610405064ac1ca99f12e0db0e02f Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Date: Sat, 9 Nov 2024 19:44:35 +0200 Subject: [PATCH 4/6] Ensure gh-issue or bpo exists in metadata --- src/blurb/blurb.py | 3 +++ tests/test_blurb.py | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/src/blurb/blurb.py b/src/blurb/blurb.py index ec0e73d..bb09b71 100755 --- a/src/blurb/blurb.py +++ b/src/blurb/blurb.py @@ -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 1ac7b02..baebd2e 100644 --- a/tests/test_blurb.py +++ b/tests/test_blurb.py @@ -240,6 +240,10 @@ def test_parse(): ".. 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): From ac5a10133077a655a3c8b40adaaf85ab197d7a02 Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Date: Sat, 9 Nov 2024 20:40:39 +0200 Subject: [PATCH 5/6] Remove unused test --- tests/test_blurb.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_blurb.py b/tests/test_blurb.py index baebd2e..3570e79 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 From 60f10af6a0df9dd8d8b433536871caca00768e6e Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Date: Sun, 10 Nov 2024 11:41:57 +0200 Subject: [PATCH 6/6] Improve error wording Co-authored-by: Ezio Melotti --- src/blurb/blurb.py | 4 ++-- tests/test_blurb.py | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/blurb/blurb.py b/src/blurb/blurb.py index bb09b71..25c3b87 100755 --- a/src/blurb/blurb.py +++ b/src/blurb/blurb.py @@ -486,10 +486,10 @@ def finish_entry(): 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"The gh-issue number must be {lowest_possible_gh_issue_number} or above, not a PR number.") + throw(f"Invalid gh-issue number: {value!r} (must be >= {lowest_possible_gh_issue_number})") if key == "section": if no_changes: diff --git a/tests/test_blurb.py b/tests/test_blurb.py index 3570e79..cd8f20a 100644 --- a/tests/test_blurb.py +++ b/tests/test_blurb.py @@ -217,15 +217,15 @@ def test_parse(): ), ( ".. gh-issue: 1\n.. section: IDLE\nHello world!", - r"The gh-issue number must be 32426 or above, not a PR number", + r"Invalid gh-issue number: '1' \(must be >= 32426\)", ), ( ".. bpo: one-two\n.. section: IDLE\nHello world!", - r"Invalid bpo issue number! \('one-two'\)", + r"Invalid bpo number: 'one-two'", ), ( ".. gh-issue: one-two\n.. section: IDLE\nHello world!", - r"Invalid GitHub issue number! \('one-two'\)", + r"Invalid GitHub number: 'one-two'", ), ( ".. gh-issue: 123456\n.. section: Funky Kong\nHello world!",