test_release_robot.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. # release_robot.py
  2. #
  3. # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  4. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  5. # General Public License as public by the Free Software Foundation; version 2.0
  6. # or (at your option) any later version. You can redistribute it and/or
  7. # modify it under the terms of either of these two licenses.
  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. # You should have received a copy of the licenses; if not, see
  16. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  17. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  18. # License, Version 2.0.
  19. #
  20. """Tests for release_robot."""
  21. import datetime
  22. import logging
  23. import os
  24. import re
  25. import shutil
  26. import sys
  27. import tempfile
  28. import time
  29. import unittest
  30. from typing import ClassVar, Optional
  31. from unittest.mock import MagicMock, patch
  32. from dulwich.contrib import release_robot
  33. from dulwich.objects import Commit, Tag
  34. from dulwich.repo import Repo
  35. from dulwich.tests.utils import make_commit, make_tag
  36. BASEDIR = os.path.abspath(os.path.dirname(__file__)) # this directory
  37. def gmtime_to_datetime(gmt):
  38. return datetime.datetime(*time.gmtime(gmt)[:6])
  39. class TagPatternTests(unittest.TestCase):
  40. """test tag patterns."""
  41. def test_tag_pattern(self) -> None:
  42. """Test tag patterns."""
  43. test_cases = {
  44. "0.3": "0.3",
  45. "v0.3": "0.3",
  46. "release0.3": "0.3",
  47. "Release-0.3": "0.3",
  48. "v0.3rc1": "0.3rc1",
  49. "v0.3-rc1": "0.3-rc1",
  50. "v0.3-rc.1": "0.3-rc.1",
  51. "version 0.3": "0.3",
  52. "version_0.3_rc_1": "0.3_rc_1",
  53. "v1": "1",
  54. "0.3rc1": "0.3rc1",
  55. }
  56. for testcase, version in test_cases.items():
  57. matches = re.match(release_robot.PATTERN, testcase)
  58. assert matches is not None
  59. self.assertEqual(matches.group(1), version)
  60. def test_pattern_no_match(self) -> None:
  61. """Test tags that don't match the pattern."""
  62. test_cases = [
  63. "master",
  64. "HEAD",
  65. "feature-branch",
  66. "no-numbers",
  67. "_",
  68. ]
  69. for testcase in test_cases:
  70. matches = re.match(release_robot.PATTERN, testcase)
  71. self.assertIsNone(matches)
  72. class GetRecentTagsTest(unittest.TestCase):
  73. """test get recent tags."""
  74. # Git repo for dulwich project
  75. test_repo = os.path.join(BASEDIR, "dulwich_test_repo.zip")
  76. committer = b"Mark Mikofski <mark.mikofski@sunpowercorp.com>"
  77. test_tags: ClassVar[list[bytes]] = [b"v0.1a", b"v0.1"]
  78. tag_test_data: ClassVar[
  79. dict[bytes, tuple[int, bytes, Optional[tuple[int, bytes]]]]
  80. ] = {
  81. test_tags[0]: (1484788003, b"3" * 40, None),
  82. test_tags[1]: (1484788314, b"1" * 40, (1484788401, b"2" * 40)),
  83. }
  84. # Class attributes set in setUpClass
  85. projdir: ClassVar[str]
  86. repo: ClassVar[Repo]
  87. c1: ClassVar[Commit]
  88. c2: ClassVar[Commit]
  89. t1: ClassVar[bytes]
  90. t2: ClassVar[Tag]
  91. @classmethod
  92. def setUpClass(cls) -> None:
  93. cls.projdir = tempfile.mkdtemp() # temporary project directory
  94. cls.repo = Repo.init(cls.projdir) # test repo
  95. obj_store = cls.repo.object_store # test repo object store
  96. # commit 1 ('2017-01-19T01:06:43')
  97. cls.c1 = make_commit(
  98. id=cls.tag_test_data[cls.test_tags[0]][1],
  99. commit_time=cls.tag_test_data[cls.test_tags[0]][0],
  100. message=b"unannotated tag",
  101. author=cls.committer,
  102. )
  103. obj_store.add_object(cls.c1)
  104. # tag 1: unannotated
  105. cls.t1 = cls.test_tags[0]
  106. cls.repo[b"refs/tags/" + cls.t1] = cls.c1.id # add unannotated tag
  107. # commit 2 ('2017-01-19T01:11:54')
  108. cls.c2 = make_commit(
  109. id=cls.tag_test_data[cls.test_tags[1]][1],
  110. commit_time=cls.tag_test_data[cls.test_tags[1]][0],
  111. message=b"annotated tag",
  112. parents=[cls.c1.id],
  113. author=cls.committer,
  114. )
  115. obj_store.add_object(cls.c2)
  116. # tag 2: annotated ('2017-01-19T01:13:21')
  117. tag_data = cls.tag_test_data[cls.test_tags[1]][2]
  118. if tag_data is None:
  119. raise AssertionError("test_tags[1] should have annotated tag data")
  120. cls.t2 = make_tag(
  121. cls.c2,
  122. id=tag_data[1],
  123. name=cls.test_tags[1],
  124. tag_time=tag_data[0],
  125. )
  126. obj_store.add_object(cls.t2)
  127. cls.repo[b"refs/heads/master"] = cls.c2.id
  128. cls.repo[b"refs/tags/" + cls.t2.name] = cls.t2.id # add annotated tag
  129. @classmethod
  130. def tearDownClass(cls) -> None:
  131. cls.repo.close()
  132. shutil.rmtree(cls.projdir)
  133. def test_get_recent_tags(self) -> None:
  134. """Test get recent tags."""
  135. tags = release_robot.get_recent_tags(self.projdir) # get test tags
  136. for tag, metadata in tags:
  137. tag_bytes = tag.encode("utf-8")
  138. test_data = self.tag_test_data[tag_bytes] # test data tag
  139. # test commit date, id and author name
  140. self.assertEqual(metadata[0], gmtime_to_datetime(test_data[0]))
  141. self.assertEqual(metadata[1].encode("utf-8"), test_data[1])
  142. self.assertEqual(metadata[2].encode("utf-8"), self.committer)
  143. # skip unannotated tags
  144. tag_obj = test_data[2]
  145. if not tag_obj:
  146. continue
  147. # tag date, id and name
  148. self.assertEqual(metadata[3][0], gmtime_to_datetime(tag_obj[0]))
  149. self.assertEqual(metadata[3][1].encode("utf-8"), tag_obj[1])
  150. self.assertEqual(metadata[3][2], tag)
  151. def test_get_recent_tags_sorting(self) -> None:
  152. """Test that tags are sorted by commit time from newest to oldest."""
  153. tags = release_robot.get_recent_tags(self.projdir)
  154. # v0.1 should be first as it's newer
  155. self.assertEqual(tags[0][0], "v0.1")
  156. # v0.1a should be second as it's older
  157. self.assertEqual(tags[1][0], "v0.1a")
  158. def test_get_recent_tags_non_tag_refs(self) -> None:
  159. """Test that non-tag refs are ignored."""
  160. # Create a commit on a branch to test that it's not included
  161. branch_commit = make_commit(
  162. message=b"branch commit",
  163. author=self.committer,
  164. commit_time=int(time.time()),
  165. )
  166. self.repo.object_store.add_object(branch_commit)
  167. self.repo[b"refs/heads/test-branch"] = branch_commit.id
  168. # Get tags and ensure only the actual tags are returned
  169. tags = release_robot.get_recent_tags(self.projdir)
  170. self.assertEqual(len(tags), 2) # Still only 2 tags
  171. tag_names = [tag[0] for tag in tags]
  172. self.assertIn("v0.1", tag_names)
  173. self.assertIn("v0.1a", tag_names)
  174. self.assertNotIn("test-branch", tag_names)
  175. class GetCurrentVersionTests(unittest.TestCase):
  176. """Test get_current_version function."""
  177. def setUp(self):
  178. """Set up a test repository for each test."""
  179. self.projdir = tempfile.mkdtemp()
  180. self.repo = Repo.init(self.projdir)
  181. self.addCleanup(self.cleanup)
  182. def cleanup(self):
  183. """Clean up after test."""
  184. self.repo.close()
  185. shutil.rmtree(self.projdir)
  186. def test_no_tags(self):
  187. """Test behavior when repo has no tags."""
  188. # Create a repo with no tags
  189. result = release_robot.get_current_version(self.projdir)
  190. self.assertIsNone(result)
  191. def test_tag_with_pattern_match(self):
  192. """Test with a tag that matches the pattern."""
  193. # Create a test commit and tag
  194. c = make_commit(message=b"Test commit")
  195. self.repo.object_store.add_object(c)
  196. self.repo[b"refs/tags/v1.2.3"] = c.id
  197. self.repo[b"HEAD"] = c.id
  198. # Test that the version is extracted correctly
  199. result = release_robot.get_current_version(self.projdir)
  200. self.assertEqual("1.2.3", result)
  201. def test_tag_no_pattern_match(self):
  202. """Test with a tag that doesn't match the pattern."""
  203. # Create a test commit and tag that won't match the default pattern
  204. c = make_commit(message=b"Test commit")
  205. self.repo.object_store.add_object(c)
  206. self.repo[b"refs/tags/no-version-tag"] = c.id
  207. self.repo[b"HEAD"] = c.id
  208. # Test that the full tag is returned when no match
  209. result = release_robot.get_current_version(self.projdir)
  210. self.assertEqual("no-version-tag", result)
  211. def test_with_logger(self):
  212. """Test with a logger when regex match fails."""
  213. # Create a test commit and tag that won't match the pattern
  214. c = make_commit(message=b"Test commit")
  215. self.repo.object_store.add_object(c)
  216. self.repo[b"refs/tags/no-version-tag"] = c.id
  217. self.repo[b"HEAD"] = c.id
  218. # Create a logger
  219. logger = logging.getLogger("test_logger")
  220. # Test with the logger
  221. result = release_robot.get_current_version(self.projdir, logger=logger)
  222. self.assertEqual("no-version-tag", result)
  223. def test_custom_pattern(self):
  224. """Test with a custom regex pattern."""
  225. # Create a test commit and tag
  226. c = make_commit(message=b"Test commit")
  227. self.repo.object_store.add_object(c)
  228. self.repo[b"refs/tags/CUSTOM-99.88.77"] = c.id
  229. self.repo[b"HEAD"] = c.id
  230. # Test with a custom pattern
  231. custom_pattern = r"CUSTOM-([\d\.]+)"
  232. result = release_robot.get_current_version(self.projdir, pattern=custom_pattern)
  233. self.assertEqual("99.88.77", result)
  234. def test_with_logger_debug_call(self):
  235. """Test that the logger.debug method is actually called."""
  236. # Create a test commit and tag that won't match the pattern
  237. c = make_commit(message=b"Test commit")
  238. self.repo.object_store.add_object(c)
  239. self.repo[b"refs/tags/no-version-tag"] = c.id
  240. self.repo[b"HEAD"] = c.id
  241. # Create a mock logger
  242. mock_logger = MagicMock()
  243. # Test with the mock logger
  244. result = release_robot.get_current_version(self.projdir, logger=mock_logger)
  245. # Verify logger.debug was called
  246. mock_logger.debug.assert_called_once()
  247. # Check the tag name is in the debug message
  248. self.assertIn("no-version-tag", mock_logger.debug.call_args[0][2])
  249. # The result should still be the full tag
  250. self.assertEqual("no-version-tag", result)
  251. def test_multiple_tags(self):
  252. """Test behavior with multiple tags to ensure we get the most recent."""
  253. # Create multiple commits and tags with different timestamps
  254. c1 = make_commit(message=b"First commit", commit_time=1000)
  255. c2 = make_commit(message=b"Second commit", commit_time=2000, parents=[c1.id])
  256. self.repo.object_store.add_object(c1)
  257. self.repo.object_store.add_object(c2)
  258. # Add tags with older commit first
  259. self.repo[b"refs/tags/v0.9.0"] = c1.id
  260. self.repo[b"refs/tags/v1.0.0"] = c2.id
  261. self.repo[b"HEAD"] = c2.id
  262. # Get the current version - should be from the most recent commit
  263. result = release_robot.get_current_version(self.projdir)
  264. self.assertEqual("1.0.0", result)
  265. class MainFunctionTests(unittest.TestCase):
  266. """Test the __main__ block."""
  267. def setUp(self):
  268. """Set up a test repository."""
  269. self.projdir = tempfile.mkdtemp()
  270. self.repo = Repo.init(self.projdir)
  271. # Create a test commit and tag
  272. c = make_commit(message=b"Test commit")
  273. self.repo.object_store.add_object(c)
  274. self.repo[b"refs/tags/v3.2.1"] = c.id
  275. self.repo[b"HEAD"] = c.id
  276. self.addCleanup(self.cleanup)
  277. def cleanup(self):
  278. """Clean up after test."""
  279. self.repo.close()
  280. shutil.rmtree(self.projdir)
  281. @patch.object(sys, "argv", ["release_robot.py"])
  282. @patch("builtins.print")
  283. def test_main_default_dir(self, mock_print):
  284. """Test main function with default directory."""
  285. # Run the __main__ block code with mocked environment
  286. module_globals = {
  287. "__name__": "__main__",
  288. "sys": sys,
  289. "get_current_version": lambda projdir: "3.2.1",
  290. "PROJDIR": ".",
  291. }
  292. exec(
  293. compile(
  294. "if __name__ == '__main__':\n if len(sys.argv) > 1:\n _PROJDIR = sys.argv[1]\n else:\n _PROJDIR = PROJDIR\n print(get_current_version(projdir=_PROJDIR))",
  295. "<string>",
  296. "exec",
  297. ),
  298. module_globals,
  299. )
  300. # Check that print was called with the version
  301. mock_print.assert_called_once_with("3.2.1")
  302. @patch.object(sys, "argv", ["release_robot.py", "/custom/path"])
  303. @patch("builtins.print")
  304. @patch("dulwich.contrib.release_robot.get_current_version")
  305. def test_main_custom_dir(self, mock_get_version, mock_print):
  306. """Test main function with custom directory from command line."""
  307. mock_get_version.return_value = "4.5.6"
  308. # Run the __main__ block code with mocked environment
  309. module_globals = {
  310. "__name__": "__main__",
  311. "sys": sys,
  312. "get_current_version": mock_get_version,
  313. "PROJDIR": ".",
  314. }
  315. exec(
  316. compile(
  317. "if __name__ == '__main__':\n if len(sys.argv) > 1:\n _PROJDIR = sys.argv[1]\n else:\n _PROJDIR = PROJDIR\n print(get_current_version(projdir=_PROJDIR))",
  318. "<string>",
  319. "exec",
  320. ),
  321. module_globals,
  322. )
  323. # Check that get_current_version was called with the right arg
  324. mock_get_version.assert_called_once_with(projdir="/custom/path")
  325. mock_print.assert_called_once_with("4.5.6")