Improve version check
[LibreOffice.git] / uitest / test_main.py
blob511f2a5c8bd4c0da2087fa37d78653992c7e1a1d
1 # -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-
3 # This Source Code Form is subject to the terms of the Mozilla Public
4 # License, v. 2.0. If a copy of the MPL was not distributed with this
5 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 import sys
9 import getopt
10 import os
11 import unittest
12 import importlib
13 import importlib.machinery
14 import types
16 from uitest.framework import UITestCase
18 from libreoffice.connection import OfficeConnection
20 test_name_limit_found = False
22 def parseArgs(argv):
23 (optlist,args) = getopt.getopt(argv[1:], "hr",
24 ["help", "soffice=", "userdir=", "dir=", "file=", "gdb"])
25 return (dict(optlist), args)
27 def usage():
28 message = """usage: {program} [option]... [task_file]..."
29 -h | --help: print usage information
30 {connection_params}
31 the 'task_file' parameters should be
32 full absolute pathnames, not URLs."""
33 print(message.format(program = os.path.basename(sys.argv[0]), \
34 connection_params = OfficeConnection.getHelpText()))
37 def find_test_files(dir_path):
38 valid_files = []
39 for f in sorted(os.listdir(dir_path)):
40 file_path = os.path.join(dir_path, f)
42 # don't go through the sub-directories
43 if not os.path.isfile(file_path):
44 continue
46 if os.path.splitext(file_path)[1] == ".swp":
47 continue # ignore VIM swap files
49 if file_path[-1:] == "~":
50 continue # ignore backup files
52 # fail on any non .py files
53 if not os.path.splitext(file_path)[1] == ".py":
54 raise Exception("file with an extension which is not .py: " + file_path)
56 # ignore the __init__.py file
57 # it is obviously not a test file
58 if f == "__init__.py":
59 continue
61 valid_files.append(file_path)
63 return valid_files
65 def get_classes_of_module(module):
66 md = module.__dict__
67 return [ md[c] for c in md if (
68 isinstance(md[c], type) and md[c].__module__ == module.__name__ ) ]
70 def get_test_case_classes_of_module(module):
71 classes = get_classes_of_module(module)
72 return [ c for c in classes if issubclass(c, UITestCase) ]
74 def add_tests_for_file(test_file, test_suite):
75 test_name_limit = os.environ.get('UITEST_TEST_NAME', '')
76 test_loader = unittest.TestLoader()
77 module_name = os.path.splitext(os.path.split(test_file)[1])[0]
79 loader = importlib.machinery.SourceFileLoader(module_name, test_file)
80 # exec_module was only introduced in 3.4
81 if sys.version_info < (3,4):
82 mod = loader.load_module()
83 else:
84 mod = types.ModuleType(loader.name)
85 loader.exec_module(mod)
86 classes = get_test_case_classes_of_module(mod)
87 global test_name_limit_found
88 for c in classes:
89 test_names = test_loader.getTestCaseNames(c)
90 for test_name in test_names:
91 full_name = ".".join([module_name, c.__name__, test_name])
92 if len(test_name_limit) > 0:
93 if test_name_limit != full_name:
94 continue
95 test_name_limit_found = True
97 obj = c(test_name, opts)
98 test_suite.addTest(obj)
100 def get_test_suite_for_dir(opts):
101 test_suite = unittest.TestSuite()
103 valid_test_files = find_test_files(opts['--dir'])
104 for test_file in valid_test_files:
105 add_tests_for_file(test_file, test_suite)
106 return test_suite
109 if __name__ == '__main__':
110 (opts,args) = parseArgs(sys.argv)
111 if "-h" in opts or "--help" in opts:
112 usage()
113 sys.exit()
114 elif not "--soffice" in opts:
115 usage()
116 sys.exit(1)
117 elif "--dir" in opts:
118 test_suite = get_test_suite_for_dir(opts)
119 test_name_limit = os.environ.get('UITEST_TEST_NAME', '')
120 if len(test_name_limit) > 0:
121 if not test_name_limit_found:
122 print("UITEST_TEST_NAME '%s' does not match any test" % test_name_limit)
123 sys.exit(1)
124 else:
125 print("UITEST_TEST_NAME '%s' active" % test_name_limit)
126 elif "--file" in opts:
127 test_suite = unittest.TestSuite()
128 add_tests_for_file(opts['--file'], test_suite)
129 else:
130 usage()
131 sys.exit()
133 result = unittest.TextTestRunner(stream=sys.stdout, verbosity=2).run(test_suite)
134 print("Tests run: %d" % result.testsRun)
135 print("Tests failed: %d" % len(result.failures))
136 print("Tests errors: %d" % len(result.errors))
137 print("Tests skipped: %d" % len(result.skipped))
138 if not result.wasSuccessful():
139 sys.exit(1)
140 sys.exit(0)
142 # vim: set shiftwidth=4 softtabstop=4 expandtab: