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/.
13 import importlib
.machinery
16 from uitest
.framework
import UITestCase
18 from libreoffice
.connection
import OfficeConnection
19 from libreoffice
.connection
import PersistentConnection
21 test_name_limit_found
= False
24 (optlist
,args
) = getopt
.getopt(argv
[1:], "hr",
25 ["help", "soffice=", "oneprocess", "userdir=", "dir=", "file=", "gdb"])
26 return (dict(optlist
), args
)
29 message
= """usage: {program} [option]... [task_file]..."
30 -h | --help: print usage information
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
):
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
):
47 if os
.path
.splitext(file_path
)[1] == ".swp":
48 continue # ignore VIM swap files
50 if file_path
[-1:] == "~":
51 continue # ignore backup files
53 # fail on any non .py files
54 if not os
.path
.splitext(file_path
)[1] == ".py":
55 raise Exception("file with an extension which is not .py: " + file_path
)
57 # ignore the __init__.py file
58 # it is obviously not a test file
59 if f
== "__init__.py":
62 valid_files
.append(file_path
)
66 def get_classes_of_module(module
):
68 return [ md
[c
] for c
in md
if (
69 isinstance(md
[c
], type) and md
[c
].__module
__ == module
.__name
__ ) ]
71 def get_test_case_classes_of_module(module
):
72 classes
= get_classes_of_module(module
)
73 return [ c
for c
in classes
if issubclass(c
, UITestCase
) ]
75 def add_tests_for_file(test_file
, test_suite
):
76 test_name_limit
= os
.environ
.get('UITEST_TEST_NAME', '')
77 test_loader
= unittest
.TestLoader()
78 module_name
= os
.path
.splitext(os
.path
.split(test_file
)[1])[0]
80 loader
= importlib
.machinery
.SourceFileLoader(module_name
, test_file
)
81 # exec_module was only introduced in 3.4
82 if sys
.version_info
< (3,4):
83 mod
= loader
.load_module()
85 mod
= types
.ModuleType(loader
.name
)
86 loader
.exec_module(mod
)
87 classes
= get_test_case_classes_of_module(mod
)
88 global test_name_limit_found
90 test_names
= test_loader
.getTestCaseNames(c
)
91 for test_name
in test_names
:
92 full_name
= ".".join([module_name
, c
.__name
__, test_name
])
93 if len(test_name_limit
) > 0:
94 if test_name_limit
!= full_name
:
96 test_name_limit_found
= True
98 obj
= c(test_name
, opts
, connection
)
99 test_suite
.addTest(obj
)
101 def get_test_suite_for_dir(opts
):
102 test_suite
= unittest
.TestSuite()
104 valid_test_files
= find_test_files(opts
['--dir'])
105 for test_file
in valid_test_files
:
106 add_tests_for_file(test_file
, test_suite
)
110 if __name__
== '__main__':
111 (opts
,args
) = parseArgs(sys
.argv
)
113 if "--oneprocess" in opts
:
114 connection
= PersistentConnection(opts
)
116 if "-h" in opts
or "--help" in opts
:
119 elif not "--soffice" in opts
:
122 elif "--dir" in opts
:
123 test_suite
= get_test_suite_for_dir(opts
)
124 test_name_limit
= os
.environ
.get('UITEST_TEST_NAME', '')
125 if len(test_name_limit
) > 0:
126 if not test_name_limit_found
:
127 print("UITEST_TEST_NAME '%s' does not match any test" % test_name_limit
)
130 print("UITEST_TEST_NAME '%s' active" % test_name_limit
)
131 elif "--file" in opts
:
132 test_suite
= unittest
.TestSuite()
133 add_tests_for_file(opts
['--file'], test_suite
)
138 result
= unittest
.TextTestRunner(stream
=sys
.stdout
, verbosity
=2).run(test_suite
)
139 print("Tests run: %d" % result
.testsRun
)
140 print("Tests failed: %d" % len(result
.failures
))
141 print("Tests errors: %d" % len(result
.errors
))
142 print("Tests skipped: %d" % len(result
.skipped
))
144 connection
.tearDown()
146 if not result
.wasSuccessful():
150 # vim: set shiftwidth=4 softtabstop=4 expandtab: