portability: Move cola.commands to cola.cmds
[git-cola.git] / test / helper.py
blob2b59f00d88b6df0185827126eb1830d9ab1523a5
1 import os
2 import sys
3 import shutil
4 import unittest
6 from cola import core
7 from cola import git
8 from cola import gitcfg
10 CUR_TEST = 0
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 setup_dir(dir):
23 newdir = dir
24 parentdir = os.path.dirname(newdir)
25 if not os.path.isdir(parentdir):
26 os.mkdir(parentdir)
27 if not os.path.isdir(newdir):
28 os.mkdir(newdir)
31 def test_path(*paths):
32 cur_tmpdir = os.path.join(tmp_path(), os.path.basename(sys.argv[0]))
33 root = '%s-%d.%04d' % (cur_tmpdir, os.getpid(), CUR_TEST)
34 return os.path.join(root, *paths)
37 def create_dir():
38 global CUR_TEST
39 CUR_TEST += 1
40 newdir = test_path()
41 setup_dir(newdir)
42 os.chdir(newdir)
43 return newdir
46 def remove_dir():
47 """Remove the test's tmp directory and return to the tmp root."""
48 global CUR_TEST
49 path = test_path()
50 if os.path.isdir(path):
51 os.chdir(tmp_path())
52 shutil.rmtree(path)
53 CUR_TEST -= 1
56 def shell(cmd):
57 return os.system(cmd)
60 def pipe(cmd):
61 p = os.popen(cmd)
62 out = core.read_nointr(p).strip()
63 p.close()
64 return out
67 class TmpPathTestCase(unittest.TestCase):
68 def setUp(self):
69 create_dir()
71 def tearDown(self):
72 remove_dir()
74 def shell(self, cmd):
75 result = shell(cmd)
76 self.failIf(result != 0)
78 def test_path(self, *paths):
79 return test_path(*paths)
82 class GitRepositoryTestCase(TmpPathTestCase):
83 """Tests that operate on temporary git repositories."""
84 def setUp(self, commit=True):
85 TmpPathTestCase.setUp(self)
86 self.initialize_repo()
87 if commit:
88 self.commit_files()
89 git.instance().load_worktree(os.getcwd())
90 gitcfg.instance().reset()
92 def initialize_repo(self):
93 self.shell("""
94 git init > /dev/null &&
95 touch A B &&
96 git add A B
97 """)
99 def commit_files(self):
100 self.shell('git commit -m"Initial commit" > /dev/null')