LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / uitest / test_main.py
blobae3367ddae2c9b3db89687f99bb1de6c489548d3
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 import uitest.config
18 from uitest.framework import UITestCase
20 from libreoffice.connection import OfficeConnection
22 test_name_limit_found = False
24 def parseArgs(argv):
25 (optlist,args) = getopt.getopt(argv[1:], "hdr",
26 ["help", "debug", "soffice=", "userdir=", "dir=", "file=", "gdb"])
27 return (dict(optlist), args)
29 def usage():
30 message = """usage: {program} [option]... [task_file]..."
31 -h | --help: print usage information
32 {connection_params}
33 the 'task_file' parameters should be
34 full absolute pathnames, not URLs."""
35 print(message.format(program = os.path.basename(sys.argv[0]), \
36 connection_params = OfficeConnection.getHelpText()))
39 def find_test_files(dir_path):
40 valid_files = []
41 for f in sorted(os.listdir(dir_path)):
42 file_path = os.path.join(dir_path, f)
44 # don't go through the sub-directories
45 if not os.path.isfile(file_path):
46 continue
48 if os.path.splitext(file_path)[1] == ".swp":
49 continue # ignore VIM swap 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 # exec_module was only introduced in 3.4
80 if sys.version_info[1] < 4:
81 mod = loader.load_module()
82 else:
83 mod = types.ModuleType(loader.name)
84 loader.exec_module(mod)
85 classes = get_test_case_classes_of_module(mod)
86 global test_name_limit_found
87 for c in classes:
88 test_names = test_loader.getTestCaseNames(c)
89 for test_name in test_names:
90 full_name = ".".join([module_name, c.__name__, test_name])
91 if len(test_name_limit) > 0:
92 if test_name_limit != full_name:
93 continue
94 test_name_limit_found = True
96 obj = c(test_name, opts)
97 test_suite.addTest(obj)
99 def get_test_suite_for_dir(opts):
100 test_suite = unittest.TestSuite()
102 valid_test_files = find_test_files(opts['--dir'])
103 for test_file in valid_test_files:
104 add_tests_for_file(test_file, test_suite)
105 return test_suite
108 if __name__ == '__main__':
109 (opts,args) = parseArgs(sys.argv)
110 if "-h" in opts or "--help" in opts:
111 usage()
112 sys.exit()
113 elif not "--soffice" in opts:
114 usage()
115 sys.exit(1)
116 elif "--dir" in opts:
117 test_suite = get_test_suite_for_dir(opts)
118 test_name_limit = os.environ.get('UITEST_TEST_NAME', '')
119 if len(test_name_limit) > 0:
120 if not test_name_limit_found:
121 print("UITEST_TEST_NAME '%s' does not match any test" % test_name_limit)
122 sys.exit(1)
123 else:
124 print("UITEST_TEST_NAME '%s' active" % test_name_limit)
125 elif "--file" in opts:
126 test_suite = unittest.TestSuite()
127 add_tests_for_file(opts['--file'], test_suite)
128 else:
129 usage()
130 sys.exit()
132 if "-d" in opts or "--debug" in opts:
133 uitest.config.use_sleep = True
135 result = unittest.TextTestRunner(stream=sys.stdout, verbosity=2).run(test_suite)
136 print("Tests run: %d" % result.testsRun)
137 print("Tests failed: %d" % len(result.failures))
138 print("Tests errors: %d" % len(result.errors))
139 print("Tests skipped: %d" % len(result.skipped))
140 if not result.wasSuccessful():
141 sys.exit(1)
142 sys.exit(0)
144 # vim: set shiftwidth=4 softtabstop=4 expandtab: