Move 2.0 changes collected in branches/pending back to trunk for further
[scons.git] / test / Actions / function.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 TestSCons
28
29 _python_ = TestSCons._python_
30
31 test = TestSCons.TestSCons()
32
33 #
34 # Test that the signature of function action includes all the
35 # necessary pieces.
36
37
38 test.write('SConstruct', r"""
39 import re
40
41 import SCons.Action
42 import SCons.Builder
43
44 options = Variables()
45 options.AddVariables(
46     ('header', 'Header string (default cell argument)', 'Head:'),
47     ('trailer', 'Trailer string (default cell argument)', 'Tail'),
48     ('NbDeps', 'Number of dependencies', '2'),
49     ('separator', 'Separator for the dependencies (function constant)', ':'),
50     ('closure_cell_value', 'Value of a closure cell', '25'),
51     ('b', 'Value of b (value default argument', '7'),
52     ('regexp', 'Regexp (object as a default argument', 'a(a*)'),
53     ('docstring', 'Docstring', 'docstring'),
54     ('extracode', 'Extra code for the builder function', ''),
55     ('extraarg', 'Extra arg builder function', ''),
56     ('nestedfuncexp', 'Expression for the nested function', 'xxx - b'),
57 )
58
59 optEnv = Environment(options=options, tools=[])
60
61 r = re.compile(optEnv['regexp'])
62
63 withClosure = \
64 r'''
65 def toto(header='%(header)s', trailer='%(trailer)s'):
66     xxx = %(closure_cell_value)s
67     def writeDeps(target, source, env, b=%(b)s, r=r %(extraarg)s ,
68                   header=header, trailer=trailer):
69         """+'"""%(docstring)s"""'+"""
70         def foo(b=b):
71             return %(nestedfuncexp)s
72         f = open(str(target[0]),'wb')
73         f.write(header)
74         for d in env['ENVDEPS']:
75             f.write(d+'%(separator)s')
76         f.write(trailer+'\\n')
77         f.write(str(foo())+'\\n')
78         f.write(r.match('aaaa').group(1)+'\\n')
79         %(extracode)s
80         try:
81            f.write(str(xarg)+'\\n')
82         except NameError:
83            pass
84         f.close()
85
86     return writeDeps
87 '''
88
89 NoClosure = \
90 r'''
91 def toto(header='%(header)s', trailer='%(trailer)s'):
92     xxx = %(closure_cell_value)s
93     def writeDeps(target, source, env, b=%(b)s, r=r %(extraarg)s ,
94                   header=header, trailer=trailer, xxx=xxx):
95         """+'"""%(docstring)s"""'+"""
96         def foo(b=b, xxx=xxx):
97             return %(nestedfuncexp)s
98         f = open(str(target[0]),'wb')
99         f.write(header)
100         for d in env['ENVDEPS']:
101             f.write(d+'%(separator)s')
102         f.write(trailer+'\\n')
103         f.write(str(foo())+'\\n')
104         f.write(r.match('aaaa').group(1)+'\\n')
105         %(extracode)s
106         try:
107            f.write(str(xarg)+'\\n')
108         except NameError:
109            pass
110         f.close()
111
112     return writeDeps
113 '''
114
115 try:
116     # Check that lexical closure are supported
117     def a():
118         x = 0
119         def b():
120             return x
121         return b
122     a().func_closure[0].cell_contents
123     exec( withClosure % optEnv )
124 except (AttributeError, TypeError):
125     exec( NoClosure % optEnv )
126
127 genHeaderBld = SCons.Builder.Builder(
128     action = SCons.Action.Action(
129         toto(), 
130         'Generating $TARGET',
131         varlist=['ENVDEPS']
132         ),
133     suffix = '.gen.h'
134     )
135
136 env = Environment()
137 env.Append(BUILDERS = {'GenHeader' : genHeaderBld})
138
139 envdeps = list(map(str, range(int(optEnv['NbDeps']))))
140
141 env.GenHeader('Out', None, ENVDEPS=envdeps)
142 """)
143
144
145 rebuildstr = """\
146 scons: Reading SConscript files ...
147 scons: done reading SConscript files.
148 scons: Building targets ...
149 Generating Out.gen.h
150 scons: done building targets.
151 """
152
153 nobuildstr = """\
154 scons: Reading SConscript files ...
155 scons: done reading SConscript files.
156 scons: Building targets ...
157 scons: `.' is up to date.
158 scons: done building targets.
159 """
160
161 import sys
162 if sys.version[:3] == '2.1':
163     expectedStderr = """\
164 %s:79: SyntaxWarning: local name 'x' in 'a' shadows use of 'x' as global in nested scope 'b'
165   def a():
166 """ % test.workpath('SConstruct')
167 else:
168     expectedStderr = ""
169
170 def runtest(arguments, expectedOutFile, expectedRebuild=True, stderr=expectedStderr):
171     test.run(arguments=arguments,
172              stdout=expectedRebuild and rebuildstr or nobuildstr,
173              stderr=expectedStderr)
174     test.must_match('Out.gen.h', expectedOutFile)
175
176     # Should not be rebuild when ran a second time with the same
177     # arguments.
178
179     test.run(arguments = arguments, stdout=nobuildstr, stderr=expectedStderr)
180     test.must_match('Out.gen.h', expectedOutFile)
181
182
183 # Original build. 
184 runtest('', """Head:0:1:Tail\n18\naaa\n""")
185
186 # Changing a docstring should not cause a rebuild
187 runtest('docstring=ThisBuilderDoesXAndY', """Head:0:1:Tail\n18\naaa\n""", False)
188 runtest('docstring=SuperBuilder', """Head:0:1:Tail\n18\naaa\n""", False)
189 runtest('docstring=', """Head:0:1:Tail\n18\naaa\n""", False)
190
191 # Changing a variable listed in the varlist should cause a rebuild
192 runtest('NbDeps=3', """Head:0:1:2:Tail\n18\naaa\n""")
193 runtest('NbDeps=4', """Head:0:1:2:3:Tail\n18\naaa\n""")
194 runtest('', """Head:0:1:Tail\n18\naaa\n""")
195
196 # Changing the function code should cause a rebuild
197 runtest('extracode=f.write("XX\\n")', """Head:0:1:Tail\n18\naaa\nXX\n""")
198 runtest('extracode=a=2', """Head:0:1:Tail\n18\naaa\n""")
199 runtest('', """Head:0:1:Tail\n18\naaa\n""")
200
201 # Changing a constant used in the function code should cause a rebuild
202 runtest('separator=,', """Head:0,1,Tail\n18\naaa\n""")
203 runtest('separator=;', """Head:0;1;Tail\n18\naaa\n""")
204 runtest('', """Head:0:1:Tail\n18\naaa\n""")
205
206 # Changing the code of a nested function should cause a rebuild
207 runtest('nestedfuncexp=b-xxx', """Head:0:1:Tail\n-18\naaa\n""")
208 runtest('nestedfuncexp=b+xxx', """Head:0:1:Tail\n32\naaa\n""")
209 runtest('', """Head:0:1:Tail\n18\naaa\n""")
210
211 # Adding an extra argument should cause a rebuild.
212 runtest('extraarg=,xarg=2', """Head:0:1:Tail\n18\naaa\n2\n""")
213 runtest('extraarg=,xarg=5', """Head:0:1:Tail\n18\naaa\n5\n""")
214 runtest('', """Head:0:1:Tail\n18\naaa\n""")
215
216 # Changing the value of a default argument should cause a rebuild
217 # case 1: a value
218 runtest('b=0', """Head:0:1:Tail\n25\naaa\n""")
219 runtest('b=9', """Head:0:1:Tail\n16\naaa\n""")
220 runtest('', """Head:0:1:Tail\n18\naaa\n""")
221
222 # case 2: an object
223 runtest('regexp=(aaaa)', """Head:0:1:Tail\n18\naaaa\n""")
224 runtest('regexp=aa(a+)', """Head:0:1:Tail\n18\naa\n""")
225 runtest('', """Head:0:1:Tail\n18\naaa\n""")
226
227 # Changing the value of a closure cell value should cause a rebuild
228 # case 1: a value
229 runtest('closure_cell_value=32', """Head:0:1:Tail\n25\naaa\n""")
230 runtest('closure_cell_value=7', """Head:0:1:Tail\n0\naaa\n""")
231 runtest('', """Head:0:1:Tail\n18\naaa\n""")
232
233 # case 2: a default argument
234 runtest('header=MyHeader:', """MyHeader:0:1:Tail\n18\naaa\n""")
235 runtest('trailer=MyTrailer', """Head:0:1:MyTrailer\n18\naaa\n""")
236 runtest('', """Head:0:1:Tail\n18\naaa\n""")
237
238 test.pass_test()
239
240 # Local Variables:
241 # tab-width:4
242 # indent-tabs-mode:nil
243 # End:
244 # vim: set expandtab tabstop=4 shiftwidth=4: