|
| 1 | +# Copyright 2025 The Bazel Authors. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import datetime |
| 16 | +import os |
| 17 | +import pathlib |
| 18 | +import shutil |
| 19 | +import tempfile |
| 20 | +import unittest |
| 21 | + |
| 22 | +from tools.private.release import release as releaser |
| 23 | + |
| 24 | +_UNRELEASED_TEMPLATE = """ |
| 25 | +<!-- |
| 26 | +BEGIN_UNRELEASED_TEMPLATE |
| 27 | +
|
| 28 | +{#v0-0-0} |
| 29 | +## Unreleased |
| 30 | +
|
| 31 | +[0.0.0]: https://github.com/bazel-contrib/rules_python/releases/tag/0.0.0 |
| 32 | +
|
| 33 | +{#v0-0-0-changed} |
| 34 | +### Changed |
| 35 | +* Nothing changed. |
| 36 | +
|
| 37 | +{#v0-0-0-fixed} |
| 38 | +### Fixed |
| 39 | +* Nothing fixed. |
| 40 | +
|
| 41 | +{#v0-0-0-added} |
| 42 | +### Added |
| 43 | +* Nothing added. |
| 44 | +
|
| 45 | +{#v0-0-0-removed} |
| 46 | +### Removed |
| 47 | +* Nothing removed. |
| 48 | +
|
| 49 | +END_UNRELEASED_TEMPLATE |
| 50 | +--> |
| 51 | +""" |
| 52 | + |
| 53 | + |
| 54 | +class ReleaserTest(unittest.TestCase): |
| 55 | + def setUp(self): |
| 56 | + self.tmpdir = pathlib.Path(tempfile.mkdtemp()) |
| 57 | + self.original_cwd = os.getcwd() |
| 58 | + os.chdir(self.tmpdir) |
| 59 | + self.addCleanup(os.chdir, self.original_cwd) |
| 60 | + self.addCleanup(shutil.rmtree, self.tmpdir) |
| 61 | + |
| 62 | + def test_update_changelog(self): |
| 63 | + changelog = f""" |
| 64 | +# Changelog |
| 65 | +
|
| 66 | +{_UNRELEASED_TEMPLATE} |
| 67 | +
|
| 68 | +{{#v0-0-0}} |
| 69 | +## Unreleased |
| 70 | +
|
| 71 | +[0.0.0]: https://github.com/bazel-contrib/rules_python/releases/tag/0.0.0 |
| 72 | +
|
| 73 | +{{#v0-0-0-changed}} |
| 74 | +### Changed |
| 75 | +* Nothing changed |
| 76 | +
|
| 77 | +{{#v0-0-0-fixed}} |
| 78 | +### Fixed |
| 79 | +* Nothing fixed |
| 80 | +
|
| 81 | +{{#v0-0-0-added}} |
| 82 | +### Added |
| 83 | +* Nothing added |
| 84 | +
|
| 85 | +{{#v0-0-0-removed}} |
| 86 | +### Removed |
| 87 | +* Nothing removed. |
| 88 | +""" |
| 89 | + changelog_path = self.tmpdir / "CHANGELOG.md" |
| 90 | + changelog_path.write_text(changelog) |
| 91 | + |
| 92 | + # Act |
| 93 | + releaser.update_changelog( |
| 94 | + "1.23.4", |
| 95 | + "2025-01-01", |
| 96 | + changelog_path=changelog_path, |
| 97 | + ) |
| 98 | + |
| 99 | + # Assert |
| 100 | + new_content = changelog_path.read_text() |
| 101 | + |
| 102 | + self.assertIn( |
| 103 | + _UNRELEASED_TEMPLATE, new_content, msg=f"ACTUAL:\n\n{new_content}\n\n" |
| 104 | + ) |
| 105 | + self.assertIn(f"## [1.23.4] - 2025-01-01", new_content) |
| 106 | + self.assertIn( |
| 107 | + f"[1.23.4]: https://github.com/bazel-contrib/rules_python/releases/tag/1.23.4", |
| 108 | + new_content, |
| 109 | + ) |
| 110 | + self.assertIn("{#v1-23-4}", new_content) |
| 111 | + self.assertIn("{#v1-23-4-changed}", new_content) |
| 112 | + self.assertIn("{#v1-23-4-fixed}", new_content) |
| 113 | + self.assertIn("{#v1-23-4-added}", new_content) |
| 114 | + self.assertIn("{#v1-23-4-removed}", new_content) |
| 115 | + |
| 116 | + def test_replace_version_next(self): |
| 117 | + # Arrange |
| 118 | + mock_file_content = """ |
| 119 | +:::{versionadded} VERSION_NEXT_FEATURE |
| 120 | +blabla |
| 121 | +::: |
| 122 | +
|
| 123 | +:::{versionchanged} VERSION_NEXT_PATCH |
| 124 | +blabla |
| 125 | +::: |
| 126 | +""" |
| 127 | + (self.tmpdir / "mock_file.bzl").write_text(mock_file_content) |
| 128 | + |
| 129 | + releaser.replace_version_next("0.28.0") |
| 130 | + |
| 131 | + new_content = (self.tmpdir / "mock_file.bzl").read_text() |
| 132 | + |
| 133 | + self.assertIn(":::{versionadded} 0.28.0", new_content) |
| 134 | + self.assertIn(":::{versionadded} 0.28.0", new_content) |
| 135 | + self.assertNotIn("VERSION_NEXT_FEATURE", new_content) |
| 136 | + self.assertNotIn("VERSION_NEXT_PATCH", new_content) |
| 137 | + |
| 138 | + def test_replace_version_next_excludes_bazel_dirs(self): |
| 139 | + # Arrange |
| 140 | + mock_file_content = """ |
| 141 | +:::{versionadded} VERSION_NEXT_FEATURE |
| 142 | +blabla |
| 143 | +::: |
| 144 | +""" |
| 145 | + bazel_dir = self.tmpdir / "bazel-rules_python" |
| 146 | + bazel_dir.mkdir() |
| 147 | + (bazel_dir / "mock_file.bzl").write_text(mock_file_content) |
| 148 | + |
| 149 | + tools_dir = self.tmpdir / "tools" / "private" / "release" |
| 150 | + tools_dir.mkdir(parents=True) |
| 151 | + (tools_dir / "mock_file.bzl").write_text(mock_file_content) |
| 152 | + |
| 153 | + tests_dir = self.tmpdir / "tests" / "tools" / "private" / "release" |
| 154 | + tests_dir.mkdir(parents=True) |
| 155 | + (tests_dir / "mock_file.bzl").write_text(mock_file_content) |
| 156 | + |
| 157 | + version = "0.28.0" |
| 158 | + |
| 159 | + # Act |
| 160 | + releaser.replace_version_next(version) |
| 161 | + |
| 162 | + # Assert |
| 163 | + new_content = (bazel_dir / "mock_file.bzl").read_text() |
| 164 | + self.assertIn("VERSION_NEXT_FEATURE", new_content) |
| 165 | + |
| 166 | + new_content = (tools_dir / "mock_file.bzl").read_text() |
| 167 | + self.assertIn("VERSION_NEXT_FEATURE", new_content) |
| 168 | + |
| 169 | + new_content = (tests_dir / "mock_file.bzl").read_text() |
| 170 | + self.assertIn("VERSION_NEXT_FEATURE", new_content) |
| 171 | + |
| 172 | + def test_valid_version(self): |
| 173 | + # These should not raise an exception |
| 174 | + releaser.create_parser().parse_args(["0.28.0"]) |
| 175 | + releaser.create_parser().parse_args(["1.0.0"]) |
| 176 | + releaser.create_parser().parse_args(["1.2.3rc4"]) |
| 177 | + |
| 178 | + def test_invalid_version(self): |
| 179 | + with self.assertRaises(SystemExit): |
| 180 | + releaser.create_parser().parse_args(["0.28"]) |
| 181 | + with self.assertRaises(SystemExit): |
| 182 | + releaser.create_parser().parse_args(["a.b.c"]) |
| 183 | + |
| 184 | + |
| 185 | +if __name__ == "__main__": |
| 186 | + unittest.main() |
0 commit comments