Remove (lots) more unnecessary imports.
[scons.git] / test / Parallel / multiple-parents.py
1 #!/usr/bin/env python
2 #
3 # __COPYRIGHT__
4 #
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:
12 #
13 # The above copyright notice and this permission notice shall be included
14 # in all copies or substantial portions of the Software.
15 #
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.
23 #
24
25 """
26 Verify that a failed build action with -j works as expected.
27 """
28
29 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
30
31 import TestSCons
32
33 _python_ = TestSCons._python_
34
35 try:
36     import threading
37 except ImportError:
38     # if threads are not supported, then
39     # there is nothing to test
40     TestCmd.no_result()
41     sys.exit()
42
43
44 test = TestSCons.TestSCons()
45
46 # Test that we can handle parallel builds with a dependency graph
47 # where:
48 #    a) Some nodes have multiple parents
49 #    b) Some targets fail building
50 #    c) Some targets succeed building
51 #    d) Some children are ignored
52 #    e) Some children are pre-requesites
53 #    f) Some children have side-effects
54 #    g) Some sources are missing
55 #    h) Builds that are interrupted
56
57 test.write('SConstruct', """
58 vars = Variables()
59 vars.Add( BoolVariable('interrupt', 'Interrupt the build.', 0 ) )
60 varEnv = Environment(variables=vars)
61
62 def fail_action(target = None, source = None, env = None):
63     return 2
64
65 def simulate_keyboard_interrupt(target = None, source = None, env = None):
66     # Directly invoked the SIGINT handler to simulate a
67     # KeyboardInterrupt. This hack is necessary because there is no
68     # easy way to get access to the current Job/Taskmaster object.
69     import signal
70     handler = signal.getsignal(signal.SIGINT)
71     handler(signal.SIGINT, None)
72     return 0
73
74 interrupt = Command(target='interrupt',  source='', action=simulate_keyboard_interrupt)
75
76
77 failed0  = Command(target='failed00',  source='', action=fail_action)
78 ok0      = Command(target=['ok00a', 'ok00b', 'ok00c'], 
79                    source='', 
80                    action=[Touch('${TARGETS[0]}'), Touch('${TARGETS[1]}'), Touch('${TARGETS[2]}')])
81 prereq0  = Command(target='prereq00',  source='', action=Touch('${TARGET}'))
82 ignore0  = Command(target='ignore00',  source='', action=Touch('${TARGET}'))
83 igreq0   = Command(target='igreq00',   source='', action=Touch('${TARGET}'))
84 missing0 = Command(target='missing00', source='MissingSrc', action=Touch('${TARGET}'))
85 withSE0  = Command(target=['withSE00a', 'withSE00b', 'withSE00c'], 
86                    source='', 
87                    action=[Touch('${TARGETS[0]}'), Touch('${TARGETS[1]}'), Touch('${TARGETS[2]}'), 
88                            Touch('side_effect')])
89 SideEffect('side_effect', withSE0) 
90
91 prev_level  = failed0 + ok0 + ignore0 + missing0 + withSE0
92 prev_prereq = prereq0
93 prev_ignore = ignore0
94 prev_igreq  = igreq0
95
96 if varEnv['interrupt']:
97     prev_level = prev_level + interrupt
98
99 for i in range(1,20):
100     
101     failed = Command(target='failed%02d' % i,  source='', action=fail_action)
102     ok     = Command(target=['ok%02da' % i, 'ok%02db' % i, 'ok%02dc' % i], 
103                      source='',
104                      action=[Touch('${TARGETS[0]}'), Touch('${TARGETS[1]}'), Touch('${TARGETS[2]}')])
105     prereq = Command(target='prereq%02d' % i,  source='', action=Touch('${TARGET}'))
106     ignore = Command(target='ignore%02d' % i,  source='', action=Touch('${TARGET}'))
107     igreq  = Command(target='igreq%02d' % i,   source='', action=Touch('${TARGET}'))
108     missing = Command(target='missing%02d' %i, source='MissingSrc', action=Touch('${TARGET}'))
109     withSE  = Command(target=['withSE%02da' % i, 'withSE%02db' % i, 'withSE%02dc' % i], 
110                        source='', 
111                        action=[Touch('${TARGETS[0]}'), Touch('${TARGETS[1]}'), Touch('${TARGETS[2]}'), 
112                                Touch('side_effect')])
113     SideEffect('side_effect', withSE) 
114
115     next_level = failed + ok + ignore + igreq + missing + withSE
116
117     for j in range(1,10):
118         a = Alias('a%02d%02d' % (i,j), prev_level)
119
120         Requires(a, prev_prereq)
121         Ignore(a, prev_ignore)
122
123         Requires(a, prev_igreq)
124         Ignore(a, prev_igreq)
125
126         next_level = next_level + a
127
128     prev_level  = next_level
129     prev_prereq = prereq
130     prev_ignore = ignore
131     prev_igreq  = igreq
132
133 all = Alias('all', prev_level)
134
135 Requires(all, prev_prereq)
136 Ignore(all,  prev_ignore)
137
138 Requires(all, prev_igreq)
139 Ignore(all,  prev_igreq)
140
141 Default(all)
142 """)
143
144 re_error = """\
145 (scons: \\*\\*\\* \\[failed\\d+] Error 2\\n)|\
146 (scons: \\*\\*\\* \\[missing\\d+] Source `MissingSrc' not found, needed by target `missing\\d+'\\.(  Stop\\.)?\\n)|\
147 (scons: \\*\\*\\* \\[\\w+] Build interrupted\.\\n)|\
148 (scons: Build interrupted\.\\n)\
149 """
150
151 re_errors = "(" + re_error + ")+"
152
153 test.run(arguments = 'all',
154          status = 2,
155          stderr = "scons: *** [failed19] Error 2\n")
156 test.must_not_exist(test.workpath('side_effect'))
157 for i in range(20):
158     test.must_not_exist(test.workpath('ok%02da' % i))
159     test.must_not_exist(test.workpath('ok%02db' % i))
160     test.must_not_exist(test.workpath('ok%02dc' % i))
161     test.must_not_exist(test.workpath('prereq%02d' % i))
162     test.must_not_exist(test.workpath('ignore%02d' % i))
163     test.must_not_exist(test.workpath('igreq%02d' % i))
164     test.must_not_exist(test.workpath('withSE%02da' % i))
165     test.must_not_exist(test.workpath('withSE%02db' % i))
166     test.must_not_exist(test.workpath('withSE%02dc' % i))
167
168
169 for i in range(5):
170     test.run(arguments = '-c all')
171
172     test.run(arguments = '-j8 all',
173              status = 2,
174              stderr = re_errors,
175              match=TestSCons.match_re_dotall)
176
177
178 for i in range(5):
179     test.run(arguments = '-c all')
180
181     test.run(arguments = '-j 8 -k all',
182              status = 2,
183              stderr = re_errors,
184              match=TestSCons.match_re_dotall)
185     test.must_exist(test.workpath('side_effect'))
186     for i in range(20):
187         test.must_exist(test.workpath('ok%02da' % i))
188         test.must_exist(test.workpath('ok%02db' % i))
189         test.must_exist(test.workpath('ok%02dc' % i))
190         test.must_exist(test.workpath('prereq%02d' % i))
191         test.must_not_exist(test.workpath('ignore%02d' % i))
192         test.must_exist(test.workpath('igreq%02d' % i))
193         test.must_exist(test.workpath('withSE%02da' % i))
194         test.must_exist(test.workpath('withSE%02db' % i))
195         test.must_exist(test.workpath('withSE%02dc' % i))
196
197
198 for i in range(5):
199     test.run(arguments = 'all --random',
200              status = 2,
201              stderr = re_errors,
202              match=TestSCons.match_re_dotall)
203
204
205 for i in range(5):
206     test.run(arguments = '-c all')
207
208     test.run(arguments = '-j8 --random all',
209              status = 2,
210              stderr = re_errors,
211              match=TestSCons.match_re_dotall)
212
213
214 for i in range(5):
215     test.run(arguments = '-c all')
216
217     test.run(arguments = '-j 8 -k --random all',
218              status = 2,
219              stderr = re_errors,
220              match=TestSCons.match_re_dotall)
221     test.must_exist(test.workpath('side_effect'))
222     for i in range(20):
223         test.must_exist(test.workpath('ok%02da' % i))
224         test.must_exist(test.workpath('ok%02db' % i))
225         test.must_exist(test.workpath('ok%02dc' % i))
226         test.must_exist(test.workpath('prereq%02d' % i))
227         test.must_not_exist(test.workpath('ignore%02d' % i))
228         test.must_exist(test.workpath('igreq%02d' % i))
229         test.must_exist(test.workpath('withSE%02da' % i))
230         test.must_exist(test.workpath('withSE%02db' % i))
231         test.must_exist(test.workpath('withSE%02dc' % i))
232
233
234 for i in range(5):
235     test.run(arguments = '-c all')
236
237     test.run(arguments = '-j 8 -k --random interrupt=yes all',
238              status = 2,
239              stderr = re_errors,
240              match=TestSCons.match_re_dotall)
241
242
243 test.pass_test()