|
| 1 | +import re |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from blurb import blurb |
| 6 | + |
| 7 | + |
| 8 | +def test_valid_no_issue_number(): |
| 9 | + assert blurb._extract_issue_number(None) is None |
| 10 | + res = blurb._blurb_template_text(issue=None) |
| 11 | + lines = frozenset(res.splitlines()) |
| 12 | + assert '.. gh-issue:' not in lines |
| 13 | + assert '.. gh-issue: ' in lines |
| 14 | + |
| 15 | + |
| 16 | +@pytest.mark.parametrize('issue', ( |
| 17 | + # issue given by their number |
| 18 | + '12345', |
| 19 | + ' 12345 ', |
| 20 | + # issue given by their number and a 'GH-' prefix |
| 21 | + 'GH-12345', |
| 22 | + ' GH-12345 ', |
| 23 | + # issue given by their number and a 'gh-' prefix |
| 24 | + 'gh-12345', |
| 25 | + ' gh-12345 ', |
| 26 | + # issue given by their number and a '#' prefix |
| 27 | + '#12345', |
| 28 | + ' #12345 ', |
| 29 | + # issue given by their URL (no scheme) |
| 30 | + 'github.com/python/cpython/issues/12345', |
| 31 | + ' github.com/python/cpython/issues/12345 ', |
| 32 | + # issue given by their URL (with scheme) |
| 33 | + 'https://github.com/python/cpython/issues/12345', |
| 34 | + ' https://github.com/python/cpython/issues/12345 ', |
| 35 | +)) |
| 36 | +def test_valid_issue_number_12345(issue): |
| 37 | + actual = blurb._extract_issue_number(issue) |
| 38 | + assert actual == 12345 |
| 39 | + |
| 40 | + res = blurb._blurb_template_text(issue=issue) |
| 41 | + lines = frozenset(res.splitlines()) |
| 42 | + assert '.. gh-issue:' not in lines |
| 43 | + assert '.. gh-issue: ' not in lines |
| 44 | + assert '.. gh-issue: 12345' in lines |
| 45 | + |
| 46 | + |
| 47 | +@pytest.mark.parametrize('issue', ( |
| 48 | + '', |
| 49 | + 'abc', |
| 50 | + 'Gh-123', |
| 51 | + 'gh-abc', |
| 52 | + 'gh- 123', |
| 53 | + 'gh -123', |
| 54 | + 'gh-', |
| 55 | + 'bpo-', |
| 56 | + 'bpo-12345', |
| 57 | + 'github.com/python/cpython/issues', |
| 58 | + 'github.com/python/cpython/issues/', |
| 59 | + 'github.com/python/cpython/issues/abc', |
| 60 | + 'github.com/python/cpython/issues/gh-abc', |
| 61 | + 'github.com/python/cpython/issues/gh-123', |
| 62 | + 'github.com/python/cpython/issues/1234?param=1', |
| 63 | + 'https://github.com/python/cpython/issues', |
| 64 | + 'https://github.com/python/cpython/issues/', |
| 65 | + 'https://github.com/python/cpython/issues/abc', |
| 66 | + 'https://github.com/python/cpython/issues/gh-abc', |
| 67 | + 'https://github.com/python/cpython/issues/gh-123', |
| 68 | + 'https://github.com/python/cpython/issues/1234?param=1', |
| 69 | +)) |
| 70 | +def test_invalid_issue_number(issue): |
| 71 | + error_message = re.escape(f'Invalid GitHub issue: {issue}') |
| 72 | + with pytest.raises(SystemExit, match=error_message): |
| 73 | + blurb._blurb_template_text(issue=issue) |
| 74 | + |
| 75 | + |
| 76 | +@pytest.mark.parametrize('invalid', ( |
| 77 | + 'gh-issue: ', |
| 78 | + 'gh-issue: 1', |
| 79 | + 'gh-issue', |
| 80 | +)) |
| 81 | +def test_malformed_gh_issue_line(invalid, monkeypatch): |
| 82 | + template = blurb.template.replace('.. gh-issue:', invalid) |
| 83 | + error_message = re.escape("Can't find gh-issue line in the template!") |
| 84 | + with monkeypatch.context() as cm: |
| 85 | + cm.setattr(blurb, 'template', template) |
| 86 | + with pytest.raises(SystemExit, match=error_message): |
| 87 | + blurb._blurb_template_text(issue='1234') |
0 commit comments