Issue 2168: Mac OS X fixes for SWIG tests. (Greg Noel)
[scons.git] / test / exceptions.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 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
26
27 import os
28 import re
29 import string
30 import sys
31 import TestSCons
32 import TestCmd
33
34 _python_ = TestSCons._python_
35
36 test = TestSCons.TestSCons(match = TestCmd.match_re_dotall)
37
38 SConstruct_path = test.workpath('SConstruct')
39
40 test.write(SConstruct_path, """\
41 def func(source = None, target = None, env = None):
42     raise Exception, "func exception"
43 B = Builder(action = func)
44 env = Environment(BUILDERS = { 'B' : B })
45 env.B(target = 'foo.out', source = 'foo.in')
46 """)
47
48 test.write('foo.in', "foo.in\n")
49
50 expected_stderr = """scons: \*\*\* \[foo.out\] Exception
51 Traceback \((most recent call|innermost) last\):
52 (  File ".+", line \d+, in \S+
53     [^\n]+
54 )*(  File ".+", line \d+, in \S+
55 )*(  File ".+", line \d+, in \S+
56     [^\n]+
57 )*  File "%s", line 2, in func
58     raise Exception, "func exception"
59 Exception: func exception
60 """ % re.escape(SConstruct_path)
61
62 test.run(arguments = "foo.out", stderr = expected_stderr, status = 2)
63
64 test.run(arguments = "-j2 foo.out", stderr = expected_stderr, status = 2)
65
66
67 # Verify that exceptions caused by exit values of builder actions are
68 # correctly signalled, for both Serial and Parallel jobs.
69
70 test.write('myfail.py', r"""\
71 import sys
72 sys.exit(1)
73 """)
74
75 test.write(SConstruct_path, """
76 Fail = Builder(action = r'%(_python_)s myfail.py $TARGETS $SOURCE')
77 env = Environment(BUILDERS = { 'Fail' : Fail })
78 env.Fail(target = 'out.f1', source = 'in.f1')
79 """ % locals())
80
81 test.write('in.f1', "in.f1\n")
82
83 expected_stderr = "scons: \\*\\*\\* \\[out.f1\\] Error 1\n"
84
85 test.run(arguments = '.', status = 2, stderr = expected_stderr)
86 test.run(arguments = '-j2 .', status = 2, stderr = expected_stderr)
87
88
89 # Verify that all exceptions from simultaneous tasks are reported,
90 # even if the exception is raised during the Task.prepare()
91 # [Node.prepare()]
92
93 test.write(SConstruct_path, """
94 Fail = Builder(action = r'%(_python_)s myfail.py $TARGETS $SOURCE')
95 env = Environment(BUILDERS = { 'Fail' : Fail })
96 env.Fail(target = 'out.f1', source = 'in.f1')
97 env.Fail(target = 'out.f2', source = 'in.f2')
98 env.Fail(target = 'out.f3', source = 'in.f3')
99 """ % locals())
100
101 # in.f2 is not created to cause a Task.prepare exception
102 test.write('in.f1', 'in.f1\n')
103 test.write('in.f3', 'in.f3\n')
104
105 # In Serial task mode, get the first exception and stop
106 test.run(arguments = '.', status = 2, stderr = expected_stderr)
107
108 # In Parallel task mode, we will get all three exceptions.
109
110 expected_stderr_list = [
111     "scons: *** [out.f1] Error 1\n",
112     "scons: *** Source `in.f2' not found, needed by target `out.f2'.\n",
113     "scons: *** [out.f3] Error 1\n",
114 ]
115
116 # To get all three exceptions simultaneously, we execute -j7 to create
117 # one thread each for the SConstruct file and {in,out}.f[123].  Note that
118 # it's important that the input (source) files sort earlier alphabetically
119 # than the output files, so they're visited first in the dependency graph
120 # walk of '.' and are already considered up-to-date when we kick off the
121 # "simultaneous" builds of the output (target) files.
122
123 test.run(arguments = '-j7 -k .', status = 2, stderr = None)
124
125 missing = []
126 for es in expected_stderr_list:
127     if string.find(test.stderr(), es) == -1:
128         missing.append(es)
129
130 if missing:
131     sys.stderr.write("Missing the following lines from stderr:\n")
132     for m in missing:
133         sys.stderr.write(m)
134     sys.stderr.write('STDERR ===============================================\n')
135     sys.stderr.write(test.stderr())
136     test.fail_test(1)
137
138
139 test.pass_test()