Move all the scons.org stuff from the scons source tree itself to a
[scons.git] / test / CacheDir.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 retrieving derived files from a CacheDir.
29 """
30
31 import os.path
32 import shutil
33
34 import TestSCons
35
36 test = TestSCons.TestSCons()
37
38 # cache2 omitted from list in order to test automatic creation of CacheDir
39 # directory.
40 test.subdir('cache1', 'cache3', 'src', 'subdir')
41
42 test.write(['src', 'SConstruct'], """\
43 CacheDir(r'%s')
44 SConscript('SConscript')
45 """ % test.workpath('cache1'))
46
47 test.write(['src', 'SConscript'], """\
48 def cat(env, source, target):
49     target = str(target[0])
50     open('cat.out', 'ab').write(target + "\\n")
51     source = map(str, source)
52     f = open(target, "wb")
53     for src in source:
54         f.write(open(src, "rb").read())
55     f.close()
56 env = Environment(BUILDERS={'Cat':Builder(action=cat)})
57 env.Cat('aaa.out', 'aaa.in')
58 env.Cat('bbb.out', 'bbb.in')
59 env.Cat('ccc.out', 'ccc.in')
60 env.Cat('all', ['aaa.out', 'bbb.out', 'ccc.out'])
61 """)
62
63 test.write(['src', 'aaa.in'], "aaa.in\n")
64 test.write(['src', 'bbb.in'], "bbb.in\n")
65 test.write(['src', 'ccc.in'], "ccc.in\n")
66
67 #############################################################################
68
69 # Verify that building with -n and an empty cache reports that proper
70 # build operations would be taken, but that nothing is actually built
71 # and that the cache is still empty.
72 test.run(chdir = 'src', arguments = '-n .', stdout = test.wrap_stdout("""\
73 cat(["aaa.out"], ["aaa.in"])
74 cat(["bbb.out"], ["bbb.in"])
75 cat(["ccc.out"], ["ccc.in"])
76 cat(["all"], ["aaa.out", "bbb.out", "ccc.out"])
77 """))
78
79 test.must_not_exist(test.workpath('src', 'aaa.out'))
80 test.must_not_exist(test.workpath('src', 'bbb.out'))
81 test.must_not_exist(test.workpath('src', 'ccc.out'))
82 test.must_not_exist(test.workpath('src', 'all'))
83 test.fail_test(len(os.listdir(test.workpath('cache1'))))
84
85 # Verify that a normal build works correctly, and clean up.
86 # This should populate the cache with our derived files.
87 test.run(chdir = 'src', arguments = '.')
88
89 test.must_match(['src', 'all'], "aaa.in\nbbb.in\nccc.in\n")
90 test.must_match(['src', 'cat.out'], "aaa.out\nbbb.out\nccc.out\nall\n")
91
92 test.up_to_date(chdir = 'src', arguments = '.')
93
94 test.run(chdir = 'src', arguments = '-c .')
95 test.unlink(['src', 'cat.out'])
96
97 # Verify that we now retrieve the derived files from cache,
98 # not rebuild them.  Then clean up.
99 test.run(chdir = 'src', arguments = '.', stdout = test.wrap_stdout("""\
100 Retrieved `aaa.out' from cache
101 Retrieved `bbb.out' from cache
102 Retrieved `ccc.out' from cache
103 Retrieved `all' from cache
104 """))
105
106 test.must_not_exist(test.workpath('src', 'cat.out'))
107
108 test.up_to_date(chdir = 'src', arguments = '.')
109
110 test.run(chdir = 'src', arguments = '-c .')
111
112 # Verify that rebuilding with -n reports that everything was retrieved
113 # from the cache, but that nothing really was.
114 test.run(chdir = 'src', arguments = '-n .', stdout = test.wrap_stdout("""\
115 Retrieved `aaa.out' from cache
116 Retrieved `bbb.out' from cache
117 Retrieved `ccc.out' from cache
118 Retrieved `all' from cache
119 """))
120
121 test.must_not_exist(test.workpath('src', 'aaa.out'))
122 test.must_not_exist(test.workpath('src', 'bbb.out'))
123 test.must_not_exist(test.workpath('src', 'ccc.out'))
124 test.must_not_exist(test.workpath('src', 'all'))
125
126 # Verify that rebuilding with -s retrieves everything from the cache
127 # even though it doesn't report anything.
128 test.run(chdir = 'src', arguments = '-s .', stdout = "")
129
130 test.must_match(['src', 'all'], "aaa.in\nbbb.in\nccc.in\n")
131 test.must_not_exist(test.workpath('src', 'cat.out'))
132
133 test.up_to_date(chdir = 'src', arguments = '.')
134
135 test.run(chdir = 'src', arguments = '-c .')
136
137 # Verify that updating one input file builds its derived file and
138 # dependency but that the other files are retrieved from cache.
139 test.write(['src', 'bbb.in'], "bbb.in 2\n")
140
141 test.run(chdir = 'src', arguments = '.', stdout = test.wrap_stdout("""\
142 Retrieved `aaa.out' from cache
143 cat(["bbb.out"], ["bbb.in"])
144 Retrieved `ccc.out' from cache
145 cat(["all"], ["aaa.out", "bbb.out", "ccc.out"])
146 """))
147
148 test.must_match(['src', 'all'], "aaa.in\nbbb.in 2\nccc.in\n")
149 test.must_match(['src', 'cat.out'], "bbb.out\nall\n")
150
151 test.up_to_date(chdir = 'src', arguments = '.')
152
153 #############################################################################
154 # Now we try out BuildDir() functionality.
155 # This is largely cut-and-paste of the above,
156 # with appropriate directory modifications.
157
158 build_aaa_out = os.path.join('build', 'aaa.out')
159 build_bbb_out = os.path.join('build', 'bbb.out')
160 build_ccc_out = os.path.join('build', 'ccc.out')
161 build_all = os.path.join('build', 'all')
162
163 # First, clean up the source directory and start over with fresh files.
164 test.run(chdir = 'src', arguments = '-c .')
165
166 test.write(['src', 'aaa.in'], "aaa.in\n")
167 test.write(['src', 'bbb.in'], "bbb.in\n")
168 test.write(['src', 'ccc.in'], "ccc.in\n")
169
170 #
171 test.write('SConstruct', """\
172 env = Environment(TWO = '2')
173 env.CacheDir(r'%s')
174 BuildDir('build', 'src', duplicate=0)
175 SConscript('build/SConscript')
176 """ % test.workpath('cache${TWO}'))
177
178 # Verify that a normal build works correctly, and clean up.
179 # This should populate the cache with our derived files.
180 test.run()
181
182 test.must_match(['build', 'all'], "aaa.in\nbbb.in\nccc.in\n")
183 test.must_match('cat.out', "%s\n%s\n%s\n%s\n" % (build_aaa_out, build_bbb_out, build_ccc_out, build_all))
184
185 test.up_to_date(arguments = '.')
186
187 test.run(arguments = '-c .')
188 test.unlink('cat.out')
189
190 # Verify that we now retrieve the derived files from cache,
191 # not rebuild them.  Then clean up.
192 test.run(stdout = test.wrap_stdout("""\
193 Retrieved `%s' from cache
194 Retrieved `%s' from cache
195 Retrieved `%s' from cache
196 Retrieved `%s' from cache
197 """ % (build_aaa_out, build_bbb_out, build_ccc_out, build_all)))
198
199 test.must_not_exist(test.workpath('cat.out'))
200
201 test.up_to_date(arguments = '.')
202
203 test.run(arguments = '-c .')
204
205 # Verify that rebuilding with -n reports that everything was retrieved
206 # from the cache, but that nothing really was.
207 test.run(arguments = '-n .', stdout = test.wrap_stdout("""\
208 Retrieved `%s' from cache
209 Retrieved `%s' from cache
210 Retrieved `%s' from cache
211 Retrieved `%s' from cache
212 """ % (build_aaa_out, build_bbb_out, build_ccc_out, build_all)))
213
214 test.must_not_exist(test.workpath('build', 'aaa.out'))
215 test.must_not_exist(test.workpath('build', 'bbb.out'))
216 test.must_not_exist(test.workpath('build', 'ccc.out'))
217 test.must_not_exist(test.workpath('build', 'all'))
218
219 # Verify that rebuilding with -s retrieves everything from the cache
220 # even though it doesn't report anything.
221 test.run(arguments = '-s .', stdout = "")
222
223 test.must_match(['build', 'all'], "aaa.in\nbbb.in\nccc.in\n")
224 test.must_not_exist(test.workpath('cat.out'))
225
226 test.up_to_date(arguments = '.')
227
228 test.run(arguments = '-c .')
229
230 # Verify that updating one input file builds its derived file and
231 # dependency but that the other files are retrieved from cache.
232 test.write(['src', 'bbb.in'], "bbb.in 2\n")
233
234 test.run(stdout = test.wrap_stdout("""\
235 Retrieved `%s' from cache
236 cat(["%s"], ["%s"])
237 Retrieved `%s' from cache
238 cat(["%s"], ["%s", "%s", "%s"])
239 """ % (build_aaa_out,
240        build_bbb_out, os.path.join('src', 'bbb.in'),
241        build_ccc_out,
242        build_all, build_aaa_out, build_bbb_out, build_ccc_out)))
243
244 test.must_match(['build', 'all'], "aaa.in\nbbb.in 2\nccc.in\n")
245 test.must_match('cat.out', "%s\n%s\n" % (build_bbb_out, build_all))
246
247 test.up_to_date(arguments = '.')
248
249 #############################################################################
250 # Test the case (reported by Jeff Petkau, bug #694744) where a target
251 # is source for another target with a scanner, which used to cause us
252 # to push the file to the CacheDir after the build signature had already
253 # been cleared (as a sign that the built file should now be rescanned).
254
255 test.write(['subdir', 'SConstruct'], """\
256 import SCons
257
258 CacheDir(r'%s')
259
260 def docopy(target,source,env):
261     data = source[0].get_contents()
262     f = open(target[0].rfile().get_abspath(), "wb")
263     f.write(data)
264     f.close()
265
266 def sillyScanner(node, env, dirs):
267     print 'This is never called (unless we build file.out)'
268     return []
269
270 SillyScanner = SCons.Scanner.Base(function = sillyScanner, skeys = ['.res'])
271
272 env = Environment(tools=[],
273                   SCANNERS = [SillyScanner],
274                   BUILDERS = {})
275
276 r = env.Command('file.res', 'file.ma', docopy)
277
278 env.Command('file.out', r, docopy)
279
280 # make r the default. Note that we don't even try to build file.out,
281 # and so SillyScanner never runs. The bug is the same if we build
282 # file.out, though.
283 Default(r)
284 """ % test.workpath('cache3'))
285
286 test.write(['subdir', 'file.ma'], "subdir/file.ma\n")
287
288 test.run(chdir = 'subdir')
289
290 test.must_not_exist(test.workpath('cache3', 'N', 'None'))
291
292 #############################################################################
293 # Test that multiple target files get retrieved from cache correctly.
294
295 test.subdir('multiple', 'cache4')
296
297 test.write(['multiple', 'SConstruct'], """\
298 def touch(env, source, target):
299     open('foo', 'w').write("")
300     open('bar', 'w').write("")
301 CacheDir(r'%s')
302 env = Environment()
303 env.Command(['foo', 'bar'], ['input'], touch)
304 """ % (test.workpath('cache4')))
305
306 test.write(['multiple', 'input'], "multiple/input\n")
307
308 test.run(chdir = 'multiple')
309
310 test.must_exist(test.workpath('multiple', 'foo'))
311 test.must_exist(test.workpath('multiple', 'bar'))
312
313 test.run(chdir = 'multiple', arguments = '-c')
314
315 test.must_not_exist(test.workpath('multiple', 'foo'))
316 test.must_not_exist(test.workpath('multiple', 'bar'))
317
318 test.run(chdir = 'multiple')
319
320 test.must_exist(test.workpath('multiple', 'foo'))
321 test.must_exist(test.workpath('multiple', 'bar'))
322
323 # All done.
324 test.pass_test()