summaryrefslogtreecommitdiff
path: root/src/engine/SCons/SConsignTests.py
blob: 8d883c5f8e3b86177d242b1b63fb46fe4b48e658 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#
# Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 The SCons Foundation
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#

__revision__ = "src/engine/SCons/SConsignTests.py 4720 2010/03/24 03:14:11 jars"

import os
import sys
import TestCmd
import unittest

import SCons.dblite

import SCons.SConsign

class BuildInfo:
    def merge(self, object):
        pass

class DummySConsignEntry:
    def __init__(self, name):
        self.name = name
        self.binfo = BuildInfo()
    def convert_to_sconsign(self):
        self.c_to_s = 1
    def convert_from_sconsign(self, dir, name):
        self.c_from_s = 1

class FS:
    def __init__(self, top):
        self.Top = top
        self.Top.repositories = []

class DummyNode:
    def __init__(self, path='not_a_valid_path', binfo=None):
        self.path = path
        self.tpath = path
        self.fs = FS(self)
        self.binfo = binfo
    def get_stored_info(self):
        return self.binfo
    def get_binfo(self):
        return self.binfo

class SConsignTestCase(unittest.TestCase):
    def setUp(self):
        self.save_cwd = os.getcwd()
        self.test = TestCmd.TestCmd(workdir = '')
        os.chdir(self.test.workpath(''))
    def tearDown(self):
        self.test.cleanup()
        SCons.SConsign.Reset()
        os.chdir(self.save_cwd)

class BaseTestCase(SConsignTestCase):

    def test_Base(self):
        aaa = DummySConsignEntry('aaa')
        bbb = DummySConsignEntry('bbb')
        bbb.arg1 = 'bbb arg1'
        ccc = DummySConsignEntry('ccc')
        ccc.arg2 = 'ccc arg2'

        f = SCons.SConsign.Base()
        f.set_entry('aaa', aaa)
        f.set_entry('bbb', bbb)

        #f.merge()

        e = f.get_entry('aaa')
        assert e == aaa, e
        assert e.name == 'aaa', e.name

        e = f.get_entry('bbb')
        assert e == bbb, e
        assert e.name == 'bbb', e.name
        assert e.arg1 == 'bbb arg1', e.arg1
        assert not hasattr(e, 'arg2'), e

        f.set_entry('bbb', ccc)

        e = f.get_entry('bbb')
        assert e.name == 'ccc', e.name
        assert not hasattr(e, 'arg1'), e
        assert e.arg2 == 'ccc arg2', e.arg1

        ddd = DummySConsignEntry('ddd')
        eee = DummySConsignEntry('eee')
        fff = DummySConsignEntry('fff')
        fff.arg = 'fff arg'

        f = SCons.SConsign.Base()
        f.set_entry('ddd', ddd)
        f.set_entry('eee', eee)

        e = f.get_entry('ddd')
        assert e == ddd, e
        assert e.name == 'ddd', e.name

        e = f.get_entry('eee')
        assert e == eee, e
        assert e.name == 'eee', e.name
        assert not hasattr(e, 'arg'), e

        f.set_entry('eee', fff)

        e = f.get_entry('eee')
        assert e.name == 'fff', e.name
        assert e.arg == 'fff arg', e.arg

    def test_store_info(self):
        aaa = DummySConsignEntry('aaa')
        bbb = DummySConsignEntry('bbb')
        bbb.arg1 = 'bbb arg1'
        ccc = DummySConsignEntry('ccc')
        ccc.arg2 = 'ccc arg2'

        f = SCons.SConsign.Base()
        f.store_info('aaa', DummyNode('aaa', aaa))
        f.store_info('bbb', DummyNode('bbb', bbb))

        try:
            e = f.get_entry('aaa')
        except KeyError:
            pass
        else:
            raise "unexpected entry %s" % e

        try:
            e = f.get_entry('bbb')
        except KeyError:
            pass
        else:
            raise "unexpected entry %s" % e

        f.merge()

        e = f.get_entry('aaa')
        assert e == aaa, "aaa = %s, e = %s" % (aaa, e)
        assert e.name == 'aaa', e.name

        e = f.get_entry('bbb')
        assert e == bbb, "bbb = %s, e = %s" % (bbb, e)
        assert e.name == 'bbb', e.name
        assert e.arg1 == 'bbb arg1', e.arg1
        assert not hasattr(e, 'arg2'), e

        f.store_info('bbb', DummyNode('bbb', ccc))

        e = f.get_entry('bbb')
        assert e == bbb, e
        assert e.name == 'bbb', e.name
        assert e.arg1 == 'bbb arg1', e.arg1
        assert not hasattr(e, 'arg2'), e

        f.merge()

        e = f.get_entry('bbb')
        assert e.name == 'ccc', e.name
        assert not hasattr(e, 'arg1'), e
        assert e.arg2 == 'ccc arg2', e.arg1

        ddd = DummySConsignEntry('ddd')
        eee = DummySConsignEntry('eee')
        fff = DummySConsignEntry('fff')
        fff.arg = 'fff arg'

        f = SCons.SConsign.Base()
        f.store_info('ddd', DummyNode('ddd', ddd))
        f.store_info('eee', DummyNode('eee', eee))

        f.merge()

        e = f.get_entry('ddd')
        assert e == ddd, e
        assert e.name == 'ddd', e.name

        e = f.get_entry('eee')
        assert e == eee, e
        assert e.name == 'eee', e.name
        assert not hasattr(e, 'arg'), e

        f.store_info('eee', DummyNode('eee', fff))

        e = f.get_entry('eee')
        assert e == eee, e
        assert e.name == 'eee', e.name
        assert not hasattr(e, 'arg'), e

        f.merge()

        e = f.get_entry('eee')
        assert e.name == 'fff', e.name
        assert e.arg == 'fff arg', e.arg

