|
1 # |
|
2 # Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 # All rights reserved. |
|
4 # This component and the accompanying materials are made available |
|
5 # under the terms of "Eclipse Public License v1.0" |
|
6 # which accompanies this distribution, and is available |
|
7 # at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 # |
|
9 # Initial Contributors: |
|
10 # Nokia Corporation - initial contribution. |
|
11 # |
|
12 # Contributors: |
|
13 # |
|
14 # Description: |
|
15 # |
|
16 |
|
17 import sys, os, unittest, re |
|
18 import build_egg_info |
|
19 |
|
20 # Path to the directory where this file is located |
|
21 ROOT_PATH = os.path.dirname(os.path.abspath(__file__)) |
|
22 |
|
23 # Path to the source/ directory |
|
24 SOURCE_ROOT = os.path.normpath(os.path.join(ROOT_PATH, '..')) |
|
25 assert os.path.split(SOURCE_ROOT)[1] == 'source' |
|
26 |
|
27 # Path to the source/plugins directory |
|
28 PLUGIN_SOURCE_ROOT = os.path.normpath(os.path.join(SOURCE_ROOT, 'plugins')) |
|
29 assert os.path.isdir(PLUGIN_SOURCE_ROOT) |
|
30 |
|
31 def plugin_test_init(root_path, plugin_source_relative_path='../..'): |
|
32 """ |
|
33 Initialize things so that plug-in unit tests can be run. |
|
34 |
|
35 @param root_path: Path of the __init__.py file calling this function. |
|
36 @param plugin_source_relative_path: Path to the plug-in's source root relative |
|
37 to root_path. Usually this should be '../..', since this function is intended |
|
38 to be called from a plug-in's tests/__init__.py file. |
|
39 |
|
40 """ |
|
41 # Add paths so that the unit tests work |
|
42 # ------------------------------------- |
|
43 extra_paths = [ |
|
44 # For module 'cone' |
|
45 SOURCE_ROOT, |
|
46 |
|
47 # For module 'testautomation' |
|
48 os.path.join(SOURCE_ROOT, 'testautomation'), |
|
49 |
|
50 # For the plug-in module. |
|
51 # Since the method is expected to be called from e.g. |
|
52 # source/plugins/ConeSomePlugin/someplugin/tests/__init__.py, this will then be |
|
53 # source/plugins/ConeSomePlugin, in which case the module 'someplugin' can be |
|
54 # imported |
|
55 os.path.join(root_path, plugin_source_relative_path) |
|
56 ] |
|
57 for p in extra_paths: |
|
58 p = os.path.normpath(p) |
|
59 if p not in sys.path: sys.path.append(p) |
|
60 |
|
61 # Generate egg-info for the plug-in. |
|
62 # The egg-info needs to be up-to-date, or the plug-in framework will not be able |
|
63 # to find the plug-in's ImplML reader classes |
|
64 plugin_path = os.path.normpath(os.path.join(root_path, plugin_source_relative_path)) |
|
65 assert 'setup.py' in os.listdir(plugin_path), "Path '%s' does not contain 'setup.py" % plugin_path |
|
66 build_egg_info.generate_egg_info(plugin_path) |
|
67 |
|
68 def integration_test_init(root_path): |
|
69 """ |
|
70 Initialize things so that integration tests can be run. |
|
71 |
|
72 This function is intended to be called from a sub-package's integration-test/__init__.py file. |
|
73 @param root_path: Path of the __init__.py file calling this function. |
|
74 """ |
|
75 # Add paths so that the unit tests work |
|
76 # ------------------------------------- |
|
77 extra_paths = [ |
|
78 # For module 'cone' (may be needed in some tests for e.g. asserts) |
|
79 SOURCE_ROOT, |
|
80 |
|
81 # For module 'testautomation' |
|
82 os.path.join(SOURCE_ROOT, 'testautomation'), |
|
83 ] |
|
84 for p in extra_paths: |
|
85 p = os.path.normpath(p) |
|
86 if p not in sys.path: sys.path.append(p) |
|
87 |
|
88 # Collect plug-in source paths (common + current) |
|
89 temp = [] |
|
90 temp += [path for path, _ in find_plugin_sources(os.path.join(PLUGIN_SOURCE_ROOT, 'common'))] |
|
91 temp += [path for path, _ in find_plugin_sources(os.path.normpath(os.path.join(root_path, '..')))] |
|
92 plugin_paths = [] |
|
93 for p in temp: |
|
94 if p not in plugin_paths: plugin_paths.append(p) |
|
95 |
|
96 # Add things to PYTHONPATH so that running cone_tool.py works |
|
97 paths = [] |
|
98 paths.append(SOURCE_ROOT) # For module 'cone' |
|
99 paths.extend(plugin_paths) |
|
100 os.environ['PYTHONPATH'] = os.environ.get('PYTHONPATH', '') + ';' + ';'.join(paths) |
|
101 |
|
102 # Generate egg-info for the plug-ins. |
|
103 # The egg-info needs to be up-to-date, or the plug-in framework will not be able |
|
104 # to find the plug-in's ImplML reader classes |
|
105 for p in plugin_paths: |
|
106 assert 'setup.py' in os.listdir(p), "Path '%s' does not contain 'setup.py" % p |
|
107 build_egg_info.generate_egg_info(p) |
|
108 |
|
109 def init_all(): |
|
110 """ |
|
111 Add all plug-ins to sys.path and PYTHONPATH so that running all |
|
112 plug-in unit tests and integration tests work. |
|
113 """ |
|
114 # Find all plug-in source directories |
|
115 plugin_paths = [] |
|
116 temp = find_all_plugin_sources(os.path.join(PLUGIN_SOURCE_ROOT)) |
|
117 for package_name, sources in temp.iteritems(): |
|
118 for path, modname in sources: |
|
119 plugin_paths.append(path) |
|
120 |
|
121 # Add paths so that the unit tests work |
|
122 # ------------------------------------- |
|
123 extra_paths = [ |
|
124 # For module 'cone' (may be needed in some tests for e.g. asserts) |
|
125 SOURCE_ROOT, |
|
126 |
|
127 # For module 'testautomation' |
|
128 os.path.join(SOURCE_ROOT, 'testautomation'), |
|
129 ] + plugin_paths |
|
130 for p in extra_paths: |
|
131 p = os.path.normpath(p) |
|
132 if p not in sys.path: sys.path.append(p) |
|
133 |
|
134 # Add things to PYTHONPATH so that running cone_tool.py works |
|
135 paths = [] |
|
136 paths.append(SOURCE_ROOT) # For module 'cone' |
|
137 paths.extend(plugin_paths) |
|
138 os.environ['PYTHONPATH'] = os.environ.get('PYTHONPATH', '') + ';' + ';'.join(paths) |
|
139 |
|
140 # Generate egg-info for the plug-ins. |
|
141 # The egg-info needs to be up-to-date, or the plug-in framework will not be able |
|
142 # to find the plug-in's ImplML reader classes |
|
143 for p in plugin_paths: |
|
144 assert 'setup.py' in os.listdir(p), "Path '%s' does not contain 'setup.py" % p |
|
145 build_egg_info.generate_egg_info(p) |
|
146 |
|
147 def collect_test_suite_from_dir(root_path): |
|
148 """ |
|
149 Return a test suite containing all tests in 'unittest_*.py' files under the given directory. |
|
150 """ |
|
151 # Find all unittest_*.py files in the folder |
|
152 test_modules = filter(lambda name: re.match(r'^unittest_.*\.py$', name) != None, os.listdir(root_path)) |
|
153 # Strip .py endings |
|
154 test_modules = map(lambda name: name[:-3], test_modules) |
|
155 |
|
156 # Add the root path as the first one in sys.path, so that |
|
157 # __import__() checks that first when attempting to import a module |
|
158 sys.path.insert(0, root_path) |
|
159 |
|
160 try: |
|
161 suite = unittest.TestSuite() |
|
162 for test_module in test_modules: |
|
163 # Load the test module dynamically and add it to the test suite |
|
164 module = __import__(test_module) |
|
165 suite.addTests(unittest.TestLoader().loadTestsFromModule(module)) |
|
166 return suite |
|
167 finally: |
|
168 # Remove root_path from sys.path |
|
169 del sys.path[0] |
|
170 |
|
171 def collect_suite_from_source_list(paths_and_modnames): |
|
172 suite = unittest.TestSuite() |
|
173 |
|
174 # Add the source paths to sys.path |
|
175 for path, modname in paths_and_modnames: |
|
176 if path not in sys.path: |
|
177 sys.path.append(path) |
|
178 |
|
179 # Import the tests in a separate loop, because otherwise the ImplML reader classes |
|
180 # could be loaded before all plug-ins are in sys.path |
|
181 for path, modname in paths_and_modnames: |
|
182 try: |
|
183 m = __import__(modname + '.tests') |
|
184 suite.addTests(m.tests.collect_suite()) |
|
185 except ImportError: |
|
186 print "Tests for '%s' not added, no 'tests' module found" % path |
|
187 |
|
188 return suite |
|
189 |
|
190 def get_plugin_module_name(plugin_src_path): |
|
191 """ |
|
192 Return the name of the plug-in's module under given plug-in source path. |
|
193 |
|
194 >>> get_tests_module('source/plugins/common/ConeContentPlugin') |
|
195 'contentplugin' |
|
196 >>> get_tests_module('foo') |
|
197 None |
|
198 """ |
|
199 if not os.path.isdir(plugin_src_path): |
|
200 return None |
|
201 |
|
202 for name in os.listdir(plugin_src_path): |
|
203 path = os.path.join(plugin_src_path, name) |
|
204 if os.path.isdir(path) and '__init__.py' in os.listdir(path): |
|
205 return name |
|
206 return None |
|
207 |
|
208 def find_plugin_sources(subpackage_root_path): |
|
209 """ |
|
210 Return ConE plug-in source directories from the given path. |
|
211 |
|
212 All sub-directories in subpackage_root_path of the form Cone*Plugin/ containing |
|
213 a sub-directory that is a python module are returned. |
|
214 |
|
215 @param path: The directory from where to find the entries. |
|
216 @return: A list of tuples, each containing (<source_dir>, <module_name>). |
|
217 """ |
|
218 pattern = re.compile(r'^Cone.*Plugin$') |
|
219 |
|
220 # Collect a list of plug-in source paths and plug-in module names |
|
221 result = [] |
|
222 for name in os.listdir(subpackage_root_path): |
|
223 path = os.path.join(subpackage_root_path, name) |
|
224 if pattern.match(name) is not None: |
|
225 modname = get_plugin_module_name(path) |
|
226 if modname is not None: |
|
227 result.append((path, modname)) |
|
228 return result |
|
229 |
|
230 def find_all_plugin_sources(plugins_root_path): |
|
231 """ |
|
232 Return all ConE plug-in source directories from the plugins/ root |
|
233 directory. |
|
234 @return: A dictionary containing the output of find_plugin_sources() by |
|
235 plug-in sub-packages. |
|
236 >>> find_all_plugin_sources('C:/work/cone-trunk/source/plugins') |
|
237 {'common': [('C:/work/cone-trunk/source/plugins/common/ConeContentPlugin', 'contentplugin'), |
|
238 ('C:/work/cone-trunk/source/plugins/common/ConeTemplatePlugin', 'templatemlplugin')], |
|
239 'example': [('C:/work/cone-trunk/source/plugins/example/ConeExamplePlugin', 'examplemlplugin')]} |
|
240 """ |
|
241 result = {} |
|
242 for name in os.listdir(plugins_root_path): |
|
243 path = os.path.join(plugins_root_path, name) |
|
244 if os.path.isdir(path): |
|
245 sources = find_plugin_sources(path) |
|
246 if sources: result[name] = sources |
|
247 return result |
|
248 |
|
249 def find_plugin_package_subpaths(subpath, package_name=None): |
|
250 """ |
|
251 Return a list of plug-in package sub-paths based on the given plug-in package name. |
|
252 |
|
253 The returned list always contains the sub-path for common plug-ins, and |
|
254 additionally for an extra plug-in package based on the given package name. |
|
255 |
|
256 This function can be used to find specifically named files or directories |
|
257 under the plug-in paths. E.g. find all 'integration-test' directories: |
|
258 |
|
259 >>> find_plugin_package_subpaths('integration-test', 'symbian') |
|
260 [('common', 'C:\\cone\\trunk\\sources\\plugins\\common\\integration-test'), |
|
261 ('symbian', 'C:\\cone\\trunk\\sources\\plugins\\symbian\\integration-test')] |
|
262 |
|
263 @param package_name: Name of the extra plug-in package. Can be None, '', |
|
264 'common' or an existing plug-in package. |
|
265 @return: List of tuples (package_name, subpath). |
|
266 |
|
267 @raise ValueError: The given package_name was invalid. |
|
268 """ |
|
269 result = [] |
|
270 |
|
271 def add(package_name): |
|
272 package_dir = os.path.join(PLUGIN_SOURCE_ROOT, package_name) |
|
273 |
|
274 if not os.path.exists(package_dir): |
|
275 raise ValueError("Invalid plug-in package name: '%s'" % package_name) |
|
276 |
|
277 path = os.path.normpath(os.path.join(package_dir, subpath)) |
|
278 if os.path.exists(path): |
|
279 result.append((package_name, path)) |
|
280 |
|
281 add('common') |
|
282 if package_name not in (None, '', 'common'): |
|
283 add(package_name) |
|
284 |
|
285 return result |
|
286 |
|
287 |
|
288 def find_plugin_sources_by_package(package_name=None): |
|
289 """ |
|
290 Return a list of plug-in source paths based on the given plug-in package name. |
|
291 |
|
292 The returned list always contains sources of common plug-ins, and |
|
293 additionally extra plug-in sources based on the given package name. |
|
294 |
|
295 @param package_name: Name of the extra plug-in package. Can be None, '', |
|
296 'common' or an existing plug-in package. |
|
297 @raise ValueError: The given package_name was invalid. |
|
298 """ |
|
299 result = [] |
|
300 |
|
301 # Find all plug-in sources |
|
302 sources = find_all_plugin_sources(PLUGIN_SOURCE_ROOT) |
|
303 |
|
304 # Always return common plug-ins |
|
305 if 'common' in sources: |
|
306 for path, modname in sources['common']: |
|
307 result.append(path) |
|
308 |
|
309 # Return extra plug-ins if necessary |
|
310 if package_name not in (None, '', 'common'): |
|
311 if package_name not in sources: |
|
312 raise ValueError("Invalid plug-in package name: '%s'" % package_name) |
|
313 else: |
|
314 for path, modname in sources[package_name]: |
|
315 result.append(path) |
|
316 |
|
317 return result |