[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [stem/master] Simplify static test check tasks



commit 3feb659a72badec309253a6e5d77ab2444d68575
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Sun May 28 14:29:16 2017 -0700

    Simplify static test check tasks
    
    Just about to add mypy as our third static check so good time to simplify how
    we do these so it slips right in.
---
 run_tests.py            | 41 +++++++++++------------------------------
 stem/util/test_tools.py | 19 ++++++++++---------
 test/task.py            | 32 ++++++++++++++++++++++++--------
 3 files changed, 45 insertions(+), 47 deletions(-)

diff --git a/run_tests.py b/run_tests.py
index 2bb75fc..8cb1404 100755
--- a/run_tests.py
+++ b/run_tests.py
@@ -149,7 +149,6 @@ def get_torrc_entries(target):
 
 def main():
   start_time = time.time()
-
   try:
     stem.prereq.check_requirements()
   except ImportError as exc:
@@ -161,6 +160,7 @@ def main():
 
   try:
     args = test.arguments.parse(sys.argv[1:])
+    test.task.TOR_VERSION.args = (args.tor_path,)
   except ValueError as exc:
     println(str(exc))
     sys.exit(1)
@@ -193,23 +193,10 @@ def main():
 
     sys.exit(1)
 
-  tor_version_check, pyflakes_task, pycodestyle_task = None, None, None
-
-  if not args.specific_test:
-    if stem.util.test_tools.is_pyflakes_available():
-      pyflakes_task = test.task.PYFLAKES_TASK
-
-    if stem.util.test_tools.is_pycodestyle_available():
-      pycodestyle_task = test.task.PYCODESTYLE_TASK
-
-  if args.run_integ:
-    tor_version_check = test.task.TOR_VERSION
-    tor_version_check.args = (args.tor_path,)
-
   test.task.run(
     'INITIALISING',
     test.task.STEM_VERSION,
-    tor_version_check,
+    test.task.TOR_VERSION if args.run_integ else None,
     test.task.PYTHON_VERSION,
     test.task.CRYPTO_VERSION,
     test.task.PYNACL_VERSION,
@@ -218,8 +205,8 @@ def main():
     test.task.PYCODESTYLE_VERSION,
     test.task.CLEAN_PYC,
     test.task.UNUSED_TESTS,
-    pyflakes_task,
-    pycodestyle_task,
+    test.task.PYFLAKES_TASK if not args.specific_test else None,
+    test.task.PYCODESTYLE_TASK if not args.specific_test else None,
   )
 
   # buffer that we log messages into so they can be printed after a test has finished
@@ -339,19 +326,13 @@ def main():
 
   static_check_issues = {}
 
-  if pyflakes_task and pyflakes_task.is_successful:
-    for path, issues in pyflakes_task.result.items():
-      for issue in issues:
-        static_check_issues.setdefault(path, []).append(issue)
-  elif not stem.util.test_tools.is_pyflakes_available():
-    println('Static error checking requires pyflakes version 0.7.3 or later. Please install it from ...\n  http://pypi.python.org/pypi/pyflakes\n', ERROR)
-
-  if pycodestyle_task and pycodestyle_task.is_successful:
-    for path, issues in pycodestyle_task.result.items():
-      for issue in issues:
-        static_check_issues.setdefault(path, []).append(issue)
-  elif not stem.util.test_tools.is_pycodestyle_available():
-    println('Style checks require pycodestyle version 1.4.2 or later. Please install it from...\n  http://pypi.python.org/pypi/pycodestyle\n', ERROR)
+  for task in (test.task.PYFLAKES_TASK, test.task.PYCODESTYLE_TASK):
+    if task and task.is_successful:
+      for path, issues in task.result.items():
+        for issue in issues:
+          static_check_issues.setdefault(path, []).append(issue)
+    elif not task.is_available and task.unavailable_msg:
+      println(task.unavailable_msg, ERROR)
 
   _print_static_issues(static_check_issues)
 
diff --git a/stem/util/test_tools.py b/stem/util/test_tools.py
index 51ac244..6964572 100644
--- a/stem/util/test_tools.py
+++ b/stem/util/test_tools.py
@@ -4,20 +4,28 @@
 """
 Helper functions for testing.
 
+Our **stylistic_issues**, **pyflakes_issues**, and **type_check_issues**
+respect a 'exclude_paths' in our test config, excluding any absolute paths
+matching those regexes. Issue strings can start or end with an asterisk
+to match just against the prefix or suffix. For instance...
+
+::
+
+  exclude_paths .*/stem/test/data/.*
+
 .. versionadded:: 1.2.0
 
 ::
 
   TimedTestRunner - test runner that tracks test runtimes
   test_runtimes - provides runtime of tests excuted through TimedTestRunners
-
   clean_orphaned_pyc - delete *.pyc files without corresponding *.py
 
   is_pyflakes_available - checks if pyflakes is available
   is_pycodestyle_available - checks if pycodestyle is available
 
-  stylistic_issues - checks for PEP8 and other stylistic issues
   pyflakes_issues - static checks for problems via pyflakes
+  stylistic_issues - checks for PEP8 and other stylistic issues
 """
 
 import collections
@@ -218,9 +226,6 @@ def stylistic_issues(paths, check_newlines = False, check_exception_keyword = Fa
 
     issues = stylistic_issues('my_project')
 
-  If a 'exclude_paths' was set in our test config then we exclude any absolute
-  paths matching those regexes.
-
   .. versionchanged:: 1.3.0
      Renamed from get_stylistic_issues() to stylistic_issues(). The old name
      still works as an alias, but will be dropped in Stem version 2.0.0.
@@ -345,10 +350,6 @@ def pyflakes_issues(paths):
     pyflakes.ignore stem/util/test_tools.py => 'pyflakes' imported but unused
     pyflakes.ignore stem/util/test_tools.py => 'pycodestyle' imported but unused
 
-  If a 'exclude_paths' was set in our test config then we exclude any absolute
-  paths matching those regexes. Issue strings can start or end with an asterisk
-  to match just against the prefix or suffix.
-
   .. versionchanged:: 1.3.0
      Renamed from get_pyflakes_issues() to pyflakes_issues(). The old name
      still works as an alias, but will be dropped in Stem version 2.0.0.
diff --git a/test/task.py b/test/task.py
index 1089cba..048eade 100644
--- a/test/task.py
+++ b/test/task.py
@@ -55,6 +55,9 @@ SRC_PATHS = [os.path.join(test.STEM_BASE, path) for path in (
   os.path.join('docs', 'roles.py'),
 )]
 
+PYFLAKES_UNAVAILABLE = 'Static error checking requires pyflakes version 0.7.3 or later. Please install it from ...\n  http://pypi.python.org/pypi/pyflakes\n'
+PYCODESTYLE_UNAVAILABLE = 'Style checks require pycodestyle version 1.4.2 or later. Please install it from...\n  http://pypi.python.org/pypi/pycodestyle\n'
+
 
 def _check_tor_version(tor_path):
   return str(test.tor_version(tor_path)).split()[0]
@@ -203,6 +206,21 @@ class ModuleVersion(Task):
     super(ModuleVersion, self).__init__(label, version_check)
 
 
+class StaticCheckTask(Task):
+  def __init__(self, label, runner, args = None, is_available = None, unavailable_msg = None):
+    super(StaticCheckTask, self).__init__(label, runner, args, is_required = False, print_result = False, print_runtime = True)
+    self.is_available = is_available
+    self.unavailable_msg = unavailable_msg
+
+  def run(self):
+    if self.is_available:
+      return super(StaticCheckTask, self).run()
+    else:
+      println('  %s...' % self.label, STATUS, NO_NL)
+      println(' ' * (50 - len(self.label)), NO_NL)
+      println('unavailable', STATUS)
+
+
 STEM_VERSION = Task('checking stem version', lambda: stem.__version__)
 TOR_VERSION = Task('checking tor version', _check_tor_version)
 PYTHON_VERSION = Task('checking python version', lambda: '.'.join(map(str, sys.version_info[:3])))
@@ -218,20 +236,18 @@ UNUSED_TESTS = Task('checking for unused tests', _check_for_unused_tests, [(
   os.path.join(test.STEM_BASE, 'test', 'integ'),
 )])
 
-PYFLAKES_TASK = Task(
+PYFLAKES_TASK = StaticCheckTask(
   'running pyflakes',
   stem.util.test_tools.pyflakes_issues,
   args = (SRC_PATHS,),
-  is_required = False,
-  print_result = False,
-  print_runtime = True,
+  is_available = stem.util.test_tools.is_pyflakes_available(),
+  unavailable_msg = PYFLAKES_UNAVAILABLE,
 )
 
-PYCODESTYLE_TASK = Task(
+PYCODESTYLE_TASK = StaticCheckTask(
   'running pycodestyle',
   stem.util.test_tools.stylistic_issues,
   args = (SRC_PATHS, True, True, True),
-  is_required = False,
-  print_result = False,
-  print_runtime = True,
+  is_available = stem.util.test_tools.is_pycodestyle_available(),
+  unavailable_msg = PYCODESTYLE_UNAVAILABLE,
 )



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits