test_repository.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. # test_repo.py -- Git repo compatibility tests
  2. # Copyright (C) 2010 Google, Inc.
  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. """Compatibility tests for dulwich repositories."""
  21. from io import BytesIO
  22. from itertools import chain
  23. import os
  24. from dulwich.objects import (
  25. hex_to_sha,
  26. )
  27. from dulwich.repo import (
  28. check_ref_format,
  29. )
  30. from dulwich.tests.compat.utils import (
  31. run_git_or_fail,
  32. CompatTestCase,
  33. )
  34. class ObjectStoreTestCase(CompatTestCase):
  35. """Tests for git repository compatibility."""
  36. def setUp(self):
  37. super(ObjectStoreTestCase, self).setUp()
  38. self._repo = self.import_repo('server_new.export')
  39. def _run_git(self, args):
  40. return run_git_or_fail(args, cwd=self._repo.path)
  41. def _parse_refs(self, output):
  42. refs = {}
  43. for line in BytesIO(output):
  44. fields = line.rstrip(b'\n').split(b' ')
  45. self.assertEqual(3, len(fields))
  46. refname, type_name, sha = fields
  47. check_ref_format(refname[5:])
  48. hex_to_sha(sha)
  49. refs[refname] = (type_name, sha)
  50. return refs
  51. def _parse_objects(self, output):
  52. return set(s.rstrip(b'\n').split(b' ')[0] for s in BytesIO(output))
  53. def test_bare(self):
  54. self.assertTrue(self._repo.bare)
  55. self.assertFalse(os.path.exists(os.path.join(self._repo.path, '.git')))
  56. def test_head(self):
  57. output = self._run_git(['rev-parse', 'HEAD'])
  58. head_sha = output.rstrip(b'\n')
  59. hex_to_sha(head_sha)
  60. self.assertEqual(head_sha, self._repo.refs[b'HEAD'])
  61. def test_refs(self):
  62. output = self._run_git(
  63. ['for-each-ref', '--format=%(refname) %(objecttype) %(objectname)'])
  64. expected_refs = self._parse_refs(output)
  65. actual_refs = {}
  66. for refname, sha in self._repo.refs.as_dict().items():
  67. if refname == b'HEAD':
  68. continue # handled in test_head
  69. obj = self._repo[sha]
  70. self.assertEqual(sha, obj.id)
  71. actual_refs[refname] = (obj.type_name, obj.id)
  72. self.assertEqual(expected_refs, actual_refs)
  73. # TODO(dborowitz): peeled ref tests
  74. def _get_loose_shas(self):
  75. output = self._run_git(['rev-list', '--all', '--objects', '--unpacked'])
  76. return self._parse_objects(output)
  77. def _get_all_shas(self):
  78. output = self._run_git(['rev-list', '--all', '--objects'])
  79. return self._parse_objects(output)
  80. def assertShasMatch(self, expected_shas, actual_shas_iter):
  81. actual_shas = set()
  82. for sha in actual_shas_iter:
  83. obj = self._repo[sha]
  84. self.assertEqual(sha, obj.id)
  85. actual_shas.add(sha)
  86. self.assertEqual(expected_shas, actual_shas)
  87. def test_loose_objects(self):
  88. # TODO(dborowitz): This is currently not very useful since fast-imported
  89. # repos only contained packed objects.
  90. expected_shas = self._get_loose_shas()
  91. self.assertShasMatch(expected_shas,
  92. self._repo.object_store._iter_loose_objects())
  93. def test_packed_objects(self):
  94. expected_shas = self._get_all_shas() - self._get_loose_shas()
  95. self.assertShasMatch(expected_shas,
  96. chain(*self._repo.object_store.packs))
  97. def test_all_objects(self):
  98. expected_shas = self._get_all_shas()
  99. self.assertShasMatch(expected_shas, iter(self._repo.object_store))