|
@@ -0,0 +1,131 @@
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+"""Compatibility tests for dulwich repositories."""
|
|
|
+
|
|
|
+
|
|
|
+from cStringIO import StringIO
|
|
|
+import itertools
|
|
|
+import os
|
|
|
+
|
|
|
+from dulwich.objects import (
|
|
|
+ hex_to_sha,
|
|
|
+ )
|
|
|
+from dulwich.repo import (
|
|
|
+ check_ref_format,
|
|
|
+ )
|
|
|
+from dulwich.tests.utils import (
|
|
|
+ tear_down_repo,
|
|
|
+ )
|
|
|
+
|
|
|
+from utils import (
|
|
|
+ run_git,
|
|
|
+ import_repo,
|
|
|
+ CompatTestCase,
|
|
|
+ )
|
|
|
+
|
|
|
+
|
|
|
+class ObjectStoreTestCase(CompatTestCase):
|
|
|
+ """Tests for git repository compatibility."""
|
|
|
+
|
|
|
+ def setUp(self):
|
|
|
+ CompatTestCase.setUp(self)
|
|
|
+ self._repo = import_repo('server_new.export')
|
|
|
+
|
|
|
+ def tearDown(self):
|
|
|
+ CompatTestCase.tearDown(self)
|
|
|
+ tear_down_repo(self._repo)
|
|
|
+
|
|
|
+ def _run_git(self, args):
|
|
|
+ returncode, output = run_git(args, capture_stdout=True,
|
|
|
+ cwd=self._repo.path)
|
|
|
+ self.assertEqual(0, returncode)
|
|
|
+ return output
|
|
|
+
|
|
|
+ def _parse_refs(self, output):
|
|
|
+ refs = {}
|
|
|
+ for line in StringIO(output):
|
|
|
+ fields = line.rstrip('\n').split(' ')
|
|
|
+ self.assertEqual(3, len(fields))
|
|
|
+ refname, type_name, sha = fields
|
|
|
+ check_ref_format(refname[5:])
|
|
|
+ hex_to_sha(sha)
|
|
|
+ refs[refname] = (type_name, sha)
|
|
|
+ return refs
|
|
|
+
|
|
|
+ def _parse_objects(self, output):
|
|
|
+ return set(s.rstrip('\n').split(' ')[0] for s in StringIO(output))
|
|
|
+
|
|
|
+ def test_bare(self):
|
|
|
+ self.assertTrue(self._repo.bare)
|
|
|
+ self.assertFalse(os.path.exists(os.path.join(self._repo.path, '.git')))
|
|
|
+
|
|
|
+ def test_head(self):
|
|
|
+ output = self._run_git(['rev-parse', 'HEAD'])
|
|
|
+ head_sha = output.rstrip('\n')
|
|
|
+ hex_to_sha(head_sha)
|
|
|
+ self.assertEqual(head_sha, self._repo.refs['HEAD'])
|
|
|
+
|
|
|
+ def test_refs(self):
|
|
|
+ output = self._run_git(
|
|
|
+ ['for-each-ref', '--format=%(refname) %(objecttype) %(objectname)'])
|
|
|
+ expected_refs = self._parse_refs(output)
|
|
|
+
|
|
|
+ actual_refs = {}
|
|
|
+ for refname, sha in self._repo.refs.as_dict().iteritems():
|
|
|
+ if refname == 'HEAD':
|
|
|
+ continue
|
|
|
+ obj = self._repo[sha]
|
|
|
+ self.assertEqual(sha, obj.id)
|
|
|
+ actual_refs[refname] = (obj.type_name, obj.id)
|
|
|
+ self.assertEqual(expected_refs, actual_refs)
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ def _get_loose_shas(self):
|
|
|
+ output = self._run_git(['rev-list', '--all', '--objects', '--unpacked'])
|
|
|
+ return self._parse_objects(output)
|
|
|
+
|
|
|
+ def _get_all_shas(self):
|
|
|
+ output = self._run_git(['rev-list', '--all', '--objects'])
|
|
|
+ return self._parse_objects(output)
|
|
|
+
|
|
|
+ def assertShasMatch(self, expected_shas, actual_shas_iter):
|
|
|
+ actual_shas = set()
|
|
|
+ for sha in actual_shas_iter:
|
|
|
+ obj = self._repo[sha]
|
|
|
+ self.assertEqual(sha, obj.id)
|
|
|
+ actual_shas.add(sha)
|
|
|
+ self.assertEqual(expected_shas, actual_shas)
|
|
|
+
|
|
|
+ def test_loose_objects(self):
|
|
|
+
|
|
|
+
|
|
|
+ expected_shas = self._get_loose_shas()
|
|
|
+ self.assertShasMatch(expected_shas,
|
|
|
+ self._repo.object_store._iter_loose_objects())
|
|
|
+
|
|
|
+ def test_packed_objects(self):
|
|
|
+ expected_shas = self._get_all_shas() - self._get_loose_shas()
|
|
|
+ self.assertShasMatch(expected_shas,
|
|
|
+ itertools.chain(*self._repo.object_store.packs))
|
|
|
+
|
|
|
+ def test_all_objects(self):
|
|
|
+ expected_shas = self._get_all_shas()
|
|
|
+ self.assertShasMatch(expected_shas, iter(self._repo.object_store))
|