[tor-commits] [stem/master] Adding a --quiet argument to run_tests.py

atagar at torproject.org atagar at torproject.org
Thu Jan 8 17:49:04 UTC 2015


commit 3600862022bdee2ddf1bdb08736d8bc838c0fdbd
Author: Damian Johnson <atagar at torproject.org>
Date:   Thu Jan 8 09:19:37 2015 -0800

    Adding a --quiet argument to run_tests.py
    
    Adding an argument so we only print failures. This was requested by Nick on...
    
      https://trac.torproject.org/projects/tor/ticket/14110
---
 run_tests.py   |   31 ++++++++++++++++++++++---------
 test/output.py |   25 +++++++++++++++++++------
 2 files changed, 41 insertions(+), 15 deletions(-)

diff --git a/run_tests.py b/run_tests.py
index 89eb7cd..d157276 100755
--- a/run_tests.py
+++ b/run_tests.py
@@ -30,7 +30,7 @@ import test.output
 import test.runner
 import test.util
 
-from test.output import STATUS, SUCCESS, ERROR, NO_NL, println
+from test.output import STATUS, SUCCESS, ERROR, NO_NL, STDERR, println
 from test.util import STEM_BASE, Target, Task
 
 # Our default arguments. The _get_args() function provides a named tuple of
@@ -53,12 +53,13 @@ ARGS = {
   'tor_path': 'tor',
   'run_targets': [Target.RUN_OPEN],
   'attribute_targets': [],
+  'quiet': False,
   'verbose': False,
   'print_help': False,
 }
 
-OPT = 'auit:l:vh'
-OPT_EXPANDED = ['all', 'unit', 'integ', 'targets=', 'test=', 'log=', 'tor=', 'verbose', 'help']
+OPT = 'auit:l:qvh'
+OPT_EXPANDED = ['all', 'unit', 'integ', 'targets=', 'test=', 'log=', 'tor=', 'quiet', 'verbose', 'help']
 
 CONFIG = stem.util.conf.config_dict('test', {
   'target.torrc': {},
@@ -129,6 +130,9 @@ def main():
     println(str(exc))
     sys.exit(1)
 
+  if args.quiet:
+    test.output.SUPPRESS_STDOUT = True
+
   if args.print_help:
     println(test.util.get_help_message())
     sys.exit()
@@ -303,10 +307,13 @@ def main():
   runtime_label = '(%i seconds)' % (time.time() - start_time)
 
   if error_tracker.has_errors_occured():
-    println('TESTING FAILED %s' % runtime_label, ERROR)
+    if args.quiet:
+      println('', STDERR)  # extra newline
+
+    println('TESTING FAILED %s' % runtime_label, ERROR, STDERR)
 
     for line in error_tracker:
-      println('  %s' % line, ERROR)
+      println('  %s' % line, ERROR, STDERR)
   else:
     if skipped_tests > 0:
       println('%i TESTS WERE SKIPPED' % skipped_tests, STATUS)
@@ -377,6 +384,8 @@ def _get_args(argv):
       args['logging_runlevel'] = arg
     elif opt in ('--tor'):
       args['tor_path'] = arg
+    elif opt in ('-q', '--quiet'):
+      args['quiet'] = True
     elif opt in ('-v', '--verbose'):
       args['verbose'] = True
     elif opt in ('-h', '--help'):
@@ -435,13 +444,17 @@ def _run_test(args, test_class, output_filters, logging_buffer):
   run_result = unittest.TextTestRunner(test_results, verbosity=2).run(suite)
 
   if args.verbose:
-    sys.stdout.write(test.output.apply_filters(test_results.getvalue(), *output_filters))
-    println()
+    println(test.output.apply_filters(test_results.getvalue(), *output_filters))
   elif not run_result.failures and not run_result.errors:
     println(' success (%0.2fs)' % (time.time() - start_time), SUCCESS)
   else:
-    println(' failed (%0.2fs)' % (time.time() - start_time), ERROR)
-    sys.stdout.write(test.output.apply_filters(test_results.getvalue(), *output_filters))
+    if args.quiet:
+      println(label, STATUS, NO_NL, STDERR)
+      println(' failed (%0.2fs)' % (time.time() - start_time), ERROR, STDERR)
+      println(test.output.apply_filters(test_results.getvalue(), *output_filters), NO_NL, STDERR)
+    else:
+      println(' failed (%0.2fs)' % (time.time() - start_time), ERROR)
+      println(test.output.apply_filters(test_results.getvalue(), *output_filters), NO_NL)
 
   test.output.print_logging(logging_buffer)
 
diff --git a/test/output.py b/test/output.py
index 8b4648f..f338753 100644
--- a/test/output.py
+++ b/test/output.py
@@ -20,6 +20,7 @@ HEADER_ATTR = (term.Color.CYAN, term.Attr.BOLD)
 CATEGORY_ATTR = (term.Color.GREEN, term.Attr.BOLD)
 
 NO_NL = 'no newline'
+STDERR = 'stderr'
 
 # formatting for various categories of messages
 
@@ -46,23 +47,32 @@ LINE_ATTR = {
   LineType.CONTENT: (term.Color.CYAN,),
 }
 
+SUPPRESS_STDOUT = False  # prevent anything from being printed to stdout
+
 
 def println(msg = '', *attr):
+  if SUPPRESS_STDOUT and STDERR not in attr:
+    return
+
   attr = _flatten(attr)
   no_newline = False
+  stream = sys.stderr if STDERR in attr else sys.stdout
 
   if NO_NL in attr:
     no_newline = True
     attr.remove(NO_NL)
 
-  if COLOR_SUPPORT:
+  if STDERR in attr:
+    attr.remove(STDERR)
+
+  if COLOR_SUPPORT and attr:
     msg = term.format(msg, *attr)
 
-  if no_newline:
-    sys.stdout.write(msg)
-    sys.stdout.flush()
-  else:
-    print(msg)
+  if not no_newline:
+    msg += '\n'
+
+  stream.write(msg)
+  stream.flush()
 
 
 def print_divider(msg, is_header = False):
@@ -71,6 +81,9 @@ def print_divider(msg, is_header = False):
 
 
 def print_logging(logging_buffer):
+  if SUPPRESS_STDOUT:
+    return
+
   if not logging_buffer.is_empty():
     for entry in logging_buffer:
       println(entry.replace('\n', '\n  '), term.Color.MAGENTA)





More information about the tor-commits mailing list