changeset 2247:546c76e5a3e6

run-tests.py: fix handling of newlines. old code could not handle embedded "\r" or files that ended without newline.
author Vadim Gelfer <vadim.gelfer@gmail.com>
date Wed, 10 May 2006 10:31:22 -0700
parents 3fd603eb6add
children b914f0557832
files tests/run-tests.py
diffstat 1 files changed, 21 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/tests/run-tests.py	Wed May 10 10:07:53 2006 -0700
+++ b/tests/run-tests.py	Wed May 10 10:31:22 2006 -0700
@@ -34,10 +34,25 @@
             print m,
         print
 
+def splitnewlines(text):
+    '''like str.splitlines, but only split on newlines.
+    keep line endings.'''
+    i = 0
+    lines = []
+    while True:
+        n = text.find('\n', i)
+        if n == -1:
+            last = text[i:]
+            if last:
+                lines.append(last)
+            return lines
+        lines.append(text[i:n+1])
+        i = n + 1
+
 def show_diff(expected, output):
     for line in difflib.unified_diff(expected, output,
             "Expected output", "Test output", lineterm=''):
-        print line
+        sys.stdout.write(line)
 
 def find_program(program):
     """Search PATH for a executable program"""
@@ -125,7 +140,7 @@
         vlog("# Running: "+cmd)
         os.system(cmd)
 
-def run(cmd, split_lines=True):
+def run(cmd):
     """Run command in a sub-process, capturing the output (stdout and stderr).
     Return the exist code, and output."""
     # TODO: Use subprocess.Popen if we're running on Python 2.4
@@ -141,9 +156,7 @@
         proc.tochild.close()
         output = proc.fromchild.read()
         ret = proc.wait()
-    if split_lines:
-        output = output.splitlines()
-    return ret, output
+    return ret, splitnewlines(output)
 
 def run_one(test):
     vlog("# Test", test)
@@ -180,10 +193,10 @@
     # If reference output file exists, check test output against it
     if os.path.exists(ref):
         f = open(ref, "r")
-        ref_out = f.read().splitlines()
+        ref_out = splitnewlines(f.read())
         f.close()
     else:
-        ref_out = ''
+        ref_out = ['']
     if out != ref_out:
         diffret = 1
         print "\nERROR: %s output changed" % (test)
@@ -194,10 +207,9 @@
         ret = diffret
 
     if ret != 0: # Save errors to a file for diagnosis
-        f = open(err, "w")
+        f = open(err, "wb")
         for line in out:
             f.write(line)
-            f.write("\n")
         f.close()
 
     os.chdir(TESTDIR)