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