2 Test for import machinery
9 from modulegraph
import modulegraph
11 class TestNativeImport (unittest
.TestCase
):
12 # The tests check that Python's import statement
13 # works as these tests expect.
15 def importModule(self
, name
):
17 script
= textwrap
.dedent("""\
23 """) %(name
, name
.rsplit('.', 1)[0], name
)
25 script
= textwrap
.dedent("""\
30 p
= subprocess
.Popen([sys
.executable
, '-c', script
],
31 stdout
=subprocess
.PIPE
,
32 stderr
=subprocess
.STDOUT
,
34 os
.path
.dirname(os
.path
.abspath(__file__
)),
37 data
= p
.communicate()[0]
38 if sys
.version_info
[0] != 2:
39 data
= data
.decode('UTF-8')
42 if data
.endswith(' refs]'):
43 # with --with-pydebug builds
44 data
= data
.rsplit('\n', 1)[0].strip()
50 self
.assertEqual(sts
, 0)
54 def testRootModule(self
):
55 m
= self
.importModule('mod')
56 self
.assertEqual(m
, 'mod')
58 def testRootPkg(self
):
59 m
= self
.importModule('pkg')
60 self
.assertEqual(m
, 'pkg')
62 def testSubModule(self
):
63 m
= self
.importModule('pkg.mod')
64 self
.assertEqual(m
, 'pkg.mod')
66 if sys
.version_info
[0] == 2:
67 def testOldStyle(self
):
68 m
= self
.importModule('pkg.oldstyle.mod')
69 self
.assertEqual(m
, 'pkg.mod')
71 # python3 always has __future__.absolute_import
72 def testOldStyle(self
):
73 m
= self
.importModule('pkg.oldstyle.mod')
74 self
.assertEqual(m
, 'mod')
76 def testNewStyle(self
):
77 m
= self
.importModule('pkg.toplevel.mod')
78 self
.assertEqual(m
, 'mod')
80 def testRelativeImport(self
):
81 m
= self
.importModule('pkg.relative.mod')
82 self
.assertEqual(m
, 'pkg.mod')
84 m
= self
.importModule('pkg.subpkg.relative.mod')
85 self
.assertEqual(m
, 'pkg.mod')
87 m
= self
.importModule('pkg.subpkg.mod2.mod')
88 self
.assertEqual(m
, 'pkg.sub2.mod')
90 m
= self
.importModule('pkg.subpkg.relative2')
91 self
.assertEqual(m
, 'pkg.subpkg.relative2')
93 class TestModuleGraphImport (unittest
.TestCase
):
94 if not hasattr(unittest
.TestCase
, 'assertIsInstance'):
95 def assertIsInstance(self
, value
, types
):
96 if not isinstance(value
, types
):
97 self
.fail("%r is not an instance of %r"%(value
, types
))
101 os
.path
.dirname(os
.path
.abspath(__file__
)),
103 self
.mf
= modulegraph
.ModuleGraph(path
=[ root
] + sys
.path
)
105 self
.script_name
= os
.path
.join(root
, 'script.py')
106 self
.mf
.run_script(self
.script_name
)
108 def testGraphStructure(self
):
110 # 1. Script to imported modules
111 n
= self
.mf
.findNode(self
.script_name
)
112 self
.assertIsInstance(n
, modulegraph
.Script
)
114 imported
= ('mod', 'pkg', 'pkg.mod', 'pkg.oldstyle',
115 'pkg.relative', 'pkg.toplevel', 'pkg.subpkg.relative',
116 'pkg.subpkg.relative2', 'pkg.subpkg.mod2')
119 n2
= self
.mf
.findNode(nm
)
120 ed
= self
.mf
.edgeData(n
, n2
)
121 self
.assertIsInstance(ed
, modulegraph
.DependencyInfo
)
122 self
.assertEqual(ed
, modulegraph
.DependencyInfo(
123 fromlist
=False, conditional
=False, function
=False, tryexcept
=False))
125 refs
= self
.mf
.getReferences(n
)
126 self
.assertEqual(set(refs
), set(self
.mf
.findNode(nm
) for nm
in imported
))
128 refs
= list(self
.mf
.getReferers(n
))
129 # The script is a toplevel item and is therefore referred to from the graph root (aka 'None')
130 self
.assertEqual(refs
, [None])
134 n
= self
.mf
.findNode('mod')
135 self
.assertIsInstance(n
, modulegraph
.SourceModule
)
136 refs
= list(self
.mf
.getReferences(n
))
137 self
.assertEqual(refs
, [])
139 #refs = list(self.mf.getReferers(n))
140 #self.assertEquals(refs, [])
143 n
= self
.mf
.findNode('pkg')
144 self
.assertIsInstance(n
, modulegraph
.Package
)
145 refs
= list(self
.mf
.getReferences(n
))
147 self
.assertEqual(refs
, [n
])
149 #refs = list(self.mf.getReferers(n))
150 #self.assertEquals(refs, [])
153 n
= self
.mf
.findNode('pkg.mod')
154 self
.assertIsInstance(n
, modulegraph
.SourceModule
)
155 refs
= set(self
.mf
.getReferences(n
))
156 self
.assertEqual(refs
, set([self
.mf
.findNode('pkg')]))
157 ed
= self
.mf
.edgeData(n
, self
.mf
.findNode('pkg'))
158 self
.assertIsInstance(ed
, modulegraph
.DependencyInfo
)
159 self
.assertEqual(ed
, modulegraph
.DependencyInfo(
160 fromlist
=False, conditional
=False, function
=False, tryexcept
=False))
164 n
= self
.mf
.findNode('pkg.oldstyle')
165 self
.assertIsInstance(n
, modulegraph
.SourceModule
)
166 refs
= set(self
.mf
.getReferences(n
))
167 if sys
.version_info
[0] == 2:
168 n2
= self
.mf
.findNode('pkg.mod')
170 n2
= self
.mf
.findNode('mod')
171 self
.assertEqual(refs
, set([self
.mf
.findNode('pkg'), n2
]))
172 ed
= self
.mf
.edgeData(n
, n2
)
173 self
.assertIsInstance(ed
, modulegraph
.DependencyInfo
)
174 self
.assertEqual(ed
, modulegraph
.DependencyInfo(
175 fromlist
=False, conditional
=False, function
=False, tryexcept
=False))
179 n
= self
.mf
.findNode('pkg.relative')
180 self
.assertIsInstance(n
, modulegraph
.SourceModule
)
181 refs
= set(self
.mf
.getReferences(n
))
182 self
.assertEqual(refs
, set([self
.mf
.findNode('__future__'), self
.mf
.findNode('pkg'), self
.mf
.findNode('pkg.mod')]))
184 ed
= self
.mf
.edgeData(n
, self
.mf
.findNode('pkg.mod'))
185 self
.assertIsInstance(ed
, modulegraph
.DependencyInfo
)
186 self
.assertEqual(ed
, modulegraph
.DependencyInfo(
187 fromlist
=True, conditional
=False, function
=False, tryexcept
=False))
189 ed
= self
.mf
.edgeData(n
, self
.mf
.findNode('__future__'))
190 self
.assertIsInstance(ed
, modulegraph
.DependencyInfo
)
191 self
.assertEqual(ed
, modulegraph
.DependencyInfo(
192 fromlist
=False, conditional
=False, function
=False, tryexcept
=False))
194 #ed = self.mf.edgeData(n, self.mf.findNode('__future__.absolute_import'))
195 #self.assertIsInstance(ed, modulegraph.DependencyInfo)
196 #self.assertEqual(ed, modulegraph.DependencyInfo(
197 #fromlist=True, conditional=False, function=False, tryexcept=False))
200 n
= self
.mf
.findNode('pkg.toplevel')
201 self
.assertIsInstance(n
, modulegraph
.SourceModule
)
202 refs
= set(self
.mf
.getReferences(n
))
203 self
.assertEqual(refs
, set([self
.mf
.findNode('__future__'), self
.mf
.findNode('pkg'), self
.mf
.findNode('mod')]))
205 ed
= self
.mf
.edgeData(n
, self
.mf
.findNode('mod'))
206 self
.assertIsInstance(ed
, modulegraph
.DependencyInfo
)
207 self
.assertEqual(ed
, modulegraph
.DependencyInfo(
208 fromlist
=False, conditional
=False, function
=False, tryexcept
=False))
210 ed
= self
.mf
.edgeData(n
, self
.mf
.findNode('__future__'))
211 self
.assertIsInstance(ed
, modulegraph
.DependencyInfo
)
212 self
.assertEqual(ed
, modulegraph
.DependencyInfo(
213 fromlist
=False, conditional
=False, function
=False, tryexcept
=False))
215 #ed = self.mf.edgeData(n, self.mf.findNode('__future__.absolute_import'))
216 #self.assertIsInstance(ed, modulegraph.DependencyInfo)
217 #self.assertEqual(ed, modulegraph.DependencyInfo(
218 #fromlist=True, conditional=False, function=False, tryexcept=False))
221 n
= self
.mf
.findNode('pkg.subpkg')
222 self
.assertIsInstance(n
, modulegraph
.Package
)
223 refs
= set(self
.mf
.getReferences(n
))
224 self
.assertEqual(refs
, set([self
.mf
.findNode('pkg')]))
226 ed
= self
.mf
.edgeData(n
, self
.mf
.findNode('pkg'))
227 self
.assertIsInstance(ed
, modulegraph
.DependencyInfo
)
228 self
.assertEqual(ed
, modulegraph
.DependencyInfo(
229 fromlist
=False, conditional
=False, function
=False, tryexcept
=False))
231 # 9. pkg.subpkg.relative
232 n
= self
.mf
.findNode('pkg.subpkg.relative')
233 self
.assertIsInstance(n
, modulegraph
.SourceModule
)
234 refs
= set(self
.mf
.getReferences(n
))
235 self
.assertEqual(refs
, set([self
.mf
.findNode('__future__'), self
.mf
.findNode('pkg'), self
.mf
.findNode('pkg.subpkg'), self
.mf
.findNode('pkg.mod')]))
237 ed
= self
.mf
.edgeData(n
, self
.mf
.findNode('pkg.subpkg'))
238 self
.assertIsInstance(ed
, modulegraph
.DependencyInfo
)
239 self
.assertEqual(ed
, modulegraph
.DependencyInfo(
240 fromlist
=False, conditional
=False, function
=False, tryexcept
=False))
242 ed
= self
.mf
.edgeData(n
, self
.mf
.findNode('pkg.mod'))
243 self
.assertIsInstance(ed
, modulegraph
.DependencyInfo
)
244 self
.assertEqual(ed
, modulegraph
.DependencyInfo(
245 fromlist
=True, conditional
=False, function
=False, tryexcept
=False))
247 # 10. pkg.subpkg.relative2
248 n
= self
.mf
.findNode('pkg.subpkg.relative2')
249 self
.assertIsInstance(n
, modulegraph
.SourceModule
)
250 refs
= set(self
.mf
.getReferences(n
))
251 self
.assertEqual(refs
, set([self
.mf
.findNode('pkg.subpkg'), self
.mf
.findNode('pkg.relimport'), self
.mf
.findNode('__future__')]))
253 # 10. pkg.subpkg.mod2
254 n
= self
.mf
.findNode('pkg.subpkg.mod2')
255 self
.assertIsInstance(n
, modulegraph
.SourceModule
)
256 refs
= set(self
.mf
.getReferences(n
))
257 self
.assertEqual(refs
, set([
258 self
.mf
.findNode('__future__'),
259 self
.mf
.findNode('pkg.subpkg'),
260 self
.mf
.findNode('pkg.sub2.mod'),
261 self
.mf
.findNode('pkg.sub2'),
265 def testRootModule(self
):
266 node
= self
.mf
.findNode('mod')
267 self
.assertIsInstance(node
, modulegraph
.SourceModule
)
268 self
.assertEqual(node
.identifier
, 'mod')
270 def testRootPkg(self
):
271 node
= self
.mf
.findNode('pkg')
272 self
.assertIsInstance(node
, modulegraph
.Package
)
273 self
.assertEqual(node
.identifier
, 'pkg')
275 def testSubModule(self
):
276 node
= self
.mf
.findNode('pkg.mod')
277 self
.assertIsInstance(node
, modulegraph
.SourceModule
)
278 self
.assertEqual(node
.identifier
, 'pkg.mod')
280 if sys
.version_info
[0] == 2:
281 def testOldStyle(self
):
282 node
= self
.mf
.findNode('pkg.oldstyle')
283 self
.assertIsInstance(node
, modulegraph
.SourceModule
)
284 self
.assertEqual(node
.identifier
, 'pkg.oldstyle')
285 sub
= [ n
for n
in self
.mf
.get_edges(node
)[0] if n
.identifier
!= '__future__' ][0]
286 self
.assertEqual(sub
.identifier
, 'pkg.mod')
288 # python3 always has __future__.absolute_import
289 def testOldStyle(self
):
290 node
= self
.mf
.findNode('pkg.oldstyle')
291 self
.assertIsInstance(node
, modulegraph
.SourceModule
)
292 self
.assertEqual(node
.identifier
, 'pkg.oldstyle')
293 sub
= [ n
for n
in self
.mf
.get_edges(node
)[0] if n
.identifier
!= '__future__' ][0]
294 self
.assertEqual(sub
.identifier
, 'mod')
296 def testNewStyle(self
):
297 node
= self
.mf
.findNode('pkg.toplevel')
298 self
.assertIsInstance(node
, modulegraph
.SourceModule
)
299 self
.assertEqual(node
.identifier
, 'pkg.toplevel')
300 sub
= [ n
for n
in self
.mf
.get_edges(node
)[0] if not n
.identifier
.startswith('__future__')][0]
301 self
.assertEqual(sub
.identifier
, 'mod')
303 def testRelativeImport(self
):
304 node
= self
.mf
.findNode('pkg.relative')
305 self
.assertIsInstance(node
, modulegraph
.SourceModule
)
306 self
.assertEqual(node
.identifier
, 'pkg.relative')
307 sub
= [ n
for n
in self
.mf
.get_edges(node
)[0] if not n
.identifier
.startswith('__future__') ][0]
308 self
.assertIsInstance(sub
, modulegraph
.Package
)
309 self
.assertEqual(sub
.identifier
, 'pkg')
311 node
= self
.mf
.findNode('pkg.subpkg.relative')
312 self
.assertIsInstance(node
, modulegraph
.SourceModule
)
313 self
.assertEqual(node
.identifier
, 'pkg.subpkg.relative')
314 sub
= [ n
for n
in self
.mf
.get_edges(node
)[0] if not n
.identifier
.startswith('__future__') ][0]
315 self
.assertIsInstance(sub
, modulegraph
.Package
)
316 self
.assertEqual(sub
.identifier
, 'pkg')
318 node
= self
.mf
.findNode('pkg.subpkg.mod2')
319 self
.assertIsInstance(node
, modulegraph
.SourceModule
)
320 self
.assertEqual(node
.identifier
, 'pkg.subpkg.mod2')
321 sub
= [ n
for n
in self
.mf
.get_edges(node
)[0] if not n
.identifier
.startswith('__future__') ][0]
322 self
.assertIsInstance(sub
, modulegraph
.SourceModule
)
323 self
.assertEqual(sub
.identifier
, 'pkg.sub2.mod')
325 node
= self
.mf
.findNode('pkg.subpkg.relative2')
326 self
.assertIsInstance(node
, modulegraph
.SourceModule
)
327 self
.assertEqual(node
.identifier
, 'pkg.subpkg.relative2')
329 node
= self
.mf
.findNode('pkg.relimport')
330 self
.assertIsInstance(node
, modulegraph
.SourceModule
)
332 class TestRegressions1 (unittest
.TestCase
):
333 if not hasattr(unittest
.TestCase
, 'assertIsInstance'):
334 def assertIsInstance(self
, value
, types
):
335 if not isinstance(value
, types
):
336 self
.fail("%r is not an instance of %r", value
, types
)
340 os
.path
.dirname(os
.path
.abspath(__file__
)),
342 self
.mf
= modulegraph
.ModuleGraph(path
=[ root
] + sys
.path
)
343 self
.mf
.run_script(os
.path
.join(root
, 'main_script.py'))
346 node
= self
.mf
.findNode('pkg.a')
347 self
.assertIsInstance(node
, modulegraph
.SourceModule
)
348 node
= self
.mf
.findNode('pkg.b')
349 self
.assertIsInstance(node
, modulegraph
.SourceModule
)
352 def testMissingPathEntry(self
):
354 os
.path
.dirname(os
.path
.abspath(__file__
)),
357 mf
= modulegraph
.ModuleGraph(path
=[ root
] + sys
.path
)
359 self
.fail('modulegraph initialiser raises os.error')
361 class TestRegressions2 (unittest
.TestCase
):
362 if not hasattr(unittest
.TestCase
, 'assertIsInstance'):
363 def assertIsInstance(self
, value
, types
):
364 if not isinstance(value
, types
):
365 self
.fail("%r is not an instance of %r"%(value
, types
))
369 os
.path
.dirname(os
.path
.abspath(__file__
)),
371 self
.mf
= modulegraph
.ModuleGraph(path
=[ root
] + sys
.path
)
372 self
.mf
.run_script(os
.path
.join(root
, 'main_script.py'))
375 node
= self
.mf
.findNode('pkg.base')
376 self
.assertIsInstance(node
, modulegraph
.SourceModule
)
377 node
= self
.mf
.findNode('pkg.pkg')
378 self
.assertIsInstance(node
, modulegraph
.SourceModule
)
380 class TestRegressions3 (unittest
.TestCase
):
381 if not hasattr(unittest
.TestCase
, 'assertIsInstance'):
382 def assertIsInstance(self
, value
, types
):
383 if not isinstance(value
, types
):
384 self
.fail("%r is not an instance of %r"%(value
, types
))
386 def assertStartswith(self
, value
, test
):
387 if not value
.startswith(test
):
388 self
.fail("%r does not start with %r"%(value
, test
))
392 os
.path
.dirname(os
.path
.abspath(__file__
)),
394 self
.mf
= modulegraph
.ModuleGraph(path
=[ root
] + sys
.path
)
395 self
.mf
.run_script(os
.path
.join(root
, 'script.py'))
398 node
= self
.mf
.findNode('mypkg.distutils')
399 self
.assertIsInstance(node
, modulegraph
.Package
)
400 node
= self
.mf
.findNode('mypkg.distutils.ccompiler')
401 self
.assertIsInstance(node
, modulegraph
.SourceModule
)
402 self
.assertStartswith(node
.filename
, os
.path
.dirname(__file__
))
404 import distutils
.sysconfig
, distutils
.ccompiler
405 node
= self
.mf
.findNode('distutils.ccompiler')
406 self
.assertIsInstance(node
, modulegraph
.SourceModule
)
407 self
.assertEqual(os
.path
.dirname(node
.filename
),
408 os
.path
.dirname(distutils
.ccompiler
.__file
__))
410 node
= self
.mf
.findNode('distutils.sysconfig')
411 self
.assertIsInstance(node
, modulegraph
.SourceModule
)
412 self
.assertEqual(os
.path
.dirname(node
.filename
),
413 os
.path
.dirname(distutils
.sysconfig
.__file
__))
415 class TestRegression4 (unittest
.TestCase
):
416 if not hasattr(unittest
.TestCase
, 'assertIsInstance'):
417 def assertIsInstance(self
, value
, types
):
418 if not isinstance(value
, types
):
419 self
.fail("%r is not an instance of %r"%(value
, types
))
423 os
.path
.dirname(os
.path
.abspath(__file__
)),
425 self
.mf
= modulegraph
.ModuleGraph(path
=[ root
] + sys
.path
)
426 self
.mf
.run_script(os
.path
.join(root
, 'script.py'))
429 node
= self
.mf
.findNode('pkg.core')
430 self
.assertIsInstance(node
, modulegraph
.Package
)
432 node
= self
.mf
.findNode('pkg.core.callables')
433 self
.assertIsInstance(node
, modulegraph
.SourceModule
)
435 node
= self
.mf
.findNode('pkg.core.listener')
436 self
.assertIsInstance(node
, modulegraph
.SourceModule
)
438 node
= self
.mf
.findNode('pkg.core.listenerimpl')
439 self
.assertIsInstance(node
, modulegraph
.SourceModule
)
441 class TestRegression5 (unittest
.TestCase
):
442 if not hasattr(unittest
.TestCase
, 'assertIsInstance'):
443 def assertIsInstance(self
, value
, types
):
444 if not isinstance(value
, types
):
445 self
.fail("%r is not an instance of %r"%(value
, types
))
449 os
.path
.dirname(os
.path
.abspath(__file__
)),
451 self
.mf
= modulegraph
.ModuleGraph(path
=[ root
] + sys
.path
)
452 self
.mf
.run_script(os
.path
.join(root
, 'script.py'))
455 node
= self
.mf
.findNode('distutils')
456 self
.assertIsInstance(node
, modulegraph
.Package
)
457 self
.assertIn('distutils/__init__', node
.filename
)
459 class TestDeeplyNested (unittest
.TestCase
):
462 os
.path
.dirname(os
.path
.abspath(__file__
)),
464 self
.mf
= modulegraph
.ModuleGraph(path
=[ root
] + sys
.path
)
465 self
.mf
.run_script(os
.path
.join(root
, 'script.py'))
468 node
= self
.mf
.findNode('os')
469 self
.assertTrue(node
is not None)
472 if __name__
== "__main__":