Issue 1568: fix a stack trace when --debug=include tries to handle
[scons.git] / test / option-c.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 various uses of the -c (clean) option.
29 """
30
31 import os.path
32 import sys
33 import TestSCons
34
35 _python_ = TestSCons._python_
36
37 test = TestSCons.TestSCons()
38
39 test.write('build.py', r"""
40 import sys
41 contents = open(sys.argv[2], 'rb').read()
42 file = open(sys.argv[1], 'wb')
43 file.write(contents)
44 file.close()
45 """)
46
47 test.write('SConstruct', """
48 B = Builder(action = r'%(_python_)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')
54 env.B(target = 'foo4.out', source = 'foo4.in')
55 env.NoClean('foo4.out')
56 import os
57 if hasattr(os, 'symlink'):
58     def symlink1(env, target, source):
59         # symlink to a file that exists
60         os.symlink(str(source[0]), str(target[0]))
61     env.Command(target = 'symlink1', source = 'foo1.in', action = symlink1)
62     def symlink2(env, target, source):
63         # force symlink to a file that doesn't exist
64         os.symlink('does_not_exist', str(target[0]))
65     env.Command(target = 'symlink2', source = 'foo1.in', action = symlink2)
66 # Test handling of Builder calls that have multiple targets.
67 env.Command(['touch1.out', 'touch2.out'],
68             [],
69             [Touch('${TARGETS[0]}'), Touch('${TARGETS[1]}')])
70 """ % locals())
71
72 test.write('foo1.in', "foo1.in\n")
73
74 test.write('foo2.in', "foo2.in\n")
75
76 test.write('foo3.in', "foo3.in\n")
77
78 test.write('foo4.in', "foo4.in\n")
79
80 test.run(arguments = 'foo1.out foo2.out foo3.out foo4.out')
81
82 test.must_match(test.workpath('foo1.out'), "foo1.in\n")
83 test.must_match(test.workpath('foo2.xxx'), "foo2.in\n")
84 test.must_match(test.workpath('foo2.out'), "foo2.in\n")
85 test.must_match(test.workpath('foo3.out'), "foo3.in\n")
86 test.must_match(test.workpath('foo4.out'), "foo4.in\n")
87
88 test.run(arguments = '-c foo1.out',
89          stdout = test.wrap_stdout("Removed foo1.out\n", cleaning=1))
90
91 test.must_not_exist(test.workpath('foo1.out'))
92 test.must_exist(test.workpath('foo2.xxx'))
93 test.must_exist(test.workpath('foo2.out'))
94 test.must_exist(test.workpath('foo3.out'))
95 test.must_exist(test.workpath('foo4.out'))
96
97 test.run(arguments = '--clean foo2.out foo2.xxx',
98          stdout = test.wrap_stdout("Removed foo2.xxx\nRemoved foo2.out\n",
99                                    cleaning=1))
100
101 test.must_not_exist(test.workpath('foo1.out'))
102 test.must_not_exist(test.workpath('foo2.xxx'))
103 test.must_not_exist(test.workpath('foo2.out'))
104 test.must_exist(test.workpath('foo3.out'))
105 test.must_exist(test.workpath('foo4.out'))
106
107 test.run(arguments = '--remove foo3.out',
108          stdout = test.wrap_stdout("Removed foo3.out\n", cleaning=1))
109
110 test.must_not_exist(test.workpath('foo1.out'))
111 test.must_not_exist(test.workpath('foo2.xxx'))
112 test.must_not_exist(test.workpath('foo2.out'))
113 test.must_not_exist(test.workpath('foo3.out'))
114 test.must_exist(test.workpath('foo4.out'))
115
116 test.run(arguments = '.')
117
118 test.must_match(test.workpath('foo1.out'), "foo1.in\n")
119 test.must_match(test.workpath('foo2.xxx'), "foo2.in\n")
120 test.must_match(test.workpath('foo2.out'), "foo2.in\n")
121 test.must_match(test.workpath('foo3.out'), "foo3.in\n")
122 test.must_match(test.workpath('foo3.out'), "foo3.in\n")
123 test.must_match(test.workpath('foo4.out'), "foo4.in\n")
124 test.must_exist(test.workpath('touch1.out'))
125 test.must_exist(test.workpath('touch2.out'))
126
127 if hasattr(os, 'symlink'):
128     test.fail_test(not os.path.islink(test.workpath('symlink1')))
129     test.fail_test(not os.path.islink(test.workpath('symlink2')))
130
131 test.run(arguments = '-c foo2.xxx',
132          stdout = test.wrap_stdout("Removed foo2.xxx\n", cleaning=1))
133
134 test.must_match(test.workpath('foo1.out'), "foo1.in\n")
135 test.must_not_exist(test.workpath('foo2.xxx'))
136 test.must_match(test.workpath('foo2.out'), "foo2.in\n")
137 test.must_match(test.workpath('foo3.out'), "foo3.in\n")
138 test.must_match(test.workpath('foo4.out'), "foo4.in\n")
139 test.must_exist(test.workpath('touch1.out'))
140 test.must_exist(test.workpath('touch2.out'))
141
142 test.run(arguments = '-c .')
143
144 test.must_not_exist(test.workpath('foo1.out'))
145 test.must_not_exist(test.workpath('foo2.out'))
146 test.must_not_exist(test.workpath('foo3.out'))
147 test.must_exist(test.workpath('foo4.out'))
148 test.must_not_exist(test.workpath('touch1.out'))
149 test.must_not_exist(test.workpath('touch2.out'))
150
151 if hasattr(os, 'symlink'):
152     test.fail_test(os.path.islink(test.workpath('symlink1')))
153     test.fail_test(os.path.islink(test.workpath('symlink2')))
154
155 args = 'foo1.out foo2.out foo3.out touch1.out'
156
157 expect = test.wrap_stdout("""\
158 Removed foo1.out
159 Removed foo2.xxx
160 Removed foo2.out
161 Removed foo3.out
162 Removed touch1.out
163 Removed touch2.out
164 """, cleaning=1)
165
166 test.run(arguments = args)
167
168 test.run(arguments = '-c -n ' + args, stdout = expect)
169
170 test.run(arguments = '-n -c ' + args, stdout = expect)
171
172 test.must_match(test.workpath('foo1.out'), "foo1.in\n")
173 test.must_match(test.workpath('foo2.xxx'), "foo2.in\n")
174 test.must_match(test.workpath('foo2.out'), "foo2.in\n")
175 test.must_match(test.workpath('foo3.out'), "foo3.in\n")
176 test.must_match(test.workpath('foo4.out'), "foo4.in\n")
177 test.must_exist(test.workpath('touch1.out'))
178 test.must_exist(test.workpath('touch2.out'))
179
180
181 expect1 = "scons: Could not remove 'foo1.out': Permission denied\n"
182 expect2 = "scons: Could not remove 'foo1.out': The process cannot access the file because it is being used by another process\n"
183
184 expect = [
185     test.wrap_stdout(expect1, cleaning=1),
186     test.wrap_stdout(expect2, cleaning=1),
187 ]
188
189 test.writable('.', 0)
190 f = open(test.workpath('foo1.out'))
191 test.run(arguments = '-c foo1.out')
192 stdout = test.stdout()
193 matched = None
194 for e in expect:
195     if stdout == e:
196         matched = 1
197         break
198 if not matched:
199     print stdout
200     test.fail_test()
201 test.must_exist(test.workpath('foo1.out'))
202 f.close()
203 test.writable('.', 1)
204
205 test.subdir('subd')
206 test.write(['subd', 'foon.in'], "foon.in\n")
207 test.write(['subd', 'foox.in'], "foox.in\n")
208 test.write('aux1.x', "aux1.x\n")
209 test.write('aux2.x', "aux2.x\n")
210 test.write('SConstruct', """
211 B = Builder(action = r'%(_python_)s build.py $TARGETS $SOURCES')
212 env = Environment(BUILDERS = { 'B' : B }, FOO = 'foo2')
213 env.B(target = 'foo1.out', source = 'foo1.in')
214 env.B(target = 'foo2.out', source = 'foo2.xxx')
215 foo2_xxx = env.B(target = 'foo2.xxx', source = 'foo2.in')
216 env.B(target = 'foo3.out', source = 'foo3.in')
217 SConscript('subd/SConscript')
218 Clean(foo2_xxx, ['aux1.x'])
219 env.Clean(['${FOO}.xxx'], ['aux2.x'])
220 Clean('.', ['subd'])
221 """ % locals())
222
223 test.write(['subd', 'SConscript'], """
224 Clean('.', 'foox.in')
225 """)
226
227 expect = test.wrap_stdout("""Removed foo2.xxx
228 Removed aux1.x
229 Removed aux2.x
230 """, cleaning=1)
231 test.run(arguments = '-c foo2.xxx', stdout=expect)
232 test.must_match(test.workpath('foo1.out'), "foo1.in\n")
233 test.must_not_exist(test.workpath('foo2.xxx'))
234 test.must_match(test.workpath('foo2.out'), "foo2.in\n")
235 test.must_match(test.workpath('foo3.out'), "foo3.in\n")
236
237 expect = test.wrap_stdout("Removed %s\n" % os.path.join('subd', 'foox.in'),
238                           cleaning = 1)
239 test.run(arguments = '-c subd', stdout=expect)
240 test.must_not_exist(test.workpath('foox.in'))
241
242 expect = test.wrap_stdout("""Removed foo1.out
243 Removed foo2.xxx
244 Removed foo2.out
245 Removed foo3.out
246 Removed %s
247 Removed %s
248 Removed directory subd
249 """ % (os.path.join('subd','SConscript'), os.path.join('subd', 'foon.in')),
250                           cleaning = 1)
251 test.run(arguments = '-c -n .', stdout=expect)
252
253 expect = test.wrap_stdout("""Removed foo1.out
254 Removed foo2.out
255 Removed foo3.out
256 Removed %s
257 Removed %s
258 Removed directory subd
259 """ % (os.path.join('subd','SConscript'), os.path.join('subd', 'foon.in')),
260                           cleaning = 1)
261 test.run(arguments = '-c .', stdout=expect)
262 test.must_not_exist(test.workpath('subdir', 'foon.in'))
263 test.must_not_exist(test.workpath('subdir'))
264
265
266 # Ensure that Set/GetOption('clean') works correctly:
267 test.write('SConstruct', """
268 B = Builder(action = r'%(_python_)s build.py $TARGETS $SOURCES')
269 env = Environment(BUILDERS = { 'B' : B })
270 env.B(target = 'foo.out', source = 'foo.in')
271
272 assert not GetOption('clean')
273 """ % locals())
274
275 test.write('foo.in', '"Foo", I say!\n')
276
277 test.run(arguments='foo.out')
278 test.must_match(test.workpath('foo.out'), '"Foo", I say!\n')
279
280 test.write('SConstruct', """
281 B = Builder(action = r'%(_python_)s build.py $TARGETS $SOURCES')
282 env = Environment(BUILDERS = { 'B' : B })
283 env.B(target = 'foo.out', source = 'foo.in')
284
285 assert GetOption('clean')
286 SetOption('clean', 0)
287 assert GetOption('clean')
288 """ % locals())
289
290 test.run(arguments='-c foo.out')
291 test.must_not_exist(test.workpath('foo.out'))
292
293 test.write('SConstruct', """
294 B = Builder(action = r'%(_python_)s build.py $TARGETS $SOURCES')
295 env = Environment(BUILDERS = { 'B' : B })
296 env.B(target = 'foo.out', source = 'foo.in')
297 """ % locals())
298
299 test.run(arguments='foo.out')
300 test.must_match(test.workpath('foo.out'), '"Foo", I say!\n')
301
302 test.write('SConstruct', """
303 B = Builder(action = r'%(_python_)s build.py $TARGETS $SOURCES')
304 env = Environment(BUILDERS = { 'B' : B })
305 env.B(target = 'foo.out', source = 'foo.in')
306
307 assert not GetOption('clean')
308 SetOption('clean', 1)
309 assert GetOption('clean')
310 """ % locals())
311
312 test.run(arguments='foo.out')
313 test.must_not_exist(test.workpath('foo.out'))
314
315 test.pass_test()
316
317