|
| 1 | +import re |
| 2 | +from pathlib import Path |
| 3 | + |
| 4 | + |
| 5 | +class TestChangelog: |
| 6 | + def setup_method(self): |
| 7 | + self.changelog_path = Path(__file__).parent.parent / "CHANGELOG.md" |
| 8 | + |
| 9 | + def test_changelog_exists(self): |
| 10 | + assert self.changelog_path.exists(), "CHANGELOG.md file should exist" |
| 11 | + |
| 12 | + def test_changelog_starts_with_header(self): |
| 13 | + content = self.changelog_path.read_text() |
| 14 | + assert content.startswith("# Changelog"), ( |
| 15 | + "Changelog should start with '# Changelog'" |
| 16 | + ) |
| 17 | + |
| 18 | + def test_changelog_has_valid_version_format(self): |
| 19 | + content = self.changelog_path.read_text() |
| 20 | + lines = content.split("\n") |
| 21 | + |
| 22 | + version_pattern = re.compile(r"^## \d+\.\d+\.\d+(?:\s+\(\d{4}-\d{2}-\d{2}\))?$") |
| 23 | + versions = [] |
| 24 | + |
| 25 | + for line in lines: |
| 26 | + if line.startswith("## "): |
| 27 | + assert version_pattern.match(line), f"Invalid version format: {line}" |
| 28 | + version_match = re.match(r"^## (\d+\.\d+\.\d+)", line) |
| 29 | + if version_match: |
| 30 | + versions.append(version_match.group(1)) |
| 31 | + |
| 32 | + assert len(versions) > 0, "Changelog should contain at least one version" |
| 33 | + |
| 34 | + def test_changelog_has_bullet_points(self): |
| 35 | + content = self.changelog_path.read_text() |
| 36 | + lines = content.split("\n") |
| 37 | + |
| 38 | + in_version_section = False |
| 39 | + has_bullet_points = False |
| 40 | + |
| 41 | + for i, line in enumerate(lines): |
| 42 | + if line.startswith("## "): |
| 43 | + if in_version_section and not has_bullet_points: |
| 44 | + raise AssertionError( |
| 45 | + "Previous version section should have at least one bullet point" |
| 46 | + ) |
| 47 | + in_version_section = True |
| 48 | + has_bullet_points = False |
| 49 | + elif in_version_section and line.startswith("- "): |
| 50 | + has_bullet_points = True |
| 51 | + elif in_version_section and line.strip() == "" and i == len(lines) - 1: |
| 52 | + # Last line check |
| 53 | + assert has_bullet_points, ( |
| 54 | + "Each version should have at least one bullet point" |
| 55 | + ) |
| 56 | + |
| 57 | + # Check the last section |
| 58 | + if in_version_section: |
| 59 | + assert has_bullet_points, ( |
| 60 | + "Last version section should have at least one bullet point" |
| 61 | + ) |
| 62 | + |
| 63 | + def test_changelog_versions_in_descending_order(self): |
| 64 | + content = self.changelog_path.read_text() |
| 65 | + lines = content.split("\n") |
| 66 | + |
| 67 | + versions = [] |
| 68 | + for line in lines: |
| 69 | + if line.startswith("## "): |
| 70 | + version_match = re.match(r"^## (\d+)\.(\d+)\.(\d+)", line) |
| 71 | + if version_match: |
| 72 | + versions.append(tuple(map(int, version_match.groups()))) |
| 73 | + |
| 74 | + for i in range(1, len(versions)): |
| 75 | + assert versions[i - 1] > versions[i], ( |
| 76 | + f"Versions should be in descending order: {versions[i - 1]} should be > {versions[i]}" |
| 77 | + ) |
| 78 | + |
| 79 | + def test_changelog_no_empty_bullet_points(self): |
| 80 | + content = self.changelog_path.read_text() |
| 81 | + lines = content.split("\n") |
| 82 | + |
| 83 | + for line in lines: |
| 84 | + if line.strip() == "-": |
| 85 | + raise AssertionError("Changelog should not have empty bullet points") |
0 commit comments