Move 2.0 changes collected in branches/pending back to trunk for further
[scons.git] / test / Actions / actions.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 test.write('build.py', r"""
34 import sys
35 file = open(sys.argv[1], 'wb')
36 file.write(sys.argv[2] + "\n")
37 file.write(open(sys.argv[3], 'rb').read())
38 file.close
39 sys.exit(0)
40 """)
41
42 test.write('SConstruct', """
43 B = Builder(action = r'%(_python_)s build.py $TARGET 1 $SOURCES')
44 env = Environment(BUILDERS = { 'B' : B })
45 env.B(target = 'foo.out', source = 'foo.in')
46 """ % locals())
47
48 test.write('foo.in', "foo.in\n")
49
50 test.run(arguments = '.')
51
52 test.fail_test(test.read('foo.out') != "1\nfoo.in\n")
53
54 test.up_to_date(arguments = '.')
55
56 test.write('SConstruct', """
57 B = Builder(action = r'%(_python_)s build.py $TARGET 2 $SOURCES')
58 env = Environment(BUILDERS = { 'B' : B })
59 env.B(target = 'foo.out', source = 'foo.in')
60 """ % locals())
61
62 test.run(arguments = '.')
63
64 test.fail_test(test.read('foo.out') != "2\nfoo.in\n")
65
66 test.up_to_date(arguments = '.')
67
68 test.write('SConstruct', """
69 import os
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     return os.system(cmd)
75 B = Builder(action = func)
76 env = Environment(BUILDERS = { 'B' : B })
77 env.B(target = 'foo.out', source = 'foo.in')
78 """ % locals())
79
80 test.run(arguments = '.', stderr = None)
81
82 test.fail_test(test.read('foo.out') != "3\nfoo.in\n")
83
84 test.up_to_date(arguments = '.')
85
86 test.write('SConstruct', """
87 import os
88 assert 'string' not in globals()
89 class bld:
90     def __init__(self):
91         self.cmd = r'%(_python_)s build.py %%s 4 %%s'
92     def __call__(self, env, target, source):
93         cmd = self.get_contents(env, target, source)
94         print cmd
95         return os.system(cmd)
96     def get_contents(self, env, target, source):
97         return self.cmd %% (' '.join(map(str, target)),
98                             ' '.join(map(str, source)))
99 B = Builder(action = bld())
100 env = Environment(BUILDERS = { 'B' : B })
101 env.B(target = 'foo.out', source = 'foo.in')
102 """ % locals())
103
104 test.run(arguments = '.')
105
106 test.fail_test(test.read('foo.out') != "4\nfoo.in\n")
107
108 test.up_to_date(arguments = '.')
109
110 # Make sure we can expand actions in substitutions.
111 test.write('SConstruct', """\
112 def func(env, target, source):
113     pass
114 env = Environment(S = Action('foo'),
115                   F = Action(func),
116                   L = Action(['arg1', 'arg2']))
117 print env.subst('$S')
118 print env.subst('$F')
119 print env.subst('$L')
120 """)
121
122 test.run(arguments = '-Q .', stdout = """\
123 foo
124 func(target, source, env)
125 arg1
126 arg2
127 scons: `.' is up to date.
128 """)
129
130 test.pass_test()
131
132 # Local Variables:
133 # tab-width:4
134 # indent-tabs-mode:nil
135 # End:
136 # vim: set expandtab tabstop=4 shiftwidth=4: