ยปCore Development>Code coverage>Lib/unittest/test/test_break.py

Python code coverage for Lib/unittest/test/test_break.py

#countcontent
1n/aimport gc
2n/aimport io
3n/aimport os
4n/aimport sys
5n/aimport signal
6n/aimport weakref
7n/a
8n/aimport unittest
9n/a
10n/a
11n/a@unittest.skipUnless(hasattr(os, 'kill'), "Test requires os.kill")
12n/a@unittest.skipIf(sys.platform =="win32", "Test cannot run on Windows")
13n/a@unittest.skipIf(sys.platform == 'freebsd6', "Test kills regrtest on freebsd6 "
14n/a "if threads have been used")
15n/aclass TestBreak(unittest.TestCase):
16n/a int_handler = None
17n/a
18n/a def setUp(self):
19n/a self._default_handler = signal.getsignal(signal.SIGINT)
20n/a if self.int_handler is not None:
21n/a signal.signal(signal.SIGINT, self.int_handler)
22n/a
23n/a def tearDown(self):
24n/a signal.signal(signal.SIGINT, self._default_handler)
25n/a unittest.signals._results = weakref.WeakKeyDictionary()
26n/a unittest.signals._interrupt_handler = None
27n/a
28n/a
29n/a def testInstallHandler(self):
30n/a default_handler = signal.getsignal(signal.SIGINT)
31n/a unittest.installHandler()
32n/a self.assertNotEqual(signal.getsignal(signal.SIGINT), default_handler)
33n/a
34n/a try:
35n/a pid = os.getpid()
36n/a os.kill(pid, signal.SIGINT)
37n/a except KeyboardInterrupt:
38n/a self.fail("KeyboardInterrupt not handled")
39n/a
40n/a self.assertTrue(unittest.signals._interrupt_handler.called)
41n/a
42n/a def testRegisterResult(self):
43n/a result = unittest.TestResult()
44n/a unittest.registerResult(result)
45n/a
46n/a for ref in unittest.signals._results:
47n/a if ref is result:
48n/a break
49n/a elif ref is not result:
50n/a self.fail("odd object in result set")
51n/a else:
52n/a self.fail("result not found")
53n/a
54n/a
55n/a def testInterruptCaught(self):
56n/a default_handler = signal.getsignal(signal.SIGINT)
57n/a
58n/a result = unittest.TestResult()
59n/a unittest.installHandler()
60n/a unittest.registerResult(result)
61n/a
62n/a self.assertNotEqual(signal.getsignal(signal.SIGINT), default_handler)
63n/a
64n/a def test(result):
65n/a pid = os.getpid()
66n/a os.kill(pid, signal.SIGINT)
67n/a result.breakCaught = True
68n/a self.assertTrue(result.shouldStop)
69n/a
70n/a try:
71n/a test(result)
72n/a except KeyboardInterrupt:
73n/a self.fail("KeyboardInterrupt not handled")
74n/a self.assertTrue(result.breakCaught)
75n/a
76n/a
77n/a def testSecondInterrupt(self):
78n/a # Can't use skipIf decorator because the signal handler may have
79n/a # been changed after defining this method.
80n/a if signal.getsignal(signal.SIGINT) == signal.SIG_IGN:
81n/a self.skipTest("test requires SIGINT to not be ignored")
82n/a result = unittest.TestResult()
83n/a unittest.installHandler()
84n/a unittest.registerResult(result)
85n/a
86n/a def test(result):
87n/a pid = os.getpid()
88n/a os.kill(pid, signal.SIGINT)
89n/a result.breakCaught = True
90n/a self.assertTrue(result.shouldStop)
91n/a os.kill(pid, signal.SIGINT)
92n/a self.fail("Second KeyboardInterrupt not raised")
93n/a
94n/a try:
95n/a test(result)
96n/a except KeyboardInterrupt:
97n/a pass
98n/a else:
99n/a self.fail("Second KeyboardInterrupt not raised")
100n/a self.assertTrue(result.breakCaught)
101n/a
102n/a
103n/a def testTwoResults(self):
104n/a unittest.installHandler()
105n/a
106n/a result = unittest.TestResult()
107n/a unittest.registerResult(result)
108n/a new_handler = signal.getsignal(signal.SIGINT)
109n/a
110n/a result2 = unittest.TestResult()
111n/a unittest.registerResult(result2)
112n/a self.assertEqual(signal.getsignal(signal.SIGINT), new_handler)
113n/a
114n/a result3 = unittest.TestResult()
115n/a
116n/a def test(result):
117n/a pid = os.getpid()
118n/a os.kill(pid, signal.SIGINT)
119n/a
120n/a try:
121n/a test(result)
122n/a except KeyboardInterrupt:
123n/a self.fail("KeyboardInterrupt not handled")
124n/a
125n/a self.assertTrue(result.shouldStop)
126n/a self.assertTrue(result2.shouldStop)
127n/a self.assertFalse(result3.shouldStop)
128n/a
129n/a
130n/a def testHandlerReplacedButCalled(self):
131n/a # Can't use skipIf decorator because the signal handler may have
132n/a # been changed after defining this method.
133n/a if signal.getsignal(signal.SIGINT) == signal.SIG_IGN:
134n/a self.skipTest("test requires SIGINT to not be ignored")
135n/a # If our handler has been replaced (is no longer installed) but is
136n/a # called by the *new* handler, then it isn't safe to delay the
137n/a # SIGINT and we should immediately delegate to the default handler
138n/a unittest.installHandler()
139n/a
140n/a handler = signal.getsignal(signal.SIGINT)
141n/a def new_handler(frame, signum):
142n/a handler(frame, signum)
143n/a signal.signal(signal.SIGINT, new_handler)
144n/a
145n/a try:
146n/a pid = os.getpid()
147n/a os.kill(pid, signal.SIGINT)
148n/a except KeyboardInterrupt:
149n/a pass
150n/a else:
151n/a self.fail("replaced but delegated handler doesn't raise interrupt")
152n/a
153n/a def testRunner(self):
154n/a # Creating a TextTestRunner with the appropriate argument should
155n/a # register the TextTestResult it creates
156n/a runner = unittest.TextTestRunner(stream=io.StringIO())
157n/a
158n/a result = runner.run(unittest.TestSuite())
159n/a self.assertIn(result, unittest.signals._results)
160n/a
161n/a def testWeakReferences(self):
162n/a # Calling registerResult on a result should not keep it alive
163n/a result = unittest.TestResult()
164n/a unittest.registerResult(result)
165n/a
166n/a ref = weakref.ref(result)
167n/a del result
168n/a
169n/a # For non-reference counting implementations
170n/a gc.collect();gc.collect()
171n/a self.assertIsNone(ref())
172n/a
173n/a
174n/a def testRemoveResult(self):
175n/a result = unittest.TestResult()
176n/a unittest.registerResult(result)
177n/a
178n/a unittest.installHandler()
179n/a self.assertTrue(unittest.removeResult(result))
180n/a
181n/a # Should this raise an error instead?
182n/a self.assertFalse(unittest.removeResult(unittest.TestResult()))
183n/a
184n/a try:
185n/a pid = os.getpid()
186n/a os.kill(pid, signal.SIGINT)
187n/a except KeyboardInterrupt:
188n/a pass
189n/a
190n/a self.assertFalse(result.shouldStop)
191n/a
192n/a def testMainInstallsHandler(self):
193n/a failfast = object()
194n/a test = object()
195n/a verbosity = object()
196n/a result = object()
197n/a default_handler = signal.getsignal(signal.SIGINT)
198n/a
199n/a class FakeRunner(object):
200n/a initArgs = []
201n/a runArgs = []
202n/a def __init__(self, *args, **kwargs):
203n/a self.initArgs.append((args, kwargs))
204n/a def run(self, test):
205n/a self.runArgs.append(test)
206n/a return result
207n/a
208n/a class Program(unittest.TestProgram):
209n/a def __init__(self, catchbreak):
210n/a self.exit = False
211n/a self.verbosity = verbosity
212n/a self.failfast = failfast
213n/a self.catchbreak = catchbreak
214n/a self.tb_locals = False
215n/a self.testRunner = FakeRunner
216n/a self.test = test
217n/a self.result = None
218n/a
219n/a p = Program(False)
220n/a p.runTests()
221n/a
222n/a self.assertEqual(FakeRunner.initArgs, [((), {'buffer': None,
223n/a 'verbosity': verbosity,
224n/a 'failfast': failfast,
225n/a 'tb_locals': False,
226n/a 'warnings': None})])
227n/a self.assertEqual(FakeRunner.runArgs, [test])
228n/a self.assertEqual(p.result, result)
229n/a
230n/a self.assertEqual(signal.getsignal(signal.SIGINT), default_handler)
231n/a
232n/a FakeRunner.initArgs = []
233n/a FakeRunner.runArgs = []
234n/a p = Program(True)
235n/a p.runTests()
236n/a
237n/a self.assertEqual(FakeRunner.initArgs, [((), {'buffer': None,
238n/a 'verbosity': verbosity,
239n/a 'failfast': failfast,
240n/a 'tb_locals': False,
241n/a 'warnings': None})])
242n/a self.assertEqual(FakeRunner.runArgs, [test])
243n/a self.assertEqual(p.result, result)
244n/a
245n/a self.assertNotEqual(signal.getsignal(signal.SIGINT), default_handler)
246n/a
247n/a def testRemoveHandler(self):
248n/a default_handler = signal.getsignal(signal.SIGINT)
249n/a unittest.installHandler()
250n/a unittest.removeHandler()
251n/a self.assertEqual(signal.getsignal(signal.SIGINT), default_handler)
252n/a
253n/a # check that calling removeHandler multiple times has no ill-effect
254n/a unittest.removeHandler()
255n/a self.assertEqual(signal.getsignal(signal.SIGINT), default_handler)
256n/a
257n/a def testRemoveHandlerAsDecorator(self):
258n/a default_handler = signal.getsignal(signal.SIGINT)
259n/a unittest.installHandler()
260n/a
261n/a @unittest.removeHandler
262n/a def test():
263n/a self.assertEqual(signal.getsignal(signal.SIGINT), default_handler)
264n/a
265n/a test()
266n/a self.assertNotEqual(signal.getsignal(signal.SIGINT), default_handler)
267n/a
268n/a@unittest.skipUnless(hasattr(os, 'kill'), "Test requires os.kill")
269n/a@unittest.skipIf(sys.platform =="win32", "Test cannot run on Windows")
270n/a@unittest.skipIf(sys.platform == 'freebsd6', "Test kills regrtest on freebsd6 "
271n/a "if threads have been used")
272n/aclass TestBreakDefaultIntHandler(TestBreak):
273n/a int_handler = signal.default_int_handler
274n/a
275n/a@unittest.skipUnless(hasattr(os, 'kill'), "Test requires os.kill")
276n/a@unittest.skipIf(sys.platform =="win32", "Test cannot run on Windows")
277n/a@unittest.skipIf(sys.platform == 'freebsd6', "Test kills regrtest on freebsd6 "
278n/a "if threads have been used")
279n/aclass TestBreakSignalIgnored(TestBreak):
280n/a int_handler = signal.SIG_IGN
281n/a
282n/a@unittest.skipUnless(hasattr(os, 'kill'), "Test requires os.kill")
283n/a@unittest.skipIf(sys.platform =="win32", "Test cannot run on Windows")
284n/a@unittest.skipIf(sys.platform == 'freebsd6', "Test kills regrtest on freebsd6 "
285n/a "if threads have been used")
286n/aclass TestBreakSignalDefault(TestBreak):
287n/a int_handler = signal.SIG_DFL
288n/a
289n/a
290n/aif __name__ == "__main__":
291n/a unittest.main()