| OLD | NEW |
| (Empty) |
| 1 """ | |
| 2 Tests that deal with pep420 namespace packages. | |
| 3 | |
| 4 PEP 420 is new in Python 3.3 | |
| 5 """ | |
| 6 import os | |
| 7 import shutil | |
| 8 import sys | |
| 9 import subprocess | |
| 10 import textwrap | |
| 11 | |
| 12 if sys.version_info[:2] <= (2,6): | |
| 13 import unittest2 as unittest | |
| 14 else: | |
| 15 import unittest | |
| 16 | |
| 17 from modulegraph import modulegraph | |
| 18 | |
| 19 gRootDir = os.path.dirname(os.path.abspath(__file__)) | |
| 20 gSrcDir = os.path.join(gRootDir, 'testpkg-pep420-namespace') | |
| 21 | |
| 22 if sys.version_info[:2] >= (3,3): | |
| 23 | |
| 24 class TestPythonBehaviour (unittest.TestCase): | |
| 25 def importModule(self, name): | |
| 26 test_dir1 = os.path.join(gSrcDir, 'path1') | |
| 27 test_dir2 = os.path.join(gSrcDir, 'path2') | |
| 28 if '.' in name: | |
| 29 script = textwrap.dedent("""\ | |
| 30 import site | |
| 31 site.addsitedir(%r) | |
| 32 site.addsitedir(%r) | |
| 33 try: | |
| 34 import %s | |
| 35 except ImportError: | |
| 36 import %s | |
| 37 print (%s.__name__) | |
| 38 """) %(test_dir1, test_dir2, name, name.rsplit('.', 1)[0], name) | |
| 39 else: | |
| 40 script = textwrap.dedent("""\ | |
| 41 import site | |
| 42 site.addsitedir(%r) | |
| 43 site.addsitedir(%r) | |
| 44 import %s | |
| 45 print (%s.__name__) | |
| 46 """) %(test_dir1, test_dir2, name, name) | |
| 47 | |
| 48 p = subprocess.Popen([sys.executable, '-c', script], | |
| 49 stdout=subprocess.PIPE, | |
| 50 stderr=subprocess.STDOUT, | |
| 51 cwd=os.path.join( | |
| 52 os.path.dirname(os.path.abspath(__file__)), | |
| 53 'testpkg-relimport'), | |
| 54 ) | |
| 55 data = p.communicate()[0] | |
| 56 if sys.version_info[0] != 2: | |
| 57 data = data.decode('UTF-8') | |
| 58 data = data.strip() | |
| 59 if data.endswith(' refs]'): | |
| 60 data = data.rsplit('\n', 1)[0].strip() | |
| 61 | |
| 62 sts = p.wait() | |
| 63 | |
| 64 if sts != 0: | |
| 65 print (data) | |
| 66 self.fail("import of %r failed"%(name,)) | |
| 67 | |
| 68 return data | |
| 69 | |
| 70 def testToplevel(self): | |
| 71 m = self.importModule('package.sub1') | |
| 72 self.assertEqual(m, 'package.sub1') | |
| 73 | |
| 74 m = self.importModule('package.sub2') | |
| 75 self.assertEqual(m, 'package.sub2') | |
| 76 | |
| 77 def testSub(self): | |
| 78 m = self.importModule('package.subpackage.sub') | |
| 79 self.assertEqual(m, 'package.subpackage.sub') | |
| 80 | |
| 81 m = self.importModule('package.nspkg.mod') | |
| 82 self.assertEqual(m, 'package.nspkg.mod') | |
| 83 | |
| 84 class TestModuleGraphImport (unittest.TestCase): | |
| 85 if not hasattr(unittest.TestCase, 'assertIsInstance'): | |
| 86 def assertIsInstance(self, value, types): | |
| 87 if not isinstance(value, types): | |
| 88 self.fail("%r is not an instance of %r", value, types) | |
| 89 | |
| 90 def setUp(self): | |
| 91 self.mf = modulegraph.ModuleGraph(path=[ | |
| 92 os.path.join(gSrcDir, 'path1'), | |
| 93 os.path.join(gSrcDir, 'path2'), | |
| 94 ] + sys.path) | |
| 95 | |
| 96 | |
| 97 def testRootPkg(self): | |
| 98 self.mf.import_hook('package') | |
| 99 | |
| 100 node = self.mf.findNode('package') | |
| 101 self.assertIsInstance(node, modulegraph.NamespacePackage) | |
| 102 self.assertEqual(node.identifier, 'package') | |
| 103 self.assertEqual(node.filename, '-') | |
| 104 | |
| 105 def testRootPkgModule(self): | |
| 106 self.mf.import_hook('package.sub1') | |
| 107 | |
| 108 node = self.mf.findNode('package.sub1') | |
| 109 self.assertIsInstance(node, modulegraph.SourceModule) | |
| 110 self.assertEqual(node.identifier, 'package.sub1') | |
| 111 | |
| 112 self.mf.import_hook('package.sub2') | |
| 113 node = self.mf.findNode('package.sub2') | |
| 114 self.assertIsInstance(node, modulegraph.SourceModule) | |
| 115 self.assertEqual(node.identifier, 'package.sub2') | |
| 116 | |
| 117 def testSubRootPkgModule(self): | |
| 118 self.mf.import_hook('package.subpackage.sub') | |
| 119 | |
| 120 node = self.mf.findNode('package.subpackage.sub') | |
| 121 self.assertIsInstance(node, modulegraph.SourceModule) | |
| 122 self.assertEqual(node.identifier, 'package.subpackage.sub') | |
| 123 | |
| 124 node = self.mf.findNode('package') | |
| 125 self.assertIsInstance(node, modulegraph.NamespacePackage) | |
| 126 | |
| 127 self.mf.import_hook('package.nspkg.mod') | |
| 128 node = self.mf.findNode('package.nspkg.mod') | |
| 129 self.assertIsInstance(node, modulegraph.SourceModule) | |
| 130 self.assertEqual(node.identifier, 'package.nspkg.mod') | |
| 131 | |
| 132 else: | |
| 133 # Check that PEP 420 is not implemented in python 3.2 and earlier | |
| 134 # (and that modulegraph also doesn't do this) | |
| 135 | |
| 136 class TestPythonBehaviour (unittest.TestCase): | |
| 137 def importModule(self, name): | |
| 138 test_dir1 = os.path.join(gSrcDir, 'path1') | |
| 139 test_dir2 = os.path.join(gSrcDir, 'path2') | |
| 140 if '.' in name: | |
| 141 script = textwrap.dedent("""\ | |
| 142 import site | |
| 143 site.addsitedir(%r) | |
| 144 site.addsitedir(%r) | |
| 145 try: | |
| 146 import %s | |
| 147 except ImportError: | |
| 148 import %s | |
| 149 print (%s.__name__) | |
| 150 """) %(test_dir1, test_dir2, name, name.rsplit('.', 1)[0], name) | |
| 151 else: | |
| 152 script = textwrap.dedent("""\ | |
| 153 import site | |
| 154 site.addsitedir(%r) | |
| 155 site.addsitedir(%r) | |
| 156 import %s | |
| 157 print (%s.__name__) | |
| 158 """) %(test_dir1, test_dir2, name, name) | |
| 159 | |
| 160 p = subprocess.Popen([sys.executable, '-c', script], | |
| 161 stdout=subprocess.PIPE, | |
| 162 stderr=subprocess.STDOUT, | |
| 163 cwd=os.path.join( | |
| 164 os.path.dirname(os.path.abspath(__file__)), | |
| 165 'testpkg-relimport'), | |
| 166 ) | |
| 167 data = p.communicate()[0] | |
| 168 if sys.version_info[0] != 2: | |
| 169 data = data.decode('UTF-8') | |
| 170 data = data.strip() | |
| 171 if data.endswith(' refs]'): | |
| 172 data = data.rsplit('\n', 1)[0].strip() | |
| 173 | |
| 174 sts = p.wait() | |
| 175 | |
| 176 if sts != 0: | |
| 177 raise ImportError(name) | |
| 178 | |
| 179 return data | |
| 180 | |
| 181 def testToplevel(self): | |
| 182 m = self.importModule('sys') | |
| 183 self.assertEqual(m, 'sys') | |
| 184 | |
| 185 self.assertRaises(ImportError, self.importModule, 'package.sub1') | |
| 186 self.assertRaises(ImportError, self.importModule, 'package.sub2') | |
| 187 | |
| 188 def testSub(self): | |
| 189 self.assertRaises(ImportError, self.importModule, 'package.subpackag
e.sub') | |
| 190 | |
| 191 class TestModuleGraphImport (unittest.TestCase): | |
| 192 if not hasattr(unittest.TestCase, 'assertIsInstance'): | |
| 193 def assertIsInstance(self, value, types): | |
| 194 if not isinstance(value, types): | |
| 195 self.fail("%r is not an instance of %r", value, types) | |
| 196 | |
| 197 def setUp(self): | |
| 198 self.mf = modulegraph.ModuleGraph(path=[ | |
| 199 os.path.join(gSrcDir, 'path1'), | |
| 200 os.path.join(gSrcDir, 'path2'), | |
| 201 ] + sys.path) | |
| 202 | |
| 203 | |
| 204 def testRootPkg(self): | |
| 205 self.assertRaises(ImportError, self.mf.import_hook, 'package') | |
| 206 | |
| 207 node = self.mf.findNode('package') | |
| 208 self.assertIs(node, None) | |
| 209 | |
| 210 def testRootPkgModule(self): | |
| 211 self.assertRaises(ImportError, self.mf.import_hook, 'package.sub1') | |
| 212 | |
| 213 node = self.mf.findNode('package.sub1') | |
| 214 self.assertIs(node, None) | |
| 215 | |
| 216 node = self.mf.findNode('package.sub2') | |
| 217 self.assertIs(node, None) | |
| 218 | |
| 219 if __name__ == "__main__": | |
| 220 unittest.main() | |
| OLD | NEW |