1 # This Source Code Form is subject to the terms of the Mozilla Public
2 # License, v. 2.0. If a copy of the MPL was not distributed with this
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 from __future__
import absolute_import
, print_function
, unicode_literals
12 from multiprocessing
import cpu_count
14 from concurrent
.futures
import (
21 from mozfile
import which
22 from manifestparser
import TestManifest
23 from manifestparser
import filters
as mpf
25 from mozbuild
.base
import (
28 from mozbuild
.virtualenv
import VirtualenvManager
30 from mach
.decorators
import (
36 here
= os
.path
.abspath(os
.path
.dirname(__file__
))
40 class MachCommands(MachCommandBase
):
41 @Command('python', category
='devenv',
42 description
='Run Python.')
43 @CommandArgument('--no-virtualenv', action
='store_true',
44 help='Do not set up a virtualenv')
45 @CommandArgument('--exec-file',
47 help='Execute this Python file using `exec`')
48 @CommandArgument('--ipython',
51 help='Use ipython instead of the default Python REPL.')
52 @CommandArgument('args', nargs
=argparse
.REMAINDER
)
53 def python(self
, no_virtualenv
, exec_file
, ipython
, args
):
54 # Avoid logging the command
55 self
.log_manager
.terminal_handler
.setLevel(logging
.CRITICAL
)
57 # Note: subprocess requires native strings in os.environ on Windows.
59 b
'PYTHONDONTWRITEBYTECODE': str('1'),
63 python_path
= sys
.executable
64 append_env
[b
'PYTHONPATH'] = os
.pathsep
.join(sys
.path
)
66 self
._activate
_virtualenv
()
67 python_path
= self
.virtualenv_manager
.python_path
70 exec(open(exec_file
).read())
74 bindir
= os
.path
.dirname(python_path
)
75 python_path
= which('ipython', path
=bindir
)
78 # Use `_run_pip` directly rather than `install_pip_package` to bypass
79 # `req.check_if_exists()` which may detect a system installed ipython.
80 self
.virtualenv_manager
._run
_pip
(['install', 'ipython'])
81 python_path
= which('ipython', path
=bindir
)
84 print("error: could not detect or install ipython")
87 return self
.run_process([python_path
] + args
,
88 pass_thru
=True, # Allow user to run Python interactively.
89 ensure_exit_code
=False, # Don't throw on non-zero exit code.
90 append_env
=append_env
)
92 @Command('python-test', category
='testing',
93 description
='Run Python unit tests with an appropriate test runner.')
94 @CommandArgument('-v', '--verbose',
97 help='Verbose output.')
98 @CommandArgument('--python',
100 help='Version of Python for Pipenv to use. When given a '
101 'Python version, Pipenv will automatically scan your '
102 'system for a Python that matches that given version.')
103 @CommandArgument('-j', '--jobs',
106 help='Number of concurrent jobs to run. Default is the number of CPUs '
108 @CommandArgument('-x', '--exitfirst',
111 help='Runs all tests sequentially and breaks at the first failure.')
112 @CommandArgument('--subsuite',
114 help=('Python subsuite to run. If not specified, all subsuites are run. '
115 'Use the string `default` to only run tests without a subsuite.'))
116 @CommandArgument('tests', nargs
='*',
118 help=('Tests to run. Each test can be a single file or a directory. '
119 'Default test resolution relies on PYTHON_UNITTEST_MANIFESTS.'))
120 @CommandArgument('extra', nargs
=argparse
.REMAINDER
,
121 metavar
='PYTEST ARGS',
122 help=('Arguments that aren\'t recognized by mach. These will be '
123 'passed as it is to pytest'))
124 def python_test(self
, *args
, **kwargs
):
126 tempdir
= os
.environ
[b
'PYTHON_TEST_TMP'] = str(tempfile
.mkdtemp(suffix
='-python-test'))
127 return self
.run_python_tests(*args
, **kwargs
)
130 mozfile
.remove(tempdir
)
132 def run_python_tests(self
,
142 self
._activate
_test
_virtualenvs
(python
)
144 if test_objects
is None:
145 from moztest
.resolve
import TestResolver
146 resolver
= self
._spawn
(TestResolver
)
147 # If we were given test paths, try to find tests matching them.
148 test_objects
= resolver
.resolve_tests(paths
=tests
, flavor
='python')
150 # We've received test_objects from |mach test|. We need to ignore
151 # the subsuite because python-tests don't use this key like other
152 # harnesses do and |mach test| doesn't realize this.
156 mp
.tests
.extend(test_objects
)
159 if subsuite
== 'default':
160 filters
.append(mpf
.subsuite(None))
162 filters
.append(mpf
.subsuite(subsuite
))
164 tests
= mp
.active_tests(
167 python
=self
.virtualenv_manager
.version_info
[0],
171 submsg
= "for subsuite '{}' ".format(subsuite
) if subsuite
else ""
172 message
= "TEST-UNEXPECTED-FAIL | No tests collected " + \
173 "{}(Not in PYTHON_UNITTEST_MANIFESTS?)".format(submsg
)
174 self
.log(logging
.WARN
, 'python-test', {}, message
)
179 os
.environ
.setdefault('PYTEST_ADDOPTS', '')
182 os
.environ
['PYTEST_ADDOPTS'] += " " + " ".join(extra
)
186 os
.environ
['PYTEST_ADDOPTS'] += " -x"
189 if test
.get('sequential'):
190 sequential
.append(test
)
192 parallel
.append(test
)
194 self
.jobs
= jobs
or cpu_count()
195 self
.terminate
= False
196 self
.verbose
= verbose
200 def on_test_finished(result
):
201 output
, ret
, test_path
= result
204 self
.log(logging
.INFO
, 'python-test', {'line': line
.rstrip()}, '{line}')
206 if ret
and not return_code
:
207 self
.log(logging
.ERROR
, 'python-test', {'test_path': test_path
, 'ret': ret
},
208 'Setting retcode to {ret} from {test_path}')
209 return return_code
or ret
211 with
ThreadPoolExecutor(max_workers
=self
.jobs
) as executor
:
212 futures
= [executor
.submit(self
._run
_python
_test
, test
)
213 for test
in parallel
]
216 for future
in as_completed(futures
):
217 return_code
= on_test_finished(future
.result())
218 except KeyboardInterrupt:
219 # Hack to force stop currently running threads.
220 # https://gist.github.com/clchiou/f2608cbe54403edb0b13
221 executor
._threads
.clear()
222 thread
._threads
_queues
.clear()
225 for test
in sequential
:
226 return_code
= on_test_finished(self
._run
_python
_test
(test
))
227 if return_code
and exitfirst
:
230 self
.log(logging
.INFO
, 'python-test', {'return_code': return_code
},
231 'Return code from mach python-test: {return_code}')
234 def _activate_test_virtualenvs(self
, python
):
235 """Make sure the test suite virtualenvs are set up and activated.
238 python: Optional python version string we want to run the suite with.
239 See the `--python` argument to the `mach python-test` command.
241 from mozbuild
.pythonutil
import find_python3_executable
243 default_manager
= self
.virtualenv_manager
245 # Grab the default virtualenv properties before we activate other virtualenvs.
246 python
= python
or default_manager
.python_path
247 py3_root
= default_manager
.virtualenv_root
+ '_py3'
249 self
.activate_pipenv(pipfile
=None, populate
=True, python
=python
)
251 # The current process might be running under Python 2 and the Python 3
252 # virtualenv will not be set up by mach bootstrap. To avoid problems in tests
253 # that implicitly depend on the Python 3 virtualenv we ensure the Python 3
254 # virtualenv is up to date before the tests start.
255 python3
, version
= find_python3_executable(min_version
='3.5.0')
257 py3_manager
= VirtualenvManager(
258 default_manager
.topsrcdir
,
259 default_manager
.topobjdir
,
261 default_manager
.log_handle
,
262 default_manager
.manifest_path
,
264 py3_manager
.ensure(python3
)
266 def _run_python_test(self
, test
):
267 from mozprocess
import ProcessHandler
269 if test
.get('requirements'):
270 self
.virtualenv_manager
.install_pip_requirements(test
['requirements'], quiet
=True)
275 # Buffer messages if more than one worker to avoid interleaving
279 self
.log(logging
.INFO
, 'python-test', {'line': line
.rstrip()}, '{line}')
281 file_displayed_test
= [] # used as boolean
283 def _line_handler(line
):
284 if not file_displayed_test
:
285 output
= ('Ran' in line
or 'collected' in line
or
286 line
.startswith('TEST-'))
288 file_displayed_test
.append(True)
290 # Hack to make sure treeherder highlights pytest failures
291 if b
'FAILED' in line
.rsplit(b
' ', 1)[-1]:
292 line
= line
.replace(b
'FAILED', b
'TEST-UNEXPECTED-FAIL')
297 python
= self
.virtualenv_manager
.python_path
298 cmd
= [python
, test
['path']]
299 env
= os
.environ
.copy()
300 env
[b
'PYTHONDONTWRITEBYTECODE'] = b
'1'
302 # Homebrew on OS X will change Python's sys.executable to a custom value
303 # which messes with mach's virtualenv handling code. Override Homebrew's
304 # changes with the correct sys.executable value.
305 env
[b
'PYTHONEXECUTABLE'] = python
.encode('utf-8')
307 proc
= ProcessHandler(cmd
, env
=env
, processOutputLine
=_line_handler
, storeOutput
=False)
310 return_code
= proc
.wait()
312 if not file_displayed_test
:
313 _log('TEST-UNEXPECTED-FAIL | No test output (missing mozunit.main() '
314 'call?): {}'.format(test
['path']))
318 _log('Test failed: {}'.format(test
['path']))
320 _log('Test passed: {}'.format(test
['path']))
322 return output
, return_code
, test
['path']