Bug 777574 - Skip quickCheckAPI-B2.html on Linux. r=bjacob, a=test-only
[gecko.git] / python / mach_commands.py
blob53ca71032dd8abe4823c06fa3c0d0ad30de397ac
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 print_function, unicode_literals
7 import argparse
8 import glob
9 import logging
10 import mozpack.path
11 import os
12 import sys
14 from mozbuild.base import (
15 MachCommandBase,
18 from mach.decorators import (
19 CommandArgument,
20 CommandProvider,
21 Command,
25 @CommandProvider
26 class MachCommands(MachCommandBase):
27 '''
28 Easily run Python and Python unit tests.
29 '''
30 def __init__(self, context):
31 MachCommandBase.__init__(self, context)
32 self._python_executable = None
34 @property
35 def python_executable(self):
36 '''
37 Return path to Python executable, or print and sys.exit(1) if
38 executable does not exist.
39 '''
40 if self._python_executable:
41 return self._python_executable
42 if self._is_windows():
43 executable = '_virtualenv/Scripts/python.exe'
44 else:
45 executable = '_virtualenv/bin/python'
46 path = mozpack.path.join(self.topobjdir, executable)
47 if not os.path.exists(path):
48 print("Could not find Python executable at %s." % path,
49 "Run |mach configure| or |mach build| to install it.")
50 sys.exit(1)
51 self._python_executable = path
52 return path
54 @Command('python', category='devenv',
55 allow_all_args=True,
56 description='Run Python.')
57 @CommandArgument('args', nargs=argparse.REMAINDER)
58 def python(self, args):
59 # Avoid logging the command
60 self.log_manager.terminal_handler.setLevel(logging.CRITICAL)
61 return self.run_process([self.python_executable] + args,
62 pass_thru=True, # Allow user to run Python interactively.
63 ensure_exit_code=False, # Don't throw on non-zero exit code.
64 # Note: subprocess requires native strings in os.environ on Windows
65 append_env={b'PYTHONDONTWRITEBYTECODE': str('1')})
67 @Command('python-test', category='testing',
68 description='Run Python unit tests.')
69 @CommandArgument('--verbose',
70 default=False,
71 action='store_true',
72 help='Verbose output.')
73 @CommandArgument('--stop',
74 default=False,
75 action='store_true',
76 help='Stop running tests after the first error or failure.')
77 @CommandArgument('tests', nargs='+',
78 metavar='TEST',
79 help='Tests to run. Each test can be a single file or a directory.')
80 def python_test(self, tests, verbose=False, stop=False):
81 # Make sure we can find Python before doing anything else.
82 self.python_executable
84 # Python's unittest, and in particular discover, has problems with
85 # clashing namespaces when importing multiple test modules. What follows
86 # is a simple way to keep environments separate, at the price of
87 # launching Python multiple times. This also runs tests via mozunit,
88 # which produces output in the format Mozilla infrastructure expects.
89 return_code = 0
90 files = []
91 for test in tests:
92 if test.endswith('.py') and os.path.isfile(test):
93 files.append(test)
94 elif os.path.isfile(test + '.py'):
95 files.append(test + '.py')
96 elif os.path.isdir(test):
97 files += glob.glob(mozpack.path.join(test, 'test*.py'))
98 files += glob.glob(mozpack.path.join(test, 'unit*.py'))
99 else:
100 self.log(logging.WARN, 'python-test', {'test': test},
101 'TEST-UNEXPECTED-FAIL | Invalid test: {test}')
102 if stop:
103 return 1
105 for file in files:
106 file_displayed_test = [] # Used as a boolean.
107 def _line_handler(line):
108 if not file_displayed_test and line.startswith('TEST-'):
109 file_displayed_test.append(True)
111 inner_return_code = self.run_process(
112 [self.python_executable, file],
113 ensure_exit_code=False, # Don't throw on non-zero exit code.
114 log_name='python-test',
115 # subprocess requires native strings in os.environ on Windows
116 append_env={b'PYTHONDONTWRITEBYTECODE': str('1')},
117 line_handler=_line_handler)
118 return_code += inner_return_code
120 if not file_displayed_test:
121 self.log(logging.WARN, 'python-test', {'file': file},
122 'TEST-UNEXPECTED-FAIL | No test output (missing mozunit.main() call?): {file}')
124 if verbose:
125 if inner_return_code != 0:
126 self.log(logging.INFO, 'python-test', {'file': file},
127 'Test failed: {file}')
128 else:
129 self.log(logging.INFO, 'python-test', {'file': file},
130 'Test passed: {file}')
131 if stop and return_code > 0:
132 return 1
134 return 0 if return_code == 0 else 1