6ee5cc35f525970ff8be967cc811d0c02ab1768f
[scons.git] / test / GetBuildFailures / parallel.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 # We want to verify that -j 4 starts all four jobs, the first and last of
47 # which fail and the second and third of which succeed, and then stops
48 # processing due to the build failures.  To try to control the timing,
49 # the created build scripts use marker directories to avoid doing their
50 # processing until the previous script has finished.
51
52 contents = r"""\
53 import os.path
54 import sys
55 import time
56 wait_marker = sys.argv[1] + '.marker'
57 write_marker = sys.argv[2] + '.marker'
58 if wait_marker != '-.marker':
59     while not os.path.exists(wait_marker):
60         time.sleep(1)
61 if sys.argv[0] == 'mypass.py':
62     open(sys.argv[3], 'wb').write(open(sys.argv[4], 'rb').read())
63     exit_value = 0
64 elif sys.argv[0] == 'myfail.py':
65     exit_value = 1
66 if write_marker != '-.marker':
67     os.mkdir(write_marker)
68 sys.exit(exit_value)
69 """
70
71 test.write('mypass.py', contents)
72 test.write('myfail.py', contents)
73
74 test.write('SConstruct', """\
75 Command('f3', 'f3.in', r'@%(_python_)s mypass.py -  f3 $TARGET $SOURCE')
76 Command('f4', 'f4.in', r'@%(_python_)s myfail.py f3 f4 $TARGET $SOURCE')
77 Command('f5', 'f5.in', r'@%(_python_)s myfail.py f4 f5 $TARGET $SOURCE')
78 Command('f6', 'f6.in', r'@%(_python_)s mypass.py f5 -  $TARGET $SOURCE')
79
80 def print_build_failures():
81     from SCons.Script import GetBuildFailures
82     bf_list = GetBuildFailures()
83     bf_list.sort(lambda a,b: cmp(a.filename, b.filename))
84     for bf in bf_list:
85         print "%%s failed:  %%s" %% (bf.node, bf.errstr)
86
87 try:
88     import atexit
89 except ImportError:
90     import sys
91     sys.exitfunc = print_build_failures
92 else:
93     atexit.register(print_build_failures)
94 """ % locals())
95
96 test.write('f3.in', "f3.in\n")
97 test.write('f4.in', "f4.in\n")
98 test.write('f5.in', "f5.in\n")
99 test.write('f6.in', "f6.in\n")
100
101 test.run(arguments = '-Q -j 4 .',
102          status = 2,
103          stderr = None)
104
105 f4_error = "scons: *** [f4] Error 1\n" 
106 f5_error = "scons: *** [f5] Error 1\n"
107
108 error_45 = f4_error + f5_error
109 error_54 = f5_error + f4_error
110
111 if test.stderr() not in [error_45, error_54]:
112     print "Did not find the following output in list of expected strings:"
113     print test.stderr(),
114     test.fail_test()
115
116 # We jump through hoops above to try to make sure that the individual
117 # commands execute and exit in the order we want, but we still can't be
118 # 100% sure that SCons will actually detect and record the failures in
119 # that order; the thread for f5 may detect its command's failure before
120 # the thread for f4.  Just sidestep the issue by allowing the failure
121 # strings in the output to come in either order.  If there's a genuine
122 # problem in the way things get ordered, it'll show up in stderr.
123
124 f4_failed = "f4 failed:  Error 1\n"
125 f5_failed = "f5 failed:  Error 1\n"
126
127 failed_45 = f4_failed + f5_failed
128 failed_54 = f5_failed + f4_failed
129
130 if test.stdout() not in [failed_45, failed_54]:
131     print "Did not find the following output in list of expected strings:"
132     print test.stdout(),
133     test.fail_test()
134
135 test.must_match(test.workpath('f3'), 'f3.in\n')
136 test.must_not_exist(test.workpath('f4'))
137 test.must_not_exist(test.workpath('f5'))
138 test.must_match(test.workpath('f6'), 'f6.in\n') 
139
140
141
142 test.pass_test()
143
144 # Local Variables:
145 # tab-width:4
146 # indent-tabs-mode:nil
147 # End:
148 # vim: set expandtab tabstop=4 shiftwidth=4: