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