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 retrieving derived files from a CacheDir.
36 test = TestSCons.TestSCons()
38 # cache2 omitted from list in order to test automatic creation of CacheDir
40 test.subdir('cache1', 'cache3', 'src', 'subdir')
42 test.write(['src', 'SConstruct'], """\
44 SConscript('SConscript')
45 """ % test.workpath('cache1'))
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")
54 f.write(open(src, "rb").read())
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'])
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")
67 #############################################################################
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"])
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'))))
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 = '.')
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")
92 test.up_to_date(chdir = 'src', arguments = '.')
94 test.run(chdir = 'src', arguments = '-c .')
95 test.unlink(['src', 'cat.out'])
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
106 test.must_not_exist(test.workpath('src', 'cat.out'))
108 test.up_to_date(chdir = 'src', arguments = '.')
110 test.run(chdir = 'src', arguments = '-c .')
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
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'))
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 = "")
130 test.must_match(['src', 'all'], "aaa.in\nbbb.in\nccc.in\n")
131 test.must_not_exist(test.workpath('src', 'cat.out'))
133 test.up_to_date(chdir = 'src', arguments = '.')
135 test.run(chdir = 'src', arguments = '-c .')
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")
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"])
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")
151 test.up_to_date(chdir = 'src', arguments = '.')
153 #############################################################################
154 # Now we try out BuildDir() functionality.
155 # This is largely cut-and-paste of the above,
156 # with appropriate directory modifications.
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')
163 # First, clean up the source directory and start over with fresh files.
164 test.run(chdir = 'src', arguments = '-c .')
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")
171 test.write('SConstruct', """\
172 env = Environment(TWO = '2')
174 BuildDir('build', 'src', duplicate=0)
175 SConscript('build/SConscript')
176 """ % test.workpath('cache${TWO}'))
178 # Verify that a normal build works correctly, and clean up.
179 # This should populate the cache with our derived files.
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))
185 test.up_to_date(arguments = '.')
187 test.run(arguments = '-c .')
188 test.unlink('cat.out')
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)))
199 test.must_not_exist(test.workpath('cat.out'))
201 test.up_to_date(arguments = '.')
203 test.run(arguments = '-c .')
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)))
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'))
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 = "")
223 test.must_match(['build', 'all'], "aaa.in\nbbb.in\nccc.in\n")
224 test.must_not_exist(test.workpath('cat.out'))
226 test.up_to_date(arguments = '.')
228 test.run(arguments = '-c .')
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")
234 test.run(stdout = test.wrap_stdout("""\
235 Retrieved `%s' from cache
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'),
242 build_all, build_aaa_out, build_bbb_out, build_ccc_out)))
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))
247 test.up_to_date(arguments = '.')
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).
255 test.write(['subdir', 'SConstruct'], """\
260 def docopy(target,source,env):
261 data = source[0].get_contents()
262 f = open(target[0].rfile().get_abspath(), "wb")
266 def sillyScanner(node, env, dirs):
267 print 'This is never called (unless we build file.out)'
270 SillyScanner = SCons.Scanner.Base(function = sillyScanner, skeys = ['.res'])
272 env = Environment(tools=[],
273 SCANNERS = [SillyScanner],
276 r = env.Command('file.res', 'file.ma', docopy)
278 env.Command('file.out', r, docopy)
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
284 """ % test.workpath('cache3'))
286 test.write(['subdir', 'file.ma'], "subdir/file.ma\n")
288 test.run(chdir = 'subdir')
290 test.must_not_exist(test.workpath('cache3', 'N', 'None'))
292 #############################################################################
293 # Test that multiple target files get retrieved from cache correctly.
295 test.subdir('multiple', 'cache4')
297 test.write(['multiple', 'SConstruct'], """\
298 def touch(env, source, target):
299 open('foo', 'w').write("")
300 open('bar', 'w').write("")
303 env.Command(['foo', 'bar'], ['input'], touch)
304 """ % (test.workpath('cache4')))
306 test.write(['multiple', 'input'], "multiple/input\n")
308 test.run(chdir = 'multiple')
310 test.must_exist(test.workpath('multiple', 'foo'))
311 test.must_exist(test.workpath('multiple', 'bar'))
313 test.run(chdir = 'multiple', arguments = '-c')
315 test.must_not_exist(test.workpath('multiple', 'foo'))
316 test.must_not_exist(test.workpath('multiple', 'bar'))
318 test.run(chdir = 'multiple')
320 test.must_exist(test.workpath('multiple', 'foo'))
321 test.must_exist(test.workpath('multiple', 'bar'))