class SConsignDBTestCase(SConsignTestCase):

    def test_SConsignDB(self):
        save_DataBase = SCons.SConsign.DataBase
        SCons.SConsign.DataBase = {}
        try:
            d1 = SCons.SConsign.DB(DummyNode('dir1'))
            d1.set_entry('aaa', DummySConsignEntry('aaa name'))
            d1.set_entry('bbb', DummySConsignEntry('bbb name'))

            aaa = d1.get_entry('aaa')
            assert aaa.name == 'aaa name'
            bbb = d1.get_entry('bbb')
            assert bbb.name == 'bbb name'

            d2 = SCons.SConsign.DB(DummyNode('dir2'))
            d2.set_entry('ccc', DummySConsignEntry('ccc name'))
            d2.set_entry('ddd', DummySConsignEntry('ddd name'))

            ccc = d2.get_entry('ccc')
            assert ccc.name == 'ccc name'
            ddd = d2.get_entry('ddd')
            assert ddd.name == 'ddd name'

            d31 = SCons.SConsign.DB(DummyNode('dir3/sub1'))
            d31.set_entry('eee', DummySConsignEntry('eee name'))
            d31.set_entry('fff', DummySConsignEntry('fff name'))

            eee = d31.get_entry('eee')
            assert eee.name == 'eee name'
            fff = d31.get_entry('fff')
            assert fff.name == 'fff name'

            d32 = SCons.SConsign.DB(DummyNode('dir3%ssub2' % os.sep))
            d32.set_entry('ggg', DummySConsignEntry('ggg name'))
            d32.set_entry('hhh', DummySConsignEntry('hhh name'))

            ggg = d32.get_entry('ggg')
            assert ggg.name == 'ggg name'
            hhh = d32.get_entry('hhh')
            assert hhh.name == 'hhh name'

        finally:

            SCons.SConsign.DataBase = save_DataBase

class SConsignDirFileTestCase(SConsignTestCase):

    def test_SConsignDirFile(self):
        bi_foo = DummySConsignEntry('foo')
        bi_bar = DummySConsignEntry('bar')

        f = SCons.SConsign.DirFile(DummyNode())
        f.set_entry('foo', bi_foo)
        f.set_entry('bar', bi_bar)

        e = f.get_entry('foo')
        assert e == bi_foo, e
        assert e.name == 'foo', e.name

        e = f.get_entry('bar')
        assert e == bi_bar, e
        assert e.name == 'bar', e.name
        assert not hasattr(e, 'arg'), e

        bbb = DummySConsignEntry('bbb')
        bbb.arg = 'bbb arg'

        f.set_entry('bar', bbb)

        e = f.get_entry('bar')
        assert e.name == 'bbb', e.name
        assert e.arg == 'bbb arg', e.arg


