git-cola v1.9.0
[git-cola.git] / test / helper.py
blob909398f8196f62317a9d702b09b71294ea243907
1 import os
2 import shutil
3 import unittest
4 import tempfile
6 from cola import core
7 from cola import git
8 from cola import gitcfg
9 from cola import gitcmds
12 def tmp_path(*paths):
13 """Returns a path relative to the test/tmp directory"""
14 return os.path.join(os.path.dirname(__file__), 'tmp', *paths)
17 def fixture(*paths):
18 return os.path.join(os.path.dirname(__file__), 'fixtures', *paths)
21 def shell(cmd):
22 return os.system(cmd)
25 def pipe(cmd):
26 p = os.popen(cmd)
27 out = core.fread(p).strip()
28 p.close()
29 return out
32 class TmpPathTestCase(unittest.TestCase):
33 def setUp(self):
34 self._testdir = tempfile.mkdtemp('_cola_test')
35 os.chdir(self._testdir)
37 def tearDown(self):
38 """Remove the test directory and return to the tmp root."""
39 path = self._testdir
40 os.chdir(tmp_path())
41 shutil.rmtree(path)
43 def shell(self, cmd):
44 result = shell(cmd)
45 self.failIf(result != 0)
47 def test_path(self, *paths):
48 return os.path.join(self._testdir, *paths)
51 class GitRepositoryTestCase(TmpPathTestCase):
52 """Tests that operate on temporary git repositories."""
53 def setUp(self, commit=True):
54 TmpPathTestCase.setUp(self)
55 self.initialize_repo()
56 if commit:
57 self.commit_files()
58 git.instance().set_worktree(core.getcwd())
59 gitcfg.instance().reset()
60 gitcmds.clear_cache()
62 def initialize_repo(self):
63 self.shell("""
64 git init > /dev/null &&
65 touch A B &&
66 git add A B
67 """)
69 def commit_files(self):
70 self.shell('git commit -m"Initial commit" > /dev/null')