def test_search_package(self): python_dir = join(FILES_DIR, 'python') script = join(python_dir, 'a.py') name = utils.resolve_modulename( script, [dirname(python_dir)]) self.assertEqual("python.a", name) name = utils.resolve_modulename( script, [python_dir]) self.assertEqual("a", name) name = utils.resolve_modulename( script, [dirname(python_dir), python_dir]) self.assertEqual("python.a", name)
def assert_resolve_modulename_in_dir(self, expected_name, filepath, directory): name = utils.resolve_modulename(filepath, [directory]) self.assertEqual(expected_name, name, "Expected module name is '%s', but was '%s' (%s in %s)" % (expected_name, name, filepath, directory)) # '/' directory += '/' name = utils.resolve_modulename(filepath, [directory]) self.assertEqual(expected_name, name, "Expected module name is '%s', but was '%s' (%s in %s)" % (expected_name, name, filepath, directory))
def gather_tests(): """Returns unittest.TestSuite instance""" tests = unittest.TestSuite() # unittest for dirpath, dirnames, filenames in os.walk(dirname(__file__)): if 'files' in dirnames: dirnames.remove('files') for name in filenames: if not (name.startswith("test_") and name.endswith(".py")): continue filepath = join(dirpath, name) modulename = resolve_modulename(filepath) module = import_module(modulename) suite = unittest.defaultTestLoader.loadTestsFromModule(module) tests.addTest(suite) # doctest modipyd_dir = join(dirname(__file__), '..', 'modipyd') for modcode in collect_module_code(modipyd_dir): module = import_module(modcode.name) try: suite = doctest.DocTestSuite(module) except ValueError: pass else: tests.addTest(suite) return tests
def test_sys_path(self): top = join(dirname(__file__), "..") search_path = sys.path[:] search_path.insert(0, top) self.assertEqual("modipyd", utils.resolve_modulename( join(top, "modipyd/__init__.py"), search_path)) self.assertEqual("modipyd.utils", utils.resolve_modulename( join(top, "modipyd/utils/__init__.py"), search_path)) self.assertEqual("modipyd.tools", utils.resolve_modulename( join(top, "modipyd/tools/__init__.py"), search_path))
def test_common_prefix_path(self): python_dir = join(FILES_DIR, 'python') python2_dir = join(FILES_DIR, 'python2') search_path = [python_dir, python2_dir] self.assertEqual("b", utils.resolve_modulename( join(python2_dir, "b.py"), search_path))
def test_package(self): self.assert_resolve_modulename_in_dir('python.a', join(FILES_DIR, 'python/a.py'), FILES_DIR) self.assert_resolve_modulename_in_dir('python2.b', join(FILES_DIR, 'python2/b.py'), FILES_DIR) # not a package name = utils.resolve_modulename( join(FILES_DIR, 'python3/c.py'), [join(FILES_DIR, 'python3')]) self.assertEqual('c', name) self.assertRaises( ImportError, utils.resolve_modulename, join(FILES_DIR, 'python3/c.py'), [FILES_DIR])
def test_search_package_priority(self): """ Consider: src/packageA/__init__.py /a.py and *sys.path* is: ['src/packageA', 'src'] a.py is in src/packageA, so module name 'a' is matched. But better module name is 'packageA.a' because a.py is created under the package 'packageA'. """ search_path = [join(FILES_DIR, 'python'), FILES_DIR] script = join(FILES_DIR, 'python', 'a.py') name = utils.resolve_modulename(script, search_path) self.assertEqual('python.a', name)