2 Tests that deal with setuptools namespace
3 packages, and in particular the installation
13 from modulegraph
import modulegraph
15 gRootDir
= os
.path
.dirname(os
.path
.abspath(__file__
))
16 gSrcDir
= os
.path
.join(gRootDir
, 'testpkg-setuptools-namespace')
18 def install_testpkg(test_dir
):
19 p
= subprocess
.Popen([
20 sys
.executable
, 'setup.py', 'install',
21 '--install-lib', test_dir
,
22 '--single-version-externally-managed',
23 '--record', os
.path
.join(test_dir
, 'record.lst'),
24 ], cwd
=gSrcDir
, stdout
=subprocess
.PIPE
, stderr
=subprocess
.STDOUT
)
26 data
= p
.communicate()[0]
32 class TestPythonBehaviour (unittest
.TestCase
):
34 test_dir
= os
.path
.join(gRootDir
, 'test.dir')
35 if os
.path
.exists(test_dir
):
36 shutil
.rmtree(test_dir
)
39 exit
= install_testpkg(test_dir
)
40 self
.assertEqual(exit
, 0)
43 test_dir
= os
.path
.join(gRootDir
, 'test.dir')
44 if os
.path
.exists(test_dir
):
45 shutil
.rmtree(test_dir
)
47 def importModule(self
, name
):
48 test_dir
= os
.path
.join(gRootDir
, 'test.dir')
50 script
= textwrap
.dedent("""\
58 """) %(test_dir
, name
, name
.rsplit('.', 1)[0], name
)
60 script
= textwrap
.dedent("""\
65 """) %(test_dir
, name
, name
)
67 p
= subprocess
.Popen([sys
.executable
, '-c', script
],
68 stdout
=subprocess
.PIPE
,
69 stderr
=subprocess
.STDOUT
,
71 os
.path
.dirname(os
.path
.abspath(__file__
)),
74 data
= p
.communicate()[0]
75 if sys
.version_info
[0] != 2:
76 data
= data
.decode('UTF-8')
78 if data
.endswith(' refs]'):
79 data
= data
.rsplit('\n', 1)[0].strip()
85 self
.fail("import of %r failed"%(name
,))
89 def testToplevel(self
):
90 m
= self
.importModule('nspkg.module')
91 self
.assertEqual(m
, 'nspkg.module')
94 m
= self
.importModule('nspkg.nssubpkg.sub')
95 self
.assertEqual(m
, 'nspkg.nssubpkg.sub')
97 class TestModuleGraphImport (unittest
.TestCase
):
98 if not hasattr(unittest
.TestCase
, 'assertIsInstance'):
99 def assertIsInstance(self
, value
, types
):
100 if not isinstance(value
, types
):
101 self
.fail("%r is not an instance of %r", value
, types
)
104 test_dir
= os
.path
.join(gRootDir
, 'test.dir')
105 if os
.path
.exists(test_dir
):
106 shutil
.rmtree(test_dir
)
109 exit
= install_testpkg(test_dir
)
110 self
.assertEqual(exit
, 0)
112 self
.mf
= modulegraph
.ModuleGraph(path
=[ test_dir
] + sys
.path
)
115 test_dir
= os
.path
.join(gRootDir
, 'test.dir')
116 if os
.path
.exists(test_dir
):
117 shutil
.rmtree(test_dir
)
119 def testRootPkg(self
):
120 self
.mf
.import_hook('nspkg')
122 node
= self
.mf
.findNode('nspkg')
123 self
.assertIsInstance(node
, modulegraph
.NamespacePackage
)
124 self
.assertEqual(node
.identifier
, 'nspkg')
125 self
.assertEqual(node
.filename
, '-')
127 def testRootPkgModule(self
):
128 self
.mf
.import_hook('nspkg.module')
130 node
= self
.mf
.findNode('nspkg.module')
131 self
.assertIsInstance(node
, modulegraph
.SourceModule
)
132 self
.assertEqual(node
.identifier
, 'nspkg.module')
134 def testSubRootPkgModule(self
):
135 self
.mf
.import_hook('nspkg.nssubpkg.sub')
137 node
= self
.mf
.findNode('nspkg.nssubpkg.sub')
138 self
.assertIsInstance(node
, modulegraph
.SourceModule
)
139 self
.assertEqual(node
.identifier
, 'nspkg.nssubpkg.sub')
142 node
= self
.mf
.findNode('nspkg')
143 self
.assertIsInstance(node
, modulegraph
.NamespacePackage
)
146 if __name__
== "__main__":