Christian Heimes | 05e8be1 | 2008-02-23 18:30:17 +0000 | [diff] [blame] | 1 | import os |
Éric Araujo | 1e3a68d | 2011-07-28 23:35:29 +0200 | [diff] [blame] | 2 | import errno |
Brett Cannon | 298bb96 | 2014-02-28 10:44:45 -0500 | [diff] [blame] | 3 | import importlib.machinery |
| 4 | import py_compile |
Éric Araujo | 1e3a68d | 2011-07-28 23:35:29 +0200 | [diff] [blame] | 5 | import shutil |
Guido van Rossum | fc2a0a8 | 2006-10-27 23:06:01 +0000 | [diff] [blame] | 6 | import unittest |
Guido van Rossum | fc2a0a8 | 2006-10-27 23:06:01 +0000 | [diff] [blame] | 7 | import tempfile |
| 8 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 9 | from test import support |
Guido van Rossum | fc2a0a8 | 2006-10-27 23:06:01 +0000 | [diff] [blame] | 10 | |
Guido van Rossum | fc2a0a8 | 2006-10-27 23:06:01 +0000 | [diff] [blame] | 11 | import modulefinder |
| 12 | |
Guido van Rossum | fc2a0a8 | 2006-10-27 23:06:01 +0000 | [diff] [blame] | 13 | TEST_DIR = tempfile.mkdtemp() |
Éric Araujo | 1e3a68d | 2011-07-28 23:35:29 +0200 | [diff] [blame] | 14 | TEST_PATH = [TEST_DIR, os.path.dirname(tempfile.__file__)] |
Guido van Rossum | fc2a0a8 | 2006-10-27 23:06:01 +0000 | [diff] [blame] | 15 | |
| 16 | # Each test description is a list of 5 items: |
| 17 | # |
| 18 | # 1. a module name that will be imported by modulefinder |
| 19 | # 2. a list of module names that modulefinder is required to find |
| 20 | # 3. a list of module names that modulefinder should complain |
| 21 | # about because they are not found |
| 22 | # 4. a list of module names that modulefinder should complain |
| 23 | # about because they MAY be not found |
| 24 | # 5. a string specifying packages to create; the format is obvious imo. |
| 25 | # |
| 26 | # Each package will be created in TEST_DIR, and TEST_DIR will be |
| 27 | # removed after the tests again. |
| 28 | # Modulefinder searches in a path that contains TEST_DIR, plus |
| 29 | # the standard Lib directory. |
| 30 | |
| 31 | maybe_test = [ |
| 32 | "a.module", |
| 33 | ["a", "a.module", "sys", |
| 34 | "b"], |
| 35 | ["c"], ["b.something"], |
| 36 | """\ |
| 37 | a/__init__.py |
| 38 | a/module.py |
| 39 | from b import something |
| 40 | from c import something |
| 41 | b/__init__.py |
| 42 | from sys import * |
| 43 | """] |
| 44 | |
| 45 | maybe_test_new = [ |
| 46 | "a.module", |
| 47 | ["a", "a.module", "sys", |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 48 | "b", "__future__"], |
Guido van Rossum | fc2a0a8 | 2006-10-27 23:06:01 +0000 | [diff] [blame] | 49 | ["c"], ["b.something"], |
| 50 | """\ |
| 51 | a/__init__.py |
| 52 | a/module.py |
| 53 | from b import something |
| 54 | from c import something |
| 55 | b/__init__.py |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 56 | from __future__ import absolute_import |
Guido van Rossum | fc2a0a8 | 2006-10-27 23:06:01 +0000 | [diff] [blame] | 57 | from sys import * |
| 58 | """] |
| 59 | |
| 60 | package_test = [ |
| 61 | "a.module", |
| 62 | ["a", "a.b", "a.c", "a.module", "mymodule", "sys"], |
| 63 | ["blahblah", "c"], [], |
| 64 | """\ |
| 65 | mymodule.py |
| 66 | a/__init__.py |
| 67 | import blahblah |
| 68 | from a import b |
| 69 | import c |
| 70 | a/module.py |
| 71 | import sys |
| 72 | from a import b as x |
| 73 | from a.c import sillyname |
| 74 | a/b.py |
| 75 | a/c.py |
| 76 | from a.module import x |
| 77 | import mymodule as sillyname |
| 78 | from sys import version_info |
| 79 | """] |
| 80 | |
| 81 | absolute_import_test = [ |
| 82 | "a.module", |
| 83 | ["a", "a.module", |
| 84 | "b", "b.x", "b.y", "b.z", |
Neal Norwitz | 2633c69 | 2007-02-26 22:22:47 +0000 | [diff] [blame] | 85 | "__future__", "sys", "gc"], |
Guido van Rossum | fc2a0a8 | 2006-10-27 23:06:01 +0000 | [diff] [blame] | 86 | ["blahblah", "z"], [], |
| 87 | """\ |
| 88 | mymodule.py |
| 89 | a/__init__.py |
| 90 | a/module.py |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 91 | from __future__ import absolute_import |
Guido van Rossum | fc2a0a8 | 2006-10-27 23:06:01 +0000 | [diff] [blame] | 92 | import sys # sys |
| 93 | import blahblah # fails |
Neal Norwitz | 2633c69 | 2007-02-26 22:22:47 +0000 | [diff] [blame] | 94 | import gc # gc |
Guido van Rossum | fc2a0a8 | 2006-10-27 23:06:01 +0000 | [diff] [blame] | 95 | import b.x # b.x |
| 96 | from b import y # b.y |
| 97 | from b.z import * # b.z.* |
Neal Norwitz | 2633c69 | 2007-02-26 22:22:47 +0000 | [diff] [blame] | 98 | a/gc.py |
Guido van Rossum | fc2a0a8 | 2006-10-27 23:06:01 +0000 | [diff] [blame] | 99 | a/sys.py |
| 100 | import mymodule |
| 101 | a/b/__init__.py |
| 102 | a/b/x.py |
| 103 | a/b/y.py |
| 104 | a/b/z.py |
| 105 | b/__init__.py |
| 106 | import z |
| 107 | b/unused.py |
| 108 | b/x.py |
| 109 | b/y.py |
| 110 | b/z.py |
| 111 | """] |
| 112 | |
| 113 | relative_import_test = [ |
| 114 | "a.module", |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 115 | ["__future__", |
| 116 | "a", "a.module", |
Guido van Rossum | fc2a0a8 | 2006-10-27 23:06:01 +0000 | [diff] [blame] | 117 | "a.b", "a.b.y", "a.b.z", |
| 118 | "a.b.c", "a.b.c.moduleC", |
| 119 | "a.b.c.d", "a.b.c.e", |
| 120 | "a.b.x", |
Neal Norwitz | 2633c69 | 2007-02-26 22:22:47 +0000 | [diff] [blame] | 121 | "gc"], |
Guido van Rossum | fc2a0a8 | 2006-10-27 23:06:01 +0000 | [diff] [blame] | 122 | [], [], |
| 123 | """\ |
| 124 | mymodule.py |
| 125 | a/__init__.py |
| 126 | from .b import y, z # a.b.y, a.b.z |
| 127 | a/module.py |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 128 | from __future__ import absolute_import # __future__ |
Neal Norwitz | 2633c69 | 2007-02-26 22:22:47 +0000 | [diff] [blame] | 129 | import gc # gc |
| 130 | a/gc.py |
Guido van Rossum | fc2a0a8 | 2006-10-27 23:06:01 +0000 | [diff] [blame] | 131 | a/sys.py |
| 132 | a/b/__init__.py |
| 133 | from ..b import x # a.b.x |
| 134 | #from a.b.c import moduleC |
| 135 | from .c import moduleC # a.b.moduleC |
| 136 | a/b/x.py |
| 137 | a/b/y.py |
| 138 | a/b/z.py |
| 139 | a/b/g.py |
| 140 | a/b/c/__init__.py |
| 141 | from ..c import e # a.b.c.e |
| 142 | a/b/c/moduleC.py |
| 143 | from ..c import d # a.b.c.d |
| 144 | a/b/c/d.py |
| 145 | a/b/c/e.py |
| 146 | a/b/c/x.py |
| 147 | """] |
| 148 | |
| 149 | relative_import_test_2 = [ |
| 150 | "a.module", |
| 151 | ["a", "a.module", |
| 152 | "a.sys", |
| 153 | "a.b", "a.b.y", "a.b.z", |
| 154 | "a.b.c", "a.b.c.d", |
| 155 | "a.b.c.e", |
| 156 | "a.b.c.moduleC", |
| 157 | "a.b.c.f", |
| 158 | "a.b.x", |
| 159 | "a.another"], |
| 160 | [], [], |
| 161 | """\ |
| 162 | mymodule.py |
| 163 | a/__init__.py |
| 164 | from . import sys # a.sys |
| 165 | a/another.py |
| 166 | a/module.py |
| 167 | from .b import y, z # a.b.y, a.b.z |
Neal Norwitz | 2633c69 | 2007-02-26 22:22:47 +0000 | [diff] [blame] | 168 | a/gc.py |
Guido van Rossum | fc2a0a8 | 2006-10-27 23:06:01 +0000 | [diff] [blame] | 169 | a/sys.py |
| 170 | a/b/__init__.py |
| 171 | from .c import moduleC # a.b.c.moduleC |
| 172 | from .c import d # a.b.c.d |
| 173 | a/b/x.py |
| 174 | a/b/y.py |
| 175 | a/b/z.py |
| 176 | a/b/c/__init__.py |
| 177 | from . import e # a.b.c.e |
| 178 | a/b/c/moduleC.py |
| 179 | # |
| 180 | from . import f # a.b.c.f |
| 181 | from .. import x # a.b.x |
| 182 | from ... import another # a.another |
| 183 | a/b/c/d.py |
| 184 | a/b/c/e.py |
| 185 | a/b/c/f.py |
| 186 | """] |
| 187 | |
Benjamin Peterson | c0747cf | 2008-11-03 20:31:38 +0000 | [diff] [blame] | 188 | relative_import_test_3 = [ |
| 189 | "a.module", |
| 190 | ["a", "a.module"], |
| 191 | ["a.bar"], |
| 192 | [], |
| 193 | """\ |
| 194 | a/__init__.py |
| 195 | def foo(): pass |
| 196 | a/module.py |
| 197 | from . import foo |
| 198 | from . import bar |
| 199 | """] |
| 200 | |
Brett Cannon | 73b969e | 2012-12-22 19:34:21 -0500 | [diff] [blame] | 201 | relative_import_test_4 = [ |
| 202 | "a.module", |
| 203 | ["a", "a.module"], |
| 204 | [], |
| 205 | [], |
| 206 | """\ |
| 207 | a/__init__.py |
| 208 | def foo(): pass |
| 209 | a/module.py |
| 210 | from . import * |
| 211 | """] |
| 212 | |
Brett Cannon | 298bb96 | 2014-02-28 10:44:45 -0500 | [diff] [blame] | 213 | bytecode_test = [ |
| 214 | "a", |
| 215 | ["a"], |
| 216 | [], |
| 217 | [], |
| 218 | "" |
| 219 | ] |
| 220 | |
Éric Araujo | 1e3a68d | 2011-07-28 23:35:29 +0200 | [diff] [blame] | 221 | |
Guido van Rossum | fc2a0a8 | 2006-10-27 23:06:01 +0000 | [diff] [blame] | 222 | def open_file(path): |
Guido van Rossum | fc2a0a8 | 2006-10-27 23:06:01 +0000 | [diff] [blame] | 223 | dirname = os.path.dirname(path) |
Éric Araujo | 1e3a68d | 2011-07-28 23:35:29 +0200 | [diff] [blame] | 224 | try: |
| 225 | os.makedirs(dirname) |
| 226 | except OSError as e: |
| 227 | if e.errno != errno.EEXIST: |
| 228 | raise |
Guido van Rossum | fc2a0a8 | 2006-10-27 23:06:01 +0000 | [diff] [blame] | 229 | return open(path, "w") |
| 230 | |
Éric Araujo | 1e3a68d | 2011-07-28 23:35:29 +0200 | [diff] [blame] | 231 | |
Guido van Rossum | fc2a0a8 | 2006-10-27 23:06:01 +0000 | [diff] [blame] | 232 | def create_package(source): |
| 233 | ofi = None |
Antoine Pitrou | 92f60ed | 2010-10-14 22:11:44 +0000 | [diff] [blame] | 234 | try: |
| 235 | for line in source.splitlines(): |
| 236 | if line.startswith(" ") or line.startswith("\t"): |
| 237 | ofi.write(line.strip() + "\n") |
| 238 | else: |
| 239 | if ofi: |
| 240 | ofi.close() |
| 241 | ofi = open_file(os.path.join(TEST_DIR, line.strip())) |
| 242 | finally: |
| 243 | if ofi: |
| 244 | ofi.close() |
Guido van Rossum | fc2a0a8 | 2006-10-27 23:06:01 +0000 | [diff] [blame] | 245 | |
Éric Araujo | 1e3a68d | 2011-07-28 23:35:29 +0200 | [diff] [blame] | 246 | |
Guido van Rossum | fc2a0a8 | 2006-10-27 23:06:01 +0000 | [diff] [blame] | 247 | class ModuleFinderTest(unittest.TestCase): |
Berker Peksag | 0a0d1da | 2014-07-07 14:58:12 +0300 | [diff] [blame] | 248 | def _do_test(self, info, report=False, debug=0, replace_paths=[]): |
Guido van Rossum | fc2a0a8 | 2006-10-27 23:06:01 +0000 | [diff] [blame] | 249 | import_this, modules, missing, maybe_missing, source = info |
| 250 | create_package(source) |
| 251 | try: |
Berker Peksag | 0a0d1da | 2014-07-07 14:58:12 +0300 | [diff] [blame] | 252 | mf = modulefinder.ModuleFinder(path=TEST_PATH, debug=debug, |
| 253 | replace_paths=replace_paths) |
Guido van Rossum | fc2a0a8 | 2006-10-27 23:06:01 +0000 | [diff] [blame] | 254 | mf.import_hook(import_this) |
| 255 | if report: |
| 256 | mf.report() |
| 257 | ## # This wouldn't work in general when executed several times: |
| 258 | ## opath = sys.path[:] |
| 259 | ## sys.path = TEST_PATH |
| 260 | ## try: |
| 261 | ## __import__(import_this) |
| 262 | ## except: |
| 263 | ## import traceback; traceback.print_exc() |
| 264 | ## sys.path = opath |
| 265 | ## return |
Éric Araujo | 1e3a68d | 2011-07-28 23:35:29 +0200 | [diff] [blame] | 266 | modules = sorted(set(modules)) |
| 267 | found = sorted(mf.modules) |
Guido van Rossum | fc2a0a8 | 2006-10-27 23:06:01 +0000 | [diff] [blame] | 268 | # check if we found what we expected, not more, not less |
Éric Araujo | 1e3a68d | 2011-07-28 23:35:29 +0200 | [diff] [blame] | 269 | self.assertEqual(found, modules) |
Guido van Rossum | fc2a0a8 | 2006-10-27 23:06:01 +0000 | [diff] [blame] | 270 | |
| 271 | # check for missing and maybe missing modules |
| 272 | bad, maybe = mf.any_missing_maybe() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 273 | self.assertEqual(bad, missing) |
| 274 | self.assertEqual(maybe, maybe_missing) |
Guido van Rossum | fc2a0a8 | 2006-10-27 23:06:01 +0000 | [diff] [blame] | 275 | finally: |
Éric Araujo | 1e3a68d | 2011-07-28 23:35:29 +0200 | [diff] [blame] | 276 | shutil.rmtree(TEST_DIR) |
Guido van Rossum | fc2a0a8 | 2006-10-27 23:06:01 +0000 | [diff] [blame] | 277 | |
| 278 | def test_package(self): |
| 279 | self._do_test(package_test) |
| 280 | |
| 281 | def test_maybe(self): |
| 282 | self._do_test(maybe_test) |
| 283 | |
Éric Araujo | 1e3a68d | 2011-07-28 23:35:29 +0200 | [diff] [blame] | 284 | def test_maybe_new(self): |
| 285 | self._do_test(maybe_test_new) |
Guido van Rossum | fc2a0a8 | 2006-10-27 23:06:01 +0000 | [diff] [blame] | 286 | |
Éric Araujo | 1e3a68d | 2011-07-28 23:35:29 +0200 | [diff] [blame] | 287 | def test_absolute_imports(self): |
| 288 | self._do_test(absolute_import_test) |
Guido van Rossum | fc2a0a8 | 2006-10-27 23:06:01 +0000 | [diff] [blame] | 289 | |
Éric Araujo | 1e3a68d | 2011-07-28 23:35:29 +0200 | [diff] [blame] | 290 | def test_relative_imports(self): |
| 291 | self._do_test(relative_import_test) |
Guido van Rossum | fc2a0a8 | 2006-10-27 23:06:01 +0000 | [diff] [blame] | 292 | |
Éric Araujo | 1e3a68d | 2011-07-28 23:35:29 +0200 | [diff] [blame] | 293 | def test_relative_imports_2(self): |
| 294 | self._do_test(relative_import_test_2) |
Guido van Rossum | fc2a0a8 | 2006-10-27 23:06:01 +0000 | [diff] [blame] | 295 | |
Éric Araujo | 1e3a68d | 2011-07-28 23:35:29 +0200 | [diff] [blame] | 296 | def test_relative_imports_3(self): |
| 297 | self._do_test(relative_import_test_3) |
Guido van Rossum | fc2a0a8 | 2006-10-27 23:06:01 +0000 | [diff] [blame] | 298 | |
Brett Cannon | 73b969e | 2012-12-22 19:34:21 -0500 | [diff] [blame] | 299 | def test_relative_imports_4(self): |
| 300 | self._do_test(relative_import_test_4) |
| 301 | |
Brett Cannon | 298bb96 | 2014-02-28 10:44:45 -0500 | [diff] [blame] | 302 | def test_bytecode(self): |
| 303 | base_path = os.path.join(TEST_DIR, 'a') |
| 304 | source_path = base_path + importlib.machinery.SOURCE_SUFFIXES[0] |
| 305 | bytecode_path = base_path + importlib.machinery.BYTECODE_SUFFIXES[0] |
| 306 | with open_file(source_path) as file: |
| 307 | file.write('testing_modulefinder = True\n') |
| 308 | py_compile.compile(source_path, cfile=bytecode_path) |
| 309 | os.remove(source_path) |
| 310 | self._do_test(bytecode_test) |
| 311 | |
Berker Peksag | 0a0d1da | 2014-07-07 14:58:12 +0300 | [diff] [blame] | 312 | def test_replace_paths(self): |
| 313 | old_path = os.path.join(TEST_DIR, 'a', 'module.py') |
| 314 | new_path = os.path.join(TEST_DIR, 'a', 'spam.py') |
| 315 | with support.captured_stdout() as output: |
| 316 | self._do_test(maybe_test, debug=2, |
| 317 | replace_paths=[(old_path, new_path)]) |
| 318 | output = output.getvalue() |
Berker Peksag | a90afbc | 2014-07-07 21:29:50 +0300 | [diff] [blame] | 319 | expected = "co_filename %r changed to %r" % (old_path, new_path) |
Berker Peksag | 0a0d1da | 2014-07-07 14:58:12 +0300 | [diff] [blame] | 320 | self.assertIn(expected, output) |
Benjamin Peterson | c0747cf | 2008-11-03 20:31:38 +0000 | [diff] [blame] | 321 | |
Guido van Rossum | fc2a0a8 | 2006-10-27 23:06:01 +0000 | [diff] [blame] | 322 | |
| 323 | if __name__ == "__main__": |
| 324 | unittest.main() |