ef8c483d3742e6c8930b76203fd925c44878527b
[scons.git] / test / strfunction.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 how using strfunction() to report different types of
29 """
30
31 import TestSCons
32
33 python = TestSCons.python
34
35 test = TestSCons.TestSCons()
36
37 test.write('cat.py', """\
38 import sys
39 open(sys.argv[2], "wb").write(open(sys.argv[1], "rb").read())
40 sys.exit(0)
41 """)
42
43 test.write('SConstruct', """\
44 def strfunction(target, source, env):
45     t = str(target[0])
46     s = str(source[0])
47     return "Building %%s from %%s" %% (t, s)
48 def func(target, source, env):
49     t = str(target[0])
50     s = str(source[0])
51     open(t, 'w').write(open(s, 'r').read())
52 funcaction = Action(func, strfunction=strfunction)
53 cmd = r"%s cat.py $SOURCE $TARGET"
54 cmdaction = Action(cmd, strfunction=strfunction)
55 list = [ r"%s cat.py $SOURCE .temp", r"%s cat.py .temp $TARGET" ]
56 listaction = Action(list, strfunction=strfunction)
57 lazy = '$LAZY'
58 lazyaction = Action(lazy, strfunction=strfunction)
59 dict = {
60     '.cmd'      : cmd,
61     '.cmdstr'   : cmdaction,
62     '.func'     : func,
63     '.funcstr'  : funcaction,
64     '.list'     : list,
65     '.liststr'  : listaction,
66     '.lazy'     : lazy,
67     '.lazystr'  : lazyaction,
68 }
69 env = Environment(BUILDERS = {
70                         'Cmd'           : Builder(action=cmd),
71                         'CmdStr'        : Builder(action=cmdaction),
72                         'Func'          : Builder(action=func),
73                         'FuncStr'       : Builder(action=funcaction),
74                         'Lazy'          : Builder(action=lazy),
75                         'LazyStr'       : Builder(action=lazyaction),
76                         'List'          : Builder(action=list),
77                         'ListStr'       : Builder(action=listaction),
78
79                         'Dict'          : Builder(action=dict),
80                   },
81                   LAZY = r"%s cat.py $SOURCE $TARGET")
82 env.Cmd('cmd.out', 'cmd.in')
83 env.CmdStr('cmdstr.out', 'cmdstr.in')
84 env.Func('func.out', 'func.in')
85 env.FuncStr('funcstr.out', 'funcstr.in')
86 env.Lazy('lazy.out', 'lazy.in')
87 env.LazyStr('lazystr.out', 'lazystr.in')
88 env.List('list.out', 'list.in')
89 env.ListStr('liststr.out', 'liststr.in')
90
91 env.Dict('dict1.out', 'dict1.cmd')
92 env.Dict('dict2.out', 'dict2.cmdstr')
93 env.Dict('dict3.out', 'dict3.func')
94 env.Dict('dict4.out', 'dict4.funcstr')
95 env.Dict('dict5.out', 'dict5.lazy')
96 env.Dict('dict6.out', 'dict6.lazystr')
97 env.Dict('dict7.out', 'dict7.list')
98 env.Dict('dict8.out', 'dict8.liststr')
99 """ % (python, python, python, python))
100
101 test.write('func.in',           "func.in\n")
102 test.write('funcstr.in',        "funcstr.in\n")
103 test.write('cmd.in',            "cmd.in\n")
104 test.write('cmdstr.in',         "cmdstr.in\n")
105 test.write('lazy.in',           "lazy.in\n")
106 test.write('lazystr.in',        "lazystr.in\n")
107 test.write('list.in',           "list.in\n")
108 test.write('liststr.in',        "liststr.in\n")
109
110 test.write('dict1.cmd',         "dict1.cmd\n")
111 test.write('dict2.cmdstr',      "dict2.cmdstr\n")
112 test.write('dict3.func',        "dict3.func\n")
113 test.write('dict4.funcstr',     "dict4.funcstr\n")
114 test.write('dict5.lazy',        "dict4.lazy\n")
115 test.write('dict6.lazystr',     "dict6.lazystr\n")
116 test.write('dict7.list',        "dict7.list\n")
117 test.write('dict8.liststr',     "dict8.liststr\n")
118
119 test.run(arguments = '.', stdout=test.wrap_stdout("""\
120 %s cat.py cmd.in cmd.out
121 Building cmdstr.out from cmdstr.in
122 %s cat.py dict1.cmd dict1.out
123 Building dict2.out from dict2.cmdstr
124 func(["dict3.out"], ["dict3.func"])
125 Building dict4.out from dict4.funcstr
126 %s cat.py dict5.lazy dict5.out
127 Building dict6.out from dict6.lazystr
128 %s cat.py dict7.list .temp
129 %s cat.py .temp dict7.out
130 Building dict8.out from dict8.liststr
131 Building dict8.out from dict8.liststr
132 func(["func.out"], ["func.in"])
133 Building funcstr.out from funcstr.in
134 %s cat.py lazy.in lazy.out
135 Building lazystr.out from lazystr.in
136 %s cat.py list.in .temp
137 %s cat.py .temp list.out
138 Building liststr.out from liststr.in
139 Building liststr.out from liststr.in
140 """) % (python, python, python, python, python, python, python, python))
141
142 test.pass_test()