diff options
author | Sam Thursfield <sam.thursfield@codethink.co.uk> | 2014-11-17 13:00:41 +0000 |
---|---|---|
committer | Sam Thursfield <sam.thursfield@codethink.co.uk> | 2014-11-17 13:00:41 +0000 |
commit | 69a413473a337f9789454ee0fc512f1a47910252 (patch) | |
tree | 792537cf237428d271b139b8546c99597d11089a | |
parent | 2826fdb9b55bca64a40cd9489fcd862355092430 (diff) | |
parent | 987dceee2471e1611f0fe8a0cfe43d9986b1c7c1 (diff) | |
download | morph-69a413473a337f9789454ee0fc512f1a47910252.tar.gz |
Merge branch 'sam/ensure-git-directory-exists'
Reviewed-By: Adam Coldrick <adam.coldrick@codethink.co.uk>
Reviewed-By: Francisco Redondo Marchena <francisco.marchena@codethink.co.uk>
-rw-r--r-- | morphlib/gitdir.py | 17 | ||||
-rw-r--r-- | morphlib/gitdir_tests.py | 26 |
2 files changed, 31 insertions, 12 deletions
diff --git a/morphlib/gitdir.py b/morphlib/gitdir.py index a0f35eef..ed71e422 100644 --- a/morphlib/gitdir.py +++ b/morphlib/gitdir.py @@ -24,6 +24,13 @@ import re import morphlib +class NoGitRepoError(cliapp.AppException): + + def __init__(self, dirname): + cliapp.AppException.__init__( + self, 'Directory %s is not a Git repository. ' % dirname) + + class NoWorkingTreeError(cliapp.AppException): def __init__(self, repo): @@ -375,6 +382,8 @@ class GitDirectory(object): self.dirname = dirname self._config = {} + self._ensure_is_git_repo() + def _runcmd(self, argv, **kwargs): '''Run a command at the root of the git directory. @@ -390,6 +399,13 @@ class GitDirectory(object): def _runcmd_unchecked(self, *args, **kwargs): return cliapp.runcmd_unchecked(*args, cwd=self.dirname, **kwargs) + def _ensure_is_git_repo(self): + try: + self._runcmd(['git', 'rev-parse', '--git-dir']) + except cliapp.AppException as e: + # Exact error is logged already by the runcmd() function. + raise NoGitRepoError(self.dirname) + def checkout(self, branch_name): # pragma: no cover '''Check out a git branch.''' morphlib.git.gitcmd(self._runcmd, 'checkout', branch_name) @@ -752,4 +768,3 @@ def clone_from_cached_repo(cached_repo, dirname, ref): # pragma: no cover cached_repo.clone_checkout(ref, dirname) return GitDirectory(dirname) - diff --git a/morphlib/gitdir_tests.py b/morphlib/gitdir_tests.py index 55b3caa5..fe71ab3a 100644 --- a/morphlib/gitdir_tests.py +++ b/morphlib/gitdir_tests.py @@ -34,17 +34,24 @@ class GitDirectoryTests(unittest.TestCase): def tearDown(self): shutil.rmtree(self.tempdir) - def fake_git_clone(self): + def empty_git_directory(self): os.mkdir(self.dirname) - os.mkdir(os.path.join(self.dirname, '.git')) + return morphlib.gitdir.init(self.dirname) + + def test_ensures_is_a_git_repo(self): + self.assertRaises(OSError, + morphlib.gitdir.GitDirectory, self.dirname) + + os.mkdir(self.dirname) + self.assertRaises(morphlib.gitdir.NoGitRepoError, + morphlib.gitdir.GitDirectory, self.dirname) def test_has_dirname_attribute(self): - self.fake_git_clone() - gitdir = morphlib.gitdir.GitDirectory(self.dirname) + gitdir = self.empty_git_directory() self.assertEqual(gitdir.dirname, self.dirname) def test_can_search_for_top_directory(self): - self.fake_git_clone() + self.empty_git_directory() path_inside_working_tree = os.path.join(self.dirname, 'a', 'b', 'c') os.makedirs(path_inside_working_tree) @@ -54,20 +61,17 @@ class GitDirectoryTests(unittest.TestCase): self.assertEqual(gitdir.dirname, self.dirname) def test_runs_command_in_right_directory(self): - self.fake_git_clone() - gitdir = morphlib.gitdir.GitDirectory(self.dirname) + gitdir = self.empty_git_directory() output = gitdir._runcmd(['pwd']) self.assertEqual(output.strip(), self.dirname) def test_sets_and_gets_configuration(self): - os.mkdir(self.dirname) - gitdir = morphlib.gitdir.init(self.dirname) + gitdir = self.empty_git_directory() gitdir.set_config('foo.bar', 'yoyo') self.assertEqual(gitdir.get_config('foo.bar'), 'yoyo') def test_gets_index(self): - os.mkdir(self.dirname) - gitdir = morphlib.gitdir.init(self.dirname) + gitdir = self.empty_git_directory() self.assertIsInstance(gitdir.get_index(), morphlib.gitindex.GitIndex) |