test_objectspec.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. # test_objectspec.py -- tests for objectspec.py
  2. # Copyright (C) 2014 Jelmer Vernooij <jelmer@samba.org>
  3. #
  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 revision spec parsing."""
  21. # TODO: Round-trip parse-serialize-parse and serialize-parse-serialize tests.
  22. from dulwich.objects import (
  23. Blob,
  24. )
  25. from dulwich.objectspec import (
  26. parse_object,
  27. parse_commit_range,
  28. parse_ref,
  29. parse_refs,
  30. parse_reftuple,
  31. parse_reftuples,
  32. )
  33. from dulwich.repo import MemoryRepo
  34. from dulwich.tests import (
  35. TestCase,
  36. )
  37. from dulwich.tests.utils import (
  38. build_commit_graph,
  39. )
  40. class ParseObjectTests(TestCase):
  41. """Test parse_object."""
  42. def test_nonexistent(self):
  43. r = MemoryRepo()
  44. self.assertRaises(KeyError, parse_object, r, "thisdoesnotexist")
  45. def test_blob_by_sha(self):
  46. r = MemoryRepo()
  47. b = Blob.from_string(b"Blah")
  48. r.object_store.add_object(b)
  49. self.assertEqual(b, parse_object(r, b.id))
  50. class ParseCommitRangeTests(TestCase):
  51. """Test parse_commit_range."""
  52. def test_nonexistent(self):
  53. r = MemoryRepo()
  54. self.assertRaises(KeyError, parse_commit_range, r, "thisdoesnotexist")
  55. def test_commit_by_sha(self):
  56. r = MemoryRepo()
  57. c1, c2, c3 = build_commit_graph(r.object_store, [[1], [2, 1],
  58. [3, 1, 2]])
  59. self.assertEqual([c1], list(parse_commit_range(r, c1.id)))
  60. class ParseRefTests(TestCase):
  61. def test_nonexistent(self):
  62. r = {}
  63. self.assertRaises(KeyError, parse_ref, r, b"thisdoesnotexist")
  64. def test_ambiguous_ref(self):
  65. r = {b"ambig1": 'bla',
  66. b"refs/ambig1": 'bla',
  67. b"refs/tags/ambig1": 'bla',
  68. b"refs/heads/ambig1": 'bla',
  69. b"refs/remotes/ambig1": 'bla',
  70. b"refs/remotes/ambig1/HEAD": "bla"}
  71. self.assertEqual(b"ambig1", parse_ref(r, b"ambig1"))
  72. def test_ambiguous_ref2(self):
  73. r = {b"refs/ambig2": 'bla',
  74. b"refs/tags/ambig2": 'bla',
  75. b"refs/heads/ambig2": 'bla',
  76. b"refs/remotes/ambig2": 'bla',
  77. b"refs/remotes/ambig2/HEAD": "bla"}
  78. self.assertEqual(b"refs/ambig2", parse_ref(r, b"ambig2"))
  79. def test_ambiguous_tag(self):
  80. r = {b"refs/tags/ambig3": 'bla',
  81. b"refs/heads/ambig3": 'bla',
  82. b"refs/remotes/ambig3": 'bla',
  83. b"refs/remotes/ambig3/HEAD": "bla"}
  84. self.assertEqual(b"refs/tags/ambig3", parse_ref(r, b"ambig3"))
  85. def test_ambiguous_head(self):
  86. r = {b"refs/heads/ambig4": 'bla',
  87. b"refs/remotes/ambig4": 'bla',
  88. b"refs/remotes/ambig4/HEAD": "bla"}
  89. self.assertEqual(b"refs/heads/ambig4", parse_ref(r, b"ambig4"))
  90. def test_ambiguous_remote(self):
  91. r = {b"refs/remotes/ambig5": 'bla',
  92. b"refs/remotes/ambig5/HEAD": "bla"}
  93. self.assertEqual(b"refs/remotes/ambig5", parse_ref(r, b"ambig5"))
  94. def test_ambiguous_remote_head(self):
  95. r = {b"refs/remotes/ambig6/HEAD": "bla"}
  96. self.assertEqual(b"refs/remotes/ambig6/HEAD", parse_ref(r, b"ambig6"))
  97. def test_heads_full(self):
  98. r = {b"refs/heads/foo": "bla"}
  99. self.assertEqual(b"refs/heads/foo", parse_ref(r, b"refs/heads/foo"))
  100. def test_heads_partial(self):
  101. r = {b"refs/heads/foo": "bla"}
  102. self.assertEqual(b"refs/heads/foo", parse_ref(r, b"heads/foo"))
  103. def test_tags_partial(self):
  104. r = {b"refs/tags/foo": "bla"}
  105. self.assertEqual(b"refs/tags/foo", parse_ref(r, b"tags/foo"))
  106. class ParseRefsTests(TestCase):
  107. def test_nonexistent(self):
  108. r = {}
  109. self.assertRaises(KeyError, parse_refs, r, [b"thisdoesnotexist"])
  110. def test_head(self):
  111. r = {b"refs/heads/foo": "bla"}
  112. self.assertEqual([b"refs/heads/foo"], parse_refs(r, [b"foo"]))
  113. def test_full(self):
  114. r = {b"refs/heads/foo": "bla"}
  115. self.assertEqual([b"refs/heads/foo"], parse_refs(r, b"refs/heads/foo"))
  116. class ParseReftupleTests(TestCase):
  117. def test_nonexistent(self):
  118. r = {}
  119. self.assertRaises(KeyError, parse_reftuple, r, r, b"thisdoesnotexist")
  120. def test_head(self):
  121. r = {b"refs/heads/foo": "bla"}
  122. self.assertEqual((b"refs/heads/foo", b"refs/heads/foo", False),
  123. parse_reftuple(r, r, b"foo"))
  124. self.assertEqual((b"refs/heads/foo", b"refs/heads/foo", True),
  125. parse_reftuple(r, r, b"+foo"))
  126. self.assertEqual((b"refs/heads/foo", b"refs/heads/foo", True),
  127. parse_reftuple(r, {}, b"+foo"))
  128. def test_full(self):
  129. r = {b"refs/heads/foo": "bla"}
  130. self.assertEqual((b"refs/heads/foo", b"refs/heads/foo", False),
  131. parse_reftuple(r, r, b"refs/heads/foo"))
  132. def test_no_left_ref(self):
  133. r = {b"refs/heads/foo": "bla"}
  134. self.assertEqual((None, b"refs/heads/foo", False),
  135. parse_reftuple(r, r, b":refs/heads/foo"))
  136. def test_no_right_ref(self):
  137. r = {b"refs/heads/foo": "bla"}
  138. self.assertEqual((b"refs/heads/foo", None, False),
  139. parse_reftuple(r, r, b"refs/heads/foo:"))
  140. class ParseReftuplesTests(TestCase):
  141. def test_nonexistent(self):
  142. r = {}
  143. self.assertRaises(KeyError, parse_reftuples, r, r,
  144. [b"thisdoesnotexist"])
  145. def test_head(self):
  146. r = {b"refs/heads/foo": "bla"}
  147. self.assertEqual([(b"refs/heads/foo", b"refs/heads/foo", False)],
  148. parse_reftuples(r, r, [b"foo"]))
  149. def test_full(self):
  150. r = {b"refs/heads/foo": "bla"}
  151. self.assertEqual([(b"refs/heads/foo", b"refs/heads/foo", False)],
  152. parse_reftuples(r, r, b"refs/heads/foo"))