Use the default timeout of 30s as 5s may be too short
[polysh.git] / tests / gsh_tests.py
blobfe8bbebd1232166be7ef8f4dcfad6e7565d68bb6
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 test_class in module.TESTS:
37 suite = unittest.defaultTestLoader.loadTestsFromTestCase(test_class)
38 for test_method in suite:
39 yield test_method
41 def import_all_tests():
42 for test in iter_over_all_tests():
43 TESTS.addTest(test)
45 def import_specified_tests(names):
46 for test in iter_over_all_tests():
47 test_name = test._testMethodName
48 if test_name in names:
49 names.remove(test_name)
50 TESTS.addTest(test)
51 if names:
52 print 'Cannot find tests:', names
53 sys.exit(1)
55 def parse_cmdline():
56 usage='Usage: %s [OPTIONS...] [TESTS...]' % sys.argv[0]
57 parser = optparse.OptionParser(usage=usage)
58 parser.add_option('--coverage', action='store_true', dest='coverage',
59 default=False, help='include coverage tests')
60 parser.add_option('--log', type='str', dest='log',
61 help='log all pexpect I/O and gsh debug info')
62 options, args = parser.parse_args()
63 return options, args
65 def remove_coverage_files():
66 for filename in os.listdir('.'):
67 if filename.startswith('.coverage'):
68 os.remove(filename)
70 def end_coverage():
71 coverage.the_coverage.start()
72 coverage.the_coverage.collect()
73 coverage.the_coverage.stop()
74 modules = [p[:-3] for p in os.listdir('../gsh') if p.endswith('.py')]
75 coverage.report(['../gsh/%s.py' % (m) for m in modules])
76 remove_coverage_files()
77 # Prevent the atexit.register(the_coverage.save) from recreating the files
78 coverage.the_coverage.usecache = coverage.the_coverage.cache = None
80 def main():
81 options, args = parse_cmdline()
82 if options.coverage:
83 remove_coverage_files()
84 if args:
85 import_specified_tests(args)
86 else:
87 import_all_tests()
88 try:
89 unittest.main(argv=[sys.argv[0], '-v'], defaultTest='TESTS')
90 finally:
91 if options.coverage:
92 end_coverage()
94 def launch_gsh(args):
95 args = ['../gsh.py'] + args
96 options, unused_args = parse_cmdline()
97 if options.coverage:
98 args = ['./coverage.py', '-x', '-p'] + args
99 if options.log:
100 logfile = open(options.log, 'a', 0644)
101 args += ['--debug']
102 print >> logfile, 'Launching:', str(args)
103 else:
104 logfile = None
105 return pexpect.spawn(args[0], args=args[1:], logfile=logfile)
107 if __name__ == '__main__':
108 main()