gui: refactor code so that all generated code lives in the cola.gui namespace
[git-cola.git] / test / testlib.py
blob0397938f5b637e0c9202c6a0ec2a7661af96753d
1 #!/usr/bin/env python
2 import os
3 import sys
4 import shutil
5 import unittest
6 from os.path import join
7 from os.path import dirname
8 from os.path import basename
10 import os
11 from cola.model import Model
13 DEBUG_MODE = os.getenv('DEBUG','')
14 LAST_IDX = 0
15 TEST_SCRIPT_DIR = dirname(__file__)
16 ROOT_TMP_DIR = join( dirname(TEST_SCRIPT_DIR), 'tmp' )
17 TEST_TMP_DIR = join( ROOT_TMP_DIR, basename(sys.argv[0]) )
19 def setup_dir(dir):
20 newdir = dir
21 parentdir = dirname(newdir)
22 if not os.path.isdir(parentdir):
23 os.mkdir(parentdir)
24 if not os.path.isdir(newdir):
25 os.mkdir(newdir)
27 def get_test_dir():
28 global LAST_IDX
29 return '%s-%d.%04d' % (TEST_TMP_DIR, os.getpid(), LAST_IDX)
31 def create_test_dir():
32 global LAST_IDX
33 LAST_IDX += 1
34 newdir = get_test_dir()
35 setup_dir(newdir)
36 os.chdir(newdir)
37 return newdir
39 def remove_dir(dir):
40 if not DEBUG_MODE:
41 os.chdir(ROOT_TMP_DIR)
42 shutil.rmtree(dir)
44 def remove_test_dir():
45 global LAST_IDX
46 testdir = get_test_dir()
47 remove_dir(testdir)
48 LAST_IDX -= 1
50 def shell(cmd):
51 result = os.system(cmd)
52 return result
54 def pipe(cmd):
55 p = os.popen(cmd)
56 out = p.read().strip()
57 p.close()
58 return out
60 # All tests that operate on temporary data derive from testlib.TestCase
61 class TestCase(unittest.TestCase):
62 def setUp(self):
63 create_test_dir()
64 def tearDown(self):
65 remove_test_dir()
66 def shell(self, cmd):
67 result = shell(cmd)
68 self.failIf(result != 0)
69 def get_test_dir(self):
70 return get_test_dir()
72 class DuckModel(Model):
73 def init(self):
74 duck = Model().create(sound='quack',name='ducky')
75 goose = Model().create(sound='cluck',name='goose')
77 self.create(attribute = 'value',
78 mylist=[duck,duck,goose])
79 self.hello = 'world'
80 self.set_mylist([duck,duck,goose, 'meow', 'caboose',42])
82 def duckMethod(self):
83 return 'duck'
85 class InnerModel(Model):
86 def init(self):
87 self.create(foo = 'bar')
89 class NestedModel(Model):
90 def init(self):
91 self.create(inner = InnerModel(),
92 innerList = [],
93 normaList = [ 1,2,3, [4,5, [6,7,8], 9]])
94 self.innerList.append(InnerModel())
95 self.innerList.append([InnerModel()])
96 self.innerList.append([[InnerModel()]])
97 self.innerList.append([[[InnerModel(),InnerModel()]]])
98 self.innerList.append({"foo": InnerModel()})