Consistency
[polysh.git] / tests / gsh_tests.py
blob24126e5c2ddc4ee4e7e3327cce1a15e657051e7f
1 #!/usr/bin/env python
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU Library General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 # See the COPYING file for license information.
19 # Copyright (c) 2007, 2008 Guillaume Chazarain <guichaz@gmail.com>
21 import os
22 import unittest
23 import sys
24 import optparse
25 import pexpect
27 import coverage
29 TESTS = unittest.TestSuite()
31 def iter_over_all_tests():
32 py_files = [p for p in os.listdir('tests') if p.endswith('.py')]
33 tests = list(set([p[:p.index('.')] for p in py_files]))
34 for name in tests:
35 module = getattr(__import__('tests.' + name), name)
36 for module_content in dir(module):
37 candidate = getattr(module, module_content)
38 if not isinstance(candidate, type):
39 continue
40 if not issubclass(candidate, unittest.TestCase):
41 continue
42 suite = unittest.defaultTestLoader.loadTestsFromTestCase(candidate)
43 for test_method in suite:
44 yield test_method
46 def import_all_tests():
47 for test in iter_over_all_tests():
48 TESTS.addTest(test)
50 def import_specified_tests(names):
51 for test in iter_over_all_tests():
52 test_name = test.id().split('.')[-1]
53 if test_name in names:
54 names.remove(test_name)
55 TESTS.addTest(test)
56 if names:
57 print 'Cannot find tests:', names
58 sys.exit(1)
60 def parse_cmdline():
61 usage='Usage: %s [OPTIONS...] [TESTS...]' % sys.argv[0]
62 parser = optparse.OptionParser(usage=usage)
63 parser.add_option('--coverage', action='store_true', dest='coverage',
64 default=False, help='include coverage tests')
65 parser.add_option('--log', type='str', dest='log',
66 help='log all pexpect I/O and gsh debug info')
67 options, args = parser.parse_args()
68 return options, args
70 def remove_coverage_files():
71 for filename in os.listdir('.'):
72 if filename.startswith('.coverage'):
73 os.remove(filename)
75 def end_coverage():
76 coverage.the_coverage.start()
77 coverage.the_coverage.collect()
78 coverage.the_coverage.stop()
79 modules = [p[:-3] for p in os.listdir('../gsh') if p.endswith('.py')]
80 coverage.report(['../gsh/%s.py' % (m) for m in modules])
81 remove_coverage_files()
82 # Prevent the atexit.register(the_coverage.save) from recreating the files
83 coverage.the_coverage.usecache = coverage.the_coverage.cache = None
85 def main():
86 options, args = parse_cmdline()
87 if options.coverage:
88 remove_coverage_files()
89 if args:
90 import_specified_tests(args)
91 else:
92 import_all_tests()
93 try:
94 unittest.main(argv=[sys.argv[0], '-v'], defaultTest='TESTS')
95 finally:
96 if options.coverage:
97 end_coverage()
99 def launch_gsh(args):
100 args = ['../gsh.py'] + args
101 options, unused_args = parse_cmdline()
102 if options.coverage:
103 args = ['./coverage.py', '-x', '-p'] + args
104 if options.log:
105 logfile = open(options.log, 'a', 0644)
106 args += ['--debug']
107 print >> logfile, 'Launching:', str(args)
108 else:
109 logfile = None
110 return pexpect.spawn(args[0], args=args[1:], logfile=logfile)
112 if __name__ == '__main__':
113 main()