diff options
Diffstat (limited to 'morphlib/gitdir_tests.py')
-rw-r--r-- | morphlib/gitdir_tests.py | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/morphlib/gitdir_tests.py b/morphlib/gitdir_tests.py index 2494981a..175b8ee7 100644 --- a/morphlib/gitdir_tests.py +++ b/morphlib/gitdir_tests.py @@ -64,3 +64,76 @@ class GitDirectoryTests(unittest.TestCase): gitdir.set_remote_fetch_url('origin', url) self.assertEqual(gitdir.get_remote_fetch_url('origin'), url) +class GitDirectoryContentsTests(unittest.TestCase): + + def setUp(self): + self.tempdir = tempfile.mkdtemp() + self.dirname = os.path.join(self.tempdir, 'foo') + os.mkdir(self.dirname) + gd = morphlib.gitdir.init(self.dirname) + for fn in ('foo', 'bar.morph', 'baz.morph', 'quux'): + with open(os.path.join(self.dirname, fn), "w") as f: + f.write('dummy morphology text') + gd._runcmd(['git', 'add', '.']) + gd._runcmd(['git', 'commit', '-m', 'Initial commit']) + os.rename(os.path.join(self.dirname, 'foo'), + os.path.join(self.dirname, 'foo.morph')) + self.mirror = os.path.join(self.tempdir, 'mirror') + gd._runcmd(['git', 'clone', '--mirror', self.dirname, self.mirror]) + + def tearDown(self): + shutil.rmtree(self.tempdir) + + def test_lists_files_in_work_tree(self): + gd = morphlib.gitdir.GitDirectory(self.dirname) + self.assertEqual(sorted(gd.list_files()), + ['bar.morph', 'baz.morph', 'foo.morph', 'quux']) + + def test_read_file_in_work_tree(self): + gd = morphlib.gitdir.GitDirectory(self.dirname) + self.assertEqual(gd.read_file('bar.morph'), + 'dummy morphology text') + + def test_list_raises_no_ref_no_work_tree(self): + gd = morphlib.gitdir.GitDirectory(self.mirror) + self.assertRaises(morphlib.gitdir.NoWorkingTreeError, + gd.list_files) + + def test_read_raises_no_ref_no_work_tree(self): + gd = morphlib.gitdir.GitDirectory(self.mirror) + self.assertRaises(morphlib.gitdir.NoWorkingTreeError, + gd.read_file, 'bar.morph') + + def test_lists_files_in_HEAD(self): + for gitdir in (self.dirname, self.mirror): + gd = morphlib.gitdir.GitDirectory(gitdir) + self.assertEqual(sorted(gd.list_files('HEAD')), + ['bar.morph', 'baz.morph', 'foo', 'quux']) + + def test_read_files_in_HEAD(self): + for gitdir in (self.dirname, self.mirror): + gd = morphlib.gitdir.GitDirectory(gitdir) + self.assertEqual(gd.read_file('bar.morph', 'HEAD'), + 'dummy morphology text') + + def test_lists_files_in_named_ref(self): + for gitdir in (self.dirname, self.mirror): + gd = morphlib.gitdir.GitDirectory(gitdir) + self.assertEqual(sorted(gd.list_files('master')), + ['bar.morph', 'baz.morph', 'foo', 'quux']) + + def test_read_file_in_named_ref(self): + for gitdir in (self.dirname, self.mirror): + gd = morphlib.gitdir.GitDirectory(gitdir) + self.assertEqual(gd.read_file('bar.morph', 'master'), + 'dummy morphology text') + + def test_list_raises_invalid_ref(self): + gd = morphlib.gitdir.GitDirectory(self.dirname) + self.assertRaises(morphlib.gitdir.InvalidRefError, + gd.list_files, 'no-such-ref') + + def test_read_raises_invalid_ref(self): + gd = morphlib.gitdir.GitDirectory(self.dirname) + self.assertRaises(morphlib.gitdir.InvalidRefError, + gd.read_file, 'bar', 'no-such-ref') |