Bug 1048446 - [python-test] Set up a temporary directory for tests to use, r?gbrown draft
authorAndrew Halberstadt <ahalberstadt@mozilla.com>
Tue, 28 Feb 2017 10:27:19 -0500
changeset 591741 83ad9475ab0e994fd363846e6e1dee27781b49bc
parent 591740 9bdde9c16d10d0a0c3dabb915a75cd20f80b7582
child 591742 4d5d91d49bc1b1f986a145866df049774fcd7ce4
push id63159
push userahalberstadt@mozilla.com
push dateFri, 09 Jun 2017 15:00:30 +0000
reviewersgbrown
bugs1048446
milestone55.0a1
Bug 1048446 - [python-test] Set up a temporary directory for tests to use, r?gbrown We set up the temporary directory here so that it persists across multiple test invocations, as each test is run in its own subprocess. MozReview-Commit-ID: 7SNip54AqEI
python/mach_commands.py
--- a/python/mach_commands.py
+++ b/python/mach_commands.py
@@ -1,29 +1,30 @@
 # This Source Code Form is subject to the terms of the Mozilla Public
 # License, v. 2.0. If a copy of the MPL was not distributed with this
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 from __future__ import absolute_import, print_function, unicode_literals
 
 import argparse
 import logging
-import mozpack.path as mozpath
 import os
+import tempfile
 
 from concurrent.futures import (
     ThreadPoolExecutor,
     as_completed,
     thread,
 )
 
 import mozinfo
 from manifestparser import TestManifest
 from manifestparser import filters as mpf
 
+import mozpack.path as mozpath
 from mozbuild.base import (
     MachCommandBase,
 )
 
 from mach.decorators import (
     CommandArgument,
     CommandProvider,
     Command,
@@ -64,23 +65,31 @@ class MachCommands(MachCommandBase):
     @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.'))
     @CommandArgument('tests', nargs='*',
         metavar='TEST',
         help=('Tests to run. Each test can be a single file or a directory. '
               'Default test resolution relies on PYTHON_UNITTEST_MANIFESTS.'))
-    def python_test(self,
-                    tests=[],
-                    test_objects=None,
-                    subsuite=None,
-                    verbose=False,
-                    stop=False,
-                    jobs=1):
+    def python_test(self, *args, **kwargs):
+        try:
+            tempdir = os.environ[b'PYTHON_TEST_TMP'] = str(tempfile.mkdtemp(suffix='-python-test'))
+            return self.run_python_tests(*args, **kwargs)
+        finally:
+            import mozfile
+            mozfile.remove(tempdir)
+
+    def run_python_tests(self,
+                         tests=[],
+                         test_objects=None,
+                         subsuite=None,
+                         verbose=False,
+                         stop=False,
+                         jobs=1):
         self._activate_virtualenv()
 
         def find_tests_by_path():
             import glob
             files = []
             for t in tests:
                 if t.endswith('.py') and os.path.isfile(t):
                     files.append(t)