class SConsignFileTestCase(SConsignTestCase):

    def test_SConsignFile(self):
        test = self.test
        file = test.workpath('sconsign_file')

        assert SCons.SConsign.DataBase == {}, SCons.SConsign.DataBase
        assert SCons.SConsign.DB_Name == ".sconsign", SCons.SConsign.DB_Name
        assert SCons.SConsign.DB_Module is SCons.dblite, SCons.SConsign.DB_Module

        SCons.SConsign.File(file)

        assert SCons.SConsign.DataBase == {}, SCons.SConsign.DataBase
        assert SCons.SConsign.DB_Name is file, SCons.SConsign.DB_Name
        assert SCons.SConsign.DB_Module is SCons.dblite, SCons.SConsign.DB_Module

        SCons.SConsign.File(None)

        assert SCons.SConsign.DataBase == {}, SCons.SConsign.DataBase
        assert SCons.SConsign.DB_Name is file, SCons.SConsign.DB_Name
        assert SCons.SConsign.DB_Module is None, SCons.SConsign.DB_Module

        class Fake_DBM:
            def open(self, name, mode):
                self.name = name
                self.mode = mode
                return self
            def __getitem__(self, key):
                pass
            def __setitem__(self, key, value):
                pass

        fake_dbm = Fake_DBM()

        SCons.SConsign.File(file, fake_dbm)

        assert SCons.SConsign.DataBase == {}, SCons.SConsign.DataBase
        assert SCons.SConsign.DB_Name is file, SCons.SConsign.DB_Name
        assert SCons.SConsign.DB_Module is fake_dbm, SCons.SConsign.DB_Module
        assert not hasattr(fake_dbm, 'name'), fake_dbm
        assert not hasattr(fake_dbm, 'mode'), fake_dbm

        SCons.SConsign.ForDirectory(DummyNode(test.workpath('dir')))

        assert not SCons.SConsign.DataBase is None, SCons.SConsign.DataBase
        assert fake_dbm.name == file, fake_dbm.name
        assert fake_dbm.mode == "c", fake_dbm.mode


class writeTestCase(SConsignTestCase):

    def test_write(self):

        test = self.test
        file = test.workpath('sconsign_file')

        class Fake_DBM:
            def __getitem__(self, key):
                return None
            def __setitem__(self, key, value):
                pass
            def open(self, name, mode):
                self.sync_count = 0
                return self
            def sync(self):
                self.sync_count = self.sync_count + 1

        fake_dbm = Fake_DBM()

        SCons.SConsign.DataBase = {}
        SCons.SConsign.File(file, fake_dbm)

        f = SCons.SConsign.DB(DummyNode())

        bi_foo = DummySConsignEntry('foo')
        bi_bar = DummySConsignEntry('bar')
        f.set_entry('foo', bi_foo)
        f.set_entry('bar', bi_bar)

        SCons.SConsign.write()

        assert bi_foo.c_to_s, bi_foo.c_to_s
        assert bi_bar.c_to_s, bi_bar.c_to_s

        assert fake_dbm.sync_count == 1, fake_dbm.sync_count



if __name__ == "__main__":
    suite = unittest.TestSuite()
    tclasses = [
        BaseTestCase,
        SConsignDBTestCase,
        SConsignDirFileTestCase,
        SConsignFileTestCase,
        writeTestCase,
    ]
    for tclass in tclasses:
        names = unittest.getTestCaseNames(tclass, 'test_')
        suite.addTests(map(tclass, names))
    if not unittest.TextTestRunner().run(suite).wasSuccessful():
        sys.exit(1)

# Local Variables:
# tab-width:4
# indent-tabs-mode:nil
# End:
# vim: set expandtab tabstop=4 shiftwidth=4: