Bug 1279005 - Refactor BaseMarionetteTestRunner: added new method _is_filename_valid() centralizing test's file name validation logic.; r?maja_zf draft
authorNelson J Morais <njmorais@gmail.com>
Fri, 15 Jul 2016 00:01:38 +0100
changeset 389234 b7dd43b2a7c0522ba9a57197afe89e64d44eb88e
parent 386010 679118259e91f40d4a8f968f03ec4cff066cdb5b
child 525683 e01eb0506efe265d247b715833f469ba6e460271
push id23327
push userbmo:njmorais@gmail.com
push dateMon, 18 Jul 2016 20:05:42 +0000
reviewersmaja_zf
bugs1279005
milestone50.0a1
Bug 1279005 - Refactor BaseMarionetteTestRunner: added new method _is_filename_valid() centralizing test's file name validation logic.; r?maja_zf MozReview-Commit-ID: 75IFA54QsWy
testing/marionette/harness/marionette/runner/base.py
--- a/testing/marionette/harness/marionette/runner/base.py
+++ b/testing/marionette/harness/marionette/runner/base.py
@@ -532,16 +532,17 @@ class BaseMarionetteTestRunner(object):
         self.test_tags = test_tags
         self.startup_timeout = startup_timeout
         self.workspace = workspace
         # If no workspace is set, default location for gecko.log is .
         # and default location for profile is TMP
         self.workspace_path = workspace or os.getcwd()
         self.verbose = verbose
         self.e10s = e10s
+        self._filename_pattern = None
 
         def gather_debug(test, status):
             rv = {}
             marionette = test._marionette_weakref()
 
             # In the event we're gathering debug without starting a session, skip marionette commands
             if marionette.session is not None:
                 try:
@@ -570,16 +571,23 @@ class BaseMarionetteTestRunner(object):
         if not gecko_log:
             self.gecko_log = os.path.join(self.workspace_path or '', 'gecko.log')
         else:
             self.gecko_log = gecko_log
 
         self.results = []
 
     @property
+    def filename_pattern(self):
+        if self._filename_pattern is None:
+            self._filename_pattern = re.compile("^test(((_.+?)+?\.((py)|(js)))|(([A-Z].*?)+?\.js))$")
+
+        return self._filename_pattern
+
+    @property
     def testvars(self):
         if self._testvars is not None:
             return self._testvars
 
         self._testvars = {}
 
         def update(d, u):
             """ Update a dictionary that may contain nested dictionaries. """
@@ -818,27 +826,27 @@ setReq.onerror = function() {
             else:
                 self.marionette.baseurl = self.server_root
                 self.logger.info("using remote content from %s" % self.marionette.baseurl)
 
     def _add_tests(self, tests):
         for test in tests:
             self.add_test(test)
 
-        pattern = re.compile("^test(((_.+?)+?\.((py)|(js)))|(([A-Z].*?)+?\.js))$")
-        def is_valid(test):
-            filename = os.path.basename(test['filepath'])
-            return pattern.match(filename)
-        invalid_tests = [t['filepath'] for t in self.tests if not is_valid(t)]
+        invalid_tests = [t['filepath'] for t in self.tests if not self._is_filename_valid(t['filepath'])]
         if invalid_tests:
             raise Exception("Test file names must be of the form "
                             "'test_something.py', 'test_something.js', or 'testSomething.js'."
                             " Invalid test names:\n  %s"
                             % '\n  '.join(invalid_tests))
 
+    def _is_filename_valid(self, filename):
+        filename = os.path.basename(filename)
+        return self.filename_pattern.match(filename)
+
     def _log_skipped_tests(self):
         for test in self.manifest_skipped_tests:
             name = os.path.basename(test['path'])
             self.logger.test_start(name)
             self.logger.test_end(name,
                                  'SKIP',
                                  message=test['disabled'])
             self.todo += 1
@@ -945,23 +953,22 @@ setReq.onerror = function() {
         return rv
 
     def add_test(self, test, expected='pass', test_container=None):
         filepath = os.path.abspath(test)
 
         if os.path.isdir(filepath):
             for root, dirs, files in os.walk(filepath):
                 for filename in files:
-                    if (filename.endswith('.ini')):
+                    if filename.endswith('.ini'):
                         msg_tmpl = ("Ignoring manifest '{0}'; running all tests in '{1}'."
                                     " See --help for details.")
                         relpath = os.path.relpath(os.path.join(root, filename), filepath)
                         self.logger.warning(msg_tmpl.format(relpath, filepath))
-                    elif (filename.startswith('test_') and
-                        (filename.endswith('.py') or filename.endswith('.js'))):
+                    elif self._is_filename_valid(filename):
                         test_file = os.path.join(root, filename)
                         self.add_test(test_file)
             return
 
 
         file_ext = os.path.splitext(os.path.split(filepath)[-1])[1]
 
         if file_ext == '.ini':