def test_nonexistentPaths(self): """ Verify that L{modules.walkModules} ignores entries in sys.path which do not exist in the filesystem. """ existentPath = FilePath(self.mktemp()) os.makedirs(existentPath.child("test_package").path) existentPath.child("test_package").child("__init__.py").setContent("") nonexistentPath = FilePath(self.mktemp()) self.failIf(nonexistentPath.exists()) originalSearchPaths = sys.path[:] sys.path[:] = [existentPath.path] try: expected = [modules.getModule("test_package")] beforeModules = list(modules.walkModules()) sys.path.append(nonexistentPath.path) afterModules = list(modules.walkModules()) finally: sys.path[:] = originalSearchPaths self.assertEqual(beforeModules, expected) self.assertEqual(afterModules, expected)
def test_nonDirectoryPaths(self): """ Verify that L{modules.walkModules} ignores entries in sys.path which refer to regular files in the filesystem. """ existentPath = self.pathEntryWithOnePackage() nonDirectoryPath = FilePath(self.mktemp()) self.failIf(nonDirectoryPath.exists()) nonDirectoryPath.setContent("zip file or whatever\n") self.replaceSysPath([existentPath.path]) beforeModules = list(modules.walkModules()) sys.path.append(nonDirectoryPath.path) afterModules = list(modules.walkModules()) self.assertEqual(beforeModules, afterModules)
def test_nonexistentPaths(self): """ Verify that L{modules.walkModules} ignores entries in sys.path which do not exist in the filesystem. """ existentPath = self.pathEntryWithOnePackage() nonexistentPath = FilePath(self.mktemp()) self.failIf(nonexistentPath.exists()) self.replaceSysPath([existentPath.path]) expected = [modules.getModule("test_package")] beforeModules = list(modules.walkModules()) sys.path.append(nonexistentPath.path) afterModules = list(modules.walkModules()) self.assertEqual(beforeModules, expected) self.assertEqual(afterModules, expected)
def test_unimportablePackageWalkModules(self): """ If a package has been explicitly forbidden from importing by setting a C{None} key in sys.modules under its name, L{modules.walkModules} should still be able to retrieve an unloaded L{modules.PythonModule} for that package. """ existentPath = self.pathEntryWithOnePackage() self.replaceSysPath([existentPath.path]) self.replaceSysModules({"test_package": None}) walked = list(modules.walkModules()) self.assertEqual([m.name for m in walked], ["test_package"]) self.assertEqual(walked[0].isLoaded(), False)
def test_nonDirectoryPaths(self): """ Verify that L{modules.walkModules} ignores entries in sys.path which refer to regular files in the filesystem. """ existentPath = FilePath(self.mktemp()) os.makedirs(existentPath.child("test_package").path) existentPath.child("test_package").child("__init__.py").setContent("") nonDirectoryPath = FilePath(self.mktemp()) self.failIf(nonDirectoryPath.exists()) nonDirectoryPath.setContent("zip file or whatever\n") originalSearchPaths = sys.path[:] sys.path[:] = [existentPath.path] try: beforeModules = list(modules.walkModules()) sys.path.append(nonDirectoryPath.path) afterModules = list(modules.walkModules()) finally: sys.path[:] = originalSearchPaths self.assertEqual(beforeModules, afterModules)