summaryrefslogtreecommitdiff
path: root/cmdtest
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2011-08-14 14:42:29 +0100
committerLars Wirzenius <liw@liw.fi>2011-08-14 14:42:29 +0100
commit0394f0d9238f8ffcc0c082dbdcf08fc0e4f0adde (patch)
treec64080c8594af425321f1977a49cca85e7780697 /cmdtest
parent7123e33faedb7427e1149c7676884665a554740e (diff)
downloadcmdtest-0394f0d9238f8ffcc0c082dbdcf08fc0e4f0adde.tar.gz
Rewrite to not use JSON but individual files.
JSON markup is not fun enough to write by hand.
Diffstat (limited to 'cmdtest')
-rwxr-xr-xcmdtest75
1 files changed, 49 insertions, 26 deletions
diff --git a/cmdtest b/cmdtest
index f2fdfd9..c55b162 100755
--- a/cmdtest
+++ b/cmdtest
@@ -19,10 +19,15 @@ __version__ = '0.0'
import cliapp
-import json
import logging
import os
import tempfile
+import unittest
+
+
+class TestCase(object):
+
+ pass
class CommandTester(cliapp.Application):
@@ -34,19 +39,26 @@ class CommandTester(cliapp.Application):
def process_args(self, args):
self.settings.require('command')
- tests = self.load_tests(args)
self.setup_tempdir()
+ tests = self.load_tests(args)
for test in tests:
self.run_test(test)
- def load_tests(self, filenames):
+ def load_tests(self, dirnames):
tests = []
- for filename in filenames:
- with open(filename) as f:
- obj = json.load(f)
- tests.extend(obj)
+ for dirname in dirnames:
+ tests.append(self.load_test(dirname))
return tests
+ def load_test(self, dirname):
+ t = TestCase()
+ t.name = os.path.basename(dirname)
+ t.setup_cmds = self.lines(os.path.join(dirname, 'setup'))
+ t.command = self.settings['command']
+ t.args = self.lines(os.path.join(dirname, 'args'))
+ t.stdout = self.cat(os.path.join(dirname, 'stdout'))
+ return t
+
def setup_tempdir(self):
self.tempdir = tempfile.mkdtemp()
logging.info('Temporary directory %s' % self.tempdir)
@@ -55,29 +67,36 @@ class CommandTester(cliapp.Application):
shutil.rmtree(self.tempdir)
logging.info('Removed temporary directory %s' % self.tempdir)
+ def cat(self, filename):
+ if os.path.exists(filename):
+ with open(filename) as f:
+ return f.read()
+ else:
+ return ''
+
+ def lines(self, filename):
+ return self.cat(filename).splitlines()
+
def run_test(self, test):
- logging.info('Running test %s' % test['test'])
- for setup_cmd in test['setup']:
- self.runcmd(self.expand(setup_cmd))
- argv = [self.settings['command']] + self.expand(test['args'])
+ logging.info('Test case: %s' % test.name)
+
+ logging.debug('Running setup commands')
+ for setup_cmd in test.setup_cmds:
+ expanded = self.expand([setup_cmd])[0]
+ self.runcmd([expanded], shell=True)
+
+ logging.debug('Running tested command')
+ argv = [self.settings['command']] + self.expand(test.args)
out = self.runcmd(argv)
- stdout_name = test['test'] + '.stdout'
- if os.path.exists(stdout_name):
- with open(stdout_name) as f:
- stdout = f.read()
- else:
- stdout = ''
- stdout_name = os.path.join(self.tempdir, 'stdout')
- with open(stdout_name, 'w') as f:
- f.write(stdout)
- if out != stdout:
- out_name = os.path.join(self.tempdir, 'out')
- with open(out_name, 'w') as f:
- f.write(out)
+
+ 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',
- stdout_name, out_name])
+ expected, actual])
raise cliapp.AppException('%s failed: stdout difference:\n%s' %
- (test['test'], diff))
+ (test.name, diff))
+ logging.info('Test %s passed' % test.name)
def expand(self, strings):
variables = {
@@ -85,6 +104,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'):
+ f.write(content)
+
if __name__ == '__main__':
CommandTester(version=__version__).run()