Initial import to Tizen
[profile/ivi/python-twisted.git] / twisted / internet / test / test_baseprocess.py
1 # Copyright (c) Twisted Matrix Laboratories.
2 # See LICENSE for details.
3
4 """
5 Tests for L{twisted.internet._baseprocess} which implements process-related
6 functionality that is useful in all platforms supporting L{IReactorProcess}.
7 """
8
9 __metaclass__ = type
10
11 from twisted.python.deprecate import getWarningMethod, setWarningMethod
12 from twisted.trial.unittest import TestCase
13 from twisted.internet._baseprocess import BaseProcess
14
15
16 class BaseProcessTests(TestCase):
17     """
18     Tests for L{BaseProcess}, a parent class for other classes which represent
19     processes which implements functionality common to many different process
20     implementations.
21     """
22     def test_callProcessExited(self):
23         """
24         L{BaseProcess._callProcessExited} calls the C{processExited} method of
25         its C{proto} attribute and passes it a L{Failure} wrapping the given
26         exception.
27         """
28         class FakeProto:
29             reason = None
30
31             def processExited(self, reason):
32                 self.reason = reason
33
34         reason = RuntimeError("fake reason")
35         process = BaseProcess(FakeProto())
36         process._callProcessExited(reason)
37         process.proto.reason.trap(RuntimeError)
38         self.assertIdentical(reason, process.proto.reason.value)
39
40
41     def test_callProcessExitedMissing(self):
42         """
43         L{BaseProcess._callProcessExited} emits a L{DeprecationWarning} if the
44         object referred to by its C{proto} attribute has no C{processExited}
45         method.
46         """
47         class FakeProto:
48             pass
49
50         reason = object()
51         process = BaseProcess(FakeProto())
52
53         self.addCleanup(setWarningMethod, getWarningMethod())
54         warnings = []
55         def collect(message, category, stacklevel):
56             warnings.append((message, category, stacklevel))
57         setWarningMethod(collect)
58
59         process._callProcessExited(reason)
60
61         [(message, category, stacklevel)] = warnings
62         self.assertEqual(
63             message,
64             "Since Twisted 8.2, IProcessProtocol.processExited is required.  "
65             "%s.%s must implement it." % (
66                 FakeProto.__module__, FakeProto.__name__))
67         self.assertIdentical(category, DeprecationWarning)
68         # The stacklevel doesn't really make sense for this kind of
69         # deprecation.  Requiring it to be 0 will at least avoid pointing to
70         # any part of Twisted or a random part of the application's code, which
71         # I think would be more misleading than having it point inside the
72         # warning system itself. -exarkun
73         self.assertEqual(stacklevel, 0)