Merge branch 'master' into jbrill-msvc-detect
[scons.git] / test / Default.py
blobf3fe5339110fb14127a1eccac546128ad05d4ac1
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 """
27 Verify various combinations of arguments to Default() work properly.
28 """
30 import os
32 import TestSCons
34 _python_ = TestSCons._python_
36 test = TestSCons.TestSCons()
38 for dirname in ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight']:
39 test.subdir(dirname)
40 test.write(os.path.join(dirname, 'foo.in'), dirname + "/foo.in\n")
41 test.write(os.path.join(dirname, 'bar.in'), dirname + "/bar.in\n")
43 test.write('build.py', r"""
44 import sys
45 with open(sys.argv[1], 'w') as f, open(sys.argv[2], 'r') as ifp:
46 f.write(ifp.read())
47 """)
50 test.write(['one', 'SConstruct'], """
51 B = Builder(action = r'%(_python_)s ../build.py $TARGET $SOURCES')
52 DefaultEnvironment(tools=[]) # test speedup
53 env = Environment(BUILDERS = { 'B' : B })
54 env.B(target = 'foo.out', source = 'foo.in')
55 env.B(target = 'bar.out', source = 'bar.in')
56 Default('foo.out')
57 """ % locals())
59 test.write(['two', 'SConstruct'], """
60 B = Builder(action = r'%(_python_)s ../build.py $TARGET $SOURCES')
61 DefaultEnvironment(tools=[]) # test speedup
62 env = Environment(BUILDERS = { 'B' : B })
63 env.B(target = 'foo.out', source = 'foo.in')
64 env.B(target = 'bar.out', source = 'bar.in')
65 Default('foo.out', 'bar.out')
66 """ % locals())
68 test.write(['three', 'SConstruct'], """
69 B = Builder(action = r'%(_python_)s ../build.py $TARGET $SOURCES')
70 DefaultEnvironment(tools=[]) # test speedup
71 env = Environment(BUILDERS = { 'B' : B })
72 env.B(target = 'foo.out', source = 'foo.in')
73 env.B(target = 'bar.out', source = 'bar.in')
74 Default(Split('foo.out bar.out'))
75 """ % locals())
77 test.write(['four', 'SConstruct'], """
78 B = Builder(action = r'%(_python_)s ../build.py $TARGET $SOURCES')
79 DefaultEnvironment(tools=[]) # test speedup
80 env = Environment(BUILDERS = { 'B' : B })
81 env.B(target = ['foo bar'], source = 'foo.in')
82 env.B(target = 'foo', source = 'foo.in')
83 env.B(target = 'bar', source = 'bar.in')
84 Default(['foo bar'])
85 """ % locals())
87 test.write(['five', 'SConstruct'], """
88 B = Builder(action = r'%(_python_)s ../build.py $TARGET $SOURCES')
89 DefaultEnvironment(tools=[]) # test speedup
90 env = Environment(BUILDERS = { 'B' : B })
91 Default(env.B(target = 'foo.out', source = 'foo.in'))
92 Default(env.B(target = 'bar.out', source = 'bar.in'))
93 """ % locals())
96 for dirname in ['one', 'two', 'three', 'four', 'five']:
97 test.run(chdir=dirname) # no arguments, use the Default
99 test.must_match(test.workpath('one', 'foo.out'), "one/foo.in\n", mode='r')
100 test.fail_test(os.path.exists(test.workpath('one', 'bar')))
102 test.must_match(test.workpath('two', 'foo.out'), "two/foo.in\n", mode='r')
103 test.must_match(test.workpath('two', 'bar.out'), "two/bar.in\n", mode='r')
105 test.must_match(test.workpath('three', 'foo.out'), "three/foo.in\n", mode='r')
106 test.must_match(test.workpath('three', 'bar.out'), "three/bar.in\n", mode='r')
108 test.fail_test(os.path.exists(test.workpath('four', 'foo')))
109 test.fail_test(os.path.exists(test.workpath('four', 'bar')))
110 test.must_match(test.workpath('four', 'foo bar'), "four/foo.in\n", mode='r')
112 test.must_match(test.workpath('five', 'foo.out'), "five/foo.in\n", mode='r')
113 test.must_match(test.workpath('five', 'bar.out'), "five/bar.in\n", mode='r')
116 # Test how a None Default() argument works to disable/reset default targets.
117 test.write(['six', 'SConstruct'], """\
118 B = Builder(action = r'%(_python_)s ../build.py $TARGET $SOURCES')
119 DefaultEnvironment(tools=[]) # test speedup
120 env = Environment(BUILDERS = { 'B' : B })
121 foo = env.B(target = 'foo.out', source = 'foo.in')
122 bar = env.B(target = 'bar.out', source = 'bar.in')
123 Default(None)
124 """ % locals())
126 test.run(chdir='six', status=2,
127 stderr="scons: *** No targets specified and no Default() targets found. Stop.\n")
129 test.write(['seven', 'SConstruct'], """\
130 B = Builder(action = r'%(_python_)s ../build.py $TARGET $SOURCES')
131 DefaultEnvironment(tools=[]) # test speedup
132 env = Environment(BUILDERS = { 'B' : B })
133 foo = env.B(target = 'foo.out', source = 'foo.in')
134 bar = env.B(target = 'bar.out', source = 'bar.in')
135 Default(foo, bar, None)
136 """ % locals())
138 test.run(chdir='seven', status=2,
139 stderr="scons: *** No targets specified and no Default() targets found. Stop.\n")
141 test.write(['eight', 'SConstruct'], """\
142 B = Builder(action = r'%(_python_)s ../build.py $TARGET $SOURCES')
143 DefaultEnvironment(tools=[]) # test speedup
144 env = Environment(BUILDERS = { 'B' : B })
145 foo = env.B(target = 'foo.out', source = 'foo.in')
146 bar = env.B(target = 'bar.out', source = 'bar.in')
147 Default(foo, None, bar)
148 """ % locals())
150 test.run(chdir='eight') # no arguments, use the Default
152 test.fail_test(os.path.exists(test.workpath('eight', 'foo.out')))
153 test.must_match(test.workpath('eight', 'bar.out'), "eight/bar.in\n", mode='r')
156 test.subdir('nine', ['nine', 'sub1'])
158 test.write(['nine', 'SConstruct'], """\
159 B = Builder(action = r'%(_python_)s build.py $TARGET $SOURCES')
160 DefaultEnvironment(tools=[]) # test speedup
161 env = Environment(BUILDERS = { 'B' : B })
162 env.B(target = 'xxx.out', source = 'xxx.in')
163 SConscript('sub1/SConscript')
164 """ % locals())
166 test.write(['nine', 'xxx.in'], "xxx.in\n")
168 test.write(['nine', 'sub1', 'SConscript'], """
169 B = Builder(action = r'%(_python_)s ../build.py $TARGET $SOURCES')
170 DefaultEnvironment(tools=[]) # test speedup
171 env = Environment(BUILDERS = { 'B' : B })
172 env.B(target = 'xxx.out', source = 'xxx.in')
173 Default('xxx.out')
174 """ % locals())
176 test.write(['nine', 'sub1', 'xxx.in'], "sub1/xxx.in\n")
178 test.run(chdir='nine') # no arguments, use the Default
180 test.fail_test(os.path.exists(test.workpath('nine', 'xxx.out')))
181 test.must_match(test.workpath('nine', 'sub1', 'xxx.out'), "sub1/xxx.in\n", mode='r')
184 test.subdir('ten', ['ten', 'sub2'])
186 test.write(['ten', 'SConstruct'], """\
187 Default('sub2')
188 B = Builder(action = r'%(_python_)s ../build.py $TARGET $SOURCES')
189 DefaultEnvironment(tools=[]) # test speedup
190 env = Environment(BUILDERS = { 'B' : B })
191 env.B(target = 'xxx.out', source = 'xxx.in')
192 SConscript('sub2/SConscript')
193 """ % locals())
195 test.write(['ten', 'xxx.in'], "xxx.in\n")
197 test.write(['ten', 'sub2', 'SConscript'], """
198 B = Builder(action = r'%(_python_)s ../build.py $TARGET $SOURCES')
199 DefaultEnvironment(tools=[]) # test speedup
200 env = Environment(BUILDERS = { 'B' : B })
201 env.B(target = 'xxx.out', source = 'xxx.in')
202 """ % locals())
204 test.write(['ten', 'sub2', 'xxx.in'], "sub2/xxx.in\n")
206 test.run(chdir='ten') # no arguments, use the Default
208 test.fail_test(os.path.exists(test.workpath('ten', 'xxx.out')))
209 test.must_match(test.workpath('ten', 'sub2', 'xxx.out'), "sub2/xxx.in\n", mode='r')
212 test.subdir('eleven')
214 test.write(['eleven', 'SConstruct'], """
215 B = Builder(action = r'%(_python_)s ../build.py $TARGET $SOURCES')
216 DefaultEnvironment(tools=[]) # test speedup
217 env = Environment(BUILDERS = { 'B' : B }, XXX = 'foo.out')
218 env.B(target = 'foo.out', source = 'foo.in')
219 env.B(target = 'bar.out', source = 'bar.in')
220 env.Default('$XXX')
221 """ % locals())
223 test.write(os.path.join('eleven', 'foo.in'), "eleven/foo.in\n")
225 test.write(os.path.join('eleven', 'bar.in'), "eleven/bar.in\n")
227 test.run(chdir='eleven') # no arguments, use the Default
229 test.must_match(test.workpath('eleven', 'foo.out'), "eleven/foo.in\n", mode='r')
230 test.fail_test(os.path.exists(test.workpath('eleven', 'bar')))
233 test.pass_test()
235 # Local Variables:
236 # tab-width:4
237 # indent-tabs-mode:nil
238 # End:
239 # vim: set expandtab tabstop=4 shiftwidth=4: