~justin-fathomdb/nova/justinsb-openstack-api-volumes

« back to all changes in this revision

Viewing changes to vendor/Twisted-10.0.0/twisted/trial/test/packages.py

  • Committer: Jesse Andrews
  • Date: 2010-05-28 06:05:26 UTC
  • Revision ID: git-v1:bf6e6e718cdc7488e2da87b21e258ccc065fe499
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
57
class PackageTest(unittest.TestCase):
 
58
    files = [
 
59
        ('badpackage/__init__.py', 'frotz\n'),
 
60
        ('badpackage/test_module.py', ''),
 
61
        ('package2/__init__.py', ''),
 
62
        ('package2/test_module.py', 'import frotz\n'),
 
63
        ('package/__init__.py', ''),
 
64
        ('package/frotz.py', 'frotz\n'),
 
65
        ('package/test_bad_module.py',
 
66
         'raise ZeroDivisionError("fake error")'),
 
67
        ('package/test_dos_module.py', dosModule),
 
68
        ('package/test_import_module.py', 'import frotz'),
 
69
        ('package/test_module.py', testModule),
 
70
        ('goodpackage/__init__.py', ''),
 
71
        ('goodpackage/test_sample.py', testSample),
 
72
        ('goodpackage/sub/__init__.py', ''),
 
73
        ('goodpackage/sub/test_sample.py', testSample)
 
74
        ]
 
75
 
 
76
    def _toModuleName(self, filename):
 
77
        name = os.path.splitext(filename)[0]
 
78
        segs = name.split('/')
 
79
        if segs[-1] == '__init__':
 
80
            segs = segs[:-1]
 
81
        return '.'.join(segs)
 
82
 
 
83
    def getModules(self):
 
84
        return map(self._toModuleName, zip(*self.files)[0])
 
85
 
 
86
    def cleanUpModules(self):
 
87
        modules = self.getModules()
 
88
        modules.sort()
 
89
        modules.reverse()
 
90
        for module in modules:
 
91
            try:
 
92
                del sys.modules[module]
 
93
            except KeyError:
 
94
                pass
 
95
 
 
96
    def createFiles(self, files, parentDir='.'):
 
97
        for filename, contents in self.files:
 
98
            filename = os.path.join(parentDir, filename)
 
99
            self._createDirectory(filename)
 
100
            fd = open(filename, 'w')
 
101
            fd.write(contents)
 
102
            fd.close()
 
103
 
 
104
    def _createDirectory(self, filename):
 
105
        directory = os.path.dirname(filename)
 
106
        if not os.path.exists(directory):
 
107
            os.makedirs(directory)
 
108
 
 
109
    def setUp(self, parentDir=None):
 
110
        if parentDir is None:
 
111
            parentDir = self.mktemp()
 
112
        self.parent = parentDir
 
113
        self.createFiles(self.files, parentDir)
 
114
 
 
115
    def tearDown(self):
 
116
        self.cleanUpModules()
 
117
 
 
118
class SysPathManglingTest(PackageTest):
 
119
    def setUp(self, parent=None):
 
120
        self.oldPath = sys.path[:]
 
121
        self.newPath = sys.path[:]
 
122
        if parent is None:
 
123
            parent = self.mktemp()
 
124
        PackageTest.setUp(self, parent)
 
125
        self.newPath.append(self.parent)
 
126
        self.mangleSysPath(self.newPath)
 
127
 
 
128
    def tearDown(self):
 
129
        PackageTest.tearDown(self)
 
130
        self.mangleSysPath(self.oldPath)
 
131
 
 
132
    def mangleSysPath(self, pathVar):
 
133
        sys.path[:] = pathVar
 
134