test_repository.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. # test_repo.py -- Git repo compatibility tests
  2. # Copyright (C) 2010 Google, Inc.
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; version 2
  7. # of the License or (at your option) any later version of
  8. # the License.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  18. # MA 02110-1301, USA.
  19. """Compatibility tests for dulwich repositories."""
  20. from io import BytesIO
  21. from itertools import chain
  22. import os
  23. from dulwich.objects import (
  24. hex_to_sha,
  25. )
  26. from dulwich.repo import (
  27. check_ref_format,
  28. )
  29. from dulwich.tests.utils import (
  30. tear_down_repo,
  31. )
  32. from dulwich.tests.compat.utils import (
  33. run_git_or_fail,
  34. import_repo,
  35. CompatTestCase,
  36. )
  37. class ObjectStoreTestCase(CompatTestCase):
  38. """Tests for git repository compatibility."""
  39. def setUp(self):
  40. super(ObjectStoreTestCase, self).setUp()
  41. self._repo = import_repo('server_new.export')
  42. self.addCleanup(tear_down_repo, self._repo)
  43. def _run_git(self, args):
  44. return run_git_or_fail(args, cwd=self._repo.path)
  45. def _parse_refs(self, output):
  46. refs = {}
  47. for line in BytesIO(output):
  48. fields = line.rstrip('\n').split(' ')
  49. self.assertEqual(3, len(fields))
  50. refname, type_name, sha = fields
  51. check_ref_format(refname[5:])
  52. hex_to_sha(sha)
  53. refs[refname] = (type_name, sha)
  54. return refs
  55. def _parse_objects(self, output):
  56. return set(s.rstrip('\n').split(' ')[0] for s in BytesIO(output))
  57. def test_bare(self):
  58. self.assertTrue(self._repo.bare)
  59. self.assertFalse(os.path.exists(os.path.join(self._repo.path, '.git')))
  60. def test_head(self):
  61. output = self._run_git(['rev-parse', 'HEAD'])
  62. head_sha = output.rstrip('\n')
  63. hex_to_sha(head_sha)
  64. self.assertEqual(head_sha, self._repo.refs['HEAD'])
  65. def test_refs(self):
  66. output = self._run_git(
  67. ['for-each-ref', '--format=%(refname) %(objecttype) %(objectname)'])
  68. expected_refs = self._parse_refs(output)
  69. actual_refs = {}
  70. for refname, sha in self._repo.refs.as_dict().iteritems():
  71. if refname == 'HEAD':
  72. continue # handled in test_head
  73. obj = self._repo[sha]
  74. self.assertEqual(sha, obj.id)
  75. actual_refs[refname] = (obj.type_name, obj.id)
  76. self.assertEqual(expected_refs, actual_refs)
  77. # TODO(dborowitz): peeled ref tests
  78. def _get_loose_shas(self):
  79. output = self._run_git(['rev-list', '--all', '--objects', '--unpacked'])
  80. return self._parse_objects(output)
  81. def _get_all_shas(self):
  82. output = self._run_git(['rev-list', '--all', '--objects'])
  83. return self._parse_objects(output)
  84. def assertShasMatch(self, expected_shas, actual_shas_iter):
  85. actual_shas = set()
  86. for sha in actual_shas_iter:
  87. obj = self._repo[sha]
  88. self.assertEqual(sha, obj.id)
  89. actual_shas.add(sha)
  90. self.assertEqual(expected_shas, actual_shas)
  91. def test_loose_objects(self):
  92. # TODO(dborowitz): This is currently not very useful since fast-imported
  93. # repos only contained packed objects.
  94. expected_shas = self._get_loose_shas()
  95. self.assertShasMatch(expected_shas,
  96. self._repo.object_store._iter_loose_objects())
  97. def test_packed_objects(self):
  98. expected_shas = self._get_all_shas() - self._get_loose_shas()
  99. self.assertShasMatch(expected_shas,
  100. chain(*self._repo.object_store.packs))
  101. def test_all_objects(self):
  102. expected_shas = self._get_all_shas()
  103. self.assertShasMatch(expected_shas, iter(self._repo.object_store))