test_objectspec.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. # test_objectspec.py -- tests for objectspec.py
  2. # Copyright (C) 2014 Jelmer Vernooij <jelmer@jelmer.uk>
  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 Blob
  23. from dulwich.objectspec import (
  24. parse_commit,
  25. parse_commit_range,
  26. parse_object,
  27. parse_ref,
  28. parse_refs,
  29. parse_reftuple,
  30. parse_reftuples,
  31. parse_tree,
  32. )
  33. from dulwich.repo import MemoryRepo
  34. from dulwich.tests.utils import build_commit_graph
  35. from . import TestCase
  36. class ParseObjectTests(TestCase):
  37. """Test parse_object."""
  38. def test_nonexistent(self):
  39. r = MemoryRepo()
  40. self.assertRaises(KeyError, parse_object, r, "thisdoesnotexist")
  41. def test_blob_by_sha(self):
  42. r = MemoryRepo()
  43. b = Blob.from_string(b"Blah")
  44. r.object_store.add_object(b)
  45. self.assertEqual(b, parse_object(r, b.id))
  46. class ParseCommitRangeTests(TestCase):
  47. """Test parse_commit_range."""
  48. def test_nonexistent(self):
  49. r = MemoryRepo()
  50. self.assertRaises(KeyError, parse_commit_range, r, "thisdoesnotexist")
  51. def test_commit_by_sha(self):
  52. r = MemoryRepo()
  53. c1, c2, c3 = build_commit_graph(r.object_store, [[1], [2, 1], [3, 1, 2]])
  54. self.assertEqual([c1], list(parse_commit_range(r, c1.id)))
  55. class ParseCommitTests(TestCase):
  56. """Test parse_commit."""
  57. def test_nonexistent(self):
  58. r = MemoryRepo()
  59. self.assertRaises(KeyError, parse_commit, r, "thisdoesnotexist")
  60. def test_commit_by_sha(self):
  61. r = MemoryRepo()
  62. [c1] = build_commit_graph(r.object_store, [[1]])
  63. self.assertEqual(c1, parse_commit(r, c1.id))
  64. def test_commit_by_short_sha(self):
  65. r = MemoryRepo()
  66. [c1] = build_commit_graph(r.object_store, [[1]])
  67. self.assertEqual(c1, parse_commit(r, c1.id[:10]))
  68. class ParseRefTests(TestCase):
  69. def test_nonexistent(self):
  70. r = {}
  71. self.assertRaises(KeyError, parse_ref, r, b"thisdoesnotexist")
  72. def test_ambiguous_ref(self):
  73. r = {
  74. b"ambig1": "bla",
  75. b"refs/ambig1": "bla",
  76. b"refs/tags/ambig1": "bla",
  77. b"refs/heads/ambig1": "bla",
  78. b"refs/remotes/ambig1": "bla",
  79. b"refs/remotes/ambig1/HEAD": "bla",
  80. }
  81. self.assertEqual(b"ambig1", parse_ref(r, b"ambig1"))
  82. def test_ambiguous_ref2(self):
  83. r = {
  84. b"refs/ambig2": "bla",
  85. b"refs/tags/ambig2": "bla",
  86. b"refs/heads/ambig2": "bla",
  87. b"refs/remotes/ambig2": "bla",
  88. b"refs/remotes/ambig2/HEAD": "bla",
  89. }
  90. self.assertEqual(b"refs/ambig2", parse_ref(r, b"ambig2"))
  91. def test_ambiguous_tag(self):
  92. r = {
  93. b"refs/tags/ambig3": "bla",
  94. b"refs/heads/ambig3": "bla",
  95. b"refs/remotes/ambig3": "bla",
  96. b"refs/remotes/ambig3/HEAD": "bla",
  97. }
  98. self.assertEqual(b"refs/tags/ambig3", parse_ref(r, b"ambig3"))
  99. def test_ambiguous_head(self):
  100. r = {
  101. b"refs/heads/ambig4": "bla",
  102. b"refs/remotes/ambig4": "bla",
  103. b"refs/remotes/ambig4/HEAD": "bla",
  104. }
  105. self.assertEqual(b"refs/heads/ambig4", parse_ref(r, b"ambig4"))
  106. def test_ambiguous_remote(self):
  107. r = {b"refs/remotes/ambig5": "bla", b"refs/remotes/ambig5/HEAD": "bla"}
  108. self.assertEqual(b"refs/remotes/ambig5", parse_ref(r, b"ambig5"))
  109. def test_ambiguous_remote_head(self):
  110. r = {b"refs/remotes/ambig6/HEAD": "bla"}
  111. self.assertEqual(b"refs/remotes/ambig6/HEAD", parse_ref(r, b"ambig6"))
  112. def test_heads_full(self):
  113. r = {b"refs/heads/foo": "bla"}
  114. self.assertEqual(b"refs/heads/foo", parse_ref(r, b"refs/heads/foo"))
  115. def test_heads_partial(self):
  116. r = {b"refs/heads/foo": "bla"}
  117. self.assertEqual(b"refs/heads/foo", parse_ref(r, b"heads/foo"))
  118. def test_tags_partial(self):
  119. r = {b"refs/tags/foo": "bla"}
  120. self.assertEqual(b"refs/tags/foo", parse_ref(r, b"tags/foo"))
  121. class ParseRefsTests(TestCase):
  122. def test_nonexistent(self):
  123. r = {}
  124. self.assertRaises(KeyError, parse_refs, r, [b"thisdoesnotexist"])
  125. def test_head(self):
  126. r = {b"refs/heads/foo": "bla"}
  127. self.assertEqual([b"refs/heads/foo"], parse_refs(r, [b"foo"]))
  128. def test_full(self):
  129. r = {b"refs/heads/foo": "bla"}
  130. self.assertEqual([b"refs/heads/foo"], parse_refs(r, b"refs/heads/foo"))
  131. class ParseReftupleTests(TestCase):
  132. def test_nonexistent(self):
  133. r = {}
  134. self.assertRaises(KeyError, parse_reftuple, r, r, b"thisdoesnotexist")
  135. def test_head(self):
  136. r = {b"refs/heads/foo": "bla"}
  137. self.assertEqual(
  138. (b"refs/heads/foo", b"refs/heads/foo", False),
  139. parse_reftuple(r, r, b"foo"),
  140. )
  141. self.assertEqual(
  142. (b"refs/heads/foo", b"refs/heads/foo", True),
  143. parse_reftuple(r, r, b"+foo"),
  144. )
  145. self.assertEqual(
  146. (b"refs/heads/foo", b"refs/heads/foo", True),
  147. parse_reftuple(r, {}, b"+foo"),
  148. )
  149. self.assertEqual(
  150. (b"refs/heads/foo", b"refs/heads/foo", True),
  151. parse_reftuple(r, {}, b"foo", True),
  152. )
  153. def test_full(self):
  154. r = {b"refs/heads/foo": "bla"}
  155. self.assertEqual(
  156. (b"refs/heads/foo", b"refs/heads/foo", False),
  157. parse_reftuple(r, r, b"refs/heads/foo"),
  158. )
  159. def test_no_left_ref(self):
  160. r = {b"refs/heads/foo": "bla"}
  161. self.assertEqual(
  162. (None, b"refs/heads/foo", False),
  163. parse_reftuple(r, r, b":refs/heads/foo"),
  164. )
  165. def test_no_right_ref(self):
  166. r = {b"refs/heads/foo": "bla"}
  167. self.assertEqual(
  168. (b"refs/heads/foo", None, False),
  169. parse_reftuple(r, r, b"refs/heads/foo:"),
  170. )
  171. def test_default_with_string(self):
  172. r = {b"refs/heads/foo": "bla"}
  173. self.assertEqual(
  174. (b"refs/heads/foo", b"refs/heads/foo", False),
  175. parse_reftuple(r, r, "foo"),
  176. )
  177. class ParseReftuplesTests(TestCase):
  178. def test_nonexistent(self):
  179. r = {}
  180. self.assertRaises(KeyError, parse_reftuples, r, r, [b"thisdoesnotexist"])
  181. def test_head(self):
  182. r = {b"refs/heads/foo": "bla"}
  183. self.assertEqual(
  184. [(b"refs/heads/foo", b"refs/heads/foo", False)],
  185. parse_reftuples(r, r, [b"foo"]),
  186. )
  187. def test_full(self):
  188. r = {b"refs/heads/foo": "bla"}
  189. self.assertEqual(
  190. [(b"refs/heads/foo", b"refs/heads/foo", False)],
  191. parse_reftuples(r, r, b"refs/heads/foo"),
  192. )
  193. r = {b"refs/heads/foo": "bla"}
  194. self.assertEqual(
  195. [(b"refs/heads/foo", b"refs/heads/foo", True)],
  196. parse_reftuples(r, r, b"refs/heads/foo", True),
  197. )
  198. class ParseTreeTests(TestCase):
  199. """Test parse_tree."""
  200. def test_nonexistent(self):
  201. r = MemoryRepo()
  202. self.assertRaises(KeyError, parse_tree, r, "thisdoesnotexist")
  203. def test_from_commit(self):
  204. r = MemoryRepo()
  205. c1, c2, c3 = build_commit_graph(r.object_store, [[1], [2, 1], [3, 1, 2]])
  206. self.assertEqual(r[c1.tree], parse_tree(r, c1.id))
  207. self.assertEqual(r[c1.tree], parse_tree(r, c1.tree))
  208. def test_from_ref(self):
  209. r = MemoryRepo()
  210. c1, c2, c3 = build_commit_graph(r.object_store, [[1], [2, 1], [3, 1, 2]])
  211. r.refs[b"refs/heads/foo"] = c1.id
  212. self.assertEqual(r[c1.tree], parse_tree(r, b"foo"))