3c11be810fec7a6a0fd5b78a2a37b53509729457
[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     import string
93     bf_list = GetBuildFailures()
94     bf_list.sort(lambda a,b: cmp(str(a.node), str(b.node)))
95     for bf in bf_list:
96         assert( isinstance(bf, SCons.Errors.BuildError) )
97         print "BF: %%s failed (%%s):  %%s" %% (bf.node, bf.status, bf.errstr)
98         if bf.command:
99             print "BF:    %%s" %% string.join(Flatten(bf.command))
100
101 try:
102     import atexit
103 except ImportError:
104     import sys
105     sys.exitfunc = print_build_failures
106 else:
107     atexit.register(print_build_failures)
108 """ % locals())
109
110 test.write('f03.in', "f03.in\n")
111 test.write('f04.in', "f04.in\n")
112 test.write('f05.in', "f05.in\n")
113 test.write('f06.in', "f06.in\n")
114 # f07.in is intentionally missing...
115 test.write('f08.in', "f08.in\n")
116 test.write('f09.in', "f09.in\n")
117 test.write('f10.in', "f10.in\n")
118 test.write('f11.in', "f11.in\n")
119 test.write('f12.in', "f12.in\n")
120 test.write('f13.in', "f13.in\n")
121 test.write('f14.in', "f14.in\n")
122 test.write('f15.in', "f15.in\n")
123
124 expect_stdout = """\
125 scons: Reading SConscript files ...
126 scons: done reading SConscript files.
127 scons: Building targets ...
128 scons: building terminated because of errors.
129 BF: f04 failed (1):  Error 1
130 BF:    %(_python_)s myfail.py f03 f04 "f04" "f04.in"
131 """ % locals()
132
133 expect_stderr = """\
134 scons: *** [f04] Error 1
135 """
136
137 test.run(arguments = '.',
138          status = 2,
139          stdout = expect_stdout,
140          stderr = expect_stderr)
141
142 test.must_match(test.workpath('f03'), 'f03.in\n')
143 test.must_not_exist(test.workpath('f04'))
144 test.must_not_exist(test.workpath('f05'))
145 test.must_not_exist(test.workpath('f06'))
146 test.must_not_exist(test.workpath('f07'))
147 test.must_not_exist(test.workpath('f08'))
148 test.must_not_exist(test.workpath('f09'))
149 test.must_not_exist(test.workpath('f10'))
150 test.must_not_exist(test.workpath('f11'))
151 test.must_not_exist(test.workpath('f12'))
152 test.must_not_exist(test.workpath('f13'))
153 test.must_not_exist(test.workpath('f14'))
154 test.must_not_exist(test.workpath('f15'))
155
156
157 expect_stdout = re.escape("""\
158 scons: Reading SConscript files ...
159 scons: done reading SConscript files.
160 scons: Building targets ...
161 action(["f08"], ["f08.in"])
162 action(["f09"], ["f09.in"])
163 action(["f10"], ["f10.in"])
164 action(["f11"], ["f11.in"])
165 action(["f12"], ["f12.in"])
166 action(["f13"], ["f13.in"])
167 action(["f14"], ["f14.in"])
168 action(["f15"], ["f15.in"])
169 scons: done building targets (errors occurred during build).
170 BF: f04 failed (1):  Error 1
171 BF:    %(_python_)s myfail.py f03 f04 "f04" "f04.in"
172 BF: f05 failed (1):  Error 1
173 BF:    %(_python_)s myfail.py f04 f05 "f05" "f05.in"
174 BF: f07 failed (2):  Source `f07.in' not found, needed by target `f07'.
175 BF: f08 failed (2):  My User Error
176 BF:    action(["f08"], ["f08.in"])
177 BF: f09 failed (2):  My User Error
178 BF:    action(["f09"], ["f09.in"])
179 BF: f10 failed (7):  My Build Error
180 BF:    action(["f10"], ["f10.in"])
181 BF: f11 failed (7):  My Build Error
182 BF:    action(["f11"], ["f11.in"])
183 BF: f12 failed (123):  My EnvironmentError
184 BF:    action(["f12"], ["f12.in"])
185 BF: f13 failed (123):  My EnvironmentError
186 BF:    action(["f13"], ["f13.in"])
187 BF: f14 failed (2):  InternalError : My InternalError
188 BF:    action(["f14"], ["f14.in"])
189 BF: f15 failed (2):  InternalError : My InternalError
190 BF:    action(["f15"], ["f15.in"])
191 """ % locals())
192
193 expect_stderr = re.escape("""\
194 scons: *** [f04] Error 1
195 scons: *** [f05] Error 1
196 scons: *** [f07] Source `f07.in' not found, needed by target `f07'.
197 scons: *** [f08] My User Error
198 scons: *** [f09] My User Error
199 scons: *** [f10] My Build Error
200 scons: *** [f11] My Build Error
201 scons: *** [f12] f12: My EnvironmentError
202 scons: *** [f13] f13: My EnvironmentError
203 scons: *** [f14] InternalError : My InternalError
204 """) + \
205 """\
206 Traceback \((most recent call|innermost) last\):
207 (  File ".+", line \d+, in \S+
208     [^\n]+
209 )*(  File ".+", line \d+, in \S+
210 )*(  File ".+", line \d+, in \S+
211     [^\n]+
212 )*\S.+
213 """ + \
214 re.escape("""\
215 scons: *** [f15] InternalError : My InternalError
216 """)
217
218 test.run(arguments = '-k .',
219          status = 2,
220          stdout = expect_stdout,
221          stderr = expect_stderr,
222          match = TestSCons.match_re_dotall)
223
224 test.must_match(test.workpath('f03'), 'f03.in\n')
225 test.must_not_exist(test.workpath('f04'))
226 test.must_not_exist(test.workpath('f05'))
227 test.must_match(test.workpath('f06'), 'f06.in\n')
228 test.must_not_exist(test.workpath('f07'))
229 test.must_not_exist(test.workpath('f08'))
230 test.must_not_exist(test.workpath('f09'))
231 test.must_not_exist(test.workpath('f10'))
232 test.must_not_exist(test.workpath('f11'))
233 test.must_not_exist(test.workpath('f12'))
234 test.must_not_exist(test.workpath('f13'))
235 test.must_not_exist(test.workpath('f14'))
236 test.must_not_exist(test.workpath('f15'))
237
238
239 test.pass_test()