Merged revisions 1582-1665 via svnmerge from
[scons.git] / test / CacheDir / BuildDir.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 when a BuildDir is used.
29 """
30
31 import os.path
32
33 import TestSCons
34
35 test = TestSCons.TestSCons()
36
37 test.subdir('cache', 'src')
38
39 cache = test.workpath('cache')
40 cat_out = test.workpath('cat.out')
41
42 test.write(['src', 'SConstruct'], """\
43 CacheDir(r'%(cache)s')
44 SConscript('SConscript')
45 """ % locals())
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 build_aaa_out = os.path.join('build', 'aaa.out')
64 build_bbb_out = os.path.join('build', 'bbb.out')
65 build_ccc_out = os.path.join('build', 'ccc.out')
66 build_all = os.path.join('build', 'all')
67
68 test.write(['src', 'aaa.in'], "aaa.in\n")
69 test.write(['src', 'bbb.in'], "bbb.in\n")
70 test.write(['src', 'ccc.in'], "ccc.in\n")
71
72 #
73 test.write('SConstruct', """\
74 env = Environment(TWO = '2')
75 env.CacheDir(r'%s')
76 BuildDir('build', 'src', duplicate=0)
77 SConscript('build/SConscript')
78 """ % test.workpath('cache${TWO}'))
79
80 # Verify that a normal build works correctly, and clean up.
81 # This should populate the cache with our derived files.
82 test.run()
83
84 test.must_match(['build', 'all'], "aaa.in\nbbb.in\nccc.in\n")
85 test.must_match('cat.out', "%s\n%s\n%s\n%s\n" % (build_aaa_out, build_bbb_out, build_ccc_out, build_all))
86
87 test.up_to_date(arguments = '.')
88
89 test.run(arguments = '-c .')
90 test.unlink('cat.out')
91
92 # Verify that we now retrieve the derived files from cache,
93 # not rebuild them.  Then clean up.
94 test.run(stdout = test.wrap_stdout("""\
95 Retrieved `%s' from cache
96 Retrieved `%s' from cache
97 Retrieved `%s' from cache
98 Retrieved `%s' from cache
99 """ % (build_aaa_out, build_bbb_out, build_ccc_out, build_all)))
100
101 test.must_not_exist(cat_out)
102
103 test.up_to_date(arguments = '.')
104
105 test.run(arguments = '-c .')
106
107 # Verify that rebuilding with -n reports that everything was retrieved
108 # from the cache, but that nothing really was.
109 test.run(arguments = '-n .', stdout = test.wrap_stdout("""\
110 Retrieved `%s' from cache
111 Retrieved `%s' from cache
112 Retrieved `%s' from cache
113 Retrieved `%s' from cache
114 """ % (build_aaa_out, build_bbb_out, build_ccc_out, build_all)))
115
116 test.must_not_exist(test.workpath('build', 'aaa.out'))
117 test.must_not_exist(test.workpath('build', 'bbb.out'))
118 test.must_not_exist(test.workpath('build', 'ccc.out'))
119 test.must_not_exist(test.workpath('build', 'all'))
120
121 # Verify that rebuilding with -s retrieves everything from the cache
122 # even though it doesn't report anything.
123 test.run(arguments = '-s .', stdout = "")
124
125 test.must_match(['build', 'all'], "aaa.in\nbbb.in\nccc.in\n")
126 test.must_not_exist(cat_out)
127
128 test.up_to_date(arguments = '.')
129
130 test.run(arguments = '-c .')
131
132 # Verify that updating one input file builds its derived file and
133 # dependency but that the other files are retrieved from cache.
134 test.write(['src', 'bbb.in'], "bbb.in 2\n")
135
136 test.run(stdout = test.wrap_stdout("""\
137 Retrieved `%s' from cache
138 cat(["%s"], ["%s"])
139 Retrieved `%s' from cache
140 cat(["%s"], ["%s", "%s", "%s"])
141 """ % (build_aaa_out,
142        build_bbb_out, os.path.join('src', 'bbb.in'),
143        build_ccc_out,
144        build_all, build_aaa_out, build_bbb_out, build_ccc_out)))
145
146 test.must_match(['build', 'all'], "aaa.in\nbbb.in 2\nccc.in\n")
147 test.must_match('cat.out', "%s\n%s\n" % (build_bbb_out, build_all))
148
149 test.up_to_date(arguments = '.')
150
151
152
153 test.pass_test()