1 # Copyright (c) Twisted Matrix Laboratories.
2 # See LICENSE for details.
5 Tests for twisted.internet.glibbase.
9 from twisted.trial.unittest import TestCase
10 from twisted.internet._glibbase import ensureNotImported
14 class EnsureNotImportedTests(TestCase):
16 L{ensureNotImported} protects against unwanted past and future imports.
19 def test_ensureWhenNotImported(self):
21 If the specified modules have never been imported, and import
22 prevention is requested, L{ensureNotImported} makes sure they will not
23 be imported in the future.
26 self.patch(sys, "modules", modules)
27 ensureNotImported(["m1", "m2"], "A message.",
28 preventImports=["m1", "m2", "m3"])
29 self.assertEquals(modules, {"m1": None, "m2": None, "m3": None})
32 def test_ensureWhenNotImportedDontPrevent(self):
34 If the specified modules have never been imported, and import
35 prevention is not requested, L{ensureNotImported} has no effect.
38 self.patch(sys, "modules", modules)
39 ensureNotImported(["m1", "m2"], "A message.")
40 self.assertEquals(modules, {})
43 def test_ensureWhenFailedToImport(self):
45 If the specified modules have been set to C{None} in C{sys.modules},
46 L{ensureNotImported} does not complain.
48 modules = {"m2": None}
49 self.patch(sys, "modules", modules)
50 ensureNotImported(["m1", "m2"], "A message.", preventImports=["m1", "m2"])
51 self.assertEquals(modules, {"m1": None, "m2": None})
54 def test_ensureFailsWhenImported(self):
56 If one of the specified modules has been previously imported,
57 L{ensureNotImported} raises an exception.
60 modules = {"m2": module}
61 self.patch(sys, "modules", modules)
62 e = self.assertRaises(ImportError, ensureNotImported,
63 ["m1", "m2"], "A message.",
64 preventImports=["m1", "m2"])
65 self.assertEquals(modules, {"m2": module})
66 self.assertEquals(e.args, ("A message.",))