From bfd20dc4b44c67f47ca0c1d0538d4b5bba0ed519 Mon Sep 17 00:00:00 2001 From: Petr Viktorin <encukou@gmail.com> Date: Mon, 8 Aug 2011 23:42:01 +0200 Subject: [PATCH] Somewhat better test coverage - create_repository - IndexEntry.path - Correct SHAs in Index iteration - Opening a bare index & attempting to add to it - Walker.reset - Walker.push - Walker.sort --- test/test_index.py | 16 +++++++++++++--- test/test_repository.py | 12 +++++++++++- test/test_revwalk.py | 20 ++++++++++++++++++++ test/utils.py | 5 +++++ 4 files changed, 49 insertions(+), 4 deletions(-) diff --git a/test/test_index.py b/test/test_index.py index f2d82f7..0f3b5d1 100644 --- a/test/test_index.py +++ b/test/test_index.py @@ -31,9 +31,11 @@ __author__ = 'jdavid@itaapy.com (J. David Ibáñez)' import unittest +import os import utils +import pygit2 class IndexBareTest(utils.BareRepoTestCase): @@ -57,6 +59,7 @@ class IndexTest(utils.RepoTestCase): sha = 'a520c24d85fbfc815d385957eed41406ca5a860b' self.assertTrue('hello.txt' in index) self.assertEqual(index['hello.txt'].sha, sha) + self.assertEqual(index['hello.txt'].path, 'hello.txt') self.assertEqual(index[1].sha, sha) def test_add(self): @@ -93,9 +96,10 @@ class IndexTest(utils.RepoTestCase): index = self.repo.index n = len(index) self.assertEqual(len(list(index)), n) - # FIXME This fails - #entries = [index[x] for x in xrange(n)] - #self.assertEqual(list(index), entries) + + # Compare SHAs, not IndexEntry object identity + entries = [index[x].sha for x in xrange(n)] + self.assertEqual(list(x.sha for x in index), entries) def test_mode(self): """ @@ -106,6 +110,12 @@ class IndexTest(utils.RepoTestCase): hello_mode = index['hello.txt'].mode self.assertEqual(hello_mode, 33188) + def test_bare_index(self): + index = pygit2.Index(os.path.join(self.repo.path, 'index')) + self.assertEqual([x.sha for x in index], + [x.sha for x in self.repo.index]) + + self.assertRaises(pygit2.GitError, lambda: index.add('bye.txt', 0)) if __name__ == '__main__': unittest.main() diff --git a/test/test_repository.py b/test/test_repository.py index 445c2e6..ed1535b 100644 --- a/test/test_repository.py +++ b/test/test_repository.py @@ -31,9 +31,11 @@ __author__ = 'dborowitz@google.com (Dave Borowitz)' import binascii import unittest +import os from os.path import join, abspath -from pygit2 import GitError, GIT_OBJ_ANY, GIT_OBJ_BLOB, GIT_OBJ_COMMIT +from pygit2 import (GitError, GIT_OBJ_ANY, GIT_OBJ_BLOB, GIT_OBJ_COMMIT, + init_repository) import utils A_HEX_SHA = 'af431f20fc541ed6d5afede3e2dc7160f6f01f16' @@ -111,6 +113,14 @@ class RepositoryTest_II(utils.RepoTestCase): self.assertEqual(directory, expected) +class NewRepositoryTest(utils.NoRepoTestCase): + def test_new_repo(self): + repo = init_repository(self.temp_dir, False) + + hex_sha = repo.write(GIT_OBJ_BLOB, "Test") + self.assertEqual(len(hex_sha), 40) + + assert os.path.exists(os.path.join(self._temp_dir, '.git')) if __name__ == '__main__': unittest.main() diff --git a/test/test_revwalk.py b/test/test_revwalk.py index 5fa912d..4bf5275 100644 --- a/test/test_revwalk.py +++ b/test/test_revwalk.py @@ -61,6 +61,26 @@ class WalkerTest(utils.RepoTestCase): walker.hide('4ec4389a8068641da2d6578db0419484972284c8') self.assertEqual(len(list(walker)), 2) + def test_reset(self): + walker = self.repo.walk(log[0], GIT_SORT_TIME) + walker.reset() + out = [ x.sha for x in walker ] + self.assertEqual(out, []) + + def test_push(self): + walker = self.repo.walk(log[-1], GIT_SORT_TIME) + out = [ x.sha for x in walker ] + self.assertEqual(out, log[-1:]) + walker.reset() + walker.push(log[0]) + out = [ x.sha for x in walker ] + self.assertEqual(out, log) + + def test_sort(self): + walker = self.repo.walk(log[0], GIT_SORT_TIME) + walker.sort(GIT_SORT_TIME | GIT_SORT_REVERSE) + out = [ x.sha for x in walker ] + self.assertEqual(out, list(reversed(log))) if __name__ == '__main__': unittest.main() diff --git a/test/utils.py b/test/utils.py index 0fcfc1c..e973a02 100644 --- a/test/utils.py +++ b/test/utils.py @@ -79,6 +79,11 @@ class RepoTestCase(BaseTestCase): temp_repo_path = os.path.join(temp_dir, repo_dir, '.git') self.repo = pygit2.Repository(temp_repo_path) +class NoRepoTestCase(BaseTestCase): + def setUp(self): + self.temp_dir = tempfile.mkdtemp() + self._temp_dir = self.temp_dir + class DirtyRepoTestCase(RepoTestCase): repo_dir = 'dirtyrepo'