Support construction variable expansion anywhere in a file or path name.
[scons.git] / test / SourceCode.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 """
28 Test fetching source files using the SourceCode() method.
29 """
30
31 import os
32 import stat
33
34 import TestSCons
35
36 test = TestSCons.TestSCons()
37
38 test.subdir('sub')
39
40 test.write('SConstruct', """\
41 import os.path
42
43 def cat(env, source, target):
44     target = str(target[0])
45     source = map(str, source)
46     f = open(target, "wb")
47     for src in source:
48         f.write(open(src, "rb").read())
49     f.close()
50
51 def sc_cat(env, source, target):
52     source = []
53     for t in target:
54         head, tail = os.path.split(str(t))
55         source.append(os.path.join(head, 'sc-' + tail))
56     cat(env, source, target)
57
58 env = Environment(BUILDERS={'Cat':Builder(action=cat)}, SUBDIR='sub')
59 env.Cat('aaa.out', 'sub/aaa.in')
60 env.Cat('bbb.out', 'sub/bbb.in')
61 env.Cat('ccc.out', 'sub/ccc.in')
62 env.Cat('all', ['aaa.out', 'bbb.out', 'ccc.out'])
63 env.SourceCode('$SUBDIR', Builder(action=sc_cat, env=env))
64 SConscript('sub/SConscript', "env")
65 """)
66
67 test.write(['sub', 'sc-aaa.in'], "sub/sc-aaa.in\n")
68 test.write(['sub', 'sc-bbb.in'], "sub/sc-bbb.in\n")
69 test.write(['sub', 'sc-ccc.in'], "sub/sc-ccc.in\n")
70
71 test.write(['sub', 'sc-SConscript'], "'sub/sc-SConscript'\n")
72
73 test.run(arguments = '.',
74          stdout = test.wrap_stdout(read_str = """\
75 sc_cat("%s", [])
76 """ % (os.path.join('sub', 'SConscript')),
77                                    build_str = """\
78 sc_cat("%s", [])
79 cat("aaa.out", "%s")
80 sc_cat("%s", [])
81 cat("bbb.out", "%s")
82 sc_cat("%s", [])
83 cat("ccc.out", "%s")
84 cat("all", ["aaa.out", "bbb.out", "ccc.out"])
85 """ % (os.path.join('sub', 'aaa.in'),
86        os.path.join('sub', 'aaa.in'),
87        os.path.join('sub', 'bbb.in'),
88        os.path.join('sub', 'bbb.in'),
89        os.path.join('sub', 'ccc.in'),
90        os.path.join('sub', 'ccc.in'))))
91
92 test.fail_test(test.read(['sub', 'SConscript']) != "'sub/sc-SConscript'\n")
93 test.fail_test(test.read('all') != "sub/sc-aaa.in\nsub/sc-bbb.in\nsub/sc-ccc.in\n")
94
95 test.pass_test()