1 # Copyright (c) Twisted Matrix Laboratories.
2 # See LICENSE for details.
5 Facilities for helping test code which interacts with L{twisted.python.modules},
6 or uses Python's own module system to load code.
11 from twisted.trial.unittest import TestCase
12 from twisted.python import modules
13 from twisted.python.filepath import FilePath
15 class TwistedModulesTestCase(TestCase):
17 def findByIteration(self, modname, where=modules, importPackages=False):
19 You don't ever actually want to do this, so it's not in the public API, but
20 sometimes we want to compare the result of an iterative call with a
21 lookup call and make sure they're the same for test purposes.
23 for modinfo in where.walkModules(importPackages=importPackages):
24 if modinfo.name == modname:
26 self.fail("Unable to find module %r through iteration." % (modname,))
29 def replaceSysPath(self, sysPath):
31 Replace sys.path, for the duration of the test, with the given value.
33 originalSysPath = sys.path[:]
35 sys.path[:] = originalSysPath
36 self.addCleanup(cleanUpSysPath)
40 def replaceSysModules(self, sysModules):
42 Replace sys.modules, for the duration of the test, with the given value.
44 originalSysModules = sys.modules.copy()
45 def cleanUpSysModules():
47 sys.modules.update(originalSysModules)
48 self.addCleanup(cleanUpSysModules)
50 sys.modules.update(sysModules)
53 def pathEntryWithOnePackage(self, pkgname="test_package"):
55 Generate a L{FilePath} with one package, named C{pkgname}, on it, and
56 return the L{FilePath} of the path entry.
58 entry = FilePath(self.mktemp())
59 pkg = entry.child("test_package")
61 pkg.child("__init__.py").setContent("")