test: Use local classes in the serialization test suite
[git-cola.git] / test / helper.py
blobef7ac953f59ba9cc73d50dc8a1b73bc12fe86d80
1 import os
2 import sys
3 import shutil
4 import unittest
5 from os.path import join
6 from os.path import dirname
7 from os.path import basename
9 from cola import core
12 DEBUG_MODE = os.getenv('COLA_TEST_DEBUG','')
14 TEST_SCRIPT_DIR = dirname(__file__)
15 ROOT_TMP_DIR = join(TEST_SCRIPT_DIR, 'tmp')
16 TEST_TMP_DIR = join(ROOT_TMP_DIR, basename(sys.argv[0]))
18 LAST_IDX = 0
21 def tmp_path(*paths):
22 """Returns a path relative to the test/tmp directory"""
23 return join(TEST_SCRIPT_DIR, 'tmp', *paths)
25 def fixture(*paths):
26 return join(TEST_SCRIPT_DIR, 'fixtures', *paths)
28 def setup_dir(dir):
29 newdir = dir
30 parentdir = dirname(newdir)
31 if not os.path.isdir(parentdir):
32 os.mkdir(parentdir)
33 if not os.path.isdir(newdir):
34 os.mkdir(newdir)
36 def get_dir():
37 global LAST_IDX
38 return '%s-%d.%04d' % (TEST_TMP_DIR, os.getpid(), LAST_IDX)
40 def create_dir():
41 global LAST_IDX
42 LAST_IDX += 1
43 newdir = get_dir()
44 setup_dir(newdir)
45 os.chdir(newdir)
46 return newdir
48 def rmdir(path):
49 if not DEBUG_MODE:
50 os.chdir(ROOT_TMP_DIR)
51 shutil.rmtree(path)
53 def remove_dir():
54 global LAST_IDX
55 testdir = get_dir()
56 rmdir(testdir)
57 LAST_IDX -= 1
59 def shell(cmd):
60 return os.system(cmd)
62 def pipe(cmd):
63 p = os.popen(cmd)
64 out = core.read_nointr(p).strip()
65 p.close()
66 return out
69 class TestCase(unittest.TestCase):
70 """Tests that operate on temporary git repositories."""
71 def setUp(self):
72 create_dir()
74 def tearDown(self):
75 remove_dir()
77 def shell(self, cmd):
78 result = shell(cmd)
79 self.failIf(result != 0)
81 def get_dir(self):
82 return get_dir()