1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
#!/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 logging
import os
import tempfile
import unittest
class TestCase(object):
pass
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')
self.setup_tempdir()
tests = self.load_tests(args)
for test in tests:
self.run_test(test)
def load_tests(self, dirnames):
tests = []
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)
def cleanup_tempdir(self):
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('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)
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))
logging.info('Test %s passed' % test.name)
def expand(self, strings):
variables = {
'tempdir': self.tempdir,
}
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()
|