summaryrefslogtreecommitdiff
path: root/test/RegressTests.py
diff options
context:
space:
mode:
authorJohn Wiegley <johnw@newartisans.com>2009-03-03 03:02:51 -0400
committerJohn Wiegley <johnw@newartisans.com>2009-03-03 03:02:51 -0400
commitd7b9f9e068d6817944854a31df1fc34258707a1a (patch)
treed459f5cfd00123ecbdecb417f8c2d2a8a1708be9 /test/RegressTests.py
parent710e4792d12bcc073e4396de8930f27dc516ef3e (diff)
downloadfork-ledger-d7b9f9e068d6817944854a31df1fc34258707a1a.tar.gz
fork-ledger-d7b9f9e068d6817944854a31df1fc34258707a1a.tar.bz2
fork-ledger-d7b9f9e068d6817944854a31df1fc34258707a1a.zip
Refactored test/RegressTests.py
Diffstat (limited to 'test/RegressTests.py')
-rwxr-xr-xtest/RegressTests.py222
1 files changed, 129 insertions, 93 deletions
diff --git a/test/RegressTests.py b/test/RegressTests.py
index 48efaaa1..d5df6ced 100755
--- a/test/RegressTests.py
+++ b/test/RegressTests.py
@@ -16,104 +16,140 @@ tests = sys.argv[2]
if not os.path.isdir(tests) and not os.path.isfile(tests):
sys.exit(1)
-def test_regression(test_file):
- bug = open(test_file)
- command = bug.readline()
-
- line = bug.readline()
- assert "<<<\n" == line
- line = bug.readline()
-
- data = []
- while line != ">>>1\n":
- data.append(line)
- line = bug.readline()
- line = bug.readline()
-
- use_stdin = False
- if command.startswith("-f - "):
- use_stdin = True
-
- command = '$ledger ' + command
- else:
- tempdata = tempfile.mkstemp()
-
- os.write(tempdata[0], join(data, ''))
- os.close(tempdata[0])
-
- command = ('$ledger -f "%s" ' % tempdata[1]) + command
-
- output = []
- while line != ">>>2\n":
- output.append(line)
- line = bug.readline()
- line = bug.readline()
-
- error = []
- while not line.startswith("==="):
- error.append(line)
- line = bug.readline()
-
- match = re.match('=== ([0-9]+)', line)
- assert match
-
- exitcode = int(match.group(1))
-
- p = harness.run(command, columns=(not re.search('--columns', command)))
-
- if use_stdin:
- p.stdin.write(join(data))
- p.stdin.close()
-
- success = True
- printed = False
- index = 0
- for line in unified_diff(output, harness.readlines(p.stdout)):
- index += 1
- if index < 3:
- continue
- if not printed:
- if success: print
- print "Regression failure in output from %s:" % \
- os.path.basename(test_file)
- success = False
- printed = True
- print " ", line,
-
- printed = False
- index = 0
- for line in unified_diff([re.sub('\$FILE', tempdata[1], line)
- for line in error], harness.readlines(p.stderr)):
- index += 1
- if index < 3:
- continue
- if not printed:
- if success: print
- print "Regression failure in error output from %s:" % \
- os.path.basename(test_file)
- success = False
- printed = True
- print " ", line,
-
- if exitcode == p.wait():
- if success:
- harness.success()
+class RegressFile:
+ def __init__(self, filename):
+ self.filename = filename
+ self.fd = open(self.filename)
+
+ def is_directive(self, line):
+ return line == "<<<\n" or \
+ line == ">>>1\n" or \
+ line == ">>>2\n" or \
+ line.startswith("===")
+
+ def read_section(self):
+ lines = []
+ line = self.fd.readline()
+ while not self.is_directive(line):
+ lines.append(line)
+ line = self.fd.readline()
+ return (lines, line)
+
+ def read_test(self, last_test = None):
+ test = {
+ 'command': None,
+ 'input': None,
+ 'output': None,
+ 'error': None,
+ 'exitcode': None
+ }
+ if last_test:
+ test['input'] = last_test['input']
+
+ line = self.fd.readline()
+ while line:
+ if line == "<<<\n":
+ (test['input'], line) = self.read_section()
+ elif line == ">>>1\n":
+ (test['output'], line) = self.read_section()
+ elif line == ">>>2\n":
+ (test['error'], line) = self.read_section()
+ elif line.startswith("==="):
+ match = re.match('=== ([0-9]+)', line)
+ assert match
+ test['exitcode'] = int(match.group(1))
+ return test
+ else:
+ test['command'] = line
+ line = self.fd.readline()
+
+ return None
+
+ def run_test(self, test):
+ use_stdin = False
+ if test['command'].find("-f - ") != -1:
+ use_stdin = True
+
+ test['command'] = '$ledger ' + test['command']
+ else:
+ tempdata = tempfile.mkstemp()
+
+ os.write(tempdata[0], join(test['input'], ''))
+ os.close(tempdata[0])
+
+ test['command'] = (('$ledger -f "%s" ' % tempdata[1]) +
+ test['command'])
+
+ p = harness.run(test['command'],
+ columns=(not re.search('--columns', test['command'])))
+
+ if use_stdin:
+ p.stdin.write(join(test['input'], ''))
+ p.stdin.close()
+
+ success = True
+ printed = False
+ index = 0
+ for line in unified_diff(test['output'], harness.readlines(p.stdout)):
+ index += 1
+ if index < 3:
+ continue
+ if not printed:
+ if success: print
+ print "Regression failure in output from %s:" % \
+ os.path.basename(self.filename)
+ success = False
+ printed = True
+ print " ", line,
+
+ printed = False
+ index = 0
+ for line in unified_diff([re.sub('\$FILE', tempdata[1], line)
+ for line in test['error']],
+ harness.readlines(p.stderr)):
+ index += 1
+ if index < 3:
+ continue
+ if not printed:
+ if success: print
+ print "Regression failure in error output from %s:" % \
+ os.path.basename(self.filename)
+ success = False
+ printed = True
+ print " ", line,
+
+ if test['exitcode'] == p.wait():
+ if success:
+ harness.success()
+ else:
+ harness.failure()
else:
+ if success: print
+ print "Regression failure in exitcode from %s: %d (expected) != %d" % \
+ (os.path.basename(self.filename), test['exitcode'], p.returncode)
harness.failure()
- else:
- if success: print
- print "Regression failure in exitcode from %s: %d (expected) != %d" % \
- (os.path.basename(test_file), exitcode, p.returncode)
- harness.failure()
- if not use_stdin:
- os.remove(tempdata[1])
+ if not use_stdin:
+ os.remove(tempdata[1])
+
+ def run_tests(self):
+ test = self.read_test()
+ while test:
+ self.run_test(test)
+ test = self.read_test()
+
+ def close(self):
+ self.fd.close()
if os.path.isdir(tests):
- for test in os.listdir(tests):
- if re.search('\.test$', test):
- test_regression(os.path.join(tests, test))
+ for test_file in os.listdir(tests):
+ if re.search('\.test$', test_file):
+ entry = RegressFile(os.path.join(tests, test_file))
+ entry.run_tests()
+ entry.close()
else:
- test_regression(tests)
+ entry = RegressFile(tests)
+ entry.run_tests()
+ entry.close()
harness.exit()