Updates and slight refactor in fix
[scons.git] / test / GetBuildFailures / serial.py
blobab8fbb5946aff02885ba07000418011cc9b10a23
1 #!/usr/bin/env python
3 # MIT License
5 # Copyright The SCons Foundation
7 # Permission is hereby granted, free of charge, to any person obtaining
8 # a copy of this software and associated documentation files (the
9 # "Software"), to deal in the Software without restriction, including
10 # without limitation the rights to use, copy, modify, merge, publish,
11 # distribute, sublicense, and/or sell copies of the Software, and to
12 # permit persons to whom the Software is furnished to do so, subject to
13 # the following conditions:
15 # The above copyright notice and this permission notice shall be included
16 # in all copies or substantial portions of the Software.
18 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
19 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
20 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 """
27 Verify that the GetBuildFailures() function returns a list of
28 BuildError exceptions. Also verify printing the BuildError
29 attributes we expect to be most commonly used.
30 """
32 import TestSCons
33 import re
35 _python_ = TestSCons._python_
37 try:
38 import threading
39 except ImportError:
40 # if threads are not supported, then
41 # there is nothing to test
42 TestCmd.no_result()
43 sys.exit()
46 test = TestSCons.TestSCons()
48 contents = r"""\
49 import sys
50 if 'mypass.py' in sys.argv[0]:
51 with open(sys.argv[3], 'wb') as ofp, open(sys.argv[4], 'rb') as ifp:
52 ofp.write(ifp.read())
53 exit_value = 0
54 elif 'myfail.py' in sys.argv[0]:
55 exit_value = 1
56 sys.exit(exit_value)
57 """
59 test.write('mypass.py', contents)
60 test.write('myfail.py', contents)
62 test.write('SConstruct', """\
63 DefaultEnvironment(tools=[])
64 Command('f03', 'f03.in', r'@%(_python_)s mypass.py - f03 $TARGET $SOURCE')
65 Command('f04', 'f04.in', r'@%(_python_)s myfail.py f03 f04 $TARGET $SOURCE')
66 Command('f05', 'f05.in', r'@%(_python_)s myfail.py f04 f05 $TARGET $SOURCE')
67 Command('f06', 'f06.in', r'@%(_python_)s mypass.py f05 - $TARGET $SOURCE')
68 Command('f07', 'f07.in', r'@%(_python_)s mypass.py f07 - $TARGET $SOURCE')
70 import SCons.Errors
71 def raiseExcAction(exc):
72 def action(env, target, source, exc=exc):
73 raise exc
74 return action
75 def returnExcAction(exc):
76 def action(env, target, source, exc=exc):
77 return exc
78 return action
79 class MyBuildError(SCons.Errors.BuildError):
80 pass
82 Command('f08', 'f08.in', raiseExcAction(SCons.Errors.UserError("My User Error")))
83 Command('f09', 'f09.in', returnExcAction(SCons.Errors.UserError("My User Error")))
84 Command('f10', 'f10.in', raiseExcAction(MyBuildError(errstr="My Build Error", status=7)))
85 Command('f11', 'f11.in', returnExcAction(MyBuildError(errstr="My Build Error", status=7)))
86 Command('f12', 'f12.in', raiseExcAction(OSError(123, "My SConsEnvironmentError", "f12")))
87 Command('f13', 'f13.in', returnExcAction(OSError(123, "My SConsEnvironmentError", "f13")))
88 Command('f14', 'f14.in', raiseExcAction(SCons.Errors.InternalError("My InternalError")))
89 Command('f15', 'f15.in', returnExcAction(SCons.Errors.InternalError("My InternalError")))
91 def print_build_failures():
92 from SCons.Script import GetBuildFailures
93 for bf in sorted(GetBuildFailures(), key=lambda t: str(t.node)):
94 assert( isinstance(bf, SCons.Errors.BuildError) )
95 print("BF: %%s failed (%%s): %%s" %% (bf.node, bf.status, bf.errstr))
96 if bf.command:
97 print("BF: %%s" %% " ".join(Flatten(bf.command)))
99 import atexit
100 atexit.register(print_build_failures)
101 """ % locals())
103 test.write('f03.in', "f03.in\n")
104 test.write('f04.in', "f04.in\n")
105 test.write('f05.in', "f05.in\n")
106 test.write('f06.in', "f06.in\n")
107 # f07.in is intentionally missing...
108 test.write('f08.in', "f08.in\n")
109 test.write('f09.in', "f09.in\n")
110 test.write('f10.in', "f10.in\n")
111 test.write('f11.in', "f11.in\n")
112 test.write('f12.in', "f12.in\n")
113 test.write('f13.in', "f13.in\n")
114 test.write('f14.in', "f14.in\n")
115 test.write('f15.in', "f15.in\n")
117 expect_stdout = """\
118 scons: Reading SConscript files ...
119 scons: done reading SConscript files.
120 scons: Building targets ...
121 scons: building terminated because of errors.
122 BF: f04 failed (1): Error 1
123 BF: %(_python_)s myfail.py f03 f04 "f04" "f04.in"
124 """ % locals()
126 expect_stderr = """\
127 scons: *** [f04] Error 1
130 test.run(arguments = '.',
131 status = 2,
132 stdout = expect_stdout,
133 stderr = expect_stderr)
135 test.must_match(test.workpath('f03'), 'f03.in\n')
136 test.must_not_exist(test.workpath('f04'))
137 test.must_not_exist(test.workpath('f05'))
138 test.must_not_exist(test.workpath('f06'))
139 test.must_not_exist(test.workpath('f07'))
140 test.must_not_exist(test.workpath('f08'))
141 test.must_not_exist(test.workpath('f09'))
142 test.must_not_exist(test.workpath('f10'))
143 test.must_not_exist(test.workpath('f11'))
144 test.must_not_exist(test.workpath('f12'))
145 test.must_not_exist(test.workpath('f13'))
146 test.must_not_exist(test.workpath('f14'))
147 test.must_not_exist(test.workpath('f15'))
150 expect_stdout = re.escape("""\
151 scons: Reading SConscript files ...
152 scons: done reading SConscript files.
153 scons: Building targets ...
154 action(["f08"], ["f08.in"])
155 action(["f09"], ["f09.in"])
156 action(["f10"], ["f10.in"])
157 action(["f11"], ["f11.in"])
158 action(["f12"], ["f12.in"])
159 action(["f13"], ["f13.in"])
160 action(["f14"], ["f14.in"])
161 action(["f15"], ["f15.in"])
162 scons: done building targets (errors occurred during build).
163 BF: f04 failed (1): Error 1
164 BF: %(_python_)s myfail.py f03 f04 "f04" "f04.in"
165 BF: f05 failed (1): Error 1
166 BF: %(_python_)s myfail.py f04 f05 "f05" "f05.in"
167 BF: f07 failed (2): Source `f07.in' not found, needed by target `f07'.
168 BF: f08 failed (2): My User Error
169 BF: action(["f08"], ["f08.in"])
170 BF: f09 failed (2): My User Error
171 BF: action(["f09"], ["f09.in"])
172 BF: f10 failed (7): My Build Error
173 BF: action(["f10"], ["f10.in"])
174 BF: f11 failed (7): My Build Error
175 BF: action(["f11"], ["f11.in"])
176 BF: f12 failed (123): My SConsEnvironmentError
177 BF: action(["f12"], ["f12.in"])
178 BF: f13 failed (123): My SConsEnvironmentError
179 BF: action(["f13"], ["f13.in"])
180 BF: f14 failed (2): InternalError : My InternalError
181 BF: action(["f14"], ["f14.in"])
182 BF: f15 failed (2): InternalError : My InternalError
183 BF: action(["f15"], ["f15.in"])
184 """ % locals())
186 expect_stderr = re.escape("""\
187 scons: *** [f04] Error 1
188 scons: *** [f05] Error 1
189 scons: *** [f07] Source `f07.in' not found, needed by target `f07'.
190 scons: *** [f08] My User Error
191 scons: *** [f09] My User Error
192 scons: *** [f10] My Build Error
193 scons: *** [f11] My Build Error
194 scons: *** [f12] f12: My SConsEnvironmentError
195 scons: *** [f13] f13: My SConsEnvironmentError
196 scons: *** [f14] InternalError : My InternalError
197 """) + \
198 r"""Traceback \(most recent call last\):
199 ( File ".+", line \d+, in \S+
200 [^\n]+
201 )*( File ".+", line \d+, in \S+
202 )*( File ".+", line \d+, in \S+
203 [^\n]+
204 )*\S.+
205 """ + \
206 re.escape("""\
207 scons: *** [f15] InternalError : My InternalError
208 """)
210 test.run(arguments = '-k .',
211 status = 2,
212 stdout = expect_stdout,
213 stderr = expect_stderr,
214 match = TestSCons.match_re_dotall)
216 test.must_match(test.workpath('f03'), 'f03.in\n')
217 test.must_not_exist(test.workpath('f04'))
218 test.must_not_exist(test.workpath('f05'))
219 test.must_match(test.workpath('f06'), 'f06.in\n')
220 test.must_not_exist(test.workpath('f07'))
221 test.must_not_exist(test.workpath('f08'))
222 test.must_not_exist(test.workpath('f09'))
223 test.must_not_exist(test.workpath('f10'))
224 test.must_not_exist(test.workpath('f11'))
225 test.must_not_exist(test.workpath('f12'))
226 test.must_not_exist(test.workpath('f13'))
227 test.must_not_exist(test.workpath('f14'))
228 test.must_not_exist(test.workpath('f15'))
231 test.pass_test()
233 # Local Variables:
234 # tab-width:4
235 # indent-tabs-mode:nil
236 # End:
237 # vim: set expandtab tabstop=4 shiftwidth=4: