summaryrefslogtreecommitdiff
path: root/morphlib/gitdir_tests.py
diff options
context:
space:
mode:
authorSam Thursfield <sam.thursfield@codethink.co.uk>2014-10-23 17:33:49 +0100
committerSam Thursfield <sam.thursfield@codethink.co.uk>2014-12-03 10:23:27 +0000
commit2dc11594d5b95d876c9997ca40dfb08bf06c830b (patch)
treec2752f38b17e765416dad9ea64688aad0e60d60a /morphlib/gitdir_tests.py
parentbb1960c7711a8a48bbaabd188158a3c5d7f0ff04 (diff)
downloadmorph-2dc11594d5b95d876c9997ca40dfb08bf06c830b.tar.gz
Rework CachedRepo to use the GitDirectory class where possible
This consolidates a bunch of code paths that were previously duplicated. This also changes the API for local cached repos to match the function names GitDirectory uses. Note that the remote repo cache still uses the old names, and should be fixed when time permits. Some unit tests that use the CachedRepo module required a bit of inelegant monkey-patching in order that they continue to work. A better way to do this would be with the 'mock' library (which would need to be added to Baserock 'build' and 'devel' systems before we could use it).
Diffstat (limited to 'morphlib/gitdir_tests.py')
-rw-r--r--morphlib/gitdir_tests.py27
1 files changed, 27 insertions, 0 deletions
diff --git a/morphlib/gitdir_tests.py b/morphlib/gitdir_tests.py
index fe71ab3a..50de18d7 100644
--- a/morphlib/gitdir_tests.py
+++ b/morphlib/gitdir_tests.py
@@ -16,6 +16,7 @@
# =*= License: GPL-2 =*=
+import contextlib
import datetime
import os
import shutil
@@ -25,6 +26,26 @@ import unittest
import morphlib
+@contextlib.contextmanager
+def monkeypatch(obj, attr, new_value):
+ old_value = getattr(obj, attr)
+ setattr(obj, attr, new_value)
+ yield
+ setattr(obj, attr, old_value)
+
+
+def allow_nonexistant_git_repos():
+ '''Disable the gitdir._ensure_is_git_repo() function.
+
+ This is used in other unit tests to avoid needing to run 'git init' at the
+ start of each test. A library like 'mock' would be a better solution for
+ this problem.
+
+ '''
+ return monkeypatch(
+ morphlib.gitdir.GitDirectory, '_ensure_is_git_repo', lambda x: None)
+
+
class GitDirectoryTests(unittest.TestCase):
def setUp(self):
@@ -154,6 +175,12 @@ class GitDirectoryContentsTests(unittest.TestCase):
self.assertRaises(morphlib.gitdir.InvalidRefError,
gd.read_file, 'bar', 'no-such-ref')
+ def test_read_raises_io_error(self):
+ for gitdir in (self.dirname, self.mirror):
+ gd = morphlib.gitdir.GitDirectory(gitdir)
+ self.assertRaises(IOError,
+ gd.read_file, 'non-existant-file', 'HEAD')
+
def test_HEAD(self):
gd = morphlib.gitdir.GitDirectory(self.dirname)
self.assertEqual(gd.HEAD, 'master')