e292aedaaa477399a3874fea90230b429d2a512e
[scons.git] / src / engine / SCons / SConsignTests.py
1 #
2 # __COPYRIGHT__
3 #
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:
11 #
12 # The above copyright notice and this permission notice shall be included
13 # in all copies or substantial portions of the Software.
14 #
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.
22 #
23
24 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
25
26 import os
27 import sys
28 import TestCmd
29 import unittest
30
31 import SCons.dblite
32
33 import SCons.SConsign
34
35 class BuildInfo:
36     def __init__(self, name):
37         self.name = name
38     def convert_to_sconsign(self):
39         self.c_to_s = 1
40     def convert_from_sconsign(self, dir, name):
41         self.c_from_s = 1
42
43 class DummyModule:
44     def to_string(self, sig):
45         return str(sig)
46
47     def from_string(self, sig):
48         return int(sig)
49
50 class FS:
51     def __init__(self, top):
52         self.Top = top
53         self.Top.repositories = []
54
55 class DummyNode:
56     def __init__(self, path='not_a_valid_path'):
57         self.path = path
58         self.tpath = path
59         self.fs = FS(self)
60
61 class SConsignTestCase(unittest.TestCase):
62     def setUp(self):
63         self.save_cwd = os.getcwd()
64         self.test = TestCmd.TestCmd(workdir = '')
65         os.chdir(self.test.workpath(''))
66     def tearDown(self):
67         self.test.cleanup()
68         SCons.SConsign.Reset()
69         os.chdir(self.save_cwd)
70
71 class BaseTestCase(SConsignTestCase):
72
73     def runTest(self):
74         aaa = BuildInfo('aaa')
75         bbb = BuildInfo('bbb')
76         bbb.arg1 = 'bbb arg1'
77         ccc = BuildInfo('ccc')
78         ccc.arg2 = 'ccc arg2'
79
80         f = SCons.SConsign.Base()
81         f.set_entry('aaa', aaa)
82         f.set_entry('bbb', bbb)
83
84         e = f.get_entry('aaa')
85         assert e == aaa, e
86         assert e.name == 'aaa', e.name
87
88         e = f.get_entry('bbb')
89         assert e == bbb, e
90         assert e.name == 'bbb', e.name
91         assert e.arg1 == 'bbb arg1', e.arg1
92         assert not hasattr(e, 'arg2'), e
93
94         f.set_entry('bbb', ccc)
95         e = f.get_entry('bbb')
96         assert e.name == 'ccc', e.name
97         assert not hasattr(e, 'arg1'), e
98         assert e.arg2 == 'ccc arg2', e.arg1
99
100         ddd = BuildInfo('ddd')
101         eee = BuildInfo('eee')
102         fff = BuildInfo('fff')
103         fff.arg = 'fff arg'
104
105         f = SCons.SConsign.Base(DummyModule())
106         f.set_entry('ddd', ddd)
107         f.set_entry('eee', eee)
108
109         e = f.get_entry('ddd')
110         assert e == ddd, e
111         assert e.name == 'ddd', e.name
112
113         e = f.get_entry('eee')
114         assert e == eee, e
115         assert e.name == 'eee', e.name
116         assert not hasattr(e, 'arg'), e
117
118         f.set_entry('eee', fff)
119         e = f.get_entry('eee')
120         assert e.name == 'fff', e.name
121         assert e.arg == 'fff arg', e.arg
122
123 class SConsignDBTestCase(SConsignTestCase):
124
125     def runTest(self):
126         save_DataBase = SCons.SConsign.DataBase
127         SCons.SConsign.DataBase = {}
128         try:
129             d1 = SCons.SConsign.DB(DummyNode('dir1'))
130             d1.set_entry('aaa', BuildInfo('aaa name'))
131             d1.set_entry('bbb', BuildInfo('bbb name'))
132             aaa = d1.get_entry('aaa')
133             assert aaa.name == 'aaa name'
134             bbb = d1.get_entry('bbb')
135             assert bbb.name == 'bbb name'
136
137             d2 = SCons.SConsign.DB(DummyNode('dir2'))
138             d2.set_entry('ccc', BuildInfo('ccc name'))
139             d2.set_entry('ddd', BuildInfo('ddd name'))
140             ccc = d2.get_entry('ccc')
141             assert ccc.name == 'ccc name'
142             ddd = d2.get_entry('ddd')
143             assert ddd.name == 'ddd name'
144
145             d31 = SCons.SConsign.DB(DummyNode('dir3/sub1'))
146             d31.set_entry('eee', BuildInfo('eee name'))
147             d31.set_entry('fff', BuildInfo('fff name'))
148             eee = d31.get_entry('eee')
149             assert eee.name == 'eee name'
150             fff = d31.get_entry('fff')
151             assert fff.name == 'fff name'
152
153             d32 = SCons.SConsign.DB(DummyNode('dir3%ssub2' % os.sep))
154             d32.set_entry('ggg', BuildInfo('ggg name'))
155             d32.set_entry('hhh', BuildInfo('hhh name'))
156             ggg = d32.get_entry('ggg')
157             assert ggg.name == 'ggg name'
158             hhh = d32.get_entry('hhh')
159             assert hhh.name == 'hhh name'
160         finally:
161             SCons.SConsign.DataBase = save_DataBase
162
163 class SConsignDirFileTestCase(SConsignTestCase):
164
165     def runTest(self):
166         bi_foo = BuildInfo('foo')
167         bi_bar = BuildInfo('bar')
168
169         f = SCons.SConsign.DirFile(DummyNode(), DummyModule())
170         f.set_entry('foo', bi_foo)
171         f.set_entry('bar', bi_bar)
172
173         e = f.get_entry('foo')
174         assert e == bi_foo, e
175         assert e.name == 'foo', e.name
176
177         assert bi_foo.c_from_s, bi_foo.c_from_s
178
179         e = f.get_entry('bar')
180         assert e == bi_bar, e
181         assert e.name == 'bar', e.name
182         assert not hasattr(e, 'arg'), e
183
184         assert bi_bar.c_from_s, bi_bar.c_from_s
185
186         bbb = BuildInfo('bbb')
187         bbb.arg = 'bbb arg'
188         f.set_entry('bar', bbb)
189         e = f.get_entry('bar')
190         assert e.name == 'bbb', e.name
191         assert e.arg == 'bbb arg', e.arg
192
193
194 class SConsignFileTestCase(SConsignTestCase):
195
196     def runTest(self):
197         test = self.test
198         file = test.workpath('sconsign_file')
199
200         assert SCons.SConsign.DataBase == {}, SCons.SConsign.DataBase
201         assert SCons.SConsign.DB_Name == ".sconsign", SCons.SConsign.DB_Name
202         assert SCons.SConsign.DB_Module is SCons.dblite, SCons.SConsign.DB_Module
203
204         SCons.SConsign.File(file)
205
206         assert SCons.SConsign.DataBase == {}, SCons.SConsign.DataBase
207         assert SCons.SConsign.DB_Name is file, SCons.SConsign.DB_Name
208         assert SCons.SConsign.DB_Module is SCons.dblite, SCons.SConsign.DB_Module
209
210         SCons.SConsign.File(None)
211
212         assert SCons.SConsign.DataBase == {}, SCons.SConsign.DataBase
213         assert SCons.SConsign.DB_Name is file, SCons.SConsign.DB_Name
214         assert SCons.SConsign.DB_Module is None, SCons.SConsign.DB_Module
215
216         class Fake_DBM:
217             def open(self, name, mode):
218                 self.name = name
219                 self.mode = mode
220                 return self
221             def __getitem__(self, key):
222                 pass
223             def __setitem__(self, key, value):
224                 pass
225
226         fake_dbm = Fake_DBM()
227
228         SCons.SConsign.File(file, fake_dbm)
229
230         assert SCons.SConsign.DataBase == {}, SCons.SConsign.DataBase
231         assert SCons.SConsign.DB_Name is file, SCons.SConsign.DB_Name
232         assert SCons.SConsign.DB_Module is fake_dbm, SCons.SConsign.DB_Module
233         assert not hasattr(fake_dbm, 'name'), fake_dbm
234         assert not hasattr(fake_dbm, 'mode'), fake_dbm
235
236         SCons.SConsign.ForDirectory(DummyNode(test.workpath('dir')))
237
238         assert not SCons.SConsign.DataBase is None, SCons.SConsign.DataBase
239         assert fake_dbm.name == file, fake_dbm.name
240         assert fake_dbm.mode == "c", fake_dbm.mode
241
242
243 class writeTestCase(SConsignTestCase):
244
245     def runTest(self):
246
247         test = self.test
248         file = test.workpath('sconsign_file')
249
250         class Fake_DBM:
251             def __getitem__(self, key):
252                 return None
253             def __setitem__(self, key, value):
254                 pass
255             def open(self, name, mode):
256                 self.sync_count = 0
257                 return self
258             def sync(self):
259                 self.sync_count = self.sync_count + 1
260
261         fake_dbm = Fake_DBM()
262
263         SCons.SConsign.DataBase = {}
264         SCons.SConsign.File(file, fake_dbm)
265
266         f = SCons.SConsign.DB(DummyNode(), DummyModule())
267
268         bi_foo = BuildInfo('foo')
269         bi_bar = BuildInfo('bar')
270         f.set_entry('foo', bi_foo)
271         f.set_entry('bar', bi_bar)
272
273         SCons.SConsign.write()
274
275         assert bi_foo.c_to_s, bi_foo.c_to_s
276         assert bi_bar.c_to_s, bi_bar.c_to_s
277
278         assert fake_dbm.sync_count == 1, fake_dbm.sync_count
279
280
281 def suite():
282     suite = unittest.TestSuite()
283     suite.addTest(BaseTestCase())
284     suite.addTest(SConsignDBTestCase())
285     suite.addTest(SConsignDirFileTestCase())
286     suite.addTest(SConsignFileTestCase())
287     suite.addTest(writeTestCase())
288     return suite
289
290 if __name__ == "__main__":
291     runner = unittest.TextTestRunner()
292     result = runner.run(suite())
293     if not result.wasSuccessful():
294         sys.exit(1)
295