summaryrefslogtreecommitdiff
path: root/cmdtest
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2011-08-14 14:09:58 +0100
committerLars Wirzenius <liw@liw.fi>2011-08-14 14:09:58 +0100
commit7123e33faedb7427e1149c7676884665a554740e (patch)
tree6bb54604a333723b9f2628420dba3e8b1976565f /cmdtest
downloadcmdtest-7123e33faedb7427e1149c7676884665a554740e.tar.gz
Initial commit.
Diffstat (limited to 'cmdtest')
-rwxr-xr-xcmdtest90
1 files changed, 90 insertions, 0 deletions
diff --git a/cmdtest b/cmdtest
new file mode 100755
index 0000000..f2fdfd9
--- /dev/null
+++ b/cmdtest
@@ -0,0 +1,90 @@
+#!/usr/bin/python
+# Copyright 2011 Lars Wirzenius
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+
+__version__ = '0.0'
+
+
+import cliapp
+import json
+import logging
+import os
+import tempfile
+
+
+class CommandTester(cliapp.Application):
+
+ def add_settings(self):
+ self.settings.string(['command', 'c'],
+ 'test COMMAND (executable name/path)',
+ metavar='COMMAND')
+
+ def process_args(self, args):
+ self.settings.require('command')
+ tests = self.load_tests(args)
+ self.setup_tempdir()
+ for test in tests:
+ self.run_test(test)
+
+ def load_tests(self, filenames):
+ tests = []
+ for filename in filenames:
+ with open(filename) as f:
+ obj = json.load(f)
+ tests.extend(obj)
+ return tests
+
+ def setup_tempdir(self):
+ self.tempdir = tempfile.mkdtemp()
+ logging.info('Temporary directory %s' % self.tempdir)
+
+ def cleanup_tempdir(self):
+ shutil.rmtree(self.tempdir)
+ logging.info('Removed temporary directory %s' % self.tempdir)
+
+ 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'])
+ 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)
+ exit, diff, err = self.runcmd_unchecked(['diff', '-u',
+ stdout_name, out_name])
+ raise cliapp.AppException('%s failed: stdout difference:\n%s' %
+ (test['test'], diff))
+
+ def expand(self, strings):
+ variables = {
+ 'tempdir': self.tempdir,
+ }
+ return [s % variables for s in strings]
+
+
+if __name__ == '__main__':
+ CommandTester(version=__version__).run()