[tor-commits] [stem/master] Support multiple '--test' arguments

atagar at torproject.org atagar at torproject.org
Tue May 28 00:31:15 UTC 2019


commit 99e5ac8220f6d50018191a4a76b5f30522519f9b
Author: Damian Johnson <atagar at torproject.org>
Date:   Mon May 27 17:28:53 2019 -0700

    Support multiple '--test' arguments
    
    When supplied our '--test' argument limits us to only running that specific
    test. I added this to help with single-test troubleshooting, but on reflection
    it could be useful to the network team as well to limit their CI to only run
    the tests they're concerned with.
    
    Only gotcha was that we didn't support multiple '--test' arguments. Resolving
    that.
---
 run_tests.py      | 56 +++++++++++++++++++++++++++++++++----------------------
 test/arguments.py |  4 ++--
 2 files changed, 36 insertions(+), 24 deletions(-)

diff --git a/run_tests.py b/run_tests.py
index 54fbbde1..088d3f62 100755
--- a/run_tests.py
+++ b/run_tests.py
@@ -114,49 +114,61 @@ def log_traceback(sig, frame):
     os._exit(-1)
 
 
-def get_unit_tests(module_prefix = None):
+def get_unit_tests(module_prefixes = None):
   """
   Provides the classes for our unit tests.
 
-  :param str module_prefix: only provide the test if the module starts with
-    this substring
+  :param list module_prefixes: only provide the test if the module starts with
+    any of these substrings
 
   :returns: an **iterator** for our unit tests
   """
 
-  if module_prefix and not module_prefix.startswith('test.unit.'):
-    module_prefix = 'test.unit.' + module_prefix
+  return _get_tests(CONFIG['test.unit_tests'].splitlines(), module_prefixes)
 
-  return _get_tests(CONFIG['test.unit_tests'].splitlines(), module_prefix)
 
-
-def get_integ_tests(module_prefix = None):
+def get_integ_tests(module_prefixes = None):
   """
   Provides the classes for our integration tests.
 
-  :param str module_prefix: only provide the test if the module starts with
-    this substring
+  :param list module_prefixes: only provide the test if the module starts with
+    any of these substrings
 
   :returns: an **iterator** for our integration tests
   """
 
-  if module_prefix and not module_prefix.startswith('test.integ.'):
-    module_prefix = 'test.integ.' + module_prefix
-
-  return _get_tests(CONFIG['test.integ_tests'].splitlines(), module_prefix)
+  return _get_tests(CONFIG['test.integ_tests'].splitlines(), module_prefixes)
 
 
-def _get_tests(modules, module_prefix):
+def _get_tests(modules, module_prefixes):
   for import_name in modules:
-    module, module_name = import_name.rsplit('.', 1)  # example: util.conf.TestConf
-
-    if not module_prefix or module.startswith(module_prefix):
+    if not module_prefixes:
       yield import_name
-    elif module_prefix.startswith(module):
-      # single test for this module
+    else:
+      # Example import_name: test.unit.util.conf.TestConf
+      #
+      # Our '--test' argument doesn't include the prefix, so excluding it from
+      # the names we look for.
+
+      if import_name.startswith('test.unit.'):
+        cropped_name = import_name[10:]
+      elif import_name.startswith('test.integ.'):
+        cropped_name = import_name[11:]
+      else:
+        cropped_name = import_name
 
-      test_name = module_prefix.rsplit('.', 1)[1]
-      yield '%s.%s' % (import_name, test_name)
+      cropped_name = cropped_name.rsplit('.', 1)[0]  # exclude the class name
+
+      for prefix in module_prefixes:
+        if cropped_name.startswith(prefix):
+          yield import_name
+          break
+        elif prefix.startswith(cropped_name):
+          # single test for this module
+
+          test_name = prefix.rsplit('.', 1)[1]
+          yield '%s.%s' % (import_name, test_name)
+          break
 
 
 def main():
diff --git a/test/arguments.py b/test/arguments.py
index 87735182..1ddf9240 100644
--- a/test/arguments.py
+++ b/test/arguments.py
@@ -26,7 +26,7 @@ CONFIG = stem.util.conf.config_dict('test', {
 DEFAULT_ARGS = {
   'run_unit': False,
   'run_integ': False,
-  'specific_test': None,
+  'specific_test': [],
   'logging_runlevel': None,
   'tor_path': 'tor',
   'run_targets': [test.Target.RUN_OPEN],
@@ -103,7 +103,7 @@ def parse(argv):
 
       args['attribute_targets'] = attribute_targets
     elif opt == '--test':
-      args['specific_test'] = arg
+      args['specific_test'].append(arg)
     elif opt in ('-l', '--log'):
       arg = arg.upper()
 



More information about the tor-commits mailing list