Skip to content

Commit ea13dc6

Browse files
committed
Now that Lib/test/output is gone, tests should not print anything,
except in verbose mode. Support code is much simpler.
1 parent bae17a8 commit ea13dc6

1 file changed

Lines changed: 19 additions & 63 deletions

File tree

Lib/test/regrtest.py

Lines changed: 19 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@
3131
unless -x is given, in which case they are names for tests not to run.
3232
If no test names are given, all tests are run.
3333
34-
-v is incompatible with -g and does not compare test output files.
35-
3634
-T turns on code coverage tracing with the trace module.
3735
3836
-D specifies the directory where coverage files are put.
@@ -178,7 +176,7 @@ def usage(code, msg=''):
178176
sys.exit(code)
179177

180178

181-
def main(tests=None, testdir=None, verbose=0, quiet=False, generate=False,
179+
def main(tests=None, testdir=None, verbose=0, quiet=False,
182180
exclude=False, single=False, randomize=False, fromfile=None,
183181
findleaks=False, use_resources=None, trace=False, coverdir='coverage',
184182
runleaks=False, huntrleaks=False, verbose2=False, print_slow=False):
@@ -198,7 +196,7 @@ def main(tests=None, testdir=None, verbose=0, quiet=False, generate=False,
198196
command-line will be used. If that's empty, too, then all *.py
199197
files beginning with test_ will be used.
200198
201-
The other default arguments (verbose, quiet, generate, exclude,
199+
The other default arguments (verbose, quiet, exclude,
202200
single, randomize, findleaks, use_resources, trace, coverdir, and
203201
print_slow) allow programmers calling main() directly to set the
204202
values that would normally be set by flags on the command line.
@@ -361,12 +359,12 @@ def main(tests=None, testdir=None, verbose=0, quiet=False, generate=False,
361359
if trace:
362360
# If we're tracing code coverage, then we don't exit with status
363361
# if on a false return value from main.
364-
tracer.runctx('runtest(test, generate, verbose, quiet,'
362+
tracer.runctx('runtest(test, verbose, quiet,'
365363
' test_times, testdir)',
366364
globals=globals(), locals=vars())
367365
else:
368366
try:
369-
ok = runtest(test, generate, verbose, quiet, test_times,
367+
ok = runtest(test, verbose, quiet, test_times,
370368
testdir, huntrleaks)
371369
except KeyboardInterrupt:
372370
# print a newline separate from the ^C
@@ -438,7 +436,7 @@ def main(tests=None, testdir=None, verbose=0, quiet=False, generate=False,
438436
sys.stdout.flush()
439437
try:
440438
test_support.verbose = True
441-
ok = runtest(test, generate, True, quiet, test_times, testdir,
439+
ok = runtest(test, True, quiet, test_times, testdir,
442440
huntrleaks)
443441
except KeyboardInterrupt:
444442
# print a newline separate from the ^C
@@ -502,7 +500,7 @@ def findtests(testdir=None, stdtests=STDTESTS, nottests=NOTTESTS):
502500
tests.sort()
503501
return stdtests + tests
504502

505-
def runtest(test, generate, verbose, quiet, test_times,
503+
def runtest(test, verbose, quiet, test_times,
506504
testdir=None, huntrleaks=False):
507505
"""Run a single test.
508506
@@ -521,27 +519,26 @@ def runtest(test, generate, verbose, quiet, test_times,
521519
"""
522520

523521
try:
524-
return runtest_inner(test, generate, verbose, quiet, test_times,
522+
return runtest_inner(test, verbose, quiet, test_times,
525523
testdir, huntrleaks)
526524
finally:
527525
cleanup_test_droppings(test, verbose)
528526

529-
def runtest_inner(test, generate, verbose, quiet, test_times,
527+
def runtest_inner(test, verbose, quiet, test_times,
530528
testdir=None, huntrleaks=False):
531529
test_support.unload(test)
532530
if not testdir:
533531
testdir = findtestdir()
534532
if verbose:
535-
cfp = None
533+
capture_stdout = None
536534
else:
537-
cfp = cStringIO.StringIO()
535+
capture_stdout = cStringIO.StringIO()
538536

539537
try:
540538
save_stdout = sys.stdout
541539
try:
542-
if cfp:
543-
sys.stdout = cfp
544-
print test # Output file starts with test name
540+
if capture_stdout:
541+
sys.stdout = capture_stdout
545542
if test.startswith('test.'):
546543
abstest = test
547544
else:
@@ -587,15 +584,16 @@ def runtest_inner(test, generate, verbose, quiet, test_times,
587584
sys.stdout.flush()
588585
return 0
589586
else:
590-
if not cfp:
587+
# Except in verbose mode, tests should not print anything
588+
if verbose or huntrleaks:
591589
return 1
592-
output = cfp.getvalue()
593-
expected = test + "\n"
594-
if output == expected or huntrleaks:
590+
output = capture_stdout.getvalue()
591+
if not output:
595592
return 1
596593
print "test", test, "produced unexpected output:"
597-
sys.stdout.flush()
598-
reportdiff(expected, output)
594+
print "*" * 70
595+
print output
596+
print "*" * 70
599597
sys.stdout.flush()
600598
return 0
601599

@@ -720,48 +718,6 @@ def dash_R_cleanup(fs, ps, pic, abcs):
720718
# Collect cyclic trash.
721719
gc.collect()
722720

723-
def reportdiff(expected, output):
724-
import difflib
725-
print "*" * 70
726-
a = expected.splitlines(1)
727-
b = output.splitlines(1)
728-
sm = difflib.SequenceMatcher(a=a, b=b)
729-
tuples = sm.get_opcodes()
730-
731-
def pair(x0, x1):
732-
# x0:x1 are 0-based slice indices; convert to 1-based line indices.
733-
x0 += 1
734-
if x0 >= x1:
735-
return "line " + str(x0)
736-
else:
737-
return "lines %d-%d" % (x0, x1)
738-
739-
for op, a0, a1, b0, b1 in tuples:
740-
if op == 'equal':
741-
pass
742-
743-
elif op == 'delete':
744-
print "***", pair(a0, a1), "of expected output missing:"
745-
for line in a[a0:a1]:
746-
print "-", line,
747-
748-
elif op == 'replace':
749-
print "*** mismatch between", pair(a0, a1), "of expected", \
750-
"output and", pair(b0, b1), "of actual output:"
751-
for line in difflib.ndiff(a[a0:a1], b[b0:b1]):
752-
print line,
753-
754-
elif op == 'insert':
755-
print "***", pair(b0, b1), "of actual output doesn't appear", \
756-
"in expected output after line", str(a1)+":"
757-
for line in b[b0:b1]:
758-
print "+", line,
759-
760-
else:
761-
print "get_opcodes() returned bad tuple?!?!", (op, a0, a1, b0, b1)
762-
763-
print "*" * 70
764-
765721
def findtestdir():
766722
if __name__ == '__main__':
767723
file = sys.argv[0]

0 commit comments

Comments
 (0)