Slightly optimize newReplaceAllFromIndex
[LibreOffice.git] / uitest / test_main.py
blobf69e7523530abec55c2b151a5e7e5fc293f31014
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
15 from uitest.framework import UITestCase
17 from libreoffice.connection import OfficeConnection
19 test_name_limit_found = False
21 def parseArgs(argv):
22 (optlist,args) = getopt.getopt(argv[1:], "hr",
23 ["help", "soffice=", "userdir=", "dir=", "file=", "gdb"])
24 return (dict(optlist), args)
26 def usage():
27 message = """usage: {program} [option]... [task_file]..."
28 -h | --help: print usage information
29 {connection_params}
30 the 'task_file' parameters should be
31 full absolute pathnames, not URLs."""
32 print(message.format(program = os.path.basename(sys.argv[0]), \
33 connection_params = OfficeConnection.getHelpText()))
36 def find_test_files(dir_path):
37 valid_files = []
38 for f in sorted(os.listdir(dir_path)):
39 file_path = os.path.join(dir_path, f)
41 # don't go through the sub-directories
42 if not os.path.isfile(file_path):
43 continue
45 if os.path.splitext(file_path)[1] == ".swp":
46 continue # ignore VIM swap files
48 if file_path[-1:] == "~":
49 continue # ignore backup files
51 # fail on any non .py files
52 if not os.path.splitext(file_path)[1] == ".py":
53 raise Exception("file with an extension which is not .py: " + file_path)
55 # ignore the __init__.py file
56 # it is obviously not a test file
57 if f == "__init__.py":
58 continue
60 valid_files.append(file_path)
62 return valid_files
64 def get_classes_of_module(module):
65 md = module.__dict__
66 return [ md[c] for c in md if (
67 isinstance(md[c], type) and md[c].__module__ == module.__name__ ) ]
69 def get_test_case_classes_of_module(module):
70 classes = get_classes_of_module(module)
71 return [ c for c in classes if issubclass(c, UITestCase) ]
73 def add_tests_for_file(test_file, test_suite):
74 test_name_limit = os.environ.get('UITEST_TEST_NAME', '')
75 test_loader = unittest.TestLoader()
76 module_name = os.path.splitext(os.path.split(test_file)[1])[0]
78 loader = importlib.machinery.SourceFileLoader(module_name, test_file)
79 mod = loader.load_module()
80 classes = get_test_case_classes_of_module(mod)
81 global test_name_limit_found
82 for c in classes:
83 test_names = test_loader.getTestCaseNames(c)
84 for test_name in test_names:
85 full_name = ".".join([module_name, c.__name__, test_name])
86 if len(test_name_limit) > 0:
87 if test_name_limit != full_name:
88 continue
89 test_name_limit_found = True
91 obj = c(test_name, opts)
92 test_suite.addTest(obj)
94 def get_test_suite_for_dir(opts):
95 test_suite = unittest.TestSuite()
97 valid_test_files = find_test_files(opts['--dir'])
98 for test_file in valid_test_files:
99 add_tests_for_file(test_file, test_suite)
100 return test_suite
103 if __name__ == '__main__':
104 (opts,args) = parseArgs(sys.argv)
105 if "-h" in opts or "--help" in opts:
106 usage()
107 sys.exit()
108 elif not "--soffice" in opts:
109 usage()
110 sys.exit(1)
111 elif "--dir" in opts:
112 test_suite = get_test_suite_for_dir(opts)
113 test_name_limit = os.environ.get('UITEST_TEST_NAME', '')
114 if len(test_name_limit) > 0:
115 if not test_name_limit_found:
116 print("UITEST_TEST_NAME '%s' does not match any test" % test_name_limit)
117 sys.exit(1)
118 else:
119 print("UITEST_TEST_NAME '%s' active" % test_name_limit)
120 elif "--file" in opts:
121 test_suite = unittest.TestSuite()
122 add_tests_for_file(opts['--file'], test_suite)
123 else:
124 usage()
125 sys.exit()
127 result = unittest.TextTestRunner(stream=sys.stdout, verbosity=2).run(test_suite)
128 print("Tests run: %d" % result.testsRun)
129 print("Tests failed: %d" % len(result.failures))
130 print("Tests errors: %d" % len(result.errors))
131 print("Tests skipped: %d" % len(result.skipped))
132 if not result.wasSuccessful():
133 sys.exit(1)
134 sys.exit(0)
136 # vim: set shiftwidth=4 softtabstop=4 expandtab: