# HG changeset patch # User Matt Mackall # Date 2011-11-07 19:46:41 # Node ID 5635a4017061c3fcbc715af5fd090a9182fab86c # Parent 4df3de1d44ce2ec7384bcb682313974d00da70c4 run-tests: replace inline python handling with more native scheme Normally changes in tests are reported like this in diffs: $ cat foo - a + b Using -i mode lets us update tests when the new results are correct and/or populate tests with their output. But with the standard doctest framework, inline Python sections in tests changes instead result in a big failure report that's unhelpful. So here, we replace the doctest calls with a simple compile/eval loop. diff --git a/tests/heredoctest.py b/tests/heredoctest.py --- a/tests/heredoctest.py +++ b/tests/heredoctest.py @@ -1,16 +1,19 @@ -import doctest, tempfile, os, sys - -if __name__ == "__main__": - if 'TERM' in os.environ: - del os.environ['TERM'] - - fd, name = tempfile.mkstemp(suffix='hg-tst') +import sys - try: - os.write(fd, sys.stdin.read()) - os.close(fd) - failures, _ = doctest.testfile(name, module_relative=False) - if failures: - sys.exit(1) - finally: - os.remove(name) +globalvars = {} +localvars = {} +lines = sys.stdin.readlines() +while lines: + l = lines.pop(0) + if l.startswith('SALT'): + print l[:-1] + elif l.startswith('>>> '): + snippet = l[4:] + while lines and lines[0].startswith('... '): + l = lines.pop(0) + snippet += "\n" + l[4:] + c = compile(snippet, '', 'single') + try: + exec c in globalvars, localvars + except Exception, inst: + print repr(inst) diff --git a/tests/run-tests.py b/tests/run-tests.py --- a/tests/run-tests.py +++ b/tests/run-tests.py @@ -563,8 +563,11 @@ def tsttest(test, wd, options, replaceme # up script results with our source. These markers include input # line number and the last return code salt = "SALT" + str(time.time()) - def addsalt(line): - script.append('echo %s %s $?\n' % (salt, line)) + def addsalt(line, inpython): + if inpython: + script.append('%s %d 0\n' % (salt, line)) + else: + script.append('echo %s %s $?\n' % (salt, line)) # After we run the shell script, we re-unify the script output # with non-active parts of the source, with synchronization by our @@ -589,13 +592,17 @@ def tsttest(test, wd, options, replaceme if not l.endswith('\n'): l += '\n' if l.startswith(' >>> '): # python inlines + after.setdefault(pos, []).append(l) + prepos = pos + pos = n if not inpython: # we've just entered a Python block, add the header inpython = True - addsalt(n) + addsalt(prepos, False) # make sure we report the exit code script.append('%s -m heredoctest < '): # continuations after.setdefault(prepos, []).append(l) script.append(l[4:]) elif l.startswith(' '): # results - if inpython: - script.append(l[2:]) - after.setdefault(prepos, []).append(l) - else: - # queue up a list of expected results - expected.setdefault(pos, []).append(l[2:]) + # queue up a list of expected results + expected.setdefault(pos, []).append(l[2:]) else: if inpython: script.append("EOF\n") @@ -626,7 +629,7 @@ def tsttest(test, wd, options, replaceme if inpython: script.append("EOF\n") - addsalt(n + 1) + addsalt(n + 1, False) # Write out the script and execute it fd, name = tempfile.mkstemp(suffix='hg-tst') diff --git a/tests/test-run-tests.t b/tests/test-run-tests.t --- a/tests/test-run-tests.t +++ b/tests/test-run-tests.t @@ -16,6 +16,11 @@ Multi-line command: $ foo bar +Return codes before inline python: + + $ false + [1] + Doctest commands: >>> print 'foo' @@ -28,7 +33,7 @@ Doctest commands: y z >>> print - + Regular expressions: