test_release_robot.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. # release_robot.py
  2. #
  3. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  4. # General Public License as public by the Free Software Foundation; version 2.0
  5. # or (at your option) any later version. You can redistribute it and/or
  6. # modify it under the terms of either of these two licenses.
  7. #
  8. # Unless required by applicable law or agreed to in writing, software
  9. # distributed under the License is distributed on an "AS IS" BASIS,
  10. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. # See the License for the specific language governing permissions and
  12. # limitations under the License.
  13. #
  14. # You should have received a copy of the licenses; if not, see
  15. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  16. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  17. # License, Version 2.0.
  18. #
  19. """Tests for release_robot."""
  20. import datetime
  21. import os
  22. import re
  23. import shutil
  24. import tempfile
  25. import time
  26. import unittest
  27. from typing import ClassVar, Dict, List, Optional, Tuple
  28. from dulwich.contrib import release_robot
  29. from dulwich.repo import Repo
  30. from dulwich.tests.utils import make_commit, make_tag
  31. BASEDIR = os.path.abspath(os.path.dirname(__file__)) # this directory
  32. def gmtime_to_datetime(gmt):
  33. return datetime.datetime(*time.gmtime(gmt)[:6])
  34. class TagPatternTests(unittest.TestCase):
  35. """test tag patterns."""
  36. def test_tag_pattern(self):
  37. """Test tag patterns."""
  38. test_cases = {
  39. "0.3": "0.3",
  40. "v0.3": "0.3",
  41. "release0.3": "0.3",
  42. "Release-0.3": "0.3",
  43. "v0.3rc1": "0.3rc1",
  44. "v0.3-rc1": "0.3-rc1",
  45. "v0.3-rc.1": "0.3-rc.1",
  46. "version 0.3": "0.3",
  47. "version_0.3_rc_1": "0.3_rc_1",
  48. "v1": "1",
  49. "0.3rc1": "0.3rc1",
  50. }
  51. for testcase, version in test_cases.items():
  52. matches = re.match(release_robot.PATTERN, testcase)
  53. self.assertEqual(matches.group(1), version)
  54. class GetRecentTagsTest(unittest.TestCase):
  55. """test get recent tags."""
  56. # Git repo for dulwich project
  57. test_repo = os.path.join(BASEDIR, "dulwich_test_repo.zip")
  58. committer = b"Mark Mikofski <mark.mikofski@sunpowercorp.com>"
  59. test_tags: ClassVar[List[bytes]] = [b"v0.1a", b"v0.1"]
  60. tag_test_data: ClassVar[
  61. Dict[bytes, Tuple[int, bytes, Optional[Tuple[int, bytes]]]]
  62. ] = {
  63. test_tags[0]: (1484788003, b"3" * 40, None),
  64. test_tags[1]: (1484788314, b"1" * 40, (1484788401, b"2" * 40)),
  65. }
  66. @classmethod
  67. def setUpClass(cls):
  68. cls.projdir = tempfile.mkdtemp() # temporary project directory
  69. cls.repo = Repo.init(cls.projdir) # test repo
  70. obj_store = cls.repo.object_store # test repo object store
  71. # commit 1 ('2017-01-19T01:06:43')
  72. cls.c1 = make_commit(
  73. id=cls.tag_test_data[cls.test_tags[0]][1],
  74. commit_time=cls.tag_test_data[cls.test_tags[0]][0],
  75. message=b"unannotated tag",
  76. author=cls.committer,
  77. )
  78. obj_store.add_object(cls.c1)
  79. # tag 1: unannotated
  80. cls.t1 = cls.test_tags[0]
  81. cls.repo[b"refs/tags/" + cls.t1] = cls.c1.id # add unannotated tag
  82. # commit 2 ('2017-01-19T01:11:54')
  83. cls.c2 = make_commit(
  84. id=cls.tag_test_data[cls.test_tags[1]][1],
  85. commit_time=cls.tag_test_data[cls.test_tags[1]][0],
  86. message=b"annotated tag",
  87. parents=[cls.c1.id],
  88. author=cls.committer,
  89. )
  90. obj_store.add_object(cls.c2)
  91. # tag 2: annotated ('2017-01-19T01:13:21')
  92. cls.t2 = make_tag(
  93. cls.c2,
  94. id=cls.tag_test_data[cls.test_tags[1]][2][1],
  95. name=cls.test_tags[1],
  96. tag_time=cls.tag_test_data[cls.test_tags[1]][2][0],
  97. )
  98. obj_store.add_object(cls.t2)
  99. cls.repo[b"refs/heads/master"] = cls.c2.id
  100. cls.repo[b"refs/tags/" + cls.t2.name] = cls.t2.id # add annotated tag
  101. @classmethod
  102. def tearDownClass(cls):
  103. cls.repo.close()
  104. shutil.rmtree(cls.projdir)
  105. def test_get_recent_tags(self):
  106. """Test get recent tags."""
  107. tags = release_robot.get_recent_tags(self.projdir) # get test tags
  108. for tag, metadata in tags:
  109. tag = tag.encode("utf-8")
  110. test_data = self.tag_test_data[tag] # test data tag
  111. # test commit date, id and author name
  112. self.assertEqual(metadata[0], gmtime_to_datetime(test_data[0]))
  113. self.assertEqual(metadata[1].encode("utf-8"), test_data[1])
  114. self.assertEqual(metadata[2].encode("utf-8"), self.committer)
  115. # skip unannotated tags
  116. tag_obj = test_data[2]
  117. if not tag_obj:
  118. continue
  119. # tag date, id and name
  120. self.assertEqual(metadata[3][0], gmtime_to_datetime(tag_obj[0]))
  121. self.assertEqual(metadata[3][1].encode("utf-8"), tag_obj[1])
  122. self.assertEqual(metadata[3][2].encode("utf-8"), tag)