[Python-checkins] r51976 - python/branches/theller_modulefinder/Lib/test/test_modulefinder.py
thomas.heller
python-checkins at python.org
Fri Sep 22 11:06:50 CEST 2006
Author: thomas.heller
Date: Fri Sep 22 11:06:50 2006
New Revision: 51976
Added:
python/branches/theller_modulefinder/Lib/test/test_modulefinder.py (contents, props changed)
Log:
A basic test for modulefinder.
Added: python/branches/theller_modulefinder/Lib/test/test_modulefinder.py
==============================================================================
--- (empty file)
+++ python/branches/theller_modulefinder/Lib/test/test_modulefinder.py Fri Sep 22 11:06:50 2006
@@ -0,0 +1,85 @@
+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
+TEST_DIR = os.path.abspath("testing")
+TEST_PATH = [TEST_DIR, os.path.dirname(__future__.__file__)]
+
+# Each test description is a list of 4 items:
+#
+# 1. a module name that will be imported by modulefinder
+# 2. a list of module names that modulefinder is required to find
+# 3. a list of module names that modulefinder should complain
+# about because they are not found
+# 4. a string specifying a package to create; the format is obvious imo.
+#
+# Each package will be created in TEST_DIR, and TEST_DIR will be
+# removed after the tests again.
+# Modulefinder searches in a path that contains TEST_DIR, plus
+# the standard Lib directory.
+
+package_test = [
+ "a.module",
+ ["a", "a.b", "a.c", "a.module", "sys", "mymodule"],
+ ["blahblah"],
+ """\
+mymodule.py
+a/__init__.py
+ import blahblah
+ from a import b
+ import c
+a/module.py
+ import sys
+ from a import b as x
+a/b.py
+a/c.py
+ from a.module import x
+ import mymodule
+"""]
+
+def open_file(path):
+ ##print "#", os.path.abspath(path)
+ dirname = os.path.dirname(path)
+ distutils.dir_util.mkpath(dirname)
+ return open(path, "w")
+
+def create_package(source):
+ ofi = None
+ for line in source.splitlines():
+ if line.startswith(" ") or line.startswith("\t"):
+ ofi.write(line.strip() + "\n")
+ else:
+ ofi = open_file(os.path.join(TEST_DIR, line.strip()))
+
+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)
+ self.failUnlessEqual((more, less), ([], []))
+ finally:
+ distutils.dir_util.remove_tree(TEST_DIR)
+
+ def test_package(self):
+ self._do_test(package_test)
+
+def test_main():
+ test_support.run_unittest(ModuleFinderTest)
+
+if __name__ == "__main__":
+ test_main()
More information about the Python-checkins
mailing list