Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / tools / telemetry / third_party / modulegraph / modulegraph_tests / test_edge_data.py
blob0760894e7ad585a7240a4f6222ef861267751867
1 import os
2 import sys
3 if sys.version_info[:2] <= (2,6):
4 import unittest2 as unittest
5 else:
6 import unittest
8 from modulegraph import modulegraph
11 # XXX: Todo: simular tests with bytecompiled modules
14 class TestEdgeData (unittest.TestCase):
15 if not hasattr(unittest.TestCase, 'assertIsInstance'):
16 def assertIsInstance(self, value, types):
17 if not isinstance(value, types):
18 self.fail("%r is not an instance of %r"%(value, types))
20 def test_regular_import(self):
21 root = os.path.join(
22 os.path.dirname(os.path.abspath(__file__)),
23 'testpkg-edgedata')
24 mf = modulegraph.ModuleGraph(path=[ root ] + sys.path)
25 script_name = os.path.join(root, 'script.py')
26 mf.run_script(script_name)
28 script_node = mf.findNode(script_name)
29 self.assertIsInstance(script_node, modulegraph.Script)
32 node = mf.findNode('toplevel_existing')
33 self.assertIsInstance(node, modulegraph.SourceModule)
34 ed = mf.edgeData(script_node, node)
35 self.assertIsInstance(ed, modulegraph.DependencyInfo)
36 self.assertEqual(ed, modulegraph.DependencyInfo(conditional=False, function=False, tryexcept=False, fromlist=False))
38 node = mf.findNode('toplevel_nonexisting')
39 self.assertIsInstance(node, modulegraph.MissingModule)
40 ed = mf.edgeData(script_node, node)
41 self.assertIsInstance(ed, modulegraph.DependencyInfo)
42 self.assertEqual(ed, modulegraph.DependencyInfo(conditional=False, function=False, tryexcept=False, fromlist=False))
44 node = mf.findNode('toplevel_class_existing')
45 self.assertIsInstance(node, modulegraph.SourceModule)
46 ed = mf.edgeData(script_node, node)
47 self.assertIsInstance(ed, modulegraph.DependencyInfo)
48 self.assertEqual(ed, modulegraph.DependencyInfo(conditional=False, function=False, tryexcept=False, fromlist=False))
50 node = mf.findNode('toplevel_class_nonexisting')
51 self.assertIsInstance(node, modulegraph.MissingModule)
52 ed = mf.edgeData(script_node, node)
53 self.assertIsInstance(ed, modulegraph.DependencyInfo)
54 self.assertEqual(ed, modulegraph.DependencyInfo(conditional=False, function=False, tryexcept=False, fromlist=False))
56 node = mf.findNode('toplevel_conditional_existing')
57 self.assertIsInstance(node, modulegraph.SourceModule)
58 ed = mf.edgeData(script_node, node)
59 self.assertIsInstance(ed, modulegraph.DependencyInfo)
60 self.assertEqual(ed, modulegraph.DependencyInfo(conditional=True, function=False, tryexcept=False, fromlist=False))
62 node = mf.findNode('toplevel_conditional_nonexisting')
63 self.assertIsInstance(node, modulegraph.MissingModule)
64 ed = mf.edgeData(script_node, node)
65 self.assertIsInstance(ed, modulegraph.DependencyInfo)
66 self.assertEqual(ed, modulegraph.DependencyInfo(conditional=True, function=False, tryexcept=False, fromlist=False))
68 node = mf.findNode('toplevel_conditional_import_existing')
69 self.assertIsInstance(node, modulegraph.SourceModule)
70 ed = mf.edgeData(script_node, node)
71 self.assertIsInstance(ed, modulegraph.DependencyInfo)
72 self.assertEqual(ed, modulegraph.DependencyInfo(conditional=True, function=False, tryexcept=True, fromlist=False))
74 node = mf.findNode('toplevel_conditional_import_nonexisting')
75 self.assertIsInstance(node, modulegraph.MissingModule)
76 ed = mf.edgeData(script_node, node)
77 self.assertIsInstance(ed, modulegraph.DependencyInfo)
78 self.assertEqual(ed, modulegraph.DependencyInfo(conditional=True, function=False, tryexcept=True, fromlist=False))
80 node = mf.findNode('toplevel_conditional_import2_existing')
81 self.assertIsInstance(node, modulegraph.SourceModule)
82 ed = mf.edgeData(script_node, node)
83 self.assertIsInstance(ed, modulegraph.DependencyInfo)
84 self.assertEqual(ed, modulegraph.DependencyInfo(conditional=True, function=False, tryexcept=True, fromlist=False))
86 node = mf.findNode('toplevel_conditional_import2_nonexisting')
87 self.assertIsInstance(node, modulegraph.MissingModule)
88 ed = mf.edgeData(script_node, node)
89 self.assertIsInstance(ed, modulegraph.DependencyInfo)
90 self.assertEqual(ed, modulegraph.DependencyInfo(conditional=True, function=False, tryexcept=True, fromlist=False))
92 node = mf.findNode('toplevel_import_existing')
93 self.assertIsInstance(node, modulegraph.SourceModule)
94 ed = mf.edgeData(script_node, node)
95 self.assertIsInstance(ed, modulegraph.DependencyInfo)
96 self.assertEqual(ed, modulegraph.DependencyInfo(conditional=False, function=False, tryexcept=True, fromlist=False))
98 node = mf.findNode('toplevel_import_nonexisting')
99 self.assertIsInstance(node, modulegraph.MissingModule)
100 ed = mf.edgeData(script_node, node)
101 self.assertIsInstance(ed, modulegraph.DependencyInfo)
102 self.assertEqual(ed, modulegraph.DependencyInfo(conditional=False, function=False, tryexcept=True, fromlist=False))
104 node = mf.findNode('toplevel_import2_existing')
105 self.assertIsInstance(node, modulegraph.SourceModule)
106 ed = mf.edgeData(script_node, node)
107 self.assertIsInstance(ed, modulegraph.DependencyInfo)
108 self.assertEqual(ed, modulegraph.DependencyInfo(conditional=False, function=False, tryexcept=True, fromlist=False))
110 node = mf.findNode('toplevel_import2_nonexisting')
111 self.assertIsInstance(node, modulegraph.MissingModule)
112 ed = mf.edgeData(script_node, node)
113 self.assertIsInstance(ed, modulegraph.DependencyInfo)
114 self.assertEqual(ed, modulegraph.DependencyInfo(conditional=False, function=False, tryexcept=True, fromlist=False))
116 node = mf.findNode('function_existing')
117 self.assertIsInstance(node, modulegraph.SourceModule)
118 ed = mf.edgeData(script_node, node)
119 self.assertIsInstance(ed, modulegraph.DependencyInfo)
120 self.assertEqual(ed, modulegraph.DependencyInfo(conditional=False, function=True, tryexcept=False, fromlist=False))
122 node = mf.findNode('function_nonexisting')
123 self.assertIsInstance(node, modulegraph.MissingModule)
124 ed = mf.edgeData(script_node, node)
125 self.assertIsInstance(ed, modulegraph.DependencyInfo)
126 self.assertEqual(ed, modulegraph.DependencyInfo(conditional=False, function=True, tryexcept=False, fromlist=False))
128 node = mf.findNode('function_class_existing')
129 self.assertIsInstance(node, modulegraph.SourceModule)
130 ed = mf.edgeData(script_node, node)
131 self.assertIsInstance(ed, modulegraph.DependencyInfo)
132 self.assertEqual(ed, modulegraph.DependencyInfo(conditional=False, function=True, tryexcept=False, fromlist=False))
134 node = mf.findNode('function_class_nonexisting')
135 self.assertIsInstance(node, modulegraph.MissingModule)
136 ed = mf.edgeData(script_node, node)
137 self.assertIsInstance(ed, modulegraph.DependencyInfo)
138 self.assertEqual(ed, modulegraph.DependencyInfo(conditional=False, function=True, tryexcept=False, fromlist=False))
140 node = mf.findNode('function_conditional_existing')
141 self.assertIsInstance(node, modulegraph.SourceModule)
142 ed = mf.edgeData(script_node, node)
143 self.assertIsInstance(ed, modulegraph.DependencyInfo)
144 self.assertEqual(ed, modulegraph.DependencyInfo(conditional=True, function=True, tryexcept=False, fromlist=False))
146 node = mf.findNode('function_conditional_nonexisting')
147 self.assertIsInstance(node, modulegraph.MissingModule)
148 ed = mf.edgeData(script_node, node)
149 self.assertIsInstance(ed, modulegraph.DependencyInfo)
150 self.assertEqual(ed, modulegraph.DependencyInfo(conditional=True, function=True, tryexcept=False, fromlist=False))
152 node = mf.findNode('function_conditional_import_existing')
153 self.assertIsInstance(node, modulegraph.SourceModule)
154 ed = mf.edgeData(script_node, node)
155 self.assertIsInstance(ed, modulegraph.DependencyInfo)
156 self.assertEqual(ed, modulegraph.DependencyInfo(conditional=True, function=True, tryexcept=True, fromlist=False))
158 node = mf.findNode('function_conditional_import_nonexisting')
159 self.assertIsInstance(node, modulegraph.MissingModule)
160 ed = mf.edgeData(script_node, node)
161 self.assertIsInstance(ed, modulegraph.DependencyInfo)
162 self.assertEqual(ed, modulegraph.DependencyInfo(conditional=True, function=True, tryexcept=True, fromlist=False))
164 node = mf.findNode('function_conditional_import2_existing')
165 self.assertIsInstance(node, modulegraph.SourceModule)
166 ed = mf.edgeData(script_node, node)
167 self.assertIsInstance(ed, modulegraph.DependencyInfo)
168 self.assertEqual(ed, modulegraph.DependencyInfo(conditional=True, function=True, tryexcept=True, fromlist=False))
170 node = mf.findNode('function_conditional_import2_nonexisting')
171 self.assertIsInstance(node, modulegraph.MissingModule)
172 ed = mf.edgeData(script_node, node)
173 self.assertIsInstance(ed, modulegraph.DependencyInfo)
174 self.assertEqual(ed, modulegraph.DependencyInfo(conditional=True, function=True, tryexcept=True, fromlist=False))
176 node = mf.findNode('function_import_existing')
177 self.assertIsInstance(node, modulegraph.SourceModule)
178 ed = mf.edgeData(script_node, node)
179 self.assertIsInstance(ed, modulegraph.DependencyInfo)
180 self.assertEqual(ed, modulegraph.DependencyInfo(conditional=False, function=True, tryexcept=True, fromlist=False))
182 node = mf.findNode('function_import_nonexisting')
183 self.assertIsInstance(node, modulegraph.MissingModule)
184 ed = mf.edgeData(script_node, node)
185 self.assertIsInstance(ed, modulegraph.DependencyInfo)
186 self.assertEqual(ed, modulegraph.DependencyInfo(conditional=False, function=True, tryexcept=True, fromlist=False))
188 node = mf.findNode('function_import2_existing')
189 self.assertIsInstance(node, modulegraph.SourceModule)
190 ed = mf.edgeData(script_node, node)
191 self.assertIsInstance(ed, modulegraph.DependencyInfo)
192 self.assertEqual(ed, modulegraph.DependencyInfo(conditional=False, function=True, tryexcept=True, fromlist=False))
194 node = mf.findNode('function_import2_nonexisting')
195 self.assertIsInstance(node, modulegraph.MissingModule)
196 ed = mf.edgeData(script_node, node)
197 self.assertIsInstance(ed, modulegraph.DependencyInfo)
198 self.assertEqual(ed, modulegraph.DependencyInfo(conditional=False, function=True, tryexcept=True, fromlist=False))
201 def test_multi_import(self):
202 root = os.path.join(
203 os.path.dirname(os.path.abspath(__file__)),
204 'testpkg-edgedata')
205 mf = modulegraph.ModuleGraph(path=[ root ] + sys.path)
206 script_name = os.path.join(root, 'script_multi_import.py')
207 mf.run_script(script_name)
209 script_node = mf.findNode(script_name)
210 self.assertIsInstance(script_node, modulegraph.Script)
213 node = mf.findNode('os.path')
214 ed = mf.edgeData(script_node, node)
215 self.assertIsInstance(ed, modulegraph.DependencyInfo)
216 self.assertEqual(ed, modulegraph.DependencyInfo(conditional=False, function=False, tryexcept=True, fromlist=False))
218 node = mf.findNode('os')
219 ed = mf.edgeData(script_node, node)
220 self.assertIsInstance(ed, modulegraph.DependencyInfo)
221 self.assertEqual(ed, modulegraph.DependencyInfo(conditional=False, function=False, tryexcept=False, fromlist=False))
223 node = mf.findNode('sys')
224 ed = mf.edgeData(script_node, node)
225 self.assertEqual(ed, modulegraph.DependencyInfo(conditional=True, function=True, tryexcept=False, fromlist=False))
227 node = mf.findNode('platform')
228 ed = mf.edgeData(script_node, node)
229 self.assertEqual(ed, modulegraph.DependencyInfo(conditional=True, function=True, tryexcept=False, fromlist=False))
231 node = mf.findNode('email')
232 ed = mf.edgeData(script_node, node)
233 self.assertEqual(ed, modulegraph.DependencyInfo(conditional=False, function=False, tryexcept=False, fromlist=False))
235 def test_from_imports(self):
236 root = os.path.join(
237 os.path.dirname(os.path.abspath(__file__)),
238 'testpkg-edgedata')
239 mf = modulegraph.ModuleGraph(path=[ root ] + sys.path)
240 script_name = os.path.join(root, 'script_from_import.py')
241 mf.run_script(script_name)
243 script_node = mf.findNode(script_name)
244 self.assertIsInstance(script_node, modulegraph.Script)
247 node = mf.findNode('pkg.toplevel_existing')
248 self.assertIsInstance(node, modulegraph.SourceModule)
249 ed = mf.edgeData(script_node, node)
250 self.assertIsInstance(ed, modulegraph.DependencyInfo)
251 self.assertEqual(ed, modulegraph.DependencyInfo(conditional=False, function=False, tryexcept=False, fromlist=True))
253 node = mf.findNode('pkg.toplevel_nonexisting')
254 self.assertIsInstance(node, modulegraph.MissingModule)
255 ed = mf.edgeData(script_node, node)
256 self.assertIsInstance(ed, modulegraph.DependencyInfo)
257 self.assertEqual(ed, modulegraph.DependencyInfo(conditional=False, function=False, tryexcept=False, fromlist=True))
259 node = mf.findNode('pkg.toplevel_class_existing')
260 self.assertIsInstance(node, modulegraph.SourceModule)
261 ed = mf.edgeData(script_node, node)
262 self.assertIsInstance(ed, modulegraph.DependencyInfo)
263 self.assertEqual(ed, modulegraph.DependencyInfo(conditional=False, function=False, tryexcept=False, fromlist=True))
265 node = mf.findNode('pkg.toplevel_class_nonexisting')
266 self.assertIsInstance(node, modulegraph.MissingModule)
267 ed = mf.edgeData(script_node, node)
268 self.assertIsInstance(ed, modulegraph.DependencyInfo)
269 self.assertEqual(ed, modulegraph.DependencyInfo(conditional=False, function=False, tryexcept=False, fromlist=True))
271 node = mf.findNode('pkg.toplevel_conditional_existing')
272 self.assertIsInstance(node, modulegraph.SourceModule)
273 ed = mf.edgeData(script_node, node)
274 self.assertIsInstance(ed, modulegraph.DependencyInfo)
275 self.assertEqual(ed, modulegraph.DependencyInfo(conditional=True, function=False, tryexcept=False, fromlist=True))
277 node = mf.findNode('pkg.toplevel_conditional_nonexisting')
278 self.assertIsInstance(node, modulegraph.MissingModule)
279 ed = mf.edgeData(script_node, node)
280 self.assertIsInstance(ed, modulegraph.DependencyInfo)
281 self.assertEqual(ed, modulegraph.DependencyInfo(conditional=True, function=False, tryexcept=False, fromlist=True))
283 node = mf.findNode('pkg.toplevel_conditional_import_existing')
284 self.assertIsInstance(node, modulegraph.SourceModule)
285 ed = mf.edgeData(script_node, node)
286 self.assertIsInstance(ed, modulegraph.DependencyInfo)
287 self.assertEqual(ed, modulegraph.DependencyInfo(conditional=True, function=False, tryexcept=True, fromlist=True))
289 node = mf.findNode('pkg.toplevel_conditional_import_nonexisting')
290 self.assertIsInstance(node, modulegraph.MissingModule)
291 ed = mf.edgeData(script_node, node)
292 self.assertIsInstance(ed, modulegraph.DependencyInfo)
293 self.assertEqual(ed, modulegraph.DependencyInfo(conditional=True, function=False, tryexcept=True, fromlist=True))
295 node = mf.findNode('pkg.toplevel_conditional_import2_existing')
296 self.assertIsInstance(node, modulegraph.SourceModule)
297 ed = mf.edgeData(script_node, node)
298 self.assertIsInstance(ed, modulegraph.DependencyInfo)
299 self.assertEqual(ed, modulegraph.DependencyInfo(conditional=True, function=False, tryexcept=True, fromlist=True))
301 node = mf.findNode('pkg.toplevel_conditional_import2_nonexisting')
302 self.assertIsInstance(node, modulegraph.MissingModule)
303 ed = mf.edgeData(script_node, node)
304 self.assertIsInstance(ed, modulegraph.DependencyInfo)
305 self.assertEqual(ed, modulegraph.DependencyInfo(conditional=True, function=False, tryexcept=True, fromlist=True))
307 node = mf.findNode('pkg.toplevel_import_existing')
308 self.assertIsInstance(node, modulegraph.SourceModule)
309 ed = mf.edgeData(script_node, node)
310 self.assertIsInstance(ed, modulegraph.DependencyInfo)
311 self.assertEqual(ed, modulegraph.DependencyInfo(conditional=False, function=False, tryexcept=True, fromlist=True))
313 node = mf.findNode('pkg.toplevel_import_nonexisting')
314 self.assertIsInstance(node, modulegraph.MissingModule)
315 ed = mf.edgeData(script_node, node)
316 self.assertIsInstance(ed, modulegraph.DependencyInfo)
317 self.assertEqual(ed, modulegraph.DependencyInfo(conditional=False, function=False, tryexcept=True, fromlist=True))
319 node = mf.findNode('pkg.toplevel_import2_existing')
320 self.assertIsInstance(node, modulegraph.SourceModule)
321 ed = mf.edgeData(script_node, node)
322 self.assertIsInstance(ed, modulegraph.DependencyInfo)
323 self.assertEqual(ed, modulegraph.DependencyInfo(conditional=False, function=False, tryexcept=True, fromlist=True))
325 node = mf.findNode('pkg.toplevel_import2_nonexisting')
326 self.assertIsInstance(node, modulegraph.MissingModule)
327 ed = mf.edgeData(script_node, node)
328 self.assertIsInstance(ed, modulegraph.DependencyInfo)
329 self.assertEqual(ed, modulegraph.DependencyInfo(conditional=False, function=False, tryexcept=True, fromlist=True))
331 node = mf.findNode('pkg.function_existing')
332 self.assertIsInstance(node, modulegraph.SourceModule)
333 ed = mf.edgeData(script_node, node)
334 self.assertIsInstance(ed, modulegraph.DependencyInfo)
335 self.assertEqual(ed, modulegraph.DependencyInfo(conditional=False, function=True, tryexcept=False, fromlist=True))
337 node = mf.findNode('pkg.function_nonexisting')
338 self.assertIsInstance(node, modulegraph.MissingModule)
339 ed = mf.edgeData(script_node, node)
340 self.assertIsInstance(ed, modulegraph.DependencyInfo)
341 self.assertEqual(ed, modulegraph.DependencyInfo(conditional=False, function=True, tryexcept=False, fromlist=True))
343 node = mf.findNode('pkg.function_class_existing')
344 self.assertIsInstance(node, modulegraph.SourceModule)
345 ed = mf.edgeData(script_node, node)
346 self.assertIsInstance(ed, modulegraph.DependencyInfo)
347 self.assertEqual(ed, modulegraph.DependencyInfo(conditional=False, function=True, tryexcept=False, fromlist=True))
349 node = mf.findNode('pkg.function_class_nonexisting')
350 self.assertIsInstance(node, modulegraph.MissingModule)
351 ed = mf.edgeData(script_node, node)
352 self.assertIsInstance(ed, modulegraph.DependencyInfo)
353 self.assertEqual(ed, modulegraph.DependencyInfo(conditional=False, function=True, tryexcept=False, fromlist=True))
355 node = mf.findNode('pkg.function_conditional_existing')
356 self.assertIsInstance(node, modulegraph.SourceModule)
357 ed = mf.edgeData(script_node, node)
358 self.assertIsInstance(ed, modulegraph.DependencyInfo)
359 self.assertEqual(ed, modulegraph.DependencyInfo(conditional=True, function=True, tryexcept=False, fromlist=True))
361 node = mf.findNode('pkg.function_conditional_nonexisting')
362 self.assertIsInstance(node, modulegraph.MissingModule)
363 ed = mf.edgeData(script_node, node)
364 self.assertIsInstance(ed, modulegraph.DependencyInfo)
365 self.assertEqual(ed, modulegraph.DependencyInfo(conditional=True, function=True, tryexcept=False, fromlist=True))
367 node = mf.findNode('pkg.function_conditional_import_existing')
368 self.assertIsInstance(node, modulegraph.SourceModule)
369 ed = mf.edgeData(script_node, node)
370 self.assertIsInstance(ed, modulegraph.DependencyInfo)
371 self.assertEqual(ed, modulegraph.DependencyInfo(conditional=True, function=True, tryexcept=True, fromlist=True))
373 node = mf.findNode('pkg.function_conditional_import_nonexisting')
374 self.assertIsInstance(node, modulegraph.MissingModule)
375 ed = mf.edgeData(script_node, node)
376 self.assertIsInstance(ed, modulegraph.DependencyInfo)
377 self.assertEqual(ed, modulegraph.DependencyInfo(conditional=True, function=True, tryexcept=True, fromlist=True))
379 node = mf.findNode('pkg.function_conditional_import2_existing')
380 self.assertIsInstance(node, modulegraph.SourceModule)
381 ed = mf.edgeData(script_node, node)
382 self.assertIsInstance(ed, modulegraph.DependencyInfo)
383 self.assertEqual(ed, modulegraph.DependencyInfo(conditional=True, function=True, tryexcept=True, fromlist=True))
385 node = mf.findNode('pkg.function_conditional_import2_nonexisting')
386 self.assertIsInstance(node, modulegraph.MissingModule)
387 ed = mf.edgeData(script_node, node)
388 self.assertIsInstance(ed, modulegraph.DependencyInfo)
389 self.assertEqual(ed, modulegraph.DependencyInfo(conditional=True, function=True, tryexcept=True, fromlist=True))
391 node = mf.findNode('pkg.function_import_existing')
392 self.assertIsInstance(node, modulegraph.SourceModule)
393 ed = mf.edgeData(script_node, node)
394 self.assertIsInstance(ed, modulegraph.DependencyInfo)
395 self.assertEqual(ed, modulegraph.DependencyInfo(conditional=False, function=True, tryexcept=True, fromlist=True))
397 node = mf.findNode('pkg.function_import_nonexisting')
398 self.assertIsInstance(node, modulegraph.MissingModule)
399 ed = mf.edgeData(script_node, node)
400 self.assertIsInstance(ed, modulegraph.DependencyInfo)
401 self.assertEqual(ed, modulegraph.DependencyInfo(conditional=False, function=True, tryexcept=True, fromlist=True))
403 node = mf.findNode('pkg.function_import2_existing')
404 self.assertIsInstance(node, modulegraph.SourceModule)
405 ed = mf.edgeData(script_node, node)
406 self.assertIsInstance(ed, modulegraph.DependencyInfo)
407 self.assertEqual(ed, modulegraph.DependencyInfo(conditional=False, function=True, tryexcept=True, fromlist=True))
409 node = mf.findNode('pkg.function_import2_nonexisting')
410 self.assertIsInstance(node, modulegraph.MissingModule)
411 ed = mf.edgeData(script_node, node)
412 self.assertIsInstance(ed, modulegraph.DependencyInfo)
413 self.assertEqual(ed, modulegraph.DependencyInfo(conditional=False, function=True, tryexcept=True, fromlist=True))
416 if __name__ == "__main__":
417 unittest.main()