From 1eeebaa2a8095861ca9cf6f59997a6037c9984c0 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sun, 14 Aug 2011 14:51:52 +0100 Subject: Add status reporting, exit code, etc. --- cmdtest | 45 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 6 deletions(-) (limited to 'cmdtest') diff --git a/cmdtest b/cmdtest index c55b162..66c7ba9 100755 --- a/cmdtest +++ b/cmdtest @@ -21,7 +21,9 @@ __version__ = '0.0' import cliapp import logging import os +import sys import tempfile +import ttystatus import unittest @@ -30,6 +32,15 @@ class TestCase(object): pass +class TestFailure(Exception): + + def __init__(self, test, msg): + self.msg = 'FAIL: %s: %s' % (test.name, msg) + + def __str__(self): + return self.msg + + class CommandTester(cliapp.Application): def add_settings(self): @@ -39,10 +50,31 @@ class CommandTester(cliapp.Application): def process_args(self, args): self.settings.require('command') + self.setup_ttystatus() self.setup_tempdir() tests = self.load_tests(args) + self.ts['tests'] = tests + errors = 0 for test in tests: - self.run_test(test) + self.ts['test'] = test + try: + self.run_test(test) + except TestFailure, e: + logging.error(str(e)) + self.ts.notify(str(e)) + errors += 1 + + ok = len(tests) - errors + self.ts.finish() + self.output.write('%d/%d tests OK, %d failures\n' % + (ok, len(tests), errors)) + if errors: + sys.exit(1) + + def setup_ttystatus(self): + self.ts = ttystatus.TerminalStatus(period=0.001) + self.ts.add(ttystatus.Literal('test ')) + self.ts.add(ttystatus.Index('test', 'tests')) def load_tests(self, dirnames): tests = [] @@ -92,10 +124,9 @@ class CommandTester(cliapp.Application): if out != test.stdout: actual = self.write_file('actual_stdout', out) expected = self.write_file('expected_stdout', test.stdout) - exit, diff, err = self.runcmd_unchecked(['diff', '-u', - expected, actual]) - raise cliapp.AppException('%s failed: stdout difference:\n%s' % - (test.name, diff)) + diff_argv = ['diff', '-u', expected, actual] + exit, diff, err = self.runcmd_unchecked(diff_argv) + raise TestFailure(test, 'stdout difference:\n%s' % diff) logging.info('Test %s passed' % test.name) def expand(self, strings): @@ -105,8 +136,10 @@ class CommandTester(cliapp.Application): return [s % variables for s in strings] def write_file(self, basename, content): - with open(os.path.join(self.tempdir, basename), 'w'): + filename = os.path.join(self.tempdir, basename) + with open(filename, 'w') as f: f.write(content) + return filename if __name__ == '__main__': -- cgit v1.2.1