3 # Copyright The SCons Foundation
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.
31 from TestCmd
import TestCmd
37 IS_WINDOWS
= sys
.platform
== 'win32'
39 IS_ROOT
= os
.geteuid() == 0
40 except AttributeError:
45 def __call__(self
, targets
, sources
, env
, **kw
) -> int:
47 if kw
.get('execute', 1):
50 def genstring(self
, target
, source
, env
):
52 def get_contents(self
, target
, source
, env
):
53 return bytearray('','utf-8')
56 def __init__(self
, environment
, action
) -> None:
57 self
.env
= environment
60 self
.source_scanner
= None
61 self
.target_scanner
= None
64 def __init__(self
, cachedir
) -> None:
65 self
.cachedir
= cachedir
66 def Override(self
, overrides
):
68 def get_CacheDir(self
):
71 class BaseTestCase(unittest
.TestCase
):
73 Base fixtures common to our other unittest classes.
75 def setUp(self
) -> None:
76 self
.test
= TestCmd(workdir
='')
79 self
.fs
= SCons
.Node
.FS
.FS()
81 self
._CacheDir
= SCons
.CacheDir
.CacheDir('cache')
83 def File(self
, name
, bsig
=None, action
=Action()):
84 node
= self
.fs
.File(name
)
85 node
.builder_set(Builder(Environment(self
._CacheDir
), action
))
88 #node.binfo = node.BuildInfo(node)
89 #node.binfo.ninfo.bsig = bsig
92 def tearDown(self
) -> None:
93 os
.remove(os
.path
.join(self
._CacheDir
.path
, 'config'))
94 os
.rmdir(self
._CacheDir
.path
)
96 class CacheDirTestCase(BaseTestCase
):
98 Test calling CacheDir code directly.
100 def test_cachepath(self
) -> None:
101 """Test the cachepath() method"""
103 # Verify how the cachepath() method determines the name
104 # of the file in cache.
105 def my_collect(list, hash_format
=None):
107 save_collect
= SCons
.Util
.hash_collect
108 SCons
.Util
.hash_collect
= my_collect
112 f5
= self
.File("cd.f5", name
)
113 result
= self
._CacheDir
.cachepath(f5
)
114 len = self
._CacheDir
.config
['prefix_len']
115 dirname
= os
.path
.join('cache', name
.upper()[:len])
116 filename
= os
.path
.join(dirname
, name
)
117 assert result
== (dirname
, filename
), result
119 SCons
.Util
.hash_collect
= save_collect
121 class ExceptionTestCase(unittest
.TestCase
):
122 """Test that the correct exceptions are thrown by CacheDir."""
124 # Don't inherit from BaseTestCase, we're by definition trying to
125 # break things so we really want a clean slate for each test.
126 def setUp(self
) -> None:
127 self
.tmpdir
= tempfile
.mkdtemp()
128 self
._CacheDir
= SCons
.CacheDir
.CacheDir(self
.tmpdir
)
130 def tearDown(self
) -> None:
131 shutil
.rmtree(self
.tmpdir
)
135 "Skip privileged CacheDir test on Windows, cannot change directory rights",
139 "Skip privileged CacheDir test if running as root.",
141 def test_throws_correct_on_OSError(self
) -> None:
142 """Test for correct error when cache directory cannot be created."""
144 privileged_dir
= os
.path
.join(self
.tmpdir
, "privileged")
145 cachedir_path
= os
.path
.join(privileged_dir
, "cache")
146 os
.makedirs(privileged_dir
, exist_ok
=True)
147 test
.writable(privileged_dir
, False)
148 with self
.assertRaises(SCons
.Errors
.SConsEnvironmentError
) as cm
:
149 cd
= SCons
.CacheDir
.CacheDir(cachedir_path
)
152 "Failed to create cache directory " + cachedir_path
154 test
.writable(privileged_dir
, True)
155 shutil
.rmtree(privileged_dir
)
158 def test_throws_correct_when_failed_to_write_configfile(self
) -> None:
159 class Unserializable
:
160 """A class which the JSON should not be able to serialize"""
162 def __init__(self
, oldconfig
) -> None:
163 self
.something
= 1 # Make the object unserializable
164 # Pretend to be the old config just enough
165 self
.__dict
__["prefix_len"] = oldconfig
["prefix_len"]
167 def __getitem__(self
, name
, default
=None):
168 if name
== "prefix_len":
169 return self
.__dict
__["prefix_len"]
173 def __setitem__(self
, name
, value
) -> None:
174 self
.__dict
__[name
] = value
176 oldconfig
= self
._CacheDir
.config
177 self
._CacheDir
.config
= Unserializable(oldconfig
)
178 # Remove the config file that got created on object creation
179 # so that _readconfig* will try to rewrite it
180 old_config
= os
.path
.join(self
._CacheDir
.path
, "config")
181 os
.remove(old_config
)
184 self
._CacheDir
._readconfig
(self
._CacheDir
.path
)
185 assert False, "Should have raised exception and did not"
186 except SCons
.Errors
.SConsEnvironmentError
as e
:
187 assert str(e
) == "Failed to write cache configuration for {}".format(self
._CacheDir
.path
)
189 def test_raise_environment_error_on_invalid_json(self
) -> None:
190 config_file
= os
.path
.join(self
._CacheDir
.path
, "config")
191 with
open(config_file
, "r") as cfg
:
193 # This will make JSON load raise a ValueError
195 with
open(config_file
, "w") as cfg
:
199 # Construct a new cache dir that will try to read the invalid config
200 new_cache_dir
= SCons
.CacheDir
.CacheDir(self
._CacheDir
.path
)
201 assert False, "Should have raised exception and did not"
202 except SCons
.Errors
.SConsEnvironmentError
as e
:
203 assert str(e
) == "Failed to read cache configuration for {}".format(self
._CacheDir
.path
)
205 class FileTestCase(BaseTestCase
):
207 Test calling CacheDir code through Node.FS.File interfaces.
209 # These tests were originally in Nodes/FSTests.py and got moved
210 # when the CacheDir support was refactored into its own module.
211 # Look in the history for Node/FSTests.py if any of this needs
213 def retrieve_succeed(self
, target
, source
, env
, execute
: int=1) -> int:
214 self
.retrieved
.append(target
)
217 def retrieve_fail(self
, target
, source
, env
, execute
: int=1) -> int:
218 self
.retrieved
.append(target
)
221 def push(self
, target
, source
, env
) -> int:
222 self
.pushed
.append(target
)
225 def test_CacheRetrieve(self
) -> None:
226 """Test the CacheRetrieve() function"""
228 save_CacheRetrieve
= SCons
.CacheDir
.CacheRetrieve
231 f1
= self
.File("cd.f1")
233 SCons
.CacheDir
.CacheRetrieve
= self
.retrieve_succeed
237 r
= f1
.retrieve_from_cache()
239 assert self
.retrieved
== [f1
], self
.retrieved
240 assert built_it
is None, built_it
242 SCons
.CacheDir
.CacheRetrieve
= self
.retrieve_fail
246 r
= f1
.retrieve_from_cache()
248 assert self
.retrieved
== [f1
], self
.retrieved
249 assert built_it
is None, built_it
251 SCons
.CacheDir
.CacheRetrieve
= save_CacheRetrieve
253 def test_CacheRetrieveSilent(self
) -> None:
254 """Test the CacheRetrieveSilent() function"""
256 save_CacheRetrieveSilent
= SCons
.CacheDir
.CacheRetrieveSilent
258 SCons
.CacheDir
.cache_show
= 1
260 f2
= self
.File("cd.f2", 'f2_bsig')
262 SCons
.CacheDir
.CacheRetrieveSilent
= self
.retrieve_succeed
266 r
= f2
.retrieve_from_cache()
268 assert self
.retrieved
== [f2
], self
.retrieved
269 assert built_it
is None, built_it
271 SCons
.CacheDir
.CacheRetrieveSilent
= self
.retrieve_fail
275 r
= f2
.retrieve_from_cache()
277 assert self
.retrieved
== [f2
], self
.retrieved
278 assert built_it
is None, built_it
280 SCons
.CacheDir
.CacheRetrieveSilent
= save_CacheRetrieveSilent
282 def test_CachePush(self
) -> None:
283 """Test the CachePush() function"""
285 save_CachePush
= SCons
.CacheDir
.CachePush
287 SCons
.CacheDir
.CachePush
= self
.push
292 cd_f3
= self
.test
.workpath("cd.f3")
293 f3
= self
.File(cd_f3
)
295 assert self
.pushed
== [], self
.pushed
296 self
.test
.write(cd_f3
, "cd.f3\n")
298 assert self
.pushed
== [f3
], self
.pushed
302 cd_f4
= self
.test
.workpath("cd.f4")
303 f4
= self
.File(cd_f4
)
305 assert self
.pushed
== [], self
.pushed
306 self
.test
.write(cd_f4
, "cd.f4\n")
309 assert self
.pushed
== [], self
.pushed
310 SCons
.CacheDir
.cache_force
= 1
313 assert self
.pushed
== [f4
], self
.pushed
315 SCons
.CacheDir
.CachePush
= save_CachePush
317 def test_warning(self
) -> None:
318 """Test raising a warning if we can't copy a file to cache."""
320 test
= TestCmd(workdir
='')
322 save_copy2
= shutil
.copy2
326 save_mkdir
= os
.mkdir
327 def mkdir(dir, mode
: int=0) -> None:
330 old_warn_exceptions
= SCons
.Warnings
.warningAsException(1)
331 SCons
.Warnings
.enableWarningClass(SCons
.Warnings
.CacheWriteErrorWarning
)
333 cd_f7
= self
.test
.workpath("cd.f7")
334 self
.test
.write(cd_f7
, "cd.f7\n")
335 f7
= self
.File(cd_f7
, 'f7_bsig')
338 r
= f7
.push_to_cache()
339 assert r
.exc_info
[0] == SCons
.Warnings
.CacheWriteErrorWarning
340 shutil
.copy2
= save_copy2
341 os
.mkdir
= save_mkdir
342 SCons
.Warnings
.warningAsException(old_warn_exceptions
)
343 SCons
.Warnings
.suppressWarningClass(SCons
.Warnings
.CacheWriteErrorWarning
)
345 def test_no_strfunction(self
) -> None:
346 """Test handling no strfunction() for an action."""
348 save_CacheRetrieveSilent
= SCons
.CacheDir
.CacheRetrieveSilent
350 f8
= self
.File("cd.f8", 'f8_bsig')
352 SCons
.CacheDir
.CacheRetrieveSilent
= self
.retrieve_succeed
356 r
= f8
.retrieve_from_cache()
358 assert self
.retrieved
== [f8
], self
.retrieved
359 assert built_it
is None, built_it
361 SCons
.CacheDir
.CacheRetrieveSilent
= self
.retrieve_fail
365 r
= f8
.retrieve_from_cache()
367 assert self
.retrieved
== [f8
], self
.retrieved
368 assert built_it
is None, built_it
370 SCons
.CacheDir
.CacheRetrieveSilent
= save_CacheRetrieveSilent
372 if __name__
== "__main__":
376 # indent-tabs-mode:nil
378 # vim: set expandtab tabstop=4 shiftwidth=4: