switch from embedding LIBLITERAL in _stripixes() to being a passed argument with...
[scons.git] / SCons / SConsignTests.py
blob84bc2000ba434e636f8e2aa542fe52b99d8438d3
1 # MIT License
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.
24 import os
25 import unittest
27 import TestCmd
29 import SCons.dblite
30 import SCons.SConsign
31 from SCons.Util import get_hash_format, get_current_hash_algorithm_used
33 class BuildInfo:
34 def merge(self, object) -> None:
35 pass
37 class DummySConsignEntry:
38 def __init__(self, name) -> None:
39 self.name = name
40 self.binfo = BuildInfo()
41 def convert_to_sconsign(self) -> None:
42 self.c_to_s = 1
43 def convert_from_sconsign(self, dir, name) -> None:
44 self.c_from_s = 1
46 class FS:
47 def __init__(self, top) -> None:
48 self.Top = top
49 self.Top.repositories = []
51 class DummyNode:
52 def __init__(self, path: str='not_a_valid_path', binfo=None) -> None:
53 self.path = path
54 self.tpath = path
55 self.fs = FS(self)
56 self.binfo = binfo
57 def get_stored_info(self):
58 return self.binfo
59 def get_binfo(self):
60 return self.binfo
61 def get_internal_path(self):
62 return self.path
63 def get_tpath(self):
64 return self.tpath
66 class SConsignTestCase(unittest.TestCase):
67 def setUp(self) -> None:
68 self.save_cwd = os.getcwd()
69 self.test = TestCmd.TestCmd(workdir = '')
70 os.chdir(self.test.workpath(''))
71 def tearDown(self) -> None:
72 self.test.cleanup()
73 SCons.SConsign.Reset()
74 os.chdir(self.save_cwd)
76 class BaseTestCase(SConsignTestCase):
78 def test_Base(self) -> None:
79 aaa = DummySConsignEntry('aaa')
80 bbb = DummySConsignEntry('bbb')
81 bbb.arg1 = 'bbb arg1'
82 ccc = DummySConsignEntry('ccc')
83 ccc.arg2 = 'ccc arg2'
85 f = SCons.SConsign.Base()
86 f.set_entry('aaa', aaa)
87 f.set_entry('bbb', bbb)
89 #f.merge()
91 e = f.get_entry('aaa')
92 assert e == aaa, e
93 assert e.name == 'aaa', e.name
95 e = f.get_entry('bbb')
96 assert e == bbb, e
97 assert e.name == 'bbb', e.name
98 assert e.arg1 == 'bbb arg1', e.arg1
99 assert not hasattr(e, 'arg2'), e
101 f.set_entry('bbb', ccc)
103 e = f.get_entry('bbb')
104 assert e.name == 'ccc', e.name
105 assert not hasattr(e, 'arg1'), e
106 assert e.arg2 == 'ccc arg2', e.arg1
108 ddd = DummySConsignEntry('ddd')
109 eee = DummySConsignEntry('eee')
110 fff = DummySConsignEntry('fff')
111 fff.arg = 'fff arg'
113 f = SCons.SConsign.Base()
114 f.set_entry('ddd', ddd)
115 f.set_entry('eee', eee)
117 e = f.get_entry('ddd')
118 assert e == ddd, e
119 assert e.name == 'ddd', e.name
121 e = f.get_entry('eee')
122 assert e == eee, e
123 assert e.name == 'eee', e.name
124 assert not hasattr(e, 'arg'), e
126 f.set_entry('eee', fff)
128 e = f.get_entry('eee')
129 assert e.name == 'fff', e.name
130 assert e.arg == 'fff arg', e.arg
132 def test_store_info(self):
133 aaa = DummySConsignEntry('aaa')
134 bbb = DummySConsignEntry('bbb')
135 bbb.arg1 = 'bbb arg1'
136 ccc = DummySConsignEntry('ccc')
137 ccc.arg2 = 'ccc arg2'
139 f = SCons.SConsign.Base()
140 f.store_info('aaa', DummyNode('aaa', aaa))
141 f.store_info('bbb', DummyNode('bbb', bbb))
143 try:
144 e = f.get_entry('aaa')
145 except KeyError:
146 pass
147 else:
148 raise Exception("unexpected entry %s" % e)
150 try:
151 e = f.get_entry('bbb')
152 except KeyError:
153 pass
154 else:
155 raise Exception("unexpected entry %s" % e)
157 f.merge()
159 e = f.get_entry('aaa')
160 assert e == aaa, "aaa = %s, e = %s" % (aaa, e)
161 assert e.name == 'aaa', e.name
163 e = f.get_entry('bbb')
164 assert e == bbb, "bbb = %s, e = %s" % (bbb, e)
165 assert e.name == 'bbb', e.name
166 assert e.arg1 == 'bbb arg1', e.arg1
167 assert not hasattr(e, 'arg2'), e
169 f.store_info('bbb', DummyNode('bbb', ccc))
171 e = f.get_entry('bbb')
172 assert e == bbb, e
173 assert e.name == 'bbb', e.name
174 assert e.arg1 == 'bbb arg1', e.arg1
175 assert not hasattr(e, 'arg2'), e
177 f.merge()
179 e = f.get_entry('bbb')
180 assert e.name == 'ccc', e.name
181 assert not hasattr(e, 'arg1'), e
182 assert e.arg2 == 'ccc arg2', e.arg1
184 ddd = DummySConsignEntry('ddd')
185 eee = DummySConsignEntry('eee')
186 fff = DummySConsignEntry('fff')
187 fff.arg = 'fff arg'
189 f = SCons.SConsign.Base()
190 f.store_info('ddd', DummyNode('ddd', ddd))
191 f.store_info('eee', DummyNode('eee', eee))
193 f.merge()
195 e = f.get_entry('ddd')
196 assert e == ddd, e
197 assert e.name == 'ddd', e.name
199 e = f.get_entry('eee')
200 assert e == eee, e
201 assert e.name == 'eee', e.name
202 assert not hasattr(e, 'arg'), e
204 f.store_info('eee', DummyNode('eee', fff))
206 e = f.get_entry('eee')
207 assert e == eee, e
208 assert e.name == 'eee', e.name
209 assert not hasattr(e, 'arg'), e
211 f.merge()
213 e = f.get_entry('eee')
214 assert e.name == 'fff', e.name
215 assert e.arg == 'fff arg', e.arg
217 class SConsignDBTestCase(SConsignTestCase):
219 def test_SConsignDB(self) -> None:
220 save_DataBase = SCons.SConsign.DataBase
221 SCons.SConsign.DataBase = {}
222 try:
223 d1 = SCons.SConsign.DB(DummyNode('dir1'))
224 d1.set_entry('aaa', DummySConsignEntry('aaa name'))
225 d1.set_entry('bbb', DummySConsignEntry('bbb name'))
227 aaa = d1.get_entry('aaa')
228 assert aaa.name == 'aaa name'
229 bbb = d1.get_entry('bbb')
230 assert bbb.name == 'bbb name'
232 d2 = SCons.SConsign.DB(DummyNode('dir2'))
233 d2.set_entry('ccc', DummySConsignEntry('ccc name'))
234 d2.set_entry('ddd', DummySConsignEntry('ddd name'))
236 ccc = d2.get_entry('ccc')
237 assert ccc.name == 'ccc name'
238 ddd = d2.get_entry('ddd')
239 assert ddd.name == 'ddd name'
241 d31 = SCons.SConsign.DB(DummyNode('dir3/sub1'))
242 d31.set_entry('eee', DummySConsignEntry('eee name'))
243 d31.set_entry('fff', DummySConsignEntry('fff name'))
245 eee = d31.get_entry('eee')
246 assert eee.name == 'eee name'
247 fff = d31.get_entry('fff')
248 assert fff.name == 'fff name'
250 d32 = SCons.SConsign.DB(DummyNode('dir3%ssub2' % os.sep))
251 d32.set_entry('ggg', DummySConsignEntry('ggg name'))
252 d32.set_entry('hhh', DummySConsignEntry('hhh name'))
254 ggg = d32.get_entry('ggg')
255 assert ggg.name == 'ggg name'
256 hhh = d32.get_entry('hhh')
257 assert hhh.name == 'hhh name'
259 finally:
261 SCons.SConsign.DataBase = save_DataBase
263 class SConsignDirFileTestCase(SConsignTestCase):
265 def test_SConsignDirFile(self) -> None:
266 bi_foo = DummySConsignEntry('foo')
267 bi_bar = DummySConsignEntry('bar')
269 f = SCons.SConsign.DirFile(DummyNode())
270 f.set_entry('foo', bi_foo)
271 f.set_entry('bar', bi_bar)
273 e = f.get_entry('foo')
274 assert e == bi_foo, e
275 assert e.name == 'foo', e.name
277 e = f.get_entry('bar')
278 assert e == bi_bar, e
279 assert e.name == 'bar', e.name
280 assert not hasattr(e, 'arg'), e
282 bbb = DummySConsignEntry('bbb')
283 bbb.arg = 'bbb arg'
285 f.set_entry('bar', bbb)
287 e = f.get_entry('bar')
288 assert e.name == 'bbb', e.name
289 assert e.arg == 'bbb arg', e.arg
292 class SConsignFileTestCase(SConsignTestCase):
294 def test_SConsignFile(self) -> None:
295 test = self.test
296 file = test.workpath('sconsign_file')
298 assert SCons.SConsign.DataBase == {}, SCons.SConsign.DataBase
299 if get_hash_format() is None and get_current_hash_algorithm_used() == 'md5':
300 assert SCons.SConsign.DB_Name == ".sconsign", SCons.SConsign.DB_Name
301 else:
302 assert SCons.SConsign.DB_Name == ".sconsign_{}".format(get_current_hash_algorithm_used()), SCons.SConsign.DB_Name
303 assert SCons.SConsign.DB_Module is SCons.dblite, SCons.SConsign.DB_Module
305 SCons.SConsign.File(file)
307 assert SCons.SConsign.DataBase == {}, SCons.SConsign.DataBase
308 assert SCons.SConsign.DB_Name is file, SCons.SConsign.DB_Name
309 assert SCons.SConsign.DB_Module is SCons.dblite, SCons.SConsign.DB_Module
311 SCons.SConsign.File(None)
313 assert SCons.SConsign.DataBase == {}, SCons.SConsign.DataBase
314 assert SCons.SConsign.DB_Name is file, SCons.SConsign.DB_Name
315 assert SCons.SConsign.DB_Module is None, SCons.SConsign.DB_Module
317 class Fake_DBM:
318 def open(self, name, mode):
319 self.name = name
320 self.mode = mode
321 return self
322 def __getitem__(self, key) -> None:
323 pass
324 def __setitem__(self, key, value) -> None:
325 pass
327 fake_dbm = Fake_DBM()
329 SCons.SConsign.File(file, fake_dbm)
331 assert SCons.SConsign.DataBase == {}, SCons.SConsign.DataBase
332 assert SCons.SConsign.DB_Name is file, SCons.SConsign.DB_Name
333 assert SCons.SConsign.DB_Module is fake_dbm, SCons.SConsign.DB_Module
334 assert not hasattr(fake_dbm, 'name'), fake_dbm
335 assert not hasattr(fake_dbm, 'mode'), fake_dbm
337 SCons.SConsign.ForDirectory(DummyNode(test.workpath('dir')))
339 assert SCons.SConsign.DataBase is not None, SCons.SConsign.DataBase
340 assert fake_dbm.name == file, fake_dbm.name
341 assert fake_dbm.mode == "c", fake_dbm.mode
344 class writeTestCase(SConsignTestCase):
346 def test_write(self) -> None:
348 test = self.test
349 file = test.workpath('sconsign_file')
351 class Fake_DBM:
352 def __getitem__(self, key):
353 return None
354 def __setitem__(self, key, value) -> None:
355 pass
356 def open(self, name, mode):
357 self.sync_count = 0
358 return self
359 def sync(self) -> None:
360 self.sync_count = self.sync_count + 1
362 fake_dbm = Fake_DBM()
364 SCons.SConsign.DataBase = {}
365 SCons.SConsign.File(file, fake_dbm)
367 f = SCons.SConsign.DB(DummyNode())
369 bi_foo = DummySConsignEntry('foo')
370 bi_bar = DummySConsignEntry('bar')
371 f.set_entry('foo', bi_foo)
372 f.set_entry('bar', bi_bar)
374 SCons.SConsign.write()
376 assert bi_foo.c_to_s, bi_foo.c_to_s
377 assert bi_bar.c_to_s, bi_bar.c_to_s
379 assert fake_dbm.sync_count == 1, fake_dbm.sync_count
383 if __name__ == "__main__":
384 unittest.main()
386 # Local Variables:
387 # tab-width:4
388 # indent-tabs-mode:nil
389 # End:
390 # vim: set expandtab tabstop=4 shiftwidth=4: