a240d1ef678824845d20693c71515f9b27c20669
[scons.git] / test / GetBuildFailures / serial.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 the GetBuildFailures() function returns a list of
27 BuildError exceptions.  Also verify printing the BuildError
28 attributes we expect to be most commonly used.
29 """
30
31 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
32
33 import TestSCons
34 import re
35
36 _python_ = TestSCons._python_
37
38 try:
39     import threading
40 except ImportError:
41     # if threads are not supported, then
42     # there is nothing to test
43     TestCmd.no_result()
44     sys.exit()
45
46
47 test = TestSCons.TestSCons()
48
49 contents = r"""\
50 import sys
51 if sys.argv[0] == 'mypass.py':
52     open(sys.argv[3], 'wb').write(open(sys.argv[4], 'rb').read())
53     exit_value = 0
54 elif sys.argv[0] == 'myfail.py':
55     exit_value = 1
56 sys.exit(exit_value)
57 """
58
59 test.write('mypass.py', contents)
60 test.write('myfail.py', contents)
61
62 test.write('SConstruct', """\
63 Command('f03', 'f03.in', r'@%(_python_)s mypass.py -   f03 $TARGET $SOURCE')
64 Command('f04', 'f04.in', r'@%(_python_)s myfail.py f03 f04 $TARGET $SOURCE')
65 Command('f05', 'f05.in', r'@%(_python_)s myfail.py f04 f05 $TARGET $SOURCE')
66 Command('f06', 'f06.in', r'@%(_python_)s mypass.py f05 -   $TARGET $SOURCE')
67 Command('f07', 'f07.in', r'@%(_python_)s mypass.py f07 -   $TARGET $SOURCE')
68
69 import SCons.Errors
70 def raiseExcAction(exc):
71     def action(env, target, source, exc=exc):
72         raise exc
73     return action
74 def returnExcAction(exc):
75     def action(env, target, source, exc=exc):
76         return exc
77     return action
78 class MyBuildError(SCons.Errors.BuildError):
79    pass
80
81 Command('f08', 'f08.in', raiseExcAction(SCons.Errors.UserError("My User Error")))
82 Command('f09', 'f09.in', returnExcAction(SCons.Errors.UserError("My User Error")))
83 Command('f10', 'f10.in', raiseExcAction(MyBuildError(errstr="My Build Error", status=7)))
84 Command('f11', 'f11.in', returnExcAction(MyBuildError(errstr="My Build Error", status=7)))
85 Command('f12', 'f12.in', raiseExcAction(EnvironmentError(123, "My EnvironmentError", "f12")))
86 Command('f13', 'f13.in', returnExcAction(EnvironmentError(123, "My EnvironmentError", "f13")))
87 Command('f14', 'f14.in', raiseExcAction(SCons.Errors.InternalError("My InternalError")))
88 Command('f15', 'f15.in', returnExcAction(SCons.Errors.InternalError("My InternalError")))
89
90 def print_build_failures():
91     from SCons.Script import GetBuildFailures
92     bf_list = GetBuildFailures()
93     bf_list.sort(lambda a,b: cmp(str(a.node), str(b.node)))
94     for bf in bf_list:
95         assert( isinstance(bf, SCons.Errors.BuildError) )
96         print "BF: %%s failed (%%s):  %%s" %% (bf.node, bf.status, bf.errstr)
97         if bf.command:
98             print "BF:    %%s" %% " ".join(Flatten(bf.command))
99
100 try:
101     import atexit
102 except ImportError:
103     import sys
104     sys.exitfunc = print_build_failures
105 else:
106     atexit.register(print_build_failures)
107 """ % locals())
108
109 test.write('f03.in', "f03.in\n")
110 test.write('f04.in', "f04.in\n")
111 test.write('f05.in', "f05.in\n")
112 test.write('f06.in', "f06.in\n")
113 # f07.in is intentionally missing...
114 test.write('f08.in', "f08.in\n")
115 test.write('f09.in', "f09.in\n")
116 test.write('f10.in', "f10.in\n")
117 test.write('f11.in', "f11.in\n")
118 test.write('f12.in', "f12.in\n")
119 test.write('f13.in', "f13.in\n")
120 test.write('f14.in', "f14.in\n")
121 test.write('f15.in', "f15.in\n")
122
123 expect_stdout = """\
124 scons: Reading SConscript files ...
125 scons: done reading SConscript files.
126 scons: Building targets ...
127 scons: building terminated because of errors.
128 BF: f04 failed (1):  Error 1
129 BF:    %(_python_)s myfail.py f03 f04 "f04" "f04.in"
130 """ % locals()
131
132 expect_stderr = """\
133 scons: *** [f04] Error 1
134 """
135
136 test.run(arguments = '.',
137          status = 2,
138          stdout = expect_stdout,
139          stderr = expect_stderr)
140
141 test.must_match(test.workpath('f03'), 'f03.in\n')
142 test.must_not_exist(test.workpath('f04'))
143 test.must_not_exist(test.workpath('f05'))
144 test.must_not_exist(test.workpath('f06'))
145 test.must_not_exist(test.workpath('f07'))
146 test.must_not_exist(test.workpath('f08'))
147 test.must_not_exist(test.workpath('f09'))
148 test.must_not_exist(test.workpath('f10'))
149 test.must_not_exist(test.workpath('f11'))
150 test.must_not_exist(test.workpath('f12'))
151 test.must_not_exist(test.workpath('f13'))
152 test.must_not_exist(test.workpath('f14'))
153 test.must_not_exist(test.workpath('f15'))
154
155
156 expect_stdout = re.escape("""\
157 scons: Reading SConscript files ...
158 scons: done reading SConscript files.
159 scons: Building targets ...
160 action(["f08"], ["f08.in"])
161 action(["f09"], ["f09.in"])
162 action(["f10"], ["f10.in"])
163 action(["f11"], ["f11.in"])
164 action(["f12"], ["f12.in"])
165 action(["f13"], ["f13.in"])
166 action(["f14"], ["f14.in"])
167 action(["f15"], ["f15.in"])
168 scons: done building targets (errors occurred during build).
169 BF: f04 failed (1):  Error 1
170 BF:    %(_python_)s myfail.py f03 f04 "f04" "f04.in"
171 BF: f05 failed (1):  Error 1
172 BF:    %(_python_)s myfail.py f04 f05 "f05" "f05.in"
173 BF: f07 failed (2):  Source `f07.in' not found, needed by target `f07'.
174 BF: f08 failed (2):  My User Error
175 BF:    action(["f08"], ["f08.in"])
176 BF: f09 failed (2):  My User Error
177 BF:    action(["f09"], ["f09.in"])
178 BF: f10 failed (7):  My Build Error
179 BF:    action(["f10"], ["f10.in"])
180 BF: f11 failed (7):  My Build Error
181 BF:    action(["f11"], ["f11.in"])
182 BF: f12 failed (123):  My EnvironmentError
183 BF:    action(["f12"], ["f12.in"])
184 BF: f13 failed (123):  My EnvironmentError
185 BF:    action(["f13"], ["f13.in"])
186 BF: f14 failed (2):  InternalError : My InternalError
187 BF:    action(["f14"], ["f14.in"])
188 BF: f15 failed (2):  InternalError : My InternalError
189 BF:    action(["f15"], ["f15.in"])
190 """ % locals())
191
192 expect_stderr = re.escape("""\
193 scons: *** [f04] Error 1
194 scons: *** [f05] Error 1
195 scons: *** [f07] Source `f07.in' not found, needed by target `f07'.
196 scons: *** [f08] My User Error
197 scons: *** [f09] My User Error
198 scons: *** [f10] My Build Error
199 scons: *** [f11] My Build Error
200 scons: *** [f12] f12: My EnvironmentError
201 scons: *** [f13] f13: My EnvironmentError
202 scons: *** [f14] InternalError : My InternalError
203 """) + \
204 """\
205 Traceback \((most recent call|innermost) last\):
206 (  File ".+", line \d+, in \S+
207     [^\n]+
208 )*(  File ".+", line \d+, in \S+
209 )*(  File ".+", line \d+, in \S+
210     [^\n]+
211 )*\S.+
212 """ + \
213 re.escape("""\
214 scons: *** [f15] InternalError : My InternalError
215 """)
216
217 test.run(arguments = '-k .',
218          status = 2,
219          stdout = expect_stdout,
220          stderr = expect_stderr,
221          match = TestSCons.match_re_dotall)
222
223 test.must_match(test.workpath('f03'), 'f03.in\n')
224 test.must_not_exist(test.workpath('f04'))
225 test.must_not_exist(test.workpath('f05'))
226 test.must_match(test.workpath('f06'), 'f06.in\n')
227 test.must_not_exist(test.workpath('f07'))
228 test.must_not_exist(test.workpath('f08'))
229 test.must_not_exist(test.workpath('f09'))
230 test.must_not_exist(test.workpath('f10'))
231 test.must_not_exist(test.workpath('f11'))
232 test.must_not_exist(test.workpath('f12'))
233 test.must_not_exist(test.workpath('f13'))
234 test.must_not_exist(test.workpath('f14'))
235 test.must_not_exist(test.workpath('f15'))
236
237
238 test.pass_test()
239
240 # Local Variables:
241 # tab-width:4
242 # indent-tabs-mode:nil
243 # End:
244 # vim: set expandtab tabstop=4 shiftwidth=4: