Commits

Marcus von Appen  committed f2bdafa

- test framework can output the test results to a file via the --logfile
option now (backported from Anatoly Techntonik for py-sdl2)

  • Participants
  • Parent commits bb3dcdb

Comments (0)

Files changed (2)

File test/util/runtests.py

                          help="Timout for subprocesses before being killed "
                          "(default: 70s per file)")
     optparser.add_option("-v", "--verbose", action="store_true", default=False,
-                         help="be verbose adnd print anything instantly")
+                         help="be verbose and print anything instantly")
     optparser.add_option("-r", "--random", action="store_true", default=False,
                          help="randomize the order of tests")
     optparser.add_option("-S", "--seed", type="int",
     optparser.add_option("-l", "--listtags", action="callback",
                          callback=list_tags,
                          help="lists all available tags and exits")
+    optparser.add_option("--logfile", type="string",
+                         help="save output to log file")
     optkeys = ["filename",
                "subprocess",
                "timeout",
     optparser, optkeys = create_options()
     options, args = optparser.parse_args()
     validate_args(options)
+    if options.logfile:
+        openlog = open(options.logfile, 'wb')
+        savedstd = sys.stderr, sys.stdout
+        # copy stdout and stderr streams to log file
+        sys.stderr = support.TeeOutput(sys.stderr, openlog)
+        sys.stdout = support.TeeOutput(sys.stdout, openlog)
     writer = support.StreamOutput(sys.stdout)
 
     if options.verbose and not options.subprocess:
             writer.writeline("FAILURE: %s" % fail[0])
             writer.writeline(HEAVYDELIM)
             writer.writeline(fail[1])
+    if options.logfile:
+        sys.stderr, sys.stdout = savedstd
+        openlog.close()
     if len(errors) > 0 or len(failures) > 0:
         return 1
     return 0

File test/util/support.py

 """Utility functions for the tests."""
 import os
+import sys
+
+PY3K = sys.version_info[0] == 3
 
 class StreamOutput(object):
     def __init__(self, stream):
             self.stream.write("%s\r" % data)
         self.curoffset = len(data)
         self.stream.flush()
+
+class TeeOutput(object):
+    def __init__(self, stream1, stream2):
+        self.outputs = [stream1, stream2]
+
+    # -- methods from sys.stdout / sys.stderr
+    def write(self, data):
+        for stream in self.outputs:
+            if PY3K:
+                if 'b' in stream.mode:
+                    data = data.encode('utf-8')
+            stream.write(data)
+
+    def tell(self):
+        raise IOError
+
+    def flush(self):
+        for stream in self.outputs:
+            stream.flush()
+    # --/ sys.stdout