Merge pull request #4359 from mwichmann/maint/dead-sigstuff
[scons.git] / test / KeyboardInterrupt.py
blobbcb576c893403909cdd34d6539fbc85ce497ecee
1 #!/usr/bin/env python
3 # __COPYRIGHT__
5 # Permission is hereby granted, free of charge, to any person obtaining
6 # a copy of this software and associated documentation files (the
7 # "Software"), to deal in the Software without restriction, including
8 # without limitation the rights to use, copy, modify, merge, publish,
9 # distribute, sublicense, and/or sell copies of the Software, and to
10 # permit persons to whom the Software is furnished to do so, subject to
11 # the following conditions:
13 # The above copyright notice and this permission notice shall be included
14 # in all copies or substantial portions of the Software.
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
17 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
18 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
27 """
28 Verify that we handle keyboard interrupts (CTRL-C) correctly.
29 """
31 import TestSCons
33 test = TestSCons.TestSCons()
35 test.write('toto.c', r"""
36 void foo()
38 """)
40 test.write('SConstruct', r"""
41 import os
42 import sys
43 import signal
45 if 'killpg' not in dir(os) or 'setpgrp' not in dir(os) or sys.platform == 'cygwin':
46 # The platform does not support process group. Therefore, we
47 # directly invoked the SIGINT handler to simulate a
48 # KeyboardInterrupt. This hack is necessary because there is no
49 # easy way to get access to the current Job/Taskmaster object.
51 # Note that this way of performing the test is not as good as
52 # using killpg because the Taskmaster is stopped synchronously. In
53 # addition, the SCons subprocesses (or forked children before the
54 # exec() of the subprocess) are never killed. This therefore
55 # exercise less SCons functionality.
57 # FIXME: There seems to be a bug on Cygwin where the compiler
58 # process hangs after sending the SIGINT signal to the process
59 # group. It is probably a bug in cygwin1.dll, or maybe in the
60 # Python 'C' code or the Python subprocess module. We therefore do
61 # not use 'killpg' on Cygwin.
62 def explode(env, target, source):
63 handler = signal.getsignal(signal.SIGINT)
64 handler(signal.SIGINT, None)
65 return 0
66 else:
67 # The platform does support process group so we use killpg to send
68 # a SIGINT to everyone.
70 # Make sure that SCons is a process group leader.
71 os.setpgrp()
73 def explode(env, target, source):
74 os.killpg(0, signal.SIGINT)
77 all = []
79 for i in range(40):
80 all.extend(Object('toto%5d' % i, 'toto.c'))
82 all.extend(Command( 'broken', 'toto.c', explode))
84 Default( Alias('all', all))
85 """
88 interruptedStr = """\
89 .*\
90 scons: building terminated because of errors\\.
91 .*\
92 scons: writing .sconsign file\\.
93 .*\
94 """
96 def runtest(arguments):
97 test.run(arguments='-c')
98 test.run(arguments=arguments, status=2,
99 stdout=interruptedStr,
100 stderr='.*scons: Build interrupted\\.',
101 match=TestSCons.match_re_dotall)
103 for i in range(2):
104 runtest('-j1')
105 runtest('-j4')
106 runtest('-j8')
107 runtest('-j16')
108 runtest('-j32')
109 runtest('-j64')
111 runtest('-j1 --random')
112 runtest('-j4 --random')
113 runtest('-j8 --random')
114 runtest('-j16 --random')
115 runtest('-j32 --random')
116 runtest('-j64 --random')
118 test.pass_test()
120 # Local Variables:
121 # tab-width:4
122 # indent-tabs-mode:nil
123 # End:
124 # vim: set expandtab tabstop=4 shiftwidth=4: