Merged revisions 1582-1665 via svnmerge from
[scons.git] / test / Command.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 sys
28 import TestSCons
29
30 python = TestSCons.python
31 _python_ = TestSCons._python_
32
33 test = TestSCons.TestSCons()
34
35 test.subdir('sub')
36
37 test.write('build.py', r"""
38 import sys
39 contents = open(sys.argv[2], 'rb').read()
40 file = open(sys.argv[1], 'wb')
41 file.write(contents)
42 file.close()
43 """)
44
45 test.write('SConstruct', """
46 import os
47
48 def buildIt(env, target, source):
49     contents = open(str(source[0]), 'rb').read()
50     file = open(str(target[0]), 'wb')
51     xyzzy = env.get('XYZZY', '')
52     if xyzzy:
53         file.write(xyzzy + '\\n')
54     file.write(contents)
55     file.close()
56     return 0
57
58 def sub(env, target, source):
59     target = str(target[0])
60     source = str(source[0])
61     t = open(target, 'wb')
62     files = os.listdir(source)
63     files.sort()
64     for f in files:
65         t.write(open(os.path.join(source, f), 'rb').read())
66     t.close()
67     return 0
68
69 env = Environment(COPY_THROUGH_TEMP = '%(_python_)s build.py .tmp $SOURCE\\n%(_python_)s build.py $TARGET .tmp',
70                   EXPAND = '$COPY_THROUGH_TEMP')
71 env.Command(target = 'f1.out', source = 'f1.in',
72             action = buildIt)
73 env.Command(target = 'f2.out', source = 'f2.in',
74             action = r'%(_python_)s build.py temp2 $SOURCES' + '\\n' + r'%(_python_)s build.py $TARGET temp2')
75 env.Command(target = 'f3.out', source = 'f3.in',
76             action = [ [ r'%(python)s', 'build.py', 'temp3', '$SOURCES' ],
77                        [ r'%(python)s', 'build.py', '$TARGET', 'temp3'] ])
78 Command(target = 'f4.out', source = 'sub', action = sub)
79 env.Command(target = 'f5.out', source = 'f5.in', action = buildIt,
80             XYZZY='XYZZY is set')
81 Command(target = 'f6.out', source = 'f6.in',
82         action = r'%(_python_)s build.py f6.out f6.in')
83 env.Command(target = 'f7.out', source = 'f7.in',
84             action = r'%(_python_)s build.py $TARGET $SOURCE')
85 Command(target = 'f8.out', source = 'f8.in',
86         action = r'%(_python_)s build.py $TARGET $SOURCE')
87 env.Command(target = 'f9.out', source = 'f9.in',
88             action = r'$EXPAND')
89 env.Command(target = '${F10}.out', source = '${F10}.in',
90             action = r'%(_python_)s build.py $TARGET $SOURCE',
91             F10 = 'f10')
92 """ % locals())
93
94 test.write('f1.in', "f1.in\n")
95 test.write('f2.in', "f2.in\n")
96 test.write('f3.in', "f3.in\n")
97 test.write(['sub', 'f4a'], "sub/f4a\n")
98 test.write(['sub', 'f4b'], "sub/f4b\n")
99 test.write(['sub', 'f4c'], "sub/f4c\n")
100 test.write('f5.in', "f5.in\n")
101 test.write('f6.in', "f6.in\n")
102 test.write('f7.in', "f7.in\n")
103 test.write('f8.in', "f8.in\n")
104 test.write('f9.in', "f9.in\n")
105 test.write('f10.in', "f10.in\n")
106
107 test.run(arguments = '.')
108
109 test.must_match('f1.out', "f1.in\n")
110 test.must_match('f2.out', "f2.in\n")
111 test.must_match('f3.out', "f3.in\n")
112 test.must_match('f4.out', "sub/f4a\nsub/f4b\nsub/f4c\n")
113 test.must_match('f5.out', "XYZZY is set\nf5.in\n")
114 test.must_match('f6.out', "f6.in\n")
115 test.must_match('f7.out', "f7.in\n")
116 test.must_match('f8.out', "f8.in\n")
117 test.must_match('f9.out', "f9.in\n")
118 test.must_match('f10.out', "f10.in\n")
119
120 test.pass_test()