2
0

test_release_robot.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 os
  23. import re
  24. import shutil
  25. import tempfile
  26. import time
  27. import unittest
  28. from typing import ClassVar, Optional
  29. from dulwich.contrib import release_robot
  30. from dulwich.repo import Repo
  31. from dulwich.tests.utils import make_commit, make_tag
  32. BASEDIR = os.path.abspath(os.path.dirname(__file__)) # this directory
  33. def gmtime_to_datetime(gmt):
  34. return datetime.datetime(*time.gmtime(gmt)[:6])
  35. class TagPatternTests(unittest.TestCase):
  36. """test tag patterns."""
  37. def test_tag_pattern(self) -> None:
  38. """Test tag patterns."""
  39. test_cases = {
  40. "0.3": "0.3",
  41. "v0.3": "0.3",
  42. "release0.3": "0.3",
  43. "Release-0.3": "0.3",
  44. "v0.3rc1": "0.3rc1",
  45. "v0.3-rc1": "0.3-rc1",
  46. "v0.3-rc.1": "0.3-rc.1",
  47. "version 0.3": "0.3",
  48. "version_0.3_rc_1": "0.3_rc_1",
  49. "v1": "1",
  50. "0.3rc1": "0.3rc1",
  51. }
  52. for testcase, version in test_cases.items():
  53. matches = re.match(release_robot.PATTERN, testcase)
  54. self.assertEqual(matches.group(1), version)
  55. class GetRecentTagsTest(unittest.TestCase):
  56. """test get recent tags."""
  57. # Git repo for dulwich project
  58. test_repo = os.path.join(BASEDIR, "dulwich_test_repo.zip")
  59. committer = b"Mark Mikofski <mark.mikofski@sunpowercorp.com>"
  60. test_tags: ClassVar[list[bytes]] = [b"v0.1a", b"v0.1"]
  61. tag_test_data: ClassVar[
  62. dict[bytes, tuple[int, bytes, Optional[tuple[int, bytes]]]]
  63. ] = {
  64. test_tags[0]: (1484788003, b"3" * 40, None),
  65. test_tags[1]: (1484788314, b"1" * 40, (1484788401, b"2" * 40)),
  66. }
  67. @classmethod
  68. def setUpClass(cls) -> None:
  69. cls.projdir = tempfile.mkdtemp() # temporary project directory
  70. cls.repo = Repo.init(cls.projdir) # test repo
  71. obj_store = cls.repo.object_store # test repo object store
  72. # commit 1 ('2017-01-19T01:06:43')
  73. cls.c1 = make_commit(
  74. id=cls.tag_test_data[cls.test_tags[0]][1],
  75. commit_time=cls.tag_test_data[cls.test_tags[0]][0],
  76. message=b"unannotated tag",
  77. author=cls.committer,
  78. )
  79. obj_store.add_object(cls.c1)
  80. # tag 1: unannotated
  81. cls.t1 = cls.test_tags[0]
  82. cls.repo[b"refs/tags/" + cls.t1] = cls.c1.id # add unannotated tag
  83. # commit 2 ('2017-01-19T01:11:54')
  84. cls.c2 = make_commit(
  85. id=cls.tag_test_data[cls.test_tags[1]][1],
  86. commit_time=cls.tag_test_data[cls.test_tags[1]][0],
  87. message=b"annotated tag",
  88. parents=[cls.c1.id],
  89. author=cls.committer,
  90. )
  91. obj_store.add_object(cls.c2)
  92. # tag 2: annotated ('2017-01-19T01:13:21')
  93. cls.t2 = make_tag(
  94. cls.c2,
  95. id=cls.tag_test_data[cls.test_tags[1]][2][1],
  96. name=cls.test_tags[1],
  97. tag_time=cls.tag_test_data[cls.test_tags[1]][2][0],
  98. )
  99. obj_store.add_object(cls.t2)
  100. cls.repo[b"refs/heads/master"] = cls.c2.id
  101. cls.repo[b"refs/tags/" + cls.t2.name] = cls.t2.id # add annotated tag
  102. @classmethod
  103. def tearDownClass(cls) -> None:
  104. cls.repo.close()
  105. shutil.rmtree(cls.projdir)
  106. def test_get_recent_tags(self) -> None:
  107. """Test get recent tags."""
  108. tags = release_robot.get_recent_tags(self.projdir) # get test tags
  109. for tag, metadata in tags:
  110. tag = tag.encode("utf-8")
  111. test_data = self.tag_test_data[tag] # test data tag
  112. # test commit date, id and author name
  113. self.assertEqual(metadata[0], gmtime_to_datetime(test_data[0]))
  114. self.assertEqual(metadata[1].encode("utf-8"), test_data[1])
  115. self.assertEqual(metadata[2].encode("utf-8"), self.committer)
  116. # skip unannotated tags
  117. tag_obj = test_data[2]
  118. if not tag_obj:
  119. continue
  120. # tag date, id and name
  121. self.assertEqual(metadata[3][0], gmtime_to_datetime(tag_obj[0]))
  122. self.assertEqual(metadata[3][1].encode("utf-8"), tag_obj[1])
  123. self.assertEqual(metadata[3][2].encode("utf-8"), tag)