[tor-commits] [stem/master] Import tests so they can report asynchronisity

atagar at torproject.org atagar at torproject.org
Sat Jun 17 18:18:16 UTC 2017


commit 30ac0298fae3a9b16018fc419bc616222cc997aa
Author: Damian Johnson <atagar at torproject.org>
Date:   Fri Jun 16 11:10:13 2017 -0700

    Import tests so they can report asynchronisity
    
    Last change was nice, but by dropping explicit imports for our aynchronous
    tests they weren't loaded until their invocation. This in turn meant they
    could't report that they were asynchronous.
    
    Adding a simple task that ensures all our test modules are imported.
---
 run_tests.py |  1 +
 test/task.py | 18 +++++++++++++++---
 2 files changed, 16 insertions(+), 3 deletions(-)

diff --git a/run_tests.py b/run_tests.py
index 22a0229..9701178 100755
--- a/run_tests.py
+++ b/run_tests.py
@@ -200,6 +200,7 @@ def main():
     test.task.PYCODESTYLE_VERSION,
     test.task.CLEAN_PYC,
     test.task.UNUSED_TESTS,
+    test.task.IMPORT_TESTS,
     test.task.PYFLAKES_TASK if not args.specific_test else None,
     test.task.PYCODESTYLE_TASK if not args.specific_test else None,
   )
diff --git a/test/task.py b/test/task.py
index c647736..ada9062 100644
--- a/test/task.py
+++ b/test/task.py
@@ -16,6 +16,7 @@
   |- PYFLAKES_VERSION - checks our version of pyflakes
   |- PYCODESTYLE_VERSION - checks our version of pycodestyle
   |- CLEAN_PYC - removes any *.pyc without a corresponding *.py
+  |- IMPORT_TESTS - ensure all test modules have been imported
   |- UNUSED_TESTS - checks to see if any tests are missing from our settings
   |- PYFLAKES_TASK - static checks
   +- PYCODESTYLE_TASK - style checks
@@ -75,6 +76,16 @@ def _clean_orphaned_pyc(paths):
   return ['removed %s' % path for path in stem.util.test_tools.clean_orphaned_pyc(paths)]
 
 
+def _import_tests():
+  """
+  Ensure all tests have been imported. This is important so tests can
+  register if they're asynchronous.
+  """
+
+  for module in (CONFIG['test.unit_tests'].splitlines() + CONFIG['test.integ_tests'].splitlines()):
+    importlib.import_module(module.rsplit('.', 1)[0])
+
+
 def _check_for_unused_tests(paths):
   """
   The 'test.unit_tests' and 'test.integ_tests' in our settings.cfg defines the
@@ -183,7 +194,7 @@ class Task(object):
       self.is_successful = True
       output_msg = 'running' if self._is_background_task else 'done'
 
-      if self.print_result and isinstance(self.result, str):
+      if self.result and self.print_result and isinstance(self.result, str):
         output_msg = self.result
       elif self.print_runtime:
         output_msg += ' (%0.1fs)' % (time.time() - start_time)
@@ -247,12 +258,13 @@ PYNACL_VERSION = ModuleVersion('checking pynacl version', 'nacl', stem.prereq._i
 MOCK_VERSION = ModuleVersion('checking mock version', ['unittest.mock', 'mock'], stem.prereq.is_mock_available)
 PYFLAKES_VERSION = ModuleVersion('checking pyflakes version', 'pyflakes')
 PYCODESTYLE_VERSION = ModuleVersion('checking pycodestyle version', ['pycodestyle', 'pep8'])
-CLEAN_PYC = Task('checking for orphaned .pyc files', _clean_orphaned_pyc, (SRC_PATHS,))
+CLEAN_PYC = Task('checking for orphaned .pyc files', _clean_orphaned_pyc, (SRC_PATHS,), print_runtime = True)
+IMPORT_TESTS = Task('importing test modules', _import_tests, print_runtime = True)
 
 UNUSED_TESTS = Task('checking for unused tests', _check_for_unused_tests, [(
   os.path.join(test.STEM_BASE, 'test', 'unit'),
   os.path.join(test.STEM_BASE, 'test', 'integ'),
-)])
+)], print_runtime = True)
 
 PYFLAKES_TASK = StaticCheckTask(
   'running pyflakes',





More information about the tor-commits mailing list