From af983b989a7d4d5e695b7457b741802c4384b685 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=2E=20David=20Ib=C3=A1=C3=B1ez?= Date: Mon, 17 Oct 2011 20:44:54 +0200 Subject: [PATCH] tests: fixing issue #20 There were two problems: - Windows refuses to remove a file that is in use. Solution, close the repo before cleaning the temporary directory. - Windows refuses to remove a read-only file. Solution, change mode to writable. --- test/test_repository.py | 2 +- test/utils.py | 62 ++++++++++++++++++++++++++++------------- 2 files changed, 43 insertions(+), 21 deletions(-) diff --git a/test/test_repository.py b/test/test_repository.py index 303ea5a..89edcbe 100644 --- a/test/test_repository.py +++ b/test/test_repository.py @@ -116,7 +116,7 @@ class RepositoryTest_II(utils.RepoTestCase): class NewRepositoryTest(utils.NoRepoTestCase): def test_new_repo(self): - repo = init_repository(self.temp_dir, False) + repo = init_repository(self._temp_dir, False) oid = repo.write(GIT_OBJ_BLOB, "Test") self.assertEqual(type(oid), bytes) diff --git a/test/utils.py b/test/utils.py index 55035cb..b9c7549 100644 --- a/test/utils.py +++ b/test/utils.py @@ -29,6 +29,7 @@ import os import shutil +import stat import tarfile import tempfile import unittest @@ -39,10 +40,31 @@ import pygit2 __author__ = 'dborowitz@google.com (Dave Borowitz)' -class BaseTestCase(unittest.TestCase): +def rmtree(path): + """In Windows a read-only file cannot be removed, and shutil.rmtree fails. + So we implement our own version of rmtree to address this issue. + """ + for root, dirs, files in os.walk(path, topdown=False): + for name in files: + filename = os.path.join(root, name) + try: + os.remove(filename) + except OSError: + # Try again + os.chmod(filename, stat.S_IWUSR) + os.remove(filename) + os.rmdir(root) + + +class NoRepoTestCase(unittest.TestCase): + + def setUp(self): + self._temp_dir = tempfile.mkdtemp() + self.repo = None def tearDown(self): - shutil.rmtree(self._temp_dir) + del self.repo + rmtree(self._temp_dir) def assertRaisesWithArg(self, exc_class, arg, func, *args, **kwargs): try: @@ -53,39 +75,39 @@ class BaseTestCase(unittest.TestCase): self.fail('%s(%r) not raised' % (exc_class.__name__, arg)) -def open_repo(repo_dir): - repo_path = os.path.join(os.path.dirname(__file__), 'data', repo_dir) - temp_dir = tempfile.mkdtemp() - temp_repo_path = os.path.join(temp_dir, repo_dir) - shutil.copytree(repo_path, temp_repo_path) - return temp_dir, pygit2.Repository(temp_repo_path) +class BareRepoTestCase(NoRepoTestCase): - -class BareRepoTestCase(BaseTestCase): + repo_dir = 'testrepo.git' def setUp(self): - self._temp_dir, self.repo = open_repo('testrepo.git') + super(BareRepoTestCase, self).setUp() + + repo_dir = self.repo_dir + repo_path = os.path.join(os.path.dirname(__file__), 'data', repo_dir) + temp_repo_path = os.path.join(self._temp_dir, repo_dir) + + shutil.copytree(repo_path, temp_repo_path) + + self.repo = pygit2.Repository(temp_repo_path) -class RepoTestCase(BaseTestCase): +class RepoTestCase(NoRepoTestCase): repo_dir = 'testrepo' def setUp(self): + super(RepoTestCase, self).setUp() + repo_dir = self.repo_dir repo_path = os.path.join(os.path.dirname(__file__), 'data', repo_dir) - temp_dir = tempfile.mkdtemp() + temp_repo_path = os.path.join(self._temp_dir, repo_dir, '.git') + tar = tarfile.open(repo_path + '.tar') - tar.extractall(temp_dir) + tar.extractall(self._temp_dir) tar.close() - self._temp_dir = temp_dir - 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):