"Fossies" - the Fresh Open Source Software Archive 
Member "polysh-polysh-0.13/tests/polysh_tests.py" (11 May 2020, 4086 Bytes) of package /linux/privat/polysh-polysh-0.13.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style:
standard) with prefixed line numbers.
Alternatively you can here
view or
download the uninterpreted source code file.
1 #!/usr/bin/env python3
2 """Polysh - Tests
3
4 Copyright (c) 2006 Guillaume Chazarain <guichaz@gmail.com>
5 Copyright (c) 2018 InnoGames GmbH
6 """
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 2 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20 import os
21 import unittest
22 import sys
23 import optparse
24 import pexpect
25 from pexpect.popen_spawn import PopenSpawn
26
27 import coverage
28
29 TESTS = unittest.TestSuite()
30
31
32 def iter_over_all_tests():
33 py_files = [p for p in os.listdir('tests') if p.endswith('.py')]
34 tests = list(set([p[:p.index('.')] for p in py_files]))
35 for name in tests:
36 module = getattr(__import__('tests.' + name), name)
37 for module_content in dir(module):
38 candidate = getattr(module, module_content)
39 if not isinstance(candidate, type):
40 continue
41 if not issubclass(candidate, unittest.TestCase):
42 continue
43 suite = unittest.defaultTestLoader.loadTestsFromTestCase(candidate)
44 for test_method in suite:
45 yield test_method
46
47
48 def import_all_tests():
49 for test in iter_over_all_tests():
50 TESTS.addTest(test)
51
52
53 def import_specified_tests(names):
54 for test in iter_over_all_tests():
55 test_name = test.id().split('.')[-1]
56 if test_name in names:
57 names.remove(test_name)
58 TESTS.addTest(test)
59 if names:
60 print('Cannot find tests:', names)
61 sys.exit(1)
62
63
64 def parse_cmdline():
65 usage = 'Usage: %s [OPTIONS...] [TESTS...]' % sys.argv[0]
66 parser = optparse.OptionParser(usage=usage)
67 parser.add_option('--coverage', action='store_true', dest='coverage',
68 default=False, help='include coverage tests')
69 parser.add_option('--log', type='str', dest='log',
70 help='log all pexpect I/O and polysh debug info')
71 options, args = parser.parse_args()
72 return options, args
73
74
75 def remove_coverage_files():
76 for filename in os.listdir('.'):
77 if filename.startswith('.coverage'):
78 os.remove(filename)
79
80
81 def end_coverage():
82 coverage.the_coverage.start()
83 coverage.the_coverage.collect()
84 coverage.the_coverage.stop()
85 modules = [p[:-3] for p in os.listdir('../polysh') if p.endswith('.py')]
86 coverage.report(['../polysh/%s.py' % (m) for m in modules])
87 remove_coverage_files()
88 # Prevent the atexit.register(the_coverage.save) from recreating the files
89 coverage.the_coverage.usecache = coverage.the_coverage.cache = None
90
91
92 def main():
93 options, args = parse_cmdline()
94 if options.coverage:
95 remove_coverage_files()
96 if args:
97 import_specified_tests(args)
98 else:
99 import_all_tests()
100 try:
101 unittest.main(argv=[sys.argv[0], '-v'], defaultTest='TESTS')
102 finally:
103 if options.coverage:
104 end_coverage()
105
106
107 def launch_polysh(args, input_data=None):
108 args = ['../run.py'] + args
109 options, unused_args = parse_cmdline()
110 if options.coverage:
111 args = ['./coverage.py', '-x', '-p'] + args
112 if options.log:
113 logfile = open(options.log, 'a', 0o644)
114 args += ['--debug']
115 print('Launching:', str(args), file=logfile)
116 else:
117 logfile = None
118
119 if input_data is None:
120 child = pexpect.spawn(args[0], args=args[1:],
121 encoding='utf-8', logfile=logfile)
122 else:
123 child = PopenSpawn(args, encoding='utf-8', logfile=logfile)
124 child.send(input_data)
125 child.sendeof()
126 return child
127
128
129 if __name__ == '__main__':
130 main()