summaryrefslogtreecommitdiff
blob: 892b066eb8d8fce3ec2e4d78c0a6780a2d34d22f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
Index: Twisted/twisted/trial/test/test_output.py
===================================================================
--- Twisted/twisted/trial/test/test_output.py	(revision 14794)
+++ Twisted/twisted/trial/test/test_output.py	(revision 14797)
@@ -1,17 +1,14 @@
 from twisted.trial import unittest
+from twisted.trial.test import packages
 from twisted.python import util
 from twisted.internet import utils, reactor, interfaces
-import os, re
+import os, re, sys
 
-def getTrialPath():
-    fp = os.path.abspath(unittest.__file__)
-    trialPath = fp.split(os.sep)[:-3] + ['bin', 'trial']
-    return os.path.normpath(os.path.join(fp, os.pardir, os.pardir,
-                                         os.pardir, 'bin', 'trial'))
 
-
 def runTrialWithEnv(env, *args):
-    return utils.getProcessOutput(getTrialPath(), args=args, errortoo=1,
+    params = [ '-c', 'from twisted.scripts.trial import run; run()' ]
+    params.extend(args)
+    return utils.getProcessOutput(sys.executable, args=params, errortoo=1,
                                   env=env)
 
 
@@ -27,16 +24,14 @@
     ## shell out for this stuff.
 
     debug = False
+    parent = "_testImportErrors"
 
-    def _getMockEnvironment(self):
-        path = self._getMockPath()
-        env = os.environ.copy()
-        if not env.has_key('PYTHONPATH'):
-            env['PYTHONPATH'] = path
-        else:
-            env['PYTHONPATH'] += os.pathsep + path
-        return env
+    def setUp(self):
+        packages.setUp(self.parent)
 
+    def tearDown(self):
+        packages.tearDown(self.parent)
+
     def _runTrial(self, env, *args):
         d = runTrialWithEnv(env, *args)
         if self.debug:
@@ -44,42 +39,29 @@
         return d
         
     def runTrial(self, *args):
-        return self._runTrial(self._getMockEnvironment(), *args)
+        env = os.environ.copy()
+        env['PYTHONPATH'] = os.pathsep.join([self.parent] + sys.path)
+        return self._runTrial(env, *args)
 
     def runTrialPure(self, *args):
-        return self._runTrial(os.environ, *args)
-
+        env = os.environ.copy()
+        env['PYTHONPATH'] = os.pathsep.join(sys.path)
+        return self._runTrial(env, *args)
+ 
     def _print(self, stuff):
         print stuff
         return stuff
 
-    def _getMockPath(self):
-        from twisted.trial import test
-        return os.path.normpath(util.sibpath(test.__file__, 'foo'))
-
     def failUnlessIn(self, container, containee, *args, **kwargs):
         # redefined to be useful in callbacks
-        unittest.TestCase.failUnlessSubstring(self, containee, container,
-                                              *args, **kwargs)
+        self.failUnlessSubstring(containee, container, *args, **kwargs)
         return container
 
     def failIfIn(self, container, containee, *args, **kwargs):
         # redefined to be useful in callbacks
-        unittest.TestCase.failIfSubstring(self, containee, container,
-                                          *args, **kwargs)
+        self.failIfSubstring(containee, container, *args, **kwargs)
         return container
 
-    def test_trialFound(self):
-        self.failUnless(os.path.isfile(getTrialPath()), getTrialPath())
-
-    def test_mockPathCorrect(self):
-        # This doesn't test a feature.  This tests that we are accurately finding
-        # the directory with all of the mock modules and packages.
-        path = self._getMockPath()
-        self.failUnless(path.endswith('twisted/trial/test/foo'), 
-                        'got path: %r' % path)
-        self.failUnless(os.path.isdir(path))
-
     def test_trialRun(self):
         d = self.runTrial('--help')
         d.addCallback(self.failUnless, 'trial')
@@ -155,7 +137,7 @@
         return d
 
     def test_nonRecurseImportErrors(self):
-        d = self.runTrial('package2')
+        d = self.runTrial('-N', 'package2')
         d.addCallback(self.failUnlessIn, 'IMPORT ERROR')
         d.addCallback(self.failUnlessIn, "No module named frotz")
         d.addCallback(self.failIfIn, '<module')
@@ -170,8 +152,8 @@
         return d
     
     def test_filename(self):
-        path = self._getMockPath()
-        d = self.runTrialPure(os.path.join(path, 'package/test_module.py'))
+        d = self.runTrialPure(os.path.join(self.parent,
+                                           'package', 'test_module.py'))
         d.addCallback(self.failIfIn, 'IMPORT ERROR')
         d.addCallback(self.failIfIn, 'IOError')
         d.addCallback(self.failUnlessIn, 'OK')
@@ -180,8 +162,8 @@
 
     def test_dosFile(self):
         ## XXX -- not really an output test, more of a script test
-        path = self._getMockPath()
-        d = self.runTrialPure(os.path.join(path, 'package/test_dos_module.py'))
+        d = self.runTrialPure(os.path.join(self.parent,
+                                           'package', 'test_dos_module.py'))
         d.addCallback(self.failIfIn, 'IMPORT ERROR')
         d.addCallback(self.failIfIn, 'IOError')
         d.addCallback(self.failUnlessIn, 'OK')
Index: Twisted/twisted/trial/test/test_loader.py
===================================================================
--- Twisted/twisted/trial/test/test_loader.py	(revision 14794)
+++ Twisted/twisted/trial/test/test_loader.py	(revision 14797)
@@ -1,13 +1,18 @@
 import sys, os
 from twisted.python import util
+from twisted.trial.test import packages
 from twisted.trial import unittest
 from twisted.trial import runner
 
 
 class FinderTest(unittest.TestCase):
     def setUp(self):
+        packages.setUp()
         self.loader = runner.TestLoader()
 
+    def tearDown(self):
+        packages.tearDown()
+
     def test_findPackage(self):
         sample1 = self.loader.findByName('twisted')
         import twisted as sample2
@@ -45,7 +50,12 @@
         
         
 class FileTest(unittest.TestCase):
-    samplePath = util.sibpath(__file__, 'foo')
+    parent = '_test_loader_FileTest'
+    
+    def setUp(self):
+        self.oldPath = sys.path[:]
+        sys.path.append(self.parent)
+        packages.setUp(self.parent)
 
     def tearDown(self):
         importedModules = ['goodpackage',
@@ -55,6 +65,8 @@
         for moduleName in importedModules:
             if sys.modules.has_key(moduleName):
                 del sys.modules[moduleName]
+        packages.tearDown(self.parent)
+        sys.path = self.oldPath
 
     def test_notFile(self):
         self.failUnlessRaises(ValueError,
@@ -66,39 +78,33 @@
         self.failUnlessEqual(sample2, sample1)
 
     def test_moduleNotInPath(self):
-        sample1 = runner.filenameToModule(os.path.join(self.samplePath,
+        sys.path, newPath = self.oldPath, sys.path
+        sample1 = runner.filenameToModule(os.path.join(self.parent,
                                                        'goodpackage',
                                                        'test_sample.py'))
-        sys.path.append(self.samplePath)
+        sys.path = newPath
         from goodpackage import test_sample as sample2
-        try:
-            self.failUnlessEqual(os.path.splitext(sample2.__file__)[0],
-                                 os.path.splitext(sample1.__file__)[0])
-        finally:
-            sys.path.remove(self.samplePath)
+        self.failUnlessEqual(os.path.splitext(sample2.__file__)[0],
+                             os.path.splitext(sample1.__file__)[0])
 
     def test_packageInPath(self):
-        sys.path.append(self.samplePath)
-        try:
-            package1 = runner.filenameToModule(os.path.join(self.samplePath,
-                                                            'goodpackage'))
-            import goodpackage
-            self.failUnlessEqual(goodpackage, package1)
-        finally:
-            sys.path.remove(self.samplePath)
+        package1 = runner.filenameToModule(os.path.join(self.parent,
+                                                        'goodpackage'))
+        import goodpackage
+        self.failUnlessEqual(goodpackage, package1)
 
     def test_packageNotInPath(self):
-        package1 = runner.filenameToModule(os.path.join(self.samplePath,
+        sys.path, newPath = self.oldPath, sys.path
+        package1 = runner.filenameToModule(os.path.join(self.parent,
                                                         'goodpackage'))
-        sys.path.append(self.samplePath)
+        sys.path = newPath
         import goodpackage
-        sys.path.remove(self.samplePath)
         self.failUnlessEqual(os.path.splitext(goodpackage.__file__)[0],
                              os.path.splitext(package1.__file__)[0])
 
     def test_directoryNotPackage(self):
         self.failUnlessRaises(ValueError, runner.filenameToModule,
-                              self.samplePath)
+                              util.sibpath(__file__, 'directory'))
 
     def test_filenameNotPython(self):
         self.failUnlessRaises(ValueError, runner.filenameToModule,
@@ -120,14 +126,17 @@
     ##   * could be a file / directory
     ##   * could be name of a python object
 
-    samplePath = util.sibpath(__file__, 'foo')
+    parent = '_test_loader'
     
     def setUp(self):
         self.loader = runner.TestLoader()
-        sys.path.append(self.samplePath)
+        self.oldPath = sys.path[:]
+        sys.path.append(self.parent)
+        packages.setUp(self.parent)
 
     def tearDown(self):
-        sys.path.remove(self.samplePath)
+        sys.path = self.oldPath
+        packages.tearDown(self.parent)
 
     def test_loadMethod(self):
         import sample
Index: Twisted/twisted/trial/test/packages.py
===================================================================
--- Twisted/twisted/trial/test/packages.py	(revision 0)
+++ Twisted/twisted/trial/test/packages.py	(revision 14797)
@@ -0,0 +1,101 @@
+import os, shutil
+
+testModule = """
+from twisted.trial import unittest
+
+class FooTest(unittest.TestCase):
+    def testFoo(self):
+        pass
+"""
+
+dosModule = testModule.replace('\n', '\r\n')
+
+
+testSample = """
+'''This module is used by test_loader to test the Trial test loading
+functionality. Do NOT change the number of tests in this module.
+Do NOT change the names the tests in this module.
+'''
+
+import unittest as pyunit
+from twisted.trial import unittest
+
+class FooTest(unittest.TestCase):
+    def test_foo(self):
+        pass
+
+    def test_bar(self):
+        pass
+
+
+class PyunitTest(pyunit.TestCase):
+    def test_foo(self):
+        pass
+
+    def test_bar(self):
+        pass
+
+
+class NotATest(object):
+    def test_foo(self):
+        pass
+
+
+class AlphabetTest(unittest.TestCase):
+    def test_a(self):
+        pass
+
+    def test_b(self):
+        pass
+
+    def test_c(self):
+        pass
+"""
+
+
+files = [
+    ('badpackage/__init__.py', 'frotz\n'),
+    ('badpackage/test_module.py', ''),
+    ('package2/__init__.py', ''),
+    ('package2/test_module.py', 'import frotz\n'),
+    ('package/__init__.py', ''),
+    ('package/frotz.py', 'frotz\n'),
+    ('package/test_bad_module.py', 'raise ZeroDivisionError("fake error")'),
+    ('package/test_dos_module.py', dosModule),
+    ('package/test_import_module.py', 'import frotz'),
+    ('package/test_module.py', testModule),
+    ('goodpackage/__init__.py', ''),
+    ('goodpackage/test_sample.py', testSample),
+    ('goodpackage/sub/__init__.py', ''),
+    ('goodpackage/sub/test_sample.py', testSample)
+    ]
+
+
+def createFiles(files, parentDir='.'):
+    for filename, contents in files:
+        filename = os.path.join(parentDir, filename)
+        _createDirectory(filename)
+        fd = open(filename, 'w')
+        fd.write(contents)
+        fd.close()
+
+def _createDirectory(filename):
+    directory = os.path.dirname(filename)
+    if not os.path.exists(directory):
+        os.makedirs(directory)
+
+def setUp(parentDir='.'):
+    createFiles(files, parentDir)
+
+def removeFiles(files, parentDir):
+    directories = {}
+    for filename, _ in files:
+        directories[os.path.dirname(os.path.join(parentDir, filename))] = True
+    dirs = directories.keys()
+    dirs.sort()
+    dirs.reverse()
+    for directory in dirs:
+        shutil.rmtree(directory)
+
+def tearDown(parentDir='.'):
+    removeFiles(files, parentDir)