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 SCons
.Util
import get_hash_format
, get_current_hash_algorithm_used
34 def merge(self
, object) -> None:
37 class DummySConsignEntry
:
38 def __init__(self
, name
) -> None:
40 self
.binfo
= BuildInfo()
41 def convert_to_sconsign(self
) -> None:
43 def convert_from_sconsign(self
, dir, name
) -> None:
47 def __init__(self
, top
) -> None:
49 self
.Top
.repositories
= []
52 def __init__(self
, path
: str='not_a_valid_path', binfo
=None) -> None:
57 def get_stored_info(self
):
61 def get_internal_path(self
):
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:
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')
82 ccc
= DummySConsignEntry('ccc')
85 f
= SCons
.SConsign
.Base()
86 f
.set_entry('aaa', aaa
)
87 f
.set_entry('bbb', bbb
)
91 e
= f
.get_entry('aaa')
93 assert e
.name
== 'aaa', e
.name
95 e
= f
.get_entry('bbb')
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')
113 f
= SCons
.SConsign
.Base()
114 f
.set_entry('ddd', ddd
)
115 f
.set_entry('eee', eee
)
117 e
= f
.get_entry('ddd')
119 assert e
.name
== 'ddd', e
.name
121 e
= f
.get_entry('eee')
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
))
144 e
= f
.get_entry('aaa')
148 raise Exception("unexpected entry %s" % e
)
151 e
= f
.get_entry('bbb')
155 raise Exception("unexpected entry %s" % e
)
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')
173 assert e
.name
== 'bbb', e
.name
174 assert e
.arg1
== 'bbb arg1', e
.arg1
175 assert not hasattr(e
, 'arg2'), e
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')
189 f
= SCons
.SConsign
.Base()
190 f
.store_info('ddd', DummyNode('ddd', ddd
))
191 f
.store_info('eee', DummyNode('eee', eee
))
195 e
= f
.get_entry('ddd')
197 assert e
.name
== 'ddd', e
.name
199 e
= f
.get_entry('eee')
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')
208 assert e
.name
== 'eee', e
.name
209 assert not hasattr(e
, 'arg'), e
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
= {}
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'
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')
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:
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
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
318 def open(self
, name
, mode
):
322 def __getitem__(self
, key
) -> None:
324 def __setitem__(self
, key
, value
) -> None:
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:
349 file = test
.workpath('sconsign_file')
352 def __getitem__(self
, key
):
354 def __setitem__(self
, key
, value
) -> None:
356 def open(self
, name
, mode
):
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__":
388 # indent-tabs-mode:nil
390 # vim: set expandtab tabstop=4 shiftwidth=4: