diff options
Diffstat (limited to 'morphlib/gitdir_tests.py')
-rw-r--r-- | morphlib/gitdir_tests.py | 73 |
1 files changed, 60 insertions, 13 deletions
diff --git a/morphlib/gitdir_tests.py b/morphlib/gitdir_tests.py index 456e3716..a6e1921d 100644 --- a/morphlib/gitdir_tests.py +++ b/morphlib/gitdir_tests.py @@ -1,4 +1,4 @@ -# Copyright (C) 2013-2014 Codethink Limited +# Copyright (C) 2013-2015 Codethink Limited # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -10,12 +10,12 @@ # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License along -# with this program; if not, write to the Free Software Foundation, Inc., -# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +# with this program. If not, see <http://www.gnu.org/licenses/>. # # =*= License: GPL-2 =*= +import contextlib import datetime import os import shutil @@ -25,6 +25,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): @@ -34,30 +54,44 @@ 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.empty_git_directory() + + path_inside_working_tree = os.path.join(self.dirname, 'a', 'b', 'c') + os.makedirs(path_inside_working_tree) + + gitdir = morphlib.gitdir.GitDirectory( + path_inside_working_tree, search_for_root=True) 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) @@ -140,6 +174,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') @@ -162,6 +202,13 @@ class GitDirectoryContentsTests(unittest.TestCase): self.assertEqual(len(tree), 40) self.assertNotEqual(commit, tree) + def test_ref_exists(self): + gd = morphlib.gitdir.GitDirectory(self.dirname) + self.assertFalse(gd.ref_exists('non-existant-ref')) + self.assertTrue(gd.ref_exists('master')) + self.assertFalse( + gd.ref_exists('0000000000000000000000000000000000000000')) + def test_store_blob_with_string(self): gd = morphlib.gitdir.GitDirectory(self.dirname) sha1 = gd.store_blob('test string') |