validate:launcher: factor out TTY check and enhance iteration output
authorThibault Saunier <tsaunier@igalia.com>
Tue, 19 Mar 2019 15:15:35 +0000 (12:15 -0300)
committerThibault Saunier <tsaunier@gnome.org>
Wed, 3 Apr 2019 13:38:42 +0000 (13:38 +0000)
validate/launcher/baseclasses.py
validate/launcher/utils.py

index 5c28437..6bb8f90 100644 (file)
@@ -55,7 +55,7 @@ from .vfb_server import get_virual_frame_buffer_server
 from .httpserver import HTTPServer
 from .utils import mkdir, Result, Colors, printc, DEFAULT_TIMEOUT, GST_SECOND, \
     Protocols, look_for_file_in_source_dir, get_data_file, BackTraceGenerator, \
-    check_bugs_resolution
+    check_bugs_resolution, is_tty
 
 # The factor by which we increase the hard timeout when running inside
 # Valgrind
@@ -622,11 +622,7 @@ class Test(Loggable):
             message = "%s %s: %s%s" % (self.number, self.classname, self.result,
                                        " (" + self.message + ")" if self.message else "")
             end = "\r"
-            if sys.stdout.isatty():
-                term_width = shutil.get_terminal_size((80, 20))[0]
-                if len(message) > term_width:
-                    message = message[0:term_width - 2] + '…'
-            else:
+            if not is_tty():
                 message = None
 
         if message is not None:
@@ -1869,7 +1865,7 @@ class _TestsLauncher(Loggable):
         if not self.all_tests:
             self.all_tests = self.list_tests()
         self.total_num_tests = len(self.all_tests)
-        if not sys.stdout.isatty():
+        if not is_tty():
             printc("\nRunning %d tests..." % self.total_num_tests, color=Colors.HEADER)
 
         self.reporter.init_timer()
@@ -1925,24 +1921,25 @@ class _TestsLauncher(Loggable):
             if self.options.forever:
                 r = 1
                 while True:
-                    printc("-> Iteration %d" % r, end='')
+                    printc("-> Iteration %d" % r, end='\r')
 
                     if not self._run_tests():
                         break
                     r += 1
                     self.clean_tests()
-                    printc("OK", Colors.OKGREEN)
+                    msg = "-> Iteration %d... %sOK%s" % (r, Colors.OKGREEN, Colors.ENDC)
+                    printc(msg, end="\r")
 
                 return False
             elif self.options.n_runs:
                 res = True
                 for r in range(self.options.n_runs):
-                    printc("-> Iteration %d" % r)
+                    printc("-> Iteration %d" % r, end='\r')
                     if not self._run_tests():
                         res = False
-                        printc("ERROR", Colors.FAIL)
+                        printc("ERROR", Colors.FAIL, end="\r")
                     else:
-                        printc("OK", Colors.OKGREEN)
+                        printc("OK", Colors.OKGREEN, end="\r")
                     self.clean_tests()
 
                 return res
index 0b1a00b..6368dd3 100644 (file)
@@ -88,14 +88,14 @@ class Protocols(object):
         return False
 
 
+def is_tty():
+    return hasattr(sys.stdout, 'isatty') and sys.stdout.isatty()
+
+
 def supports_ansi_colors():
-    if 'GST_VALIDATE_LAUNCHER_FORCE_COLORS' in os.environ:
-        return True
     platform = sys.platform
     supported_platform = platform != 'win32' or 'ANSICON' in os.environ
-    # isatty is not always implemented, #6223.
-    is_a_tty = hasattr(sys.stdout, 'isatty') and sys.stdout.isatty()
-    if not supported_platform or not is_a_tty:
+    if not supported_platform or not is_tty():
         return False
     return True
 
@@ -185,12 +185,18 @@ def printc(message, color="", title=False, title_char='', end="\n"):
     if hasattr(message, "result") and color == '':
         color = get_color_for_result(message.result)
 
-    if not sys.stdout.isatty():
+    if not is_tty():
         end = "\n"
 
     message = str(message)
     message += ' ' * max(0, last_carriage_return_len - len(message))
-    last_carriage_return_len = len(message) if end == "\r" else 0
+    if end == '\r':
+        term_width = shutil.get_terminal_size((80, 20))[0]
+        if len(message) > term_width:
+            message = message[0:term_width - 2] + '…'
+        last_carriage_return_len = len(message)
+    else:
+        last_carriage_return_len = 0
     sys.stdout.write(color + str(message) + Colors.ENDC + end)
     sys.stdout.flush()