4 # Permission is hereby granted, free of charge, to any person obtaining
5 # a copy of this software and associated documentation files (the
6 # "Software"), to deal in the Software without restriction, including
7 # without limitation the rights to use, copy, modify, merge, publish,
8 # distribute, sublicense, and/or sell copies of the Software, and to
9 # permit persons to whom the Software is furnished to do so, subject to
10 # the following conditions:
12 # The above copyright notice and this permission notice shall be included
13 # in all copies or substantial portions of the Software.
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
16 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
17 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
31 from TestCmd import TestCmd
38 def __call__(self, targets, sources, env, errfunc, **kw):
40 if kw.get('execute', 1):
45 def __init__(self, environment, action):
46 self.env = environment
51 def __init__(self, cachedir):
52 self.cachedir = cachedir
53 def Override(self, overrides):
55 def get_CacheDir(self):
58 class BaseTestCase(unittest.TestCase):
60 Base fixtures common to our other unittest classes.
63 self.test = TestCmd(workdir='')
66 self.fs = SCons.Node.FS.FS()
68 self._CacheDir = SCons.CacheDir.CacheDir('cache')
70 def File(self, name, bsig=None, action=Action()):
71 node = self.fs.File(name)
72 node.builder_set(Builder(Environment(self._CacheDir), action))
74 node.binfo = node.BuildInfo(node)
75 node.binfo.ninfo.bsig = bsig
78 class CacheDirTestCase(BaseTestCase):
80 Test calling CacheDir code directly.
82 def test_cachepath(self):
83 """Test the cachepath() method"""
85 # Verify how the cachepath() method determines the name
86 # of the file in cache.
89 save_collect = SCons.Sig.MD5.collect
90 SCons.Sig.MD5.collect = my_collect
93 f5 = self.File("cd.f5", 'a_fake_bsig')
94 result = self._CacheDir.cachepath(f5)
95 dirname = os.path.join('cache', 'A')
96 filename = os.path.join(dirname, 'a_fake_bsig')
97 assert result == (dirname, filename), result
99 SCons.Sig.MD5.collect = save_collect
101 class FileTestCase(BaseTestCase):
103 Test calling CacheDir code through Node.FS.File interfaces.
105 # These tests were originally in Nodes/FSTests.py and got moved
106 # when the CacheDir support was refactored into its own module.
107 # Look in the history for Node/FSTests.py if any of this needs
109 def retrieve_succeed(self, target, source, env, execute=1):
110 self.retrieved.append(target)
113 def retrieve_fail(self, target, source, env, execute=1):
114 self.retrieved.append(target)
117 def push(self, target, source, env):
118 self.pushed.append(target)
121 def test_CacheRetrieve(self):
122 """Test the CacheRetrieve() function"""
124 save_CacheRetrieve = SCons.CacheDir.CacheRetrieve
127 f1 = self.File("cd.f1")
129 SCons.CacheDir.CacheRetrieve = self.retrieve_succeed
133 r = f1.retrieve_from_cache()
135 assert self.retrieved == [f1], self.retrieved
136 assert built_it is None, built_it
138 SCons.CacheDir.CacheRetrieve = self.retrieve_fail
142 r = f1.retrieve_from_cache()
144 assert self.retrieved == [f1], self.retrieved
145 assert built_it is None, built_it
147 SCons.CacheDir.CacheRetrieve = save_CacheRetrieve
149 def test_CacheRetrieveSilent(self):
150 """Test the CacheRetrieveSilent() function"""
152 save_CacheRetrieveSilent = SCons.CacheDir.CacheRetrieveSilent
154 SCons.CacheDir.cache_show = 1
156 f2 = self.File("cd.f2", 'f2_bsig')
158 SCons.CacheDir.CacheRetrieveSilent = self.retrieve_succeed
162 r = f2.retrieve_from_cache()
164 assert self.retrieved == [f2], self.retrieved
165 assert built_it is None, built_it
167 SCons.CacheDir.CacheRetrieveSilent = self.retrieve_fail
171 r = f2.retrieve_from_cache()
173 assert self.retrieved == [f2], self.retrieved
174 assert built_it is None, built_it
176 SCons.CacheDir.CacheRetrieveSilent = save_CacheRetrieveSilent
178 def test_CachePush(self):
179 """Test the CachePush() function"""
181 save_CachePush = SCons.CacheDir.CachePush
183 SCons.CacheDir.CachePush = self.push
188 cd_f3 = self.test.workpath("cd.f3")
189 f3 = self.File(cd_f3)
191 assert self.pushed == [], self.pushed
192 self.test.write(cd_f3, "cd.f3\n")
194 assert self.pushed == [f3], self.pushed
198 cd_f4 = self.test.workpath("cd.f4")
199 f4 = self.File(cd_f4)
201 assert self.pushed == [], self.pushed
202 self.test.write(cd_f4, "cd.f4\n")
205 assert self.pushed == [], self.pushed
206 SCons.CacheDir.cache_force = 1
209 assert self.pushed == [f4], self.pushed
211 SCons.CacheDir.CachePush = save_CachePush
213 def test_no_bsig(self):
214 """Test that no bsig raises an InternalError"""
216 f6 = self.File("cd.f6")
217 f6.binfo = f6.BuildInfo(f6)
220 cp = self._CacheDir.cachepath(f6)
221 except SCons.Errors.InternalError:
225 def test_warning(self):
226 """Test raising a warning if we can't copy a file to cache."""
228 test = TestCmd(workdir='')
230 save_copy2 = shutil.copy2
234 save_mkdir = os.mkdir
235 def mkdir(dir, mode=0):
238 old_warn_exceptions = SCons.Warnings.warningAsException(1)
239 SCons.Warnings.enableWarningClass(SCons.Warnings.CacheWriteErrorWarning)
242 cd_f7 = self.test.workpath("cd.f7")
243 self.test.write(cd_f7, "cd.f7\n")
244 f7 = self.File(cd_f7, 'f7_bsig')
249 except SCons.Warnings.CacheWriteErrorWarning:
253 shutil.copy2 = save_copy2
254 os.mkdir = save_mkdir
255 SCons.Warnings.warningAsException(old_warn_exceptions)
256 SCons.Warnings.suppressWarningClass(SCons.Warnings.CacheWriteErrorWarning)
258 def test_no_strfunction(self):
259 """Test handling no strfunction() for an action."""
261 save_CacheRetrieveSilent = SCons.CacheDir.CacheRetrieveSilent
263 f8 = self.File("cd.f8", 'f8_bsig')
265 SCons.CacheDir.CacheRetrieveSilent = self.retrieve_succeed
269 r = f8.retrieve_from_cache()
271 assert self.retrieved == [f8], self.retrieved
272 assert built_it is None, built_it
274 SCons.CacheDir.CacheRetrieveSilent = self.retrieve_fail
278 r = f8.retrieve_from_cache()
280 assert self.retrieved == [f8], self.retrieved
281 assert built_it is None, built_it
283 SCons.CacheDir.CacheRetrieveSilent = save_CacheRetrieveSilent
285 if __name__ == "__main__":
286 suite = unittest.TestSuite()
291 for tclass in tclasses:
292 names = unittest.getTestCaseNames(tclass, 'test_')
293 suite.addTests(map(tclass, names))
294 if not unittest.TextTestRunner().run(suite).wasSuccessful():