|
| 1 | +import datetime |
| 2 | +import os |
| 3 | +import subprocess |
| 4 | +import sys |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +# Add the directory containing prepare_release.py to the path |
| 9 | +sys.path.insert(0, os.path.abspath(os.path.dirname(__file__))) |
| 10 | +import prepare_release |
| 11 | + |
| 12 | + |
| 13 | +def test_parse_version(): |
| 14 | + assert prepare_release.parse_version("2026.4.0") == (2026, 4, 0) |
| 15 | + assert prepare_release.parse_version("2026.12.5-post1") == (2026, 12, 5) |
| 16 | + |
| 17 | + with pytest.raises(ValueError, match="does not match expected CalVer pattern"): |
| 18 | + prepare_release.parse_version("v2026.4.0") |
| 19 | + with pytest.raises(ValueError, match="does not match expected CalVer pattern"): |
| 20 | + prepare_release.parse_version("abc") |
| 21 | + with pytest.raises(ValueError, match="does not match expected CalVer pattern"): |
| 22 | + prepare_release.parse_version("2026.4") |
| 23 | + |
| 24 | + |
| 25 | +def test_calculate_next_version(monkeypatch): |
| 26 | + class MockDatetime(datetime.datetime): |
| 27 | + @classmethod |
| 28 | + def now(cls, tz=None): |
| 29 | + return cls(2026, 4, 15, tzinfo=datetime.timezone.utc) |
| 30 | + |
| 31 | + monkeypatch.setattr("prepare_release.datetime.datetime", MockDatetime) |
| 32 | + |
| 33 | + # Same month: increment patch |
| 34 | + assert prepare_release.calculate_next_version("2026.4.0") == "2026.4.1" |
| 35 | + assert prepare_release.calculate_next_version("2026.4.5") == "2026.4.6" |
| 36 | + |
| 37 | + # New month: reset patch to 0 |
| 38 | + assert prepare_release.calculate_next_version("2026.3.5") == "2026.4.0" |
| 39 | + assert prepare_release.calculate_next_version("2025.12.10") == "2026.4.0" |
| 40 | + |
| 41 | + # Regression guard: new version (2026.4.0) must be newer than latest tag (2026.5.0) |
| 42 | + with pytest.raises(ValueError, match="Potential version regression"): |
| 43 | + prepare_release.calculate_next_version("2026.5.0") |
| 44 | + |
| 45 | + # Regression guard: new version (2026.4.0) must be newer than latest tag (2027.1.0) |
| 46 | + with pytest.raises(ValueError, match="Potential version regression"): |
| 47 | + prepare_release.calculate_next_version("2027.1.0") |
| 48 | + |
| 49 | + |
| 50 | +def test_get_latest_tag(monkeypatch): |
| 51 | + # Success case |
| 52 | + monkeypatch.setattr("prepare_release.run_cmd", lambda args: "2026.4.0") |
| 53 | + assert prepare_release.get_latest_tag() == "2026.4.0" |
| 54 | + |
| 55 | + # Error case |
| 56 | + def mock_run_cmd_fail(args): |
| 57 | + raise subprocess.CalledProcessError(1, args, stderr="git error") |
| 58 | + |
| 59 | + monkeypatch.setattr("prepare_release.run_cmd", mock_run_cmd_fail) |
| 60 | + with pytest.raises(subprocess.CalledProcessError): |
| 61 | + prepare_release.get_latest_tag() |
| 62 | + |
| 63 | + |
| 64 | +def test_get_changelog_entries(monkeypatch): |
| 65 | + # Success case with commits |
| 66 | + def mock_run_cmd_commits(args): |
| 67 | + assert args == [ |
| 68 | + "git", |
| 69 | + "log", |
| 70 | + "2026.4.0..HEAD", |
| 71 | + "--no-merges", |
| 72 | + "--pretty=format:* %s", |
| 73 | + ] |
| 74 | + return "* Commit 1 (abc1234)\n* Commit 2 (def5678)" |
| 75 | + |
| 76 | + monkeypatch.setattr("prepare_release.run_cmd", mock_run_cmd_commits) |
| 77 | + entries = prepare_release.get_changelog_entries("2026.4.0") |
| 78 | + assert entries == ["* Commit 1 (abc1234)", "* Commit 2 (def5678)"] |
| 79 | + |
| 80 | + # Success case with no commits (empty string) |
| 81 | + monkeypatch.setattr("prepare_release.run_cmd", lambda args: "") |
| 82 | + entries = prepare_release.get_changelog_entries("2026.4.0") |
| 83 | + assert entries == ["* No changes (released in sync with fsspec)."] |
| 84 | + |
| 85 | + # Error case: a git failure must propagate, not be swallowed into an |
| 86 | + # empty/placeholder changelog. |
| 87 | + def mock_run_cmd_fail(args): |
| 88 | + raise subprocess.CalledProcessError(1, args, stderr="git log error") |
| 89 | + |
| 90 | + monkeypatch.setattr("prepare_release.run_cmd", mock_run_cmd_fail) |
| 91 | + with pytest.raises(subprocess.CalledProcessError): |
| 92 | + prepare_release.get_changelog_entries("2026.4.0") |
| 93 | + |
| 94 | + |
| 95 | +def test_update_changelog_file(tmp_path): |
| 96 | + changelog = tmp_path / "changelog.rst" |
| 97 | + initial_content = """Changelog |
| 98 | +========= |
| 99 | +
|
| 100 | +2026.4.0 |
| 101 | +-------- |
| 102 | +
|
| 103 | +* Previous change |
| 104 | +""" |
| 105 | + changelog.write_text(initial_content, encoding="utf-8") |
| 106 | + |
| 107 | + prepare_release.update_changelog_file(str(changelog), "2026.4.1", ["* New feature"]) |
| 108 | + |
| 109 | + expected_content = """Changelog |
| 110 | +========= |
| 111 | +
|
| 112 | +2026.4.1 |
| 113 | +-------- |
| 114 | +
|
| 115 | +* New feature |
| 116 | +
|
| 117 | +2026.4.0 |
| 118 | +-------- |
| 119 | +
|
| 120 | +* Previous change |
| 121 | +""" |
| 122 | + assert changelog.read_text(encoding="utf-8") == expected_content |
| 123 | + |
| 124 | + |
| 125 | +def test_update_changelog_file_with_suffix(tmp_path): |
| 126 | + changelog = tmp_path / "changelog.rst" |
| 127 | + initial_content = """Changelog |
| 128 | +========= |
| 129 | +
|
| 130 | +2025.5.0post1 |
| 131 | +------------- |
| 132 | +
|
| 133 | +* Previous change |
| 134 | +""" |
| 135 | + changelog.write_text(initial_content, encoding="utf-8") |
| 136 | + |
| 137 | + prepare_release.update_changelog_file(str(changelog), "2026.4.1", ["* New feature"]) |
| 138 | + |
| 139 | + expected_content = """Changelog |
| 140 | +========= |
| 141 | +
|
| 142 | +2026.4.1 |
| 143 | +-------- |
| 144 | +
|
| 145 | +* New feature |
| 146 | +
|
| 147 | +2025.5.0post1 |
| 148 | +------------- |
| 149 | +
|
| 150 | +* Previous change |
| 151 | +""" |
| 152 | + assert changelog.read_text(encoding="utf-8") == expected_content |
| 153 | + |
| 154 | + |
| 155 | +def test_update_changelog_file_no_header(tmp_path): |
| 156 | + changelog = tmp_path / "changelog.rst" |
| 157 | + changelog.write_text("No header here", encoding="utf-8") |
| 158 | + |
| 159 | + with pytest.raises(ValueError, match="Could not find a valid version header"): |
| 160 | + prepare_release.update_changelog_file( |
| 161 | + str(changelog), "2026.4.1", ["* New feature"] |
| 162 | + ) |
| 163 | + |
| 164 | + |
| 165 | +def test_update_changelog_file_short_underline(tmp_path): |
| 166 | + changelog = tmp_path / "changelog.rst" |
| 167 | + initial_content = """Changelog |
| 168 | +========= |
| 169 | +
|
| 170 | +2026.4.0 |
| 171 | +-- |
| 172 | +
|
| 173 | +* Previous change |
| 174 | +""" |
| 175 | + changelog.write_text(initial_content, encoding="utf-8") |
| 176 | + |
| 177 | + with pytest.raises(ValueError, match="Could not find a valid version header"): |
| 178 | + prepare_release.update_changelog_file( |
| 179 | + str(changelog), "2026.4.1", ["* New feature"] |
| 180 | + ) |
0 commit comments