]>
crepu.dev Git - config.git/blob - djavu-asus/elpy/rpc-venv/lib/python3.11/site-packages/setuptools/command/test.py
7 from distutils
.errors
import DistutilsError
, DistutilsOptionError
8 from distutils
import log
9 from unittest
import TestLoader
11 from pkg_resources
import (
17 add_activation_listener
,
20 from .._importlib
import metadata
21 from setuptools
import Command
22 from setuptools
.extern
.more_itertools
import unique_everseen
23 from setuptools
.extern
.jaraco
.functools
import pass_none
26 class ScanningLoader(TestLoader
):
28 TestLoader
.__init
__(self
)
31 def loadTestsFromModule(self
, module
, pattern
=None):
32 """Return a suite of all tests cases contained in the given module
34 If the module is a package, load tests from all the modules in it.
35 If the module has an ``additional_tests`` function, call it and add
36 the return value to the tests.
38 if module
in self
._visited
:
40 self
._visited
.add(module
)
43 tests
.append(TestLoader
.loadTestsFromModule(self
, module
))
45 if hasattr(module
, "additional_tests"):
46 tests
.append(module
.additional_tests())
48 if hasattr(module
, '__path__'):
49 for file in resource_listdir(module
.__name
__, ''):
50 if file.endswith('.py') and file != '__init__.py':
51 submodule
= module
.__name
__ + '.' + file[:-3]
53 if resource_exists(module
.__name
__, file + '/__init__.py'):
54 submodule
= module
.__name
__ + '.' + file
57 tests
.append(self
.loadTestsFromName(submodule
))
60 return self
.suiteClass(tests
)
62 return tests
[0] # don't create a nested suite for only one return
65 # adapted from jaraco.classes.properties:NonDataProperty
66 class NonDataProperty
:
67 def __init__(self
, fget
):
70 def __get__(self
, obj
, objtype
=None):
77 """Command to run unit tests after in-place build"""
79 description
= "run unit tests after in-place build (deprecated)"
82 ('test-module=', 'm', "Run 'test_suite' in specified module"),
86 "Run single test, case or suite (e.g. 'module.test_suite')",
88 ('test-runner=', 'r', "Test runner to use"),
91 def initialize_options(self
):
92 self
.test_suite
= None
93 self
.test_module
= None
94 self
.test_loader
= None
95 self
.test_runner
= None
97 def finalize_options(self
):
99 if self
.test_suite
and self
.test_module
:
100 msg
= "You may specify a module or a suite, but not both"
101 raise DistutilsOptionError(msg
)
103 if self
.test_suite
is None:
104 if self
.test_module
is None:
105 self
.test_suite
= self
.distribution
.test_suite
107 self
.test_suite
= self
.test_module
+ ".test_suite"
109 if self
.test_loader
is None:
110 self
.test_loader
= getattr(self
.distribution
, 'test_loader', None)
111 if self
.test_loader
is None:
112 self
.test_loader
= "setuptools.command.test:ScanningLoader"
113 if self
.test_runner
is None:
114 self
.test_runner
= getattr(self
.distribution
, 'test_runner', None)
118 return list(self
._test
_args
())
120 def _test_args(self
):
121 if not self
.test_suite
:
126 yield self
.test_suite
128 def with_project_on_sys_path(self
, func
):
130 Backward compatibility for project_on_sys_path context.
132 with self
.project_on_sys_path():
135 @contextlib.contextmanager
136 def project_on_sys_path(self
, include_dists
=[]):
137 self
.run_command('egg_info')
139 # Build extensions in-place
140 self
.reinitialize_command('build_ext', inplace
=1)
141 self
.run_command('build_ext')
143 ei_cmd
= self
.get_finalized_command("egg_info")
145 old_path
= sys
.path
[:]
146 old_modules
= sys
.modules
.copy()
149 project_path
= normalize_path(ei_cmd
.egg_base
)
150 sys
.path
.insert(0, project_path
)
151 working_set
.__init
__()
152 add_activation_listener(lambda dist
: dist
.activate())
153 require('%s==%s' % (ei_cmd
.egg_name
, ei_cmd
.egg_version
))
154 with self
.paths_on_pythonpath([project_path
]):
157 sys
.path
[:] = old_path
159 sys
.modules
.update(old_modules
)
160 working_set
.__init
__()
163 @contextlib.contextmanager
164 def paths_on_pythonpath(paths
):
166 Add the indicated paths to the head of the PYTHONPATH environment
167 variable so that subprocesses will also see the packages at
170 Do this in a context that restores the value on exit.
173 orig_pythonpath
= os
.environ
.get('PYTHONPATH', nothing
)
174 current_pythonpath
= os
.environ
.get('PYTHONPATH', '')
176 prefix
= os
.pathsep
.join(unique_everseen(paths
))
177 to_join
= filter(None, [prefix
, current_pythonpath
])
178 new_path
= os
.pathsep
.join(to_join
)
180 os
.environ
['PYTHONPATH'] = new_path
183 if orig_pythonpath
is nothing
:
184 os
.environ
.pop('PYTHONPATH', None)
186 os
.environ
['PYTHONPATH'] = orig_pythonpath
189 def install_dists(dist
):
191 Install the requirements indicated by self.distribution and
192 return an iterable of the dists that were built.
194 ir_d
= dist
.fetch_build_eggs(dist
.install_requires
)
195 tr_d
= dist
.fetch_build_eggs(dist
.tests_require
or [])
196 er_d
= dist
.fetch_build_eggs(
198 for k
, v
in dist
.extras_require
.items()
199 if k
.startswith(':') and evaluate_marker(k
[1:])
201 return itertools
.chain(ir_d
, tr_d
, er_d
)
205 "WARNING: Testing via this command is deprecated and will be "
206 "removed in a future version. Users looking for a generic test "
207 "entry point independent of test runner are encouraged to use "
212 installed_dists
= self
.install_dists(self
.distribution
)
214 cmd
= ' '.join(self
._argv
)
216 self
.announce('skipping "%s" (dry run)' % cmd
)
219 self
.announce('running "%s"' % cmd
)
221 paths
= map(operator
.attrgetter('location'), installed_dists
)
222 with self
.paths_on_pythonpath(paths
):
223 with self
.project_on_sys_path():
227 test
= unittest
.main(
231 testLoader
=self
._resolve
_as
_ep
(self
.test_loader
),
232 testRunner
=self
._resolve
_as
_ep
(self
.test_runner
),
235 if not test
.result
.wasSuccessful():
236 msg
= 'Test failed: %s' % test
.result
237 self
.announce(msg
, log
.ERROR
)
238 raise DistutilsError(msg
)
242 return ['unittest'] + self
.test_args
246 def _resolve_as_ep(val
):
248 Load the indicated attribute value, called, as a as if it were
249 specified as an entry point.
251 return metadata
.EntryPoint(value
=val
, name
=None, group
=None).load()()