test_refs.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. # test_refs.py -- tests for refs.py
  2. # encoding: utf-8
  3. # Copyright (C) 2013 Jelmer Vernooij <jelmer@samba.org>
  4. #
  5. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  6. # General Public License as public by the Free Software Foundation; version 2.0
  7. # or (at your option) any later version. You can redistribute it and/or
  8. # modify it under the terms of either of these two licenses.
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. # You should have received a copy of the licenses; if not, see
  17. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  18. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  19. # License, Version 2.0.
  20. #
  21. """Tests for dulwich.refs."""
  22. from io import BytesIO
  23. import os
  24. import sys
  25. import tempfile
  26. from dulwich import errors
  27. from dulwich.file import (
  28. GitFile,
  29. )
  30. from dulwich.objects import ZERO_SHA
  31. from dulwich.refs import (
  32. DictRefsContainer,
  33. InfoRefsContainer,
  34. check_ref_format,
  35. _split_ref_line,
  36. parse_symref_value,
  37. read_packed_refs_with_peeled,
  38. read_packed_refs,
  39. write_packed_refs,
  40. )
  41. from dulwich.repo import Repo
  42. from dulwich.tests import (
  43. SkipTest,
  44. TestCase,
  45. )
  46. from dulwich.tests.utils import (
  47. open_repo,
  48. tear_down_repo,
  49. )
  50. class CheckRefFormatTests(TestCase):
  51. """Tests for the check_ref_format function.
  52. These are the same tests as in the git test suite.
  53. """
  54. def test_valid(self):
  55. self.assertTrue(check_ref_format(b'heads/foo'))
  56. self.assertTrue(check_ref_format(b'foo/bar/baz'))
  57. self.assertTrue(check_ref_format(b'refs///heads/foo'))
  58. self.assertTrue(check_ref_format(b'foo./bar'))
  59. self.assertTrue(check_ref_format(b'heads/foo@bar'))
  60. self.assertTrue(check_ref_format(b'heads/fix.lock.error'))
  61. def test_invalid(self):
  62. self.assertFalse(check_ref_format(b'foo'))
  63. self.assertFalse(check_ref_format(b'heads/foo/'))
  64. self.assertFalse(check_ref_format(b'./foo'))
  65. self.assertFalse(check_ref_format(b'.refs/foo'))
  66. self.assertFalse(check_ref_format(b'heads/foo..bar'))
  67. self.assertFalse(check_ref_format(b'heads/foo?bar'))
  68. self.assertFalse(check_ref_format(b'heads/foo.lock'))
  69. self.assertFalse(check_ref_format(b'heads/v@{ation'))
  70. self.assertFalse(check_ref_format(b'heads/foo\bar'))
  71. ONES = b'1' * 40
  72. TWOS = b'2' * 40
  73. THREES = b'3' * 40
  74. FOURS = b'4' * 40
  75. class PackedRefsFileTests(TestCase):
  76. def test_split_ref_line_errors(self):
  77. self.assertRaises(errors.PackedRefsException, _split_ref_line,
  78. b'singlefield')
  79. self.assertRaises(errors.PackedRefsException, _split_ref_line,
  80. b'badsha name')
  81. self.assertRaises(errors.PackedRefsException, _split_ref_line,
  82. ONES + b' bad/../refname')
  83. def test_read_without_peeled(self):
  84. f = BytesIO(b'\n'.join([
  85. b'# comment',
  86. ONES + b' ref/1',
  87. TWOS + b' ref/2']))
  88. self.assertEqual([(ONES, b'ref/1'), (TWOS, b'ref/2')],
  89. list(read_packed_refs(f)))
  90. def test_read_without_peeled_errors(self):
  91. f = BytesIO(b'\n'.join([
  92. ONES + b' ref/1',
  93. b'^' + TWOS]))
  94. self.assertRaises(errors.PackedRefsException, list,
  95. read_packed_refs(f))
  96. def test_read_with_peeled(self):
  97. f = BytesIO(b'\n'.join([
  98. ONES + b' ref/1',
  99. TWOS + b' ref/2',
  100. b'^' + THREES,
  101. FOURS + b' ref/4']))
  102. self.assertEqual([
  103. (ONES, b'ref/1', None),
  104. (TWOS, b'ref/2', THREES),
  105. (FOURS, b'ref/4', None),
  106. ], list(read_packed_refs_with_peeled(f)))
  107. def test_read_with_peeled_errors(self):
  108. f = BytesIO(b'\n'.join([
  109. b'^' + TWOS,
  110. ONES + b' ref/1']))
  111. self.assertRaises(errors.PackedRefsException, list,
  112. read_packed_refs(f))
  113. f = BytesIO(b'\n'.join([
  114. ONES + b' ref/1',
  115. b'^' + TWOS,
  116. b'^' + THREES]))
  117. self.assertRaises(errors.PackedRefsException, list,
  118. read_packed_refs(f))
  119. def test_write_with_peeled(self):
  120. f = BytesIO()
  121. write_packed_refs(f, {b'ref/1': ONES, b'ref/2': TWOS},
  122. {b'ref/1': THREES})
  123. self.assertEqual(
  124. b'\n'.join([b'# pack-refs with: peeled',
  125. ONES + b' ref/1',
  126. b'^' + THREES,
  127. TWOS + b' ref/2']) + b'\n',
  128. f.getvalue())
  129. def test_write_without_peeled(self):
  130. f = BytesIO()
  131. write_packed_refs(f, {b'ref/1': ONES, b'ref/2': TWOS})
  132. self.assertEqual(b'\n'.join([ONES + b' ref/1',
  133. TWOS + b' ref/2']) + b'\n',
  134. f.getvalue())
  135. # Dict of refs that we expect all RefsContainerTests subclasses to define.
  136. _TEST_REFS = {
  137. b'HEAD': b'42d06bd4b77fed026b154d16493e5deab78f02ec',
  138. b'refs/heads/40-char-ref-aaaaaaaaaaaaaaaaaa':
  139. b'42d06bd4b77fed026b154d16493e5deab78f02ec',
  140. b'refs/heads/master': b'42d06bd4b77fed026b154d16493e5deab78f02ec',
  141. b'refs/heads/packed': b'42d06bd4b77fed026b154d16493e5deab78f02ec',
  142. b'refs/tags/refs-0.1': b'df6800012397fb85c56e7418dd4eb9405dee075c',
  143. b'refs/tags/refs-0.2': b'3ec9c43c84ff242e3ef4a9fc5bc111fd780a76a8',
  144. b'refs/heads/loop': b'ref: refs/heads/loop',
  145. }
  146. class RefsContainerTests(object):
  147. def test_keys(self):
  148. actual_keys = set(self._refs.keys())
  149. self.assertEqual(set(self._refs.allkeys()), actual_keys)
  150. self.assertEqual(set(_TEST_REFS.keys()), actual_keys)
  151. actual_keys = self._refs.keys(b'refs/heads')
  152. actual_keys.discard(b'loop')
  153. self.assertEqual(
  154. [b'40-char-ref-aaaaaaaaaaaaaaaaaa', b'master', b'packed'],
  155. sorted(actual_keys))
  156. self.assertEqual([b'refs-0.1', b'refs-0.2'],
  157. sorted(self._refs.keys(b'refs/tags')))
  158. def test_as_dict(self):
  159. # refs/heads/loop does not show up even if it exists
  160. expected_refs = dict(_TEST_REFS)
  161. del expected_refs[b'refs/heads/loop']
  162. self.assertEqual(expected_refs, self._refs.as_dict())
  163. def test_get_symrefs(self):
  164. self._refs.set_symbolic_ref(b'refs/heads/src', b'refs/heads/dst')
  165. symrefs = self._refs.get_symrefs()
  166. if b'HEAD' in symrefs:
  167. symrefs.pop(b'HEAD')
  168. self.assertEqual({b'refs/heads/src': b'refs/heads/dst',
  169. b'refs/heads/loop': b'refs/heads/loop'},
  170. symrefs)
  171. def test_setitem(self):
  172. self._refs[b'refs/some/ref'] = (
  173. b'42d06bd4b77fed026b154d16493e5deab78f02ec')
  174. self.assertEqual(b'42d06bd4b77fed026b154d16493e5deab78f02ec',
  175. self._refs[b'refs/some/ref'])
  176. self.assertRaises(
  177. errors.RefFormatError, self._refs.__setitem__,
  178. b'notrefs/foo', b'42d06bd4b77fed026b154d16493e5deab78f02ec')
  179. def test_set_if_equals(self):
  180. nines = b'9' * 40
  181. self.assertFalse(self._refs.set_if_equals(b'HEAD', b'c0ffee', nines))
  182. self.assertEqual(b'42d06bd4b77fed026b154d16493e5deab78f02ec',
  183. self._refs[b'HEAD'])
  184. self.assertTrue(self._refs.set_if_equals(
  185. b'HEAD', b'42d06bd4b77fed026b154d16493e5deab78f02ec', nines))
  186. self.assertEqual(nines, self._refs[b'HEAD'])
  187. self.assertTrue(self._refs.set_if_equals(b'refs/heads/master', None,
  188. nines))
  189. self.assertEqual(nines, self._refs[b'refs/heads/master'])
  190. self.assertTrue(self._refs.set_if_equals(
  191. b'refs/heads/nonexistant', ZERO_SHA, nines))
  192. self.assertEqual(nines, self._refs[b'refs/heads/nonexistant'])
  193. def test_add_if_new(self):
  194. nines = b'9' * 40
  195. self.assertFalse(self._refs.add_if_new(b'refs/heads/master', nines))
  196. self.assertEqual(b'42d06bd4b77fed026b154d16493e5deab78f02ec',
  197. self._refs[b'refs/heads/master'])
  198. self.assertTrue(self._refs.add_if_new(b'refs/some/ref', nines))
  199. self.assertEqual(nines, self._refs[b'refs/some/ref'])
  200. def test_set_symbolic_ref(self):
  201. self._refs.set_symbolic_ref(b'refs/heads/symbolic',
  202. b'refs/heads/master')
  203. self.assertEqual(b'ref: refs/heads/master',
  204. self._refs.read_loose_ref(b'refs/heads/symbolic'))
  205. self.assertEqual(b'42d06bd4b77fed026b154d16493e5deab78f02ec',
  206. self._refs[b'refs/heads/symbolic'])
  207. def test_set_symbolic_ref_overwrite(self):
  208. nines = b'9' * 40
  209. self.assertFalse(b'refs/heads/symbolic' in self._refs)
  210. self._refs[b'refs/heads/symbolic'] = nines
  211. self.assertEqual(nines,
  212. self._refs.read_loose_ref(b'refs/heads/symbolic'))
  213. self._refs.set_symbolic_ref(b'refs/heads/symbolic',
  214. b'refs/heads/master')
  215. self.assertEqual(b'ref: refs/heads/master',
  216. self._refs.read_loose_ref(b'refs/heads/symbolic'))
  217. self.assertEqual(b'42d06bd4b77fed026b154d16493e5deab78f02ec',
  218. self._refs[b'refs/heads/symbolic'])
  219. def test_check_refname(self):
  220. self._refs._check_refname(b'HEAD')
  221. self._refs._check_refname(b'refs/stash')
  222. self._refs._check_refname(b'refs/heads/foo')
  223. self.assertRaises(errors.RefFormatError, self._refs._check_refname,
  224. b'refs')
  225. self.assertRaises(errors.RefFormatError, self._refs._check_refname,
  226. b'notrefs/foo')
  227. def test_contains(self):
  228. self.assertTrue(b'refs/heads/master' in self._refs)
  229. self.assertFalse(b'refs/heads/bar' in self._refs)
  230. def test_delitem(self):
  231. self.assertEqual(b'42d06bd4b77fed026b154d16493e5deab78f02ec',
  232. self._refs[b'refs/heads/master'])
  233. del self._refs[b'refs/heads/master']
  234. self.assertRaises(KeyError, lambda: self._refs[b'refs/heads/master'])
  235. def test_remove_if_equals(self):
  236. self.assertFalse(self._refs.remove_if_equals(b'HEAD', b'c0ffee'))
  237. self.assertEqual(b'42d06bd4b77fed026b154d16493e5deab78f02ec',
  238. self._refs[b'HEAD'])
  239. self.assertTrue(self._refs.remove_if_equals(
  240. b'refs/tags/refs-0.2',
  241. b'3ec9c43c84ff242e3ef4a9fc5bc111fd780a76a8'))
  242. self.assertTrue(self._refs.remove_if_equals(
  243. b'refs/tags/refs-0.2', ZERO_SHA))
  244. self.assertFalse(b'refs/tags/refs-0.2' in self._refs)
  245. class DictRefsContainerTests(RefsContainerTests, TestCase):
  246. def setUp(self):
  247. TestCase.setUp(self)
  248. self._refs = DictRefsContainer(dict(_TEST_REFS))
  249. def test_invalid_refname(self):
  250. # FIXME: Move this test into RefsContainerTests, but requires
  251. # some way of injecting invalid refs.
  252. self._refs._refs[b'refs/stash'] = b'00' * 20
  253. expected_refs = dict(_TEST_REFS)
  254. del expected_refs[b'refs/heads/loop']
  255. expected_refs[b'refs/stash'] = b'00' * 20
  256. self.assertEqual(expected_refs, self._refs.as_dict())
  257. class DiskRefsContainerTests(RefsContainerTests, TestCase):
  258. def setUp(self):
  259. TestCase.setUp(self)
  260. self._repo = open_repo('refs.git')
  261. self.addCleanup(tear_down_repo, self._repo)
  262. self._refs = self._repo.refs
  263. def test_get_packed_refs(self):
  264. self.assertEqual({
  265. b'refs/heads/packed': b'42d06bd4b77fed026b154d16493e5deab78f02ec',
  266. b'refs/tags/refs-0.1': b'df6800012397fb85c56e7418dd4eb9405dee075c',
  267. }, self._refs.get_packed_refs())
  268. def test_get_peeled_not_packed(self):
  269. # not packed
  270. self.assertEqual(None, self._refs.get_peeled(b'refs/tags/refs-0.2'))
  271. self.assertEqual(b'3ec9c43c84ff242e3ef4a9fc5bc111fd780a76a8',
  272. self._refs[b'refs/tags/refs-0.2'])
  273. # packed, known not peelable
  274. self.assertEqual(self._refs[b'refs/heads/packed'],
  275. self._refs.get_peeled(b'refs/heads/packed'))
  276. # packed, peeled
  277. self.assertEqual(b'42d06bd4b77fed026b154d16493e5deab78f02ec',
  278. self._refs.get_peeled(b'refs/tags/refs-0.1'))
  279. def test_setitem(self):
  280. RefsContainerTests.test_setitem(self)
  281. f = open(os.path.join(self._refs.path, 'refs', 'some', 'ref'), 'rb')
  282. self.assertEqual(b'42d06bd4b77fed026b154d16493e5deab78f02ec',
  283. f.read()[:40])
  284. f.close()
  285. def test_setitem_symbolic(self):
  286. ones = b'1' * 40
  287. self._refs[b'HEAD'] = ones
  288. self.assertEqual(ones, self._refs[b'HEAD'])
  289. # ensure HEAD was not modified
  290. f = open(os.path.join(self._refs.path, 'HEAD'), 'rb')
  291. v = next(iter(f)).rstrip(b'\n\r')
  292. f.close()
  293. self.assertEqual(b'ref: refs/heads/master', v)
  294. # ensure the symbolic link was written through
  295. f = open(os.path.join(self._refs.path, 'refs', 'heads', 'master'),
  296. 'rb')
  297. self.assertEqual(ones, f.read()[:40])
  298. f.close()
  299. def test_set_if_equals(self):
  300. RefsContainerTests.test_set_if_equals(self)
  301. # ensure symref was followed
  302. self.assertEqual(b'9' * 40, self._refs[b'refs/heads/master'])
  303. # ensure lockfile was deleted
  304. self.assertFalse(os.path.exists(
  305. os.path.join(self._refs.path, 'refs', 'heads', 'master.lock')))
  306. self.assertFalse(os.path.exists(
  307. os.path.join(self._refs.path, 'HEAD.lock')))
  308. def test_add_if_new_packed(self):
  309. # don't overwrite packed ref
  310. self.assertFalse(self._refs.add_if_new(b'refs/tags/refs-0.1',
  311. b'9' * 40))
  312. self.assertEqual(b'df6800012397fb85c56e7418dd4eb9405dee075c',
  313. self._refs[b'refs/tags/refs-0.1'])
  314. def test_add_if_new_symbolic(self):
  315. # Use an empty repo instead of the default.
  316. repo_dir = os.path.join(tempfile.mkdtemp(), 'test')
  317. os.makedirs(repo_dir)
  318. repo = Repo.init(repo_dir)
  319. self.addCleanup(tear_down_repo, repo)
  320. refs = repo.refs
  321. nines = b'9' * 40
  322. self.assertEqual(b'ref: refs/heads/master', refs.read_ref(b'HEAD'))
  323. self.assertFalse(b'refs/heads/master' in refs)
  324. self.assertTrue(refs.add_if_new(b'HEAD', nines))
  325. self.assertEqual(b'ref: refs/heads/master', refs.read_ref(b'HEAD'))
  326. self.assertEqual(nines, refs[b'HEAD'])
  327. self.assertEqual(nines, refs[b'refs/heads/master'])
  328. self.assertFalse(refs.add_if_new(b'HEAD', b'1' * 40))
  329. self.assertEqual(nines, refs[b'HEAD'])
  330. self.assertEqual(nines, refs[b'refs/heads/master'])
  331. def test_follow(self):
  332. self.assertEqual(([b'HEAD', b'refs/heads/master'],
  333. b'42d06bd4b77fed026b154d16493e5deab78f02ec'),
  334. self._refs.follow(b'HEAD'))
  335. self.assertEqual(([b'refs/heads/master'],
  336. b'42d06bd4b77fed026b154d16493e5deab78f02ec'),
  337. self._refs.follow(b'refs/heads/master'))
  338. self.assertRaises(KeyError, self._refs.follow, b'refs/heads/loop')
  339. def test_delitem(self):
  340. RefsContainerTests.test_delitem(self)
  341. ref_file = os.path.join(self._refs.path, 'refs', 'heads', 'master')
  342. self.assertFalse(os.path.exists(ref_file))
  343. self.assertFalse(b'refs/heads/master' in self._refs.get_packed_refs())
  344. def test_delitem_symbolic(self):
  345. self.assertEqual(b'ref: refs/heads/master',
  346. self._refs.read_loose_ref(b'HEAD'))
  347. del self._refs[b'HEAD']
  348. self.assertRaises(KeyError, lambda: self._refs[b'HEAD'])
  349. self.assertEqual(b'42d06bd4b77fed026b154d16493e5deab78f02ec',
  350. self._refs[b'refs/heads/master'])
  351. self.assertFalse(os.path.exists(os.path.join(self._refs.path, 'HEAD')))
  352. def test_remove_if_equals_symref(self):
  353. # HEAD is a symref, so shouldn't equal its dereferenced value
  354. self.assertFalse(self._refs.remove_if_equals(
  355. b'HEAD', b'42d06bd4b77fed026b154d16493e5deab78f02ec'))
  356. self.assertTrue(self._refs.remove_if_equals(
  357. b'refs/heads/master', b'42d06bd4b77fed026b154d16493e5deab78f02ec'))
  358. self.assertRaises(KeyError, lambda: self._refs[b'refs/heads/master'])
  359. # HEAD is now a broken symref
  360. self.assertRaises(KeyError, lambda: self._refs[b'HEAD'])
  361. self.assertEqual(b'ref: refs/heads/master',
  362. self._refs.read_loose_ref(b'HEAD'))
  363. self.assertFalse(os.path.exists(
  364. os.path.join(self._refs.path, 'refs', 'heads', 'master.lock')))
  365. self.assertFalse(os.path.exists(
  366. os.path.join(self._refs.path, 'HEAD.lock')))
  367. def test_remove_packed_without_peeled(self):
  368. refs_file = os.path.join(self._repo.path, 'packed-refs')
  369. f = GitFile(refs_file)
  370. refs_data = f.read()
  371. f.close()
  372. f = GitFile(refs_file, 'wb')
  373. f.write(b'\n'.join(l for l in refs_data.split(b'\n')
  374. if not l or l[0] not in b'#^'))
  375. f.close()
  376. self._repo = Repo(self._repo.path)
  377. refs = self._repo.refs
  378. self.assertTrue(refs.remove_if_equals(
  379. b'refs/heads/packed', b'42d06bd4b77fed026b154d16493e5deab78f02ec'))
  380. def test_remove_if_equals_packed(self):
  381. # test removing ref that is only packed
  382. self.assertEqual(b'df6800012397fb85c56e7418dd4eb9405dee075c',
  383. self._refs[b'refs/tags/refs-0.1'])
  384. self.assertTrue(
  385. self._refs.remove_if_equals(
  386. b'refs/tags/refs-0.1',
  387. b'df6800012397fb85c56e7418dd4eb9405dee075c'))
  388. self.assertRaises(KeyError, lambda: self._refs[b'refs/tags/refs-0.1'])
  389. def test_read_ref(self):
  390. self.assertEqual(b'ref: refs/heads/master',
  391. self._refs.read_ref(b'HEAD'))
  392. self.assertEqual(b'42d06bd4b77fed026b154d16493e5deab78f02ec',
  393. self._refs.read_ref(b'refs/heads/packed'))
  394. self.assertEqual(None, self._refs.read_ref(b'nonexistant'))
  395. def test_non_ascii(self):
  396. try:
  397. encoded_ref = u'refs/tags/schön'.encode(
  398. sys.getfilesystemencoding())
  399. except UnicodeEncodeError:
  400. raise SkipTest(
  401. "filesystem encoding doesn't support special character")
  402. p = os.path.join(self._repo.path, 'refs', 'tags', u'schön')
  403. with open(p, 'w') as f:
  404. f.write('00' * 20)
  405. expected_refs = dict(_TEST_REFS)
  406. expected_refs[encoded_ref] = b'00' * 20
  407. del expected_refs[b'refs/heads/loop']
  408. self.assertEqual(expected_refs, self._repo.get_refs())
  409. _TEST_REFS_SERIALIZED = (
  410. b'42d06bd4b77fed026b154d16493e5deab78f02ec\t'
  411. b'refs/heads/40-char-ref-aaaaaaaaaaaaaaaaaa\n'
  412. b'42d06bd4b77fed026b154d16493e5deab78f02ec\trefs/heads/master\n'
  413. b'42d06bd4b77fed026b154d16493e5deab78f02ec\trefs/heads/packed\n'
  414. b'df6800012397fb85c56e7418dd4eb9405dee075c\trefs/tags/refs-0.1\n'
  415. b'3ec9c43c84ff242e3ef4a9fc5bc111fd780a76a8\trefs/tags/refs-0.2\n')
  416. class InfoRefsContainerTests(TestCase):
  417. def test_invalid_refname(self):
  418. text = _TEST_REFS_SERIALIZED + b'00' * 20 + b'\trefs/stash\n'
  419. refs = InfoRefsContainer(BytesIO(text))
  420. expected_refs = dict(_TEST_REFS)
  421. del expected_refs[b'HEAD']
  422. expected_refs[b'refs/stash'] = b'00' * 20
  423. del expected_refs[b'refs/heads/loop']
  424. self.assertEqual(expected_refs, refs.as_dict())
  425. def test_keys(self):
  426. refs = InfoRefsContainer(BytesIO(_TEST_REFS_SERIALIZED))
  427. actual_keys = set(refs.keys())
  428. self.assertEqual(set(refs.allkeys()), actual_keys)
  429. expected_refs = dict(_TEST_REFS)
  430. del expected_refs[b'HEAD']
  431. del expected_refs[b'refs/heads/loop']
  432. self.assertEqual(set(expected_refs.keys()), actual_keys)
  433. actual_keys = refs.keys(b'refs/heads')
  434. actual_keys.discard(b'loop')
  435. self.assertEqual(
  436. [b'40-char-ref-aaaaaaaaaaaaaaaaaa', b'master', b'packed'],
  437. sorted(actual_keys))
  438. self.assertEqual([b'refs-0.1', b'refs-0.2'],
  439. sorted(refs.keys(b'refs/tags')))
  440. def test_as_dict(self):
  441. refs = InfoRefsContainer(BytesIO(_TEST_REFS_SERIALIZED))
  442. # refs/heads/loop does not show up even if it exists
  443. expected_refs = dict(_TEST_REFS)
  444. del expected_refs[b'HEAD']
  445. del expected_refs[b'refs/heads/loop']
  446. self.assertEqual(expected_refs, refs.as_dict())
  447. def test_contains(self):
  448. refs = InfoRefsContainer(BytesIO(_TEST_REFS_SERIALIZED))
  449. self.assertTrue(b'refs/heads/master' in refs)
  450. self.assertFalse(b'refs/heads/bar' in refs)
  451. def test_get_peeled(self):
  452. refs = InfoRefsContainer(BytesIO(_TEST_REFS_SERIALIZED))
  453. # refs/heads/loop does not show up even if it exists
  454. self.assertEqual(
  455. _TEST_REFS[b'refs/heads/master'],
  456. refs.get_peeled(b'refs/heads/master'))
  457. class ParseSymrefValueTests(TestCase):
  458. def test_valid(self):
  459. self.assertEqual(
  460. b'refs/heads/foo',
  461. parse_symref_value(b'ref: refs/heads/foo'))
  462. def test_invalid(self):
  463. self.assertRaises(ValueError, parse_symref_value, b'foobar')