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:
13 # The above copyright notice and this permission notice shall be included
14 # in all copies or substantial portions of the Software.
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.
25 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
28 Test various uses of the -c (clean) option.
35 python = TestSCons.python
37 test = TestSCons.TestSCons()
39 test.write('build.py', r"""
41 contents = open(sys.argv[2], 'rb').read()
42 file = open(sys.argv[1], 'wb')
47 test.write('SConstruct', """
48 B = Builder(action = r'%s build.py $TARGETS $SOURCES')
49 env = Environment(BUILDERS = { 'B' : B })
50 env.B(target = 'foo1.out', source = 'foo1.in')
51 env.B(target = 'foo2.out', source = 'foo2.xxx')
52 env.B(target = 'foo2.xxx', source = 'foo2.in')
53 env.B(target = 'foo3.out', source = 'foo3.in')
55 if hasattr(os, 'symlink'):
56 def symlink1(env, target, source):
57 # symlink to a file that exists
58 os.symlink(str(source[0]), str(target[0]))
59 env.Command(target = 'symlink1', source = 'foo1.in', action = symlink1)
60 def symlink2(env, target, source):
61 # force symlink to a file that doesn't exist
62 os.symlink('does_not_exist', str(target[0]))
63 env.Command(target = 'symlink2', source = 'foo1.in', action = symlink2)
64 # Test handling of Builder calls that have multiple targets.
65 env.Command(['touch1.out', 'touch2.out'],
67 [Touch('${TARGETS[0]}'), Touch('${TARGETS[1]}')])
70 test.write('foo1.in', "foo1.in\n")
72 test.write('foo2.in', "foo2.in\n")
74 test.write('foo3.in', "foo3.in\n")
76 test.run(arguments = 'foo1.out foo2.out foo3.out')
78 test.must_match(test.workpath('foo1.out'), "foo1.in\n")
79 test.must_match(test.workpath('foo2.xxx'), "foo2.in\n")
80 test.must_match(test.workpath('foo2.out'), "foo2.in\n")
81 test.must_match(test.workpath('foo3.out'), "foo3.in\n")
83 test.run(arguments = '-c foo1.out',
84 stdout = test.wrap_stdout("Removed foo1.out\n", cleaning=1))
86 test.must_not_exist(test.workpath('foo1.out'))
87 test.must_exist(test.workpath('foo2.xxx'))
88 test.must_exist(test.workpath('foo2.out'))
89 test.must_exist(test.workpath('foo3.out'))
91 test.run(arguments = '--clean foo2.out foo2.xxx',
92 stdout = test.wrap_stdout("Removed foo2.xxx\nRemoved foo2.out\n",
95 test.must_not_exist(test.workpath('foo1.out'))
96 test.must_not_exist(test.workpath('foo2.xxx'))
97 test.must_not_exist(test.workpath('foo2.out'))
98 test.must_exist(test.workpath('foo3.out'))
100 test.run(arguments = '--remove foo3.out',
101 stdout = test.wrap_stdout("Removed foo3.out\n", cleaning=1))
103 test.must_not_exist(test.workpath('foo1.out'))
104 test.must_not_exist(test.workpath('foo2.xxx'))
105 test.must_not_exist(test.workpath('foo2.out'))
106 test.must_not_exist(test.workpath('foo3.out'))
108 test.run(arguments = '.')
110 test.must_match(test.workpath('foo1.out'), "foo1.in\n")
111 test.must_match(test.workpath('foo2.xxx'), "foo2.in\n")
112 test.must_match(test.workpath('foo2.out'), "foo2.in\n")
113 test.must_match(test.workpath('foo3.out'), "foo3.in\n")
114 test.must_match(test.workpath('foo3.out'), "foo3.in\n")
115 test.must_exist(test.workpath('touch1.out'))
116 test.must_exist(test.workpath('touch2.out'))
118 if hasattr(os, 'symlink'):
119 test.fail_test(not os.path.islink(test.workpath('symlink1')))
120 test.fail_test(not os.path.islink(test.workpath('symlink2')))
122 test.run(arguments = '-c foo2.xxx',
123 stdout = test.wrap_stdout("Removed foo2.xxx\n", cleaning=1))
125 test.must_match(test.workpath('foo1.out'), "foo1.in\n")
126 test.must_not_exist(test.workpath('foo2.xxx'))
127 test.must_match(test.workpath('foo2.out'), "foo2.in\n")
128 test.must_match(test.workpath('foo3.out'), "foo3.in\n")
129 test.must_exist(test.workpath('touch1.out'))
130 test.must_exist(test.workpath('touch2.out'))
132 test.run(arguments = '-c .')
134 test.must_not_exist(test.workpath('foo1.out'))
135 test.must_not_exist(test.workpath('foo2.out'))
136 test.must_not_exist(test.workpath('foo3.out'))
137 test.must_not_exist(test.workpath('touch1.out'))
138 test.must_not_exist(test.workpath('touch2.out'))
140 if hasattr(os, 'symlink'):
141 test.fail_test(os.path.islink(test.workpath('symlink1')))
142 test.fail_test(os.path.islink(test.workpath('symlink2')))
144 args = 'foo1.out foo2.out foo3.out touch1.out'
146 expect = test.wrap_stdout("""\
155 test.run(arguments = args)
157 test.run(arguments = '-c -n ' + args, stdout = expect)
159 test.run(arguments = '-n -c ' + args, stdout = expect)
161 test.must_match(test.workpath('foo1.out'), "foo1.in\n")
162 test.must_match(test.workpath('foo2.xxx'), "foo2.in\n")
163 test.must_match(test.workpath('foo2.out'), "foo2.in\n")
164 test.must_match(test.workpath('foo3.out'), "foo3.in\n")
165 test.must_exist(test.workpath('touch1.out'))
166 test.must_exist(test.workpath('touch2.out'))
168 test.writable('.', 0)
169 f = open(test.workpath('foo1.out'))
170 test.run(arguments = '-c foo1.out',
171 stdout = test.wrap_stdout("scons: Could not remove 'foo1.out': Permission denied\n", cleaning=1))
172 test.must_exist(test.workpath('foo1.out'))
174 test.writable('.', 1)
177 test.write(['subd', 'foon.in'], "foon.in\n")
178 test.write(['subd', 'foox.in'], "foox.in\n")
179 test.write('aux1.x', "aux1.x\n")
180 test.write('aux2.x', "aux2.x\n")
181 test.write('SConstruct', """
182 B = Builder(action = r'%s build.py $TARGETS $SOURCES')
183 env = Environment(BUILDERS = { 'B' : B }, FOO = 'foo2')
184 env.B(target = 'foo1.out', source = 'foo1.in')
185 env.B(target = 'foo2.out', source = 'foo2.xxx')
186 foo2_xxx = env.B(target = 'foo2.xxx', source = 'foo2.in')
187 env.B(target = 'foo3.out', source = 'foo3.in')
188 SConscript('subd/SConscript')
189 Clean(foo2_xxx, ['aux1.x'])
190 env.Clean(['${FOO}.xxx'], ['aux2.x'])
194 test.write(['subd', 'SConscript'], """
195 Clean('.', 'foox.in')
198 expect = test.wrap_stdout("""Removed foo2.xxx
202 test.run(arguments = '-c foo2.xxx', stdout=expect)
203 test.must_match(test.workpath('foo1.out'), "foo1.in\n")
204 test.must_not_exist(test.workpath('foo2.xxx'))
205 test.must_match(test.workpath('foo2.out'), "foo2.in\n")
206 test.must_match(test.workpath('foo3.out'), "foo3.in\n")
208 expect = test.wrap_stdout("Removed %s\n" % os.path.join('subd', 'foox.in'),
210 test.run(arguments = '-c subd', stdout=expect)
211 test.must_not_exist(test.workpath('foox.in'))
213 expect = test.wrap_stdout("""Removed foo1.out
219 Removed directory subd
220 """ % (os.path.join('subd','SConscript'), os.path.join('subd', 'foon.in')),
222 test.run(arguments = '-c -n .', stdout=expect)
224 expect = test.wrap_stdout("""Removed foo1.out
229 Removed directory subd
230 """ % (os.path.join('subd','SConscript'), os.path.join('subd', 'foon.in')),
232 test.run(arguments = '-c .', stdout=expect)
233 test.must_not_exist(test.workpath('subdir', 'foon.in'))
234 test.must_not_exist(test.workpath('subdir'))
237 # Ensure that Set/GetOption('clean') works correctly:
238 test.write('SConstruct', """
239 B = Builder(action = r'%s build.py $TARGETS $SOURCES')
240 env = Environment(BUILDERS = { 'B' : B })
241 env.B(target = 'foo.out', source = 'foo.in')
243 assert not GetOption('clean')
246 test.write('foo.in', '"Foo", I say!\n')
248 test.run(arguments='foo.out')
249 test.must_match(test.workpath('foo.out'), '"Foo", I say!\n')
251 test.write('SConstruct', """
252 B = Builder(action = r'%s build.py $TARGETS $SOURCES')
253 env = Environment(BUILDERS = { 'B' : B })
254 env.B(target = 'foo.out', source = 'foo.in')
256 assert GetOption('clean')
257 SetOption('clean', 0)
258 assert GetOption('clean')
261 test.run(arguments='-c foo.out')
262 test.must_not_exist(test.workpath('foo.out'))
264 test.write('SConstruct', """
265 B = Builder(action = r'%s build.py $TARGETS $SOURCES')
266 env = Environment(BUILDERS = { 'B' : B })
267 env.B(target = 'foo.out', source = 'foo.in')
270 test.run(arguments='foo.out')
271 test.must_match(test.workpath('foo.out'), '"Foo", I say!\n')
273 test.write('SConstruct', """
274 B = Builder(action = r'%s build.py $TARGETS $SOURCES')
275 env = Environment(BUILDERS = { 'B' : B })
276 env.B(target = 'foo.out', source = 'foo.in')
278 assert not GetOption('clean')
279 SetOption('clean', 1)
280 assert GetOption('clean')
283 test.run(arguments='foo.out')
284 test.must_not_exist(test.workpath('foo.out'))