|
| 1 | +"""Tests for extract_changelog.py. |
| 2 | +
|
| 3 | +Covers two concerns: |
| 4 | +1. Correctness of the extraction logic (unit tests against fixture content). |
| 5 | +2. Format validity of the repository's actual ChangeLog file. |
| 6 | +
|
| 7 | +The format tests act as a CI guard: if a contributor adds a malformed version |
| 8 | +header or an unrecognised section type to ChangeLog, the test suite will fail. |
| 9 | +""" |
| 10 | + |
| 11 | +import pathlib |
| 12 | +import re |
| 13 | + |
| 14 | +import pytest |
| 15 | +from extract_changelog import extract |
| 16 | + |
| 17 | +# --------------------------------------------------------------------------- |
| 18 | +# Paths |
| 19 | +# --------------------------------------------------------------------------- |
| 20 | + |
| 21 | +REPO_ROOT = pathlib.Path(__file__).parent.parent.parent |
| 22 | +CHANGELOG = REPO_ROOT / "ChangeLog" |
| 23 | + |
| 24 | +# --------------------------------------------------------------------------- |
| 25 | +# ChangeLog format rules (Keep a Changelog) |
| 26 | +# --------------------------------------------------------------------------- |
| 27 | + |
| 28 | +# Pre-release suffixes (a1, b2, rc3) follow PEP 440 conventions used in this project. |
| 29 | +VERSION_HEADER_RE = re.compile( |
| 30 | + r"^## \[\d+\.\d+\.\d+(?:a\d+|b\d+|rc\d+)?\] - (\d{4}-\d{2}-\d{2}|unreleased)$" |
| 31 | +) |
| 32 | +# 'General' is a project-specific extension used for cross-cutting changes. |
| 33 | +SECTION_HEADER_RE = re.compile(r"^### (Added|Changed|Deprecated|Removed|Fixed|Security|General)$") |
| 34 | + |
| 35 | +# --------------------------------------------------------------------------- |
| 36 | +# Fixtures |
| 37 | +# --------------------------------------------------------------------------- |
| 38 | + |
| 39 | +SAMPLE_CHANGELOG = """\ |
| 40 | +# Changelog |
| 41 | +
|
| 42 | +## [2.0.0] - 2024-01-15 |
| 43 | +
|
| 44 | +### Added |
| 45 | +- Feature A |
| 46 | +
|
| 47 | +### Fixed |
| 48 | +- Bug B |
| 49 | +
|
| 50 | +## [1.0.0] - 2023-06-01 |
| 51 | +
|
| 52 | +### Added |
| 53 | +- Initial release |
| 54 | +""" |
| 55 | + |
| 56 | +UNRELEASED_CHANGELOG = ( |
| 57 | + """\ |
| 58 | +# Changelog |
| 59 | +
|
| 60 | +## [3.0.0] - unreleased |
| 61 | +
|
| 62 | +### Added |
| 63 | +- Upcoming feature |
| 64 | +
|
| 65 | +""" |
| 66 | + + SAMPLE_CHANGELOG |
| 67 | +) |
| 68 | + |
| 69 | + |
| 70 | +# --------------------------------------------------------------------------- |
| 71 | +# Extraction unit tests |
| 72 | +# --------------------------------------------------------------------------- |
| 73 | + |
| 74 | + |
| 75 | +def test_extract_known_version(tmp_path): |
| 76 | + changelog = tmp_path / "ChangeLog" |
| 77 | + changelog.write_text(SAMPLE_CHANGELOG) |
| 78 | + output = tmp_path / "notes.md" |
| 79 | + |
| 80 | + extract("2.0.0", changelog, output) |
| 81 | + |
| 82 | + content = output.read_text() |
| 83 | + assert "Feature A" in content |
| 84 | + assert "Bug B" in content |
| 85 | + |
| 86 | + |
| 87 | +def test_extract_does_not_bleed_into_next_version(tmp_path): |
| 88 | + changelog = tmp_path / "ChangeLog" |
| 89 | + changelog.write_text(SAMPLE_CHANGELOG) |
| 90 | + output = tmp_path / "notes.md" |
| 91 | + |
| 92 | + extract("2.0.0", changelog, output) |
| 93 | + |
| 94 | + assert "Initial release" not in output.read_text() |
| 95 | + |
| 96 | + |
| 97 | +def test_extract_last_version_in_file(tmp_path): |
| 98 | + changelog = tmp_path / "ChangeLog" |
| 99 | + changelog.write_text(SAMPLE_CHANGELOG) |
| 100 | + output = tmp_path / "notes.md" |
| 101 | + |
| 102 | + extract("1.0.0", changelog, output) |
| 103 | + |
| 104 | + assert "Initial release" in output.read_text() |
| 105 | + |
| 106 | + |
| 107 | +def test_extract_unreleased_version(tmp_path): |
| 108 | + changelog = tmp_path / "ChangeLog" |
| 109 | + changelog.write_text(UNRELEASED_CHANGELOG) |
| 110 | + output = tmp_path / "notes.md" |
| 111 | + |
| 112 | + extract("3.0.0", changelog, output) |
| 113 | + |
| 114 | + assert "Upcoming feature" in output.read_text() |
| 115 | + |
| 116 | + |
| 117 | +def test_extract_missing_version_exits(tmp_path): |
| 118 | + changelog = tmp_path / "ChangeLog" |
| 119 | + changelog.write_text(SAMPLE_CHANGELOG) |
| 120 | + output = tmp_path / "notes.md" |
| 121 | + |
| 122 | + with pytest.raises(SystemExit) as exc_info: |
| 123 | + extract("99.0.0", changelog, output) |
| 124 | + |
| 125 | + assert exc_info.value.code != 0 |
| 126 | + |
| 127 | + |
| 128 | +def test_extract_output_is_stripped(tmp_path): |
| 129 | + changelog = tmp_path / "ChangeLog" |
| 130 | + changelog.write_text(SAMPLE_CHANGELOG) |
| 131 | + output = tmp_path / "notes.md" |
| 132 | + |
| 133 | + extract("2.0.0", changelog, output) |
| 134 | + |
| 135 | + content = output.read_text() |
| 136 | + assert content == content.strip() |
| 137 | + |
| 138 | + |
| 139 | +# --------------------------------------------------------------------------- |
| 140 | +# ChangeLog format validation tests |
| 141 | +# --------------------------------------------------------------------------- |
| 142 | + |
| 143 | + |
| 144 | +def _changelog_lines(): |
| 145 | + """Return (line_number, line) pairs for the repository ChangeLog.""" |
| 146 | + return list(enumerate(CHANGELOG.read_text().splitlines(), start=1)) |
| 147 | + |
| 148 | + |
| 149 | +@pytest.mark.parametrize( |
| 150 | + "lineno,line", |
| 151 | + [(line_number, line) for line_number, line in _changelog_lines() if line.startswith("## ")], |
| 152 | +) |
| 153 | +def test_version_header_format(lineno, line): |
| 154 | + """Every '## ' line must match '## [X.Y.Z] - YYYY-MM-DD' or '## [X.Y.Z] - unreleased'.""" |
| 155 | + assert VERSION_HEADER_RE.match(line), ( |
| 156 | + f"ChangeLog line {lineno}: invalid version header: {line!r}\n" |
| 157 | + "Expected: ## [X.Y.Z] - YYYY-MM-DD or ## [X.Y.Z] - unreleased" |
| 158 | + ) |
| 159 | + |
| 160 | + |
| 161 | +@pytest.mark.parametrize( |
| 162 | + "lineno,line", |
| 163 | + [(line_number, line) for line_number, line in _changelog_lines() if line.startswith("### ")], |
| 164 | +) |
| 165 | +def test_section_header_type(lineno, line): |
| 166 | + """Every '### ' line must be one of the recognised Keep a Changelog types.""" |
| 167 | + assert SECTION_HEADER_RE.match(line), ( |
| 168 | + f"ChangeLog line {lineno}: unrecognised section header: {line!r}\n" |
| 169 | + "Allowed: ### Added, ### Changed, ### Deprecated, " |
| 170 | + "### Removed, ### Fixed, ### Security" |
| 171 | + ) |
0 commit comments