Initial import to Tizen
[profile/ivi/python-twisted.git] / twisted / trial / test / packages.py
1 import sys, os
2 from twisted.trial import unittest
3
4 testModule = """
5 from twisted.trial import unittest
6
7 class FooTest(unittest.TestCase):
8     def testFoo(self):
9         pass
10 """
11
12 dosModule = testModule.replace('\n', '\r\n')
13
14
15 testSample = """
16 '''This module is used by test_loader to test the Trial test loading
17 functionality. Do NOT change the number of tests in this module.
18 Do NOT change the names the tests in this module.
19 '''
20
21 import unittest as pyunit
22 from twisted.trial import unittest
23
24 class FooTest(unittest.TestCase):
25     def test_foo(self):
26         pass
27
28     def test_bar(self):
29         pass
30
31
32 class PyunitTest(pyunit.TestCase):
33     def test_foo(self):
34         pass
35
36     def test_bar(self):
37         pass
38
39
40 class NotATest(object):
41     def test_foo(self):
42         pass
43
44
45 class AlphabetTest(unittest.TestCase):
46     def test_a(self):
47         pass
48
49     def test_b(self):
50         pass
51
52     def test_c(self):
53         pass
54 """
55
56 testInheritanceSample = """
57 '''This module is used by test_loader to test the Trial test loading
58 functionality. Do NOT change the number of tests in this module.
59 Do NOT change the names the tests in this module.
60 '''
61
62 from twisted.trial import unittest
63
64 class X(object):
65     
66     def test_foo(self):
67         pass
68
69 class A(unittest.TestCase, X):
70     pass
71
72 class B(unittest.TestCase, X):
73     pass
74
75 """
76
77 class PackageTest(unittest.TestCase):
78     files = [
79         ('badpackage/__init__.py', 'frotz\n'),
80         ('badpackage/test_module.py', ''),
81         ('package2/__init__.py', ''),
82         ('package2/test_module.py', 'import frotz\n'),
83         ('package/__init__.py', ''),
84         ('package/frotz.py', 'frotz\n'),
85         ('package/test_bad_module.py',
86          'raise ZeroDivisionError("fake error")'),
87         ('package/test_dos_module.py', dosModule),
88         ('package/test_import_module.py', 'import frotz'),
89         ('package/test_module.py', testModule),
90         ('goodpackage/__init__.py', ''),
91         ('goodpackage/test_sample.py', testSample),
92         ('goodpackage/sub/__init__.py', ''),
93         ('goodpackage/sub/test_sample.py', testSample),
94         ('inheritancepackage/__init__.py', ''),
95         ('inheritancepackage/test_x.py', testInheritanceSample),
96         ]
97
98     def _toModuleName(self, filename):
99         name = os.path.splitext(filename)[0]
100         segs = name.split('/')
101         if segs[-1] == '__init__':
102             segs = segs[:-1]
103         return '.'.join(segs)
104
105     def getModules(self):
106         return map(self._toModuleName, zip(*self.files)[0])
107
108     def cleanUpModules(self):
109         modules = self.getModules()
110         modules.sort()
111         modules.reverse()
112         for module in modules:
113             try:
114                 del sys.modules[module]
115             except KeyError:
116                 pass
117
118     def createFiles(self, files, parentDir='.'):
119         for filename, contents in self.files:
120             filename = os.path.join(parentDir, filename)
121             self._createDirectory(filename)
122             fd = open(filename, 'w')
123             fd.write(contents)
124             fd.close()
125
126     def _createDirectory(self, filename):
127         directory = os.path.dirname(filename)
128         if not os.path.exists(directory):
129             os.makedirs(directory)
130
131     def setUp(self, parentDir=None):
132         if parentDir is None:
133             parentDir = self.mktemp()
134         self.parent = parentDir
135         self.createFiles(self.files, parentDir)
136
137     def tearDown(self):
138         self.cleanUpModules()
139
140 class SysPathManglingTest(PackageTest):
141     def setUp(self, parent=None):
142         self.oldPath = sys.path[:]
143         self.newPath = sys.path[:]
144         if parent is None:
145             parent = self.mktemp()
146         PackageTest.setUp(self, parent)
147         self.newPath.append(self.parent)
148         self.mangleSysPath(self.newPath)
149
150     def tearDown(self):
151         PackageTest.tearDown(self)
152         self.mangleSysPath(self.oldPath)
153
154     def mangleSysPath(self, pathVar):
155         sys.path[:] = pathVar
156