Eliminate unnecessary WIN32/Win32/win32 references in tests, too.
[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'%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 import os
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'],
66             [],
67             [Touch('${TARGETS[0]}'), Touch('${TARGETS[1]}')])
68 """ % python)
69
70 test.write('foo1.in', "foo1.in\n")
71
72 test.write('foo2.in', "foo2.in\n")
73
74 test.write('foo3.in', "foo3.in\n")
75
76 test.run(arguments = 'foo1.out foo2.out foo3.out')
77
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")
82
83 test.run(arguments = '-c foo1.out',
84          stdout = test.wrap_stdout("Removed foo1.out\n", cleaning=1))
85
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'))
90
91 test.run(arguments = '--clean foo2.out foo2.xxx',
92          stdout = test.wrap_stdout("Removed foo2.xxx\nRemoved foo2.out\n",
93                                    cleaning=1))
94
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'))
99
100 test.run(arguments = '--remove foo3.out',
101          stdout = test.wrap_stdout("Removed foo3.out\n", cleaning=1))
102
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'))
107
108 test.run(arguments = '.')
109
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'))
117
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')))
121
122 test.run(arguments = '-c foo2.xxx',
123          stdout = test.wrap_stdout("Removed foo2.xxx\n", cleaning=1))
124
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'))
131
132 test.run(arguments = '-c .')
133
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'))
139
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')))
143
144 args = 'foo1.out foo2.out foo3.out touch1.out'
145
146 expect = test.wrap_stdout("""\
147 Removed foo1.out
148 Removed foo2.xxx
149 Removed foo2.out
150 Removed foo3.out
151 Removed touch1.out
152 Removed touch2.out
153 """, cleaning=1)
154
155 test.run(arguments = args)
156
157 test.run(arguments = '-c -n ' + args, stdout = expect)
158
159 test.run(arguments = '-n -c ' + args, stdout = expect)
160
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'))
167
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'))
173 f.close()
174 test.writable('.', 1)
175
176 test.subdir('subd')
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'])
191 Clean('.', ['subd'])
192 """ % python)
193
194 test.write(['subd', 'SConscript'], """
195 Clean('.', 'foox.in')
196 """)
197
198 expect = test.wrap_stdout("""Removed foo2.xxx
199 Removed aux1.x
200 Removed aux2.x
201 """, cleaning=1)
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")
207
208 expect = test.wrap_stdout("Removed %s\n" % os.path.join('subd', 'foox.in'),
209                           cleaning = 1)
210 test.run(arguments = '-c subd', stdout=expect)
211 test.must_not_exist(test.workpath('foox.in'))
212
213 expect = test.wrap_stdout("""Removed foo1.out
214 Removed foo2.xxx
215 Removed foo2.out
216 Removed foo3.out
217 Removed %s
218 Removed %s
219 Removed directory subd
220 """ % (os.path.join('subd','SConscript'), os.path.join('subd', 'foon.in')),
221                           cleaning = 1)
222 test.run(arguments = '-c -n .', stdout=expect)
223
224 expect = test.wrap_stdout("""Removed foo1.out
225 Removed foo2.out
226 Removed foo3.out
227 Removed %s
228 Removed %s
229 Removed directory subd
230 """ % (os.path.join('subd','SConscript'), os.path.join('subd', 'foon.in')),
231                           cleaning = 1)
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'))
235
236
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')
242
243 assert not GetOption('clean')
244 """%python)
245
246 test.write('foo.in', '"Foo", I say!\n')
247
248 test.run(arguments='foo.out')
249 test.must_match(test.workpath('foo.out'), '"Foo", I say!\n')
250
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')
255
256 assert GetOption('clean')
257 SetOption('clean', 0)
258 assert GetOption('clean')
259 """%python)
260
261 test.run(arguments='-c foo.out')
262 test.must_not_exist(test.workpath('foo.out'))
263
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')
268 """%python)
269
270 test.run(arguments='foo.out')
271 test.must_match(test.workpath('foo.out'), '"Foo", I say!\n')
272
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')
277
278 assert not GetOption('clean')
279 SetOption('clean', 1)
280 assert GetOption('clean')
281 """%python)
282
283 test.run(arguments='foo.out')
284 test.must_not_exist(test.workpath('foo.out'))
285
286 test.pass_test()
287
288