test_release_robot.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 ..repo import Repo
  30. from ..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[Dict[bytes, Tuple[int, bytes, Optional[Tuple[int, bytes]]]]] = {
  61. test_tags[0]: (1484788003, b"3" * 40, None),
  62. test_tags[1]: (1484788314, b"1" * 40, (1484788401, b"2" * 40)),
  63. }
  64. @classmethod
  65. def setUpClass(cls):
  66. cls.projdir = tempfile.mkdtemp() # temporary project directory
  67. cls.repo = Repo.init(cls.projdir) # test repo
  68. obj_store = cls.repo.object_store # test repo object store
  69. # commit 1 ('2017-01-19T01:06:43')
  70. cls.c1 = make_commit(
  71. id=cls.tag_test_data[cls.test_tags[0]][1],
  72. commit_time=cls.tag_test_data[cls.test_tags[0]][0],
  73. message=b"unannotated tag",
  74. author=cls.committer,
  75. )
  76. obj_store.add_object(cls.c1)
  77. # tag 1: unannotated
  78. cls.t1 = cls.test_tags[0]
  79. cls.repo[b"refs/tags/" + cls.t1] = cls.c1.id # add unannotated tag
  80. # commit 2 ('2017-01-19T01:11:54')
  81. cls.c2 = make_commit(
  82. id=cls.tag_test_data[cls.test_tags[1]][1],
  83. commit_time=cls.tag_test_data[cls.test_tags[1]][0],
  84. message=b"annotated tag",
  85. parents=[cls.c1.id],
  86. author=cls.committer,
  87. )
  88. obj_store.add_object(cls.c2)
  89. # tag 2: annotated ('2017-01-19T01:13:21')
  90. cls.t2 = make_tag(
  91. cls.c2,
  92. id=cls.tag_test_data[cls.test_tags[1]][2][1],
  93. name=cls.test_tags[1],
  94. tag_time=cls.tag_test_data[cls.test_tags[1]][2][0],
  95. )
  96. obj_store.add_object(cls.t2)
  97. cls.repo[b"refs/heads/master"] = cls.c2.id
  98. cls.repo[b"refs/tags/" + cls.t2.name] = cls.t2.id # add annotated tag
  99. @classmethod
  100. def tearDownClass(cls):
  101. cls.repo.close()
  102. shutil.rmtree(cls.projdir)
  103. def test_get_recent_tags(self):
  104. """Test get recent tags."""
  105. tags = release_robot.get_recent_tags(self.projdir) # get test tags
  106. for tag, metadata in tags:
  107. tag = tag.encode("utf-8")
  108. test_data = self.tag_test_data[tag] # test data tag
  109. # test commit date, id and author name
  110. self.assertEqual(metadata[0], gmtime_to_datetime(test_data[0]))
  111. self.assertEqual(metadata[1].encode("utf-8"), test_data[1])
  112. self.assertEqual(metadata[2].encode("utf-8"), self.committer)
  113. # skip unannotated tags
  114. tag_obj = test_data[2]
  115. if not tag_obj:
  116. continue
  117. # tag date, id and name
  118. self.assertEqual(metadata[3][0], gmtime_to_datetime(tag_obj[0]))
  119. self.assertEqual(metadata[3][1].encode("utf-8"), tag_obj[1])
  120. self.assertEqual(metadata[3][2].encode("utf-8"), tag)