Updates to PR 4374 from mwichmann to correct config file hash changes
[scons.git] / test / Actions / actions.py
blob72317ca786c41ee68b521a78dd65fdc3bca514f5
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 TestSCons
28 _python_ = TestSCons._python_
30 test = TestSCons.TestSCons()
32 test.write('build.py', r"""
33 import sys
34 with open(sys.argv[1], 'wb') as f, open(sys.argv[3], 'rb') as infp:
35 f.write((sys.argv[2] + "\n").encode())
36 f.write(infp.read())
37 sys.exit(0)
38 """)
40 test.write('SConstruct', """
41 B = Builder(action = r'%(_python_)s build.py $TARGET 1 $SOURCES')
42 DefaultEnvironment(tools=[]) # test speedup
43 env = Environment(BUILDERS = { 'B' : B })
44 env.B(target = 'foo.out', source = 'foo.in')
45 """ % locals())
47 test.write('foo.in', "foo.in\n")
49 test.run(arguments = '.')
51 test.must_match('foo.out', '1\nfoo.in\n')
53 test.up_to_date(arguments = '.')
55 test.write('SConstruct', """
56 B = Builder(action = r'%(_python_)s build.py $TARGET 2 $SOURCES')
57 DefaultEnvironment(tools=[]) # test speedup
58 env = Environment(BUILDERS = { 'B' : B })
59 env.B(target = 'foo.out', source = 'foo.in')
60 """ % locals())
62 test.run(arguments = '.')
64 test.must_match('foo.out', '2\nfoo.in\n')
66 test.up_to_date(arguments = '.')
68 test.write('SConstruct', """
69 import subprocess
70 def func(env, target, source):
71 cmd = r'%(_python_)s build.py %%s 3 %%s' %% (' '.join(map(str, target)),
72 ' '.join(map(str, source)))
73 print(cmd)
74 cp = subprocess.run(cmd, shell=True)
75 return cp.returncode
76 B = Builder(action = func)
77 DefaultEnvironment(tools=[]) # test speedup
78 env = Environment(BUILDERS = { 'B' : B })
79 env.B(target = 'foo.out', source = 'foo.in')
80 """ % locals())
82 test.run(arguments = '.', stderr = None)
84 test.must_match('foo.out', '3\nfoo.in\n')
86 test.up_to_date(arguments = '.')
88 test.write('SConstruct', """
89 import subprocess
90 assert 'string' not in globals()
91 class bld:
92 def __init__(self):
93 self.cmd = r'%(_python_)s build.py %%s 4 %%s'
94 def __call__(self, env, target, source):
95 cmd = self.get_contents(env, target, source)
96 print(cmd)
97 cp = subprocess.run(cmd, shell=True)
98 return cp.returncode
99 def get_contents(self, env, target, source):
100 return self.cmd %% (' '.join(map(str, target)),
101 ' '.join(map(str, source)))
102 B = Builder(action = bld())
103 DefaultEnvironment(tools=[]) # test speedup
104 env = Environment(BUILDERS = { 'B' : B })
105 env.B(target = 'foo.out', source = 'foo.in')
106 """ % locals())
108 test.run(arguments = '.')
110 test.must_match('foo.out', '4\nfoo.in\n')
112 test.up_to_date(arguments = '.')
114 # Make sure we can expand actions in substitutions.
115 test.write('SConstruct', """\
116 def func(env, target, source):
117 pass
119 DefaultEnvironment(tools=[]) # test speedup
120 env = Environment(S = Action('foo'),
121 F = Action(func),
122 L = Action(['arg1', 'arg2']))
123 print(env.subst('$S'))
124 print(env.subst('$F'))
125 print(env.subst('$L'))
126 """)
128 test.run(arguments = '-Q .', stdout = """\
130 func(target, source, env)
131 arg1
132 arg2
133 scons: `.' is up to date.
134 """)
136 test.pass_test()
138 # Local Variables:
139 # tab-width:4
140 # indent-tabs-mode:nil
141 # End:
142 # vim: set expandtab tabstop=4 shiftwidth=4: