tests/run-tests.py
changeset 2247 546c76e5a3e6
parent 2246 3fd603eb6add
child 2258 7e43d68f3900
equal deleted inserted replaced
2246:3fd603eb6add 2247:546c76e5a3e6
    32     if verbose:
    32     if verbose:
    33         for m in msg:
    33         for m in msg:
    34             print m,
    34             print m,
    35         print
    35         print
    36 
    36 
       
    37 def splitnewlines(text):
       
    38     '''like str.splitlines, but only split on newlines.
       
    39     keep line endings.'''
       
    40     i = 0
       
    41     lines = []
       
    42     while True:
       
    43         n = text.find('\n', i)
       
    44         if n == -1:
       
    45             last = text[i:]
       
    46             if last:
       
    47                 lines.append(last)
       
    48             return lines
       
    49         lines.append(text[i:n+1])
       
    50         i = n + 1
       
    51 
    37 def show_diff(expected, output):
    52 def show_diff(expected, output):
    38     for line in difflib.unified_diff(expected, output,
    53     for line in difflib.unified_diff(expected, output,
    39             "Expected output", "Test output", lineterm=''):
    54             "Expected output", "Test output", lineterm=''):
    40         print line
    55         sys.stdout.write(line)
    41 
    56 
    42 def find_program(program):
    57 def find_program(program):
    43     """Search PATH for a executable program"""
    58     """Search PATH for a executable program"""
    44     for p in os.environ.get('PATH', os.defpath).split(os.pathsep):
    59     for p in os.environ.get('PATH', os.defpath).split(os.pathsep):
    45         name = os.path.join(p, program)
    60         name = os.path.join(p, program)
   123             sys.executable, os.path.join(TESTDIR, 'coverage.py'),
   138             sys.executable, os.path.join(TESTDIR, 'coverage.py'),
   124             adir, omit)
   139             adir, omit)
   125         vlog("# Running: "+cmd)
   140         vlog("# Running: "+cmd)
   126         os.system(cmd)
   141         os.system(cmd)
   127 
   142 
   128 def run(cmd, split_lines=True):
   143 def run(cmd):
   129     """Run command in a sub-process, capturing the output (stdout and stderr).
   144     """Run command in a sub-process, capturing the output (stdout and stderr).
   130     Return the exist code, and output."""
   145     Return the exist code, and output."""
   131     # TODO: Use subprocess.Popen if we're running on Python 2.4
   146     # TODO: Use subprocess.Popen if we're running on Python 2.4
   132     if os.name == 'nt':
   147     if os.name == 'nt':
   133         tochild, fromchild = os.popen4(cmd)
   148         tochild, fromchild = os.popen4(cmd)
   139     else:
   154     else:
   140         proc = popen2.Popen4(cmd)
   155         proc = popen2.Popen4(cmd)
   141         proc.tochild.close()
   156         proc.tochild.close()
   142         output = proc.fromchild.read()
   157         output = proc.fromchild.read()
   143         ret = proc.wait()
   158         ret = proc.wait()
   144     if split_lines:
   159     return ret, splitnewlines(output)
   145         output = output.splitlines()
       
   146     return ret, output
       
   147 
   160 
   148 def run_one(test):
   161 def run_one(test):
   149     vlog("# Test", test)
   162     vlog("# Test", test)
   150     if not verbose:
   163     if not verbose:
   151         sys.stdout.write('.')
   164         sys.stdout.write('.')
   178 
   191 
   179     diffret = 0
   192     diffret = 0
   180     # If reference output file exists, check test output against it
   193     # If reference output file exists, check test output against it
   181     if os.path.exists(ref):
   194     if os.path.exists(ref):
   182         f = open(ref, "r")
   195         f = open(ref, "r")
   183         ref_out = f.read().splitlines()
   196         ref_out = splitnewlines(f.read())
   184         f.close()
   197         f.close()
   185     else:
   198     else:
   186         ref_out = ''
   199         ref_out = ['']
   187     if out != ref_out:
   200     if out != ref_out:
   188         diffret = 1
   201         diffret = 1
   189         print "\nERROR: %s output changed" % (test)
   202         print "\nERROR: %s output changed" % (test)
   190         show_diff(ref_out, out)
   203         show_diff(ref_out, out)
   191     if ret:
   204     if ret:
   192         print "\nERROR: %s failed with error code %d" % (test, ret)
   205         print "\nERROR: %s failed with error code %d" % (test, ret)
   193     elif diffret:
   206     elif diffret:
   194         ret = diffret
   207         ret = diffret
   195 
   208 
   196     if ret != 0: # Save errors to a file for diagnosis
   209     if ret != 0: # Save errors to a file for diagnosis
   197         f = open(err, "w")
   210         f = open(err, "wb")
   198         for line in out:
   211         for line in out:
   199             f.write(line)
   212             f.write(line)
   200             f.write("\n")
       
   201         f.close()
   213         f.close()
   202 
   214 
   203     os.chdir(TESTDIR)
   215     os.chdir(TESTDIR)
   204     shutil.rmtree(tmpd, True)
   216     shutil.rmtree(tmpd, True)
   205     return ret == 0
   217     return ret == 0