|
| 1 | +import datetime |
| 2 | +import os |
| 3 | +import pathlib |
| 4 | +import shutil |
| 5 | +import tempfile |
| 6 | +import unittest |
| 7 | + |
| 8 | +from tools.private.release import release as releaser |
| 9 | + |
| 10 | +_UNRELEASED_TEMPLATE = """ |
| 11 | +<!-- |
| 12 | +BEGIN_UNRELEASED_TEMPLATE |
| 13 | +
|
| 14 | +{#v0-0-0} |
| 15 | +## Unreleased |
| 16 | +
|
| 17 | +[0.0.0]: https://github.com/bazel-contrib/rules_python/releases/tag/0.0.0 |
| 18 | +
|
| 19 | +{#v0-0-0-changed} |
| 20 | +### Changed |
| 21 | +* Nothing changed. |
| 22 | +
|
| 23 | +{#v0-0-0-fixed} |
| 24 | +### Fixed |
| 25 | +* Nothing fixed. |
| 26 | +
|
| 27 | +{#v0-0-0-added} |
| 28 | +### Added |
| 29 | +* Nothing added. |
| 30 | +
|
| 31 | +{#v0-0-0-removed} |
| 32 | +### Removed |
| 33 | +* Nothing removed. |
| 34 | +
|
| 35 | +END_UNRELEASED_TEMPLATE |
| 36 | +--> |
| 37 | +""" |
| 38 | + |
| 39 | + |
| 40 | +class ReleaserTest(unittest.TestCase): |
| 41 | + def setUp(self): |
| 42 | + self.tmpdir = pathlib.Path(tempfile.mkdtemp()) |
| 43 | + self.original_cwd = os.getcwd() |
| 44 | + self.addCleanup(shutil.rmtree, self.tmpdir) |
| 45 | + |
| 46 | + os.chdir(self.tmpdir) |
| 47 | + # NOTE: On windows, this must be done before files are deleted. |
| 48 | + self.addCleanup(os.chdir, self.original_cwd) |
| 49 | + |
| 50 | + def test_update_changelog(self): |
| 51 | + changelog = f""" |
| 52 | +# Changelog |
| 53 | +
|
| 54 | +{_UNRELEASED_TEMPLATE} |
| 55 | +
|
| 56 | +{{#v0-0-0}} |
| 57 | +## Unreleased |
| 58 | +
|
| 59 | +[0.0.0]: https://github.com/bazel-contrib/rules_python/releases/tag/0.0.0 |
| 60 | +
|
| 61 | +{{#v0-0-0-changed}} |
| 62 | +### Changed |
| 63 | +* Nothing changed |
| 64 | +
|
| 65 | +{{#v0-0-0-fixed}} |
| 66 | +### Fixed |
| 67 | +* Nothing fixed |
| 68 | +
|
| 69 | +{{#v0-0-0-added}} |
| 70 | +### Added |
| 71 | +* Nothing added |
| 72 | +
|
| 73 | +{{#v0-0-0-removed}} |
| 74 | +### Removed |
| 75 | +* Nothing removed. |
| 76 | +""" |
| 77 | + changelog_path = self.tmpdir / "CHANGELOG.md" |
| 78 | + changelog_path.write_text(changelog) |
| 79 | + |
| 80 | + # Act |
| 81 | + releaser.update_changelog( |
| 82 | + "1.23.4", |
| 83 | + "2025-01-01", |
| 84 | + changelog_path=changelog_path, |
| 85 | + ) |
| 86 | + |
| 87 | + # Assert |
| 88 | + new_content = changelog_path.read_text() |
| 89 | + |
| 90 | + self.assertIn( |
| 91 | + _UNRELEASED_TEMPLATE, new_content, msg=f"ACTUAL:\n\n{new_content}\n\n" |
| 92 | + ) |
| 93 | + self.assertIn(f"## [1.23.4] - 2025-01-01", new_content) |
| 94 | + self.assertIn( |
| 95 | + f"[1.23.4]: https://github.com/bazel-contrib/rules_python/releases/tag/1.23.4", |
| 96 | + new_content, |
| 97 | + ) |
| 98 | + self.assertIn("{#v1-23-4}", new_content) |
| 99 | + self.assertIn("{#v1-23-4-changed}", new_content) |
| 100 | + self.assertIn("{#v1-23-4-fixed}", new_content) |
| 101 | + self.assertIn("{#v1-23-4-added}", new_content) |
| 102 | + self.assertIn("{#v1-23-4-removed}", new_content) |
| 103 | + |
| 104 | + def test_replace_version_next(self): |
| 105 | + # Arrange |
| 106 | + mock_file_content = """ |
| 107 | +:::{versionadded} VERSION_NEXT_FEATURE |
| 108 | +blabla |
| 109 | +::: |
| 110 | +
|
| 111 | +:::{versionchanged} VERSION_NEXT_PATCH |
| 112 | +blabla |
| 113 | +::: |
| 114 | +""" |
| 115 | + (self.tmpdir / "mock_file.bzl").write_text(mock_file_content) |
| 116 | + |
| 117 | + releaser.replace_version_next("0.28.0") |
| 118 | + |
| 119 | + new_content = (self.tmpdir / "mock_file.bzl").read_text() |
| 120 | + |
| 121 | + self.assertIn(":::{versionadded} 0.28.0", new_content) |
| 122 | + self.assertIn(":::{versionadded} 0.28.0", new_content) |
| 123 | + self.assertNotIn("VERSION_NEXT_FEATURE", new_content) |
| 124 | + self.assertNotIn("VERSION_NEXT_PATCH", new_content) |
| 125 | + |
| 126 | + def test_replace_version_next_excludes_bazel_dirs(self): |
| 127 | + # Arrange |
| 128 | + mock_file_content = """ |
| 129 | +:::{versionadded} VERSION_NEXT_FEATURE |
| 130 | +blabla |
| 131 | +::: |
| 132 | +""" |
| 133 | + bazel_dir = self.tmpdir / "bazel-rules_python" |
| 134 | + bazel_dir.mkdir() |
| 135 | + (bazel_dir / "mock_file.bzl").write_text(mock_file_content) |
| 136 | + |
| 137 | + tools_dir = self.tmpdir / "tools" / "private" / "release" |
| 138 | + tools_dir.mkdir(parents=True) |
| 139 | + (tools_dir / "mock_file.bzl").write_text(mock_file_content) |
| 140 | + |
| 141 | + tests_dir = self.tmpdir / "tests" / "tools" / "private" / "release" |
| 142 | + tests_dir.mkdir(parents=True) |
| 143 | + (tests_dir / "mock_file.bzl").write_text(mock_file_content) |
| 144 | + |
| 145 | + version = "0.28.0" |
| 146 | + |
| 147 | + # Act |
| 148 | + releaser.replace_version_next(version) |
| 149 | + |
| 150 | + # Assert |
| 151 | + new_content = (bazel_dir / "mock_file.bzl").read_text() |
| 152 | + self.assertIn("VERSION_NEXT_FEATURE", new_content) |
| 153 | + |
| 154 | + new_content = (tools_dir / "mock_file.bzl").read_text() |
| 155 | + self.assertIn("VERSION_NEXT_FEATURE", new_content) |
| 156 | + |
| 157 | + new_content = (tests_dir / "mock_file.bzl").read_text() |
| 158 | + self.assertIn("VERSION_NEXT_FEATURE", new_content) |
| 159 | + |
| 160 | + def test_valid_version(self): |
| 161 | + # These should not raise an exception |
| 162 | + releaser.create_parser().parse_args(["0.28.0"]) |
| 163 | + releaser.create_parser().parse_args(["1.0.0"]) |
| 164 | + releaser.create_parser().parse_args(["1.2.3rc4"]) |
| 165 | + |
| 166 | + def test_invalid_version(self): |
| 167 | + with self.assertRaises(SystemExit): |
| 168 | + releaser.create_parser().parse_args(["0.28"]) |
| 169 | + with self.assertRaises(SystemExit): |
| 170 | + releaser.create_parser().parse_args(["a.b.c"]) |
| 171 | + |
| 172 | + |
| 173 | +if __name__ == "__main__": |
| 174 | + unittest.main() |
0 commit comments