Merged revisions 1968-2115 via svnmerge from
[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 # correectly 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 = 'f1', source = 'f1.in')
79 """ % locals())
80
81 test.write('f1.in', "f1.in\n")
82
83 expected_stderr = "scons: \*\*\* \[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 = 'f1', source = 'f1.in')
97 env.Fail(target = 'f2', source = 'f2.in')
98 env.Fail(target = 'f3', source = 'f3.in')
99 """ % locals())
100
101 # f2.in is not created to cause a Task.prepare exception
102 test.write('f3.in', 'f3.in\n')
103
104 # In Serial task mode, get the first exception and stop
105 test.run(arguments = '.', status = 2, stderr = expected_stderr)
106
107 # In Parallel task mode, we will get all three exceptions.
108
109 expected_stderr_list = [
110   expected_stderr,
111   "scons: \*\*\* Source `f2\.in' not found, needed by target `f2'\.  Stop\.\n",
112   string.replace(expected_stderr, 'f1', 'f3')
113   ]
114   
115 # Unfortunately, we aren't guaranteed what order we will get the
116 # exceptions in...
117 orders = [ (1,2,3), (1,3,2), (2,1,3), (2,3,1), (3,1,2), (3,2,1) ]
118 otexts = []
119 for A,B,C in orders:
120     otexts.append("%s%s%s"%(expected_stderr_list[A-1],
121                             expected_stderr_list[B-1],
122                             expected_stderr_list[C-1]))
123                            
124                       
125 expected_stderrs = "(" + string.join(otexts, "|") + ")"
126
127 test.run(arguments = '-j3 .', status = 2, stderr = expected_stderrs)
128
129
130 test.pass_test()