Project

General

Profile

Statistics
| Branch: | Revision:

root / env / lib / python2.7 / site-packages / distribute-0.6.19-py2.7.egg / setuptools / command / test.py @ 1a305335

History | View | Annotate | Download (4.99 KB)

1 1a305335 officers
from setuptools import Command
2
from distutils.errors import DistutilsOptionError
3
import sys
4
from pkg_resources import *
5
from unittest import TestLoader, main
6
7
class ScanningLoader(TestLoader):
8
9
    def loadTestsFromModule(self, module):
10
        """Return a suite of all tests cases contained in the given module
11

12
        If the module is a package, load tests from all the modules in it.
13
        If the module has an ``additional_tests`` function, call it and add
14
        the return value to the tests.
15
        """
16
        tests = []
17
        if module.__name__!='setuptools.tests.doctest':  # ugh
18
            tests.append(TestLoader.loadTestsFromModule(self,module))
19
20
        if hasattr(module, "additional_tests"):
21
            tests.append(module.additional_tests())
22
23
        if hasattr(module, '__path__'):
24
            for file in resource_listdir(module.__name__, ''):
25
                if file.endswith('.py') and file!='__init__.py':
26
                    submodule = module.__name__+'.'+file[:-3]
27
                else:
28
                    if resource_exists(
29
                        module.__name__, file+'/__init__.py'
30
                    ):
31
                        submodule = module.__name__+'.'+file
32
                    else:
33
                        continue
34
                tests.append(self.loadTestsFromName(submodule))
35
36
        if len(tests)!=1:
37
            return self.suiteClass(tests)
38
        else:
39
            return tests[0] # don't create a nested suite for only one return
40
41
42
class test(Command):
43
44
    """Command to run unit tests after in-place build"""
45
46
    description = "run unit tests after in-place build"
47
48
    user_options = [
49
        ('test-module=','m', "Run 'test_suite' in specified module"),
50
        ('test-suite=','s',
51
            "Test suite to run (e.g. 'some_module.test_suite')"),
52
    ]
53
54
    def initialize_options(self):
55
        self.test_suite = None
56
        self.test_module = None
57
        self.test_loader = None
58
59
60
    def finalize_options(self):
61
62
        if self.test_suite is None:
63
            if self.test_module is None:
64
                self.test_suite = self.distribution.test_suite
65
            else:
66
                self.test_suite = self.test_module+".test_suite"
67
        elif self.test_module:
68
            raise DistutilsOptionError(
69
                "You may specify a module or a suite, but not both"
70
            )
71
72
        self.test_args = [self.test_suite]
73
74
        if self.verbose:
75
            self.test_args.insert(0,'--verbose')
76
        if self.test_loader is None:
77
            self.test_loader = getattr(self.distribution,'test_loader',None)
78
        if self.test_loader is None:
79
            self.test_loader = "setuptools.command.test:ScanningLoader"
80
81
82
83
    def with_project_on_sys_path(self, func):
84
        if getattr(self.distribution, 'use_2to3', False):
85
            # If we run 2to3 we can not do this inplace:
86
87
            # Ensure metadata is up-to-date
88
            self.reinitialize_command('build_py', inplace=0)
89
            self.run_command('build_py')
90
            bpy_cmd = self.get_finalized_command("build_py")
91
            build_path = normalize_path(bpy_cmd.build_lib)
92
93
            # Build extensions
94
            self.reinitialize_command('egg_info', egg_base=build_path)
95
            self.run_command('egg_info')
96
97
            self.reinitialize_command('build_ext', inplace=0)
98
            self.run_command('build_ext')
99
        else:
100
            # Without 2to3 inplace works fine:
101
            self.run_command('egg_info')
102
103
            # Build extensions in-place
104
            self.reinitialize_command('build_ext', inplace=1)
105
            self.run_command('build_ext')
106
107
        ei_cmd = self.get_finalized_command("egg_info")
108
109
        old_path = sys.path[:]
110
        old_modules = sys.modules.copy()
111
112
        try:
113
            sys.path.insert(0, normalize_path(ei_cmd.egg_base))
114
            working_set.__init__()
115
            add_activation_listener(lambda dist: dist.activate())
116
            require('%s==%s' % (ei_cmd.egg_name, ei_cmd.egg_version))
117
            func()
118
        finally:
119
            sys.path[:] = old_path
120
            sys.modules.clear()
121
            sys.modules.update(old_modules)
122
            working_set.__init__()
123
124
125
    def run(self):
126
        if self.distribution.install_requires:
127
            self.distribution.fetch_build_eggs(self.distribution.install_requires)
128
        if self.distribution.tests_require:
129
            self.distribution.fetch_build_eggs(self.distribution.tests_require)
130
131
        if self.test_suite:
132
            cmd = ' '.join(self.test_args)
133
            if self.dry_run:
134
                self.announce('skipping "unittest %s" (dry run)' % cmd)
135
            else:
136
                self.announce('running "unittest %s"' % cmd)
137
                self.with_project_on_sys_path(self.run_tests)
138
139
140
    def run_tests(self):
141
        import unittest
142
        loader_ep = EntryPoint.parse("x="+self.test_loader)
143
        loader_class = loader_ep.load(require=False)
144
        unittest.main(
145
            None, None, [unittest.__file__]+self.test_args,
146
            testLoader = loader_class()
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