[Telemetry] Use ModuleGraph instead of modulefinder to get a detailed import
[chromium-blink-merge.git] / tools / telemetry / third_party / modulegraph / modulegraph_tests / test_setuptools_nspkg.py
blob87f845c89ddc8e075e6d38d6b187cd2c32c38ff1
1 """
2 Tests that deal with setuptools namespace
3 packages, and in particular the installation
4 flavour used by pip
5 """
6 import os
7 import shutil
8 import sys
9 import subprocess
10 import unittest
11 import textwrap
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]
28 exit = p.wait()
29 return exit
32 class TestPythonBehaviour (unittest.TestCase):
33 def setUp(self):
34 test_dir = os.path.join(gRootDir, 'test.dir')
35 if os.path.exists(test_dir):
36 shutil.rmtree(test_dir)
38 os.mkdir(test_dir)
39 exit = install_testpkg(test_dir)
40 self.assertEqual(exit, 0)
42 def tearDown(self):
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')
49 if '.' in name:
50 script = textwrap.dedent("""\
51 import site
52 site.addsitedir(%r)
53 try:
54 import %s
55 except ImportError:
56 import %s
57 print (%s.__name__)
58 """) %(test_dir, name, name.rsplit('.', 1)[0], name)
59 else:
60 script = textwrap.dedent("""\
61 import site
62 site.addsitedir(%r)
63 import %s
64 print (%s.__name__)
65 """) %(test_dir, name, name)
67 p = subprocess.Popen([sys.executable, '-c', script],
68 stdout=subprocess.PIPE,
69 stderr=subprocess.STDOUT,
70 cwd=os.path.join(
71 os.path.dirname(os.path.abspath(__file__)),
72 'testpkg-relimport'),
74 data = p.communicate()[0]
75 if sys.version_info[0] != 2:
76 data = data.decode('UTF-8')
77 data = data.strip()
78 if data.endswith(' refs]'):
79 data = data.rsplit('\n', 1)[0].strip()
81 sts = p.wait()
83 if sts != 0:
84 print (data)
85 self.fail("import of %r failed"%(name,))
87 return data
89 def testToplevel(self):
90 m = self.importModule('nspkg.module')
91 self.assertEqual(m, 'nspkg.module')
93 def testSub(self):
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)
103 def setUp(self):
104 test_dir = os.path.join(gRootDir, 'test.dir')
105 if os.path.exists(test_dir):
106 shutil.rmtree(test_dir)
108 os.mkdir(test_dir)
109 exit = install_testpkg(test_dir)
110 self.assertEqual(exit, 0)
112 self.mf = modulegraph.ModuleGraph(path=[ test_dir ] + sys.path)
114 def tearDown(self):
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__":
147 unittest.main()