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