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