Bug 1466211 - Use --python for selecting target Python when using |mach python-test|; r?ahal draft
authorDave Hunt <dhunt@mozilla.com>
Wed, 13 Jun 2018 14:18:21 -0700
changeset 810231 5a145bd992606304e9362878f258701e8120982a
parent 810230 ce3be5c4afed1b118fcdc3f38b2a64529cf77d52
child 810232 02207cd0c73975ae7f2f8f04bcf038b2702bd35a
push id113931
push userbmo:dave.hunt@gmail.com
push dateMon, 25 Jun 2018 15:20:29 +0000
reviewersahal
bugs1466211
milestone62.0a1
Bug 1466211 - Use --python for selecting target Python when using |mach python-test|; r?ahal MozReview-Commit-ID: J8DSmX8mItb
python/mach_commands.py
python/mozbuild/mozbuild/virtualenv.py
--- a/python/mach_commands.py
+++ b/python/mach_commands.py
@@ -62,20 +62,20 @@ class MachCommands(MachCommandBase):
                                 append_env=append_env)
 
     @Command('python-test', category='testing',
              description='Run Python unit tests with an appropriate test runner.')
     @CommandArgument('-v', '--verbose',
                      default=False,
                      action='store_true',
                      help='Verbose output.')
-    @CommandArgument('--three',
-                     default=False,
-                     action='store_true',
-                     help='Run tests using Python 3.')
+    @CommandArgument('--python',
+                     help='Version of Python for Pipenv to use. When given a '
+                          'Python version, Pipenv will automatically scan your '
+                          'system for a Python that matches that given version.')
     @CommandArgument('-j', '--jobs',
                      default=1,
                      type=int,
                      help='Number of concurrent jobs to run. Default is 1.')
     @CommandArgument('--subsuite',
                      default=None,
                      help=('Python subsuite to run. If not specified, all subsuites are run. '
                            'Use the string `default` to only run tests without a subsuite.'))
@@ -92,20 +92,20 @@ class MachCommands(MachCommandBase):
             mozfile.remove(tempdir)
 
     def run_python_tests(self,
                          tests=None,
                          test_objects=None,
                          subsuite=None,
                          verbose=False,
                          jobs=1,
-                         three=False,
+                         python=None,
                          **kwargs):
-        pipenv_args = ['--three' if three else '--two']
-        self.activate_pipenv(pipfile=None, args=pipenv_args, populate=True)
+        python = python or self.virtualenv_manager.python_path
+        self.activate_pipenv(pipfile=None, args=['--python', python], populate=True)
 
         if test_objects is None:
             from moztest.resolve import TestResolver
             resolver = self._spawn(TestResolver)
             # If we were given test paths, try to find tests matching them.
             test_objects = resolver.resolve_tests(paths=tests, flavor='python')
         else:
             # We've received test_objects from |mach test|. We need to ignore
@@ -117,18 +117,21 @@ class MachCommands(MachCommandBase):
         mp.tests.extend(test_objects)
 
         filters = []
         if subsuite == 'default':
             filters.append(mpf.subsuite(None))
         elif subsuite:
             filters.append(mpf.subsuite(subsuite))
 
-        python = 3 if three else 2
-        tests = mp.active_tests(filters=filters, disabled=False, python=python, **mozinfo.info)
+        tests = mp.active_tests(
+            filters=filters,
+            disabled=False,
+            python=self.virtualenv_manager.version_info[0],
+            **mozinfo.info)
 
         if not tests:
             submsg = "for subsuite '{}' ".format(subsuite) if subsuite else ""
             message = "TEST-UNEXPECTED-FAIL | No tests collected " + \
                       "{}(Not in PYTHON_UNITTEST_MANIFESTS?)".format(submsg)
             self.log(logging.WARN, 'python-test', {}, message)
             return 1
 
--- a/python/mozbuild/mozbuild/virtualenv.py
+++ b/python/mozbuild/mozbuild/virtualenv.py
@@ -88,16 +88,21 @@ class VirtualenvManager(object):
     def python_path(self):
         binary = 'python'
         if sys.platform in ('win32', 'cygwin'):
             binary += '.exe'
 
         return os.path.join(self.bin_path, binary)
 
     @property
+    def version_info(self):
+        return eval(subprocess.check_output([
+            self.python_path, '-c', 'import sys; print(sys.version_info[:])']))
+
+    @property
     def activate_path(self):
         return os.path.join(self.bin_path, 'activate_this.py')
 
     def get_exe_info(self):
         """Returns the version and file size of the python executable that was in
         use when this virutalenv was created.
         """
         with open(self.exe_info_path, 'r') as fh: