Revert "tdf#127471 Remove font width scaling hack"
[LibreOffice.git] / uitest / test_main.py
blob1904ecbb3476225db241ebd0615000f1b569d72e
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 import uitest.config
17 from uitest.framework import UITestCase
19 from libreoffice.connection import OfficeConnection
21 test_name_limit_found = False
23 def parseArgs(argv):
24 (optlist,args) = getopt.getopt(argv[1:], "hdr",
25 ["help", "debug", "soffice=", "userdir=", "dir=", "file=", "gdb"])
26 return (dict(optlist), args)
28 def usage():
29 message = """usage: {program} [option]... [task_file]..."
30 -h | --help: print usage information
31 {connection_params}
32 the 'task_file' parameters should be
33 full absolute pathnames, not URLs."""
34 print(message.format(program = os.path.basename(sys.argv[0]), \
35 connection_params = OfficeConnection.getHelpText()))
38 def find_test_files(dir_path):
39 valid_files = []
40 for f in sorted(os.listdir(dir_path)):
41 file_path = os.path.join(dir_path, f)
43 # don't go through the sub-directories
44 if not os.path.isfile(file_path):
45 continue
47 if os.path.splitext(file_path)[1] == ".swp":
48 continue # ignore VIM swap files
50 # fail on any non .py files
51 if not os.path.splitext(file_path)[1] == ".py":
52 raise Exception("file with an extension which is not .py: " + file_path)
54 # ignore the __init__.py file
55 # it is obviously not a test file
56 if f == "__init__.py":
57 continue
59 valid_files.append(file_path)
61 return valid_files
63 def get_classes_of_module(module):
64 md = module.__dict__
65 return [ md[c] for c in md if (
66 isinstance(md[c], type) and md[c].__module__ == module.__name__ ) ]
68 def get_test_case_classes_of_module(module):
69 classes = get_classes_of_module(module)
70 return [ c for c in classes if issubclass(c, UITestCase) ]
72 def add_tests_for_file(test_file, test_suite):
73 test_name_limit = os.environ.get('UITEST_TEST_NAME', '')
74 test_loader = unittest.TestLoader()
75 module_name = os.path.splitext(os.path.split(test_file)[1])[0]
77 loader = importlib.machinery.SourceFileLoader(module_name, test_file)
78 mod = loader.load_module()
79 classes = get_test_case_classes_of_module(mod)
80 global test_name_limit_found
81 for c in classes:
82 test_names = test_loader.getTestCaseNames(c)
83 for test_name in test_names:
84 full_name = ".".join([module_name, c.__name__, test_name])
85 if len(test_name_limit) > 0:
86 if test_name_limit != full_name:
87 continue
88 test_name_limit_found = True
90 obj = c(test_name, opts)
91 test_suite.addTest(obj)
93 def get_test_suite_for_dir(opts):
94 test_suite = unittest.TestSuite()
96 valid_test_files = find_test_files(opts['--dir'])
97 for test_file in valid_test_files:
98 add_tests_for_file(test_file, test_suite)
99 return test_suite
102 if __name__ == '__main__':
103 (opts,args) = parseArgs(sys.argv)
104 if "-h" in opts or "--help" in opts:
105 usage()
106 sys.exit()
107 elif not "--soffice" in opts:
108 usage()
109 sys.exit(1)
110 elif "--dir" in opts:
111 test_suite = get_test_suite_for_dir(opts)
112 test_name_limit = os.environ.get('UITEST_TEST_NAME', '')
113 print(test_name_limit_found)
114 if len(test_name_limit) > 0 and not test_name_limit_found:
115 print("UITEST_TEST_NAME '%s' does not match any test" % test_name_limit)
116 sys.exit(1)
117 elif "--file" in opts:
118 test_suite = unittest.TestSuite()
119 add_tests_for_file(opts['--file'], test_suite)
120 else:
121 usage()
122 sys.exit()
124 if "-d" in opts or "--debug" in opts:
125 uitest.config.use_sleep = True
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: