[Python-checkins] r51980 - python/branches/theller_modulefinder/Lib/test/test_modulefinder.py
thomas.heller
python-checkins at python.org
Fri Sep 22 14:08:55 CEST 2006
Author: thomas.heller
Date: Fri Sep 22 14:08:55 2006
New Revision: 51980
Modified:
python/branches/theller_modulefinder/Lib/test/test_modulefinder.py
Log:
Add a test for absolute imports when they are available.
The test currently fails with NotImplementedError in modulefinder.
Modified: python/branches/theller_modulefinder/Lib/test/test_modulefinder.py
==============================================================================
--- python/branches/theller_modulefinder/Lib/test/test_modulefinder.py (original)
+++ python/branches/theller_modulefinder/Lib/test/test_modulefinder.py Fri Sep 22 14:08:55 2006
@@ -1,12 +1,20 @@
import __future__
import sys, os
import unittest
-import modulefinder
import distutils.dir_util
from test import test_support
-# FIXME: do NOT create files in the current directory
+try: set
+except NameError: from sets import Set as set
+
+import modulefinder
+
+# XXX To test modulefinder with Python 2.2, sets.py and
+# modulefinder.py must be available - they are not in the standard
+# library.
+
+# XXX FIXME: do NOT create files in the current directory
TEST_DIR = os.path.abspath("testing")
TEST_PATH = [TEST_DIR, os.path.dirname(__future__.__file__)]
@@ -25,7 +33,7 @@
package_test = [
"a.module",
- ["a", "a.b", "a.c", "a.module", "sys", "mymodule"],
+ ["a", "a.b", "a.c", "a.module", "mymodule", "sys"],
["blahblah"],
"""\
mymodule.py
@@ -42,6 +50,38 @@
import mymodule
"""]
+absolute_import_test = [
+ "a.module",
+ ["a", "a.module",
+ "b", "b.x", "b.y", "b.z",
+ "__future__", "sys", "time"],
+ ["blahblah"],
+ """\
+mymodule.py
+a/__init__.py
+a/module.py
+ from __future__ import absolute_import
+ import sys # this is a.sys
+ import blahblah # fails
+ import time # this is NOT a.time
+ import b.x # this is NOT a.b.x
+ from b import y
+ from b.z import *
+a/time.py
+a/sys.py
+ import mymodule
+a/b/__init__.py
+a/b/x.py
+a/b/y.py
+a/b/z.py
+b/__init__.py
+ import z
+b/unused.py
+b/x.py
+b/y.py
+b/z.py
+"""]
+
def open_file(path):
##print "#", os.path.abspath(path)
dirname = os.path.dirname(path)
@@ -59,27 +99,41 @@
class ModuleFinderTest(unittest.TestCase):
def _do_test(self, info):
import_this, modules, missing, source = info
- directory = import_this.split(".")[0]
create_package(source)
try:
mf = modulefinder.ModuleFinder(path=TEST_PATH)
mf.import_hook(import_this)
## mf.report()
- bad = mf.badmodules.keys()
- self.failUnlessEqual(bad, missing)
modules = set(modules)
found = set(mf.modules.keys())
more = list(found - modules)
less = list(modules - found)
+ # check if we found what we expected, not more, not less
self.failUnlessEqual((more, less), ([], []))
+
+ # check if missing modules are reported correctly
+ bad = mf.badmodules.keys()
+ self.failUnlessEqual(bad, missing)
finally:
distutils.dir_util.remove_tree(TEST_DIR)
def test_package(self):
self._do_test(package_test)
+ if getattr(__future__, "absolute_import", None):
+
+ def test_absolute_imports(self):
+ import_this, modules, missing, source = absolute_import_test
+ create_package(source)
+ try:
+ mf = modulefinder.ModuleFinder(path=TEST_PATH)
+ self.assertRaises(NotImplementedError,
+ lambda: mf.import_hook(import_this))
+ finally:
+ distutils.dir_util.remove_tree(TEST_DIR)
+
def test_main():
test_support.run_unittest(ModuleFinderTest)
if __name__ == "__main__":
- test_main()
+ unittest.main()
More information about the Python-checkins
mailing list