summaryrefslogtreecommitdiffstats
path: root/src/engine/SCons/Sig/SigTests.py
blob: f297464d876d55a74deb44e71b2f252ebeb62054 (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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
#
# __COPYRIGHT__
#
# 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__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"

import unittest
import TestCmd
import SCons.Sig
import SCons.Sig.MD5
import SCons.Sig.TimeStamp
import sys


class DummyFile:
    """A class that simulates a file for testing purposes"""
    def __init__(self, path, contents, timestamp, builder):
        self.path = path
        self.contents = contents
        self.timestamp = timestamp
        self.builder = builder

    def modify(self, contents, timestamp):
        self.contents = contents
        self.timestamp = timestamp

class DummyNode:
    """A node workalike for testing purposes"""

    def __init__(self, file):
        self.file = file
        self.path = file.path
        self.builder = file.builder
        self.depends = []
        self.ignore = []
        self.use_signature = 1
        self.bsig = None
        self.csig = None
        self.oldtime = 0
        self.oldbsig = 0
        self.oldcsig = 0
        self.always_build = 0

    def has_builder(self):
        return self.builder

    def get_contents(self):
        # a file that doesn't exist has no contents:
        assert self.exists()

        return self.file.contents

    def get_timestamp(self):
        # a file that doesn't exist has no timestamp:
        assert self.exists()

        return self.file.timestamp

    def exists(self):
        return not self.file.contents is None

    def cached_exists(self):
        try:
            return self.exists_cache
        except AttributeError:
            self.exists_cache = self.exists()
            return self.exists_cache

    def rexists(self):
        return not self.file.contents is None

    def children(self):
        return filter(lambda x, i=self.ignore: x not in i,
                      self.sources + self.depends)

    def all_children(self):
        return self.sources + self.depends

    def current(self):
        if not self.exists():
            return 0
        return None

    def calc_signature(self, calc):
        if self.has_builder():
            return calc.bsig(self)
        else:
            return calc.csig(self)

    def set_bsig(self, bsig):
        self.bsig = bsig

    def get_bsig(self):
        return self.bsig

    def store_bsig(self):
        pass

    def set_csig(self, csig):
        self.csig = csig

    def get_csig(self):
        return self.csig

    def store_csig(self):
        pass

    def get_prevsiginfo(self):
        return (self.oldtime, self.oldbsig, self.oldcsig)

    def get_stored_implicit(self):
        return None

    def store_csig(self):
        pass

    def store_bsig(self):
        pass
    
    def store_timestamp(self):
        pass

    def get_executor(self):
        class Adapter:
            def get_contents(self):
                return 111
            def get_timestamp(self):
                return 222
        return Adapter()


def create_files(test):
    args  = [(test.workpath('f1.c'), 'blah blah', 111, 0),     #0
             (test.workpath('f1'), None, 0, 1),                #1
             (test.workpath('d1/f1.c'), 'blah blah', 111, 0),  #2
             (test.workpath('d1/f1.h'), 'blah blah', 111, 0),  #3
             (test.workpath('d1/f2.c'), 'blah blah', 111, 0),  #4
             (test.workpath('d1/f3.h'), 'blah blah', 111, 0),  #5
             (test.workpath('d1/f4.h'), 'blah blah', 111, 0),  #6
             (test.workpath('d1/f1'), None, 0, 1),             #7
             (test.workpath('d1/test.c'), 'blah blah', 111, 0),#8
             (test.workpath('d1/test.o'), None, 0, 1),         #9
             (test.workpath('d1/test'), None, 0, 1)]           #10

    files = map(lambda x: apply(DummyFile, x), args)

    return files

def create_nodes(files):
    nodes = map(DummyNode, files)

    nodes[0].sources = []
    nodes[1].sources = [nodes[0]]
    nodes[2].sources = []
    nodes[3].sources = []
    nodes[4].sources = []
    nodes[5].sources = [nodes[6]]
    nodes[6].sources = [nodes[5]]
    nodes[7].sources = [nodes[2], nodes[4], nodes[3], nodes[5]]
    nodes[8].sources = []
    nodes[9].sources = [nodes[8]]
    nodes[10].sources = [nodes[9]]

    return nodes

def current(calc, node):
    return calc.current(node, node.calc_signature(calc))

def write(calc, nodes):
    for node in nodes:
        node.oldtime = node.file.timestamp
        node.oldbsig = calc.bsig(node)
        node.oldcsig = calc.csig(node)

def clear(nodes):
    for node in nodes:
        node.csig = None
        node.bsig = None

class SigTestBase:

    def runTest(self):

        test = TestCmd.TestCmd(workdir = '')
        test.subdir('d1')

        self.files = create_files(test)
        self.test_initial()
        self.test_built()
        self.test_modify()
        self.test_modify_same_time()

    def test_initial(self):

        nodes = create_nodes(self.files)
        calc = SCons.Sig.Calculator(self.module)

        for node in nodes:
            self.failUnless(not current(calc, node),
                            "node %s should not be current" % node.path)

        # simulate a build:
        self.files[1].modify('built', 222)
        self.files[7].modify('built', 222)
        self.files[9].modify('built', 222)
        self.files[10].modify('built', 222)

    def test_built(self):

        nodes = create_nodes(self.files)

        calc = SCons.Sig.Calculator(self.module)

        write(calc, nodes)

        for node in nodes:
            self.failUnless(current(calc, node),
                            "node %s should be current" % node.path)

    def test_modify(self):

        nodes = create_nodes(self.files)

        calc = SCons.Sig.Calculator(self.module)

        write(calc, nodes)

        #simulate a modification of some files
        self.files[0].modify('blah blah blah', 333)
        self.files[3].modify('blah blah blah', 333)
        self.files[6].modify('blah blah blah', 333)
        self.files[8].modify('blah blah blah', 333)

        clear(nodes)

        self.failUnless(not current(calc, nodes[0]), "modified directly")
        self.failUnless(not current(calc, nodes[1]), "direct source modified")
        self.failUnless(current(calc, nodes[2]))
        self.failUnless(not current(calc, nodes[3]), "modified directly")
        self.failUnless(current(calc, nodes[4]))
        self.failUnless(current(calc, nodes[5]))
        self.failUnless(not current(calc, nodes[6]), "modified directly")
        self.failUnless(not current(calc, nodes[7]), "indirect source modified")
        self.failUnless(not current(calc, nodes[8]), "modified directory")
        self.failUnless(not current(calc, nodes[9]), "direct source modified")
        self.failUnless(not current(calc, nodes[10]), "indirect source modified")

    def test_modify_same_time(self):

        nodes = create_nodes(self.files)

        calc = SCons.Sig.Calculator(self.module, 0)

        write(calc, nodes)

        #simulate a modification of some files without changing the timestamp:
        self.files[0].modify('blah blah blah blah', 333)
        self.files[3].modify('blah blah blah blah', 333)
        self.files[6].modify('blah blah blah blah', 333)
        self.files[8].modify('blah blah blah blah', 333)

        clear(nodes)

        for node in nodes:
            self.failUnless(current(calc, node),
                            "node %s should be current" % node.path)


class MD5TestCase(unittest.TestCase, SigTestBase):
    """Test MD5 signatures"""

    module = SCons.Sig.MD5

class TimeStampTestCase(unittest.TestCase, SigTestBase):
    """Test timestamp signatures"""

    module = SCons.Sig.TimeStamp

class CalcTestCase(unittest.TestCase):

    def runTest(self):
        class MySigModule:
            def collect(self, signatures):
                return reduce(lambda x, y: x + y, signatures)
            def current(self, newsig, oldsig):
                return newsig == oldsig
            def signature(self, node):
                return node.get_csig()

        class MyNode:
            def __init__(self, name, bsig, csig):
                self.name = name
                self.bsig = bsig
                self.csig = csig
                self.kids = []
                self.ignore = []
                self.builder = None
                self.use_signature = 1
            def has_builder(self):
                return not self.builder is None
            def children(self):
                return filter(lambda x, i=self.ignore: x not in i, self.kids)
            def all_children(self):
                return self.kids
            def exists(self):
                return 1
            def cached_exists(self):
                return 1
            def get_bsig(self):
                return self.bsig
            def set_bsig(self, bsig):
                self.bsig = bsig
            def get_csig(self):
                return self.csig
            def set_csig(self, csig):
                self.csig = csig
            def store_csig(self):
                pass
            def store_timestamp(self):
                pass
            def get_prevsiginfo(self):
                return 0, self.bsig, self.csig
            def get_stored_implicit(self):
                return None
            def get_timestamp(self):
                return 1
            def builder_sig_adapter(self):
                class MyAdapter:
                    def get_csig(self):
                        return 333
                    def get_timestamp(self):
                        return 444
                return MyAdapter()

        self.module = MySigModule()
        self.nodeclass = MyNode
        self.test_Calc___init__()
        self.test_Calc_bsig()
        self.test_Calc_current()

    def test_Calc___init__(self):
        self.calc = SCons.Sig.Calculator(self.module)
        assert self.calc.module == self.module

    def test_Calc_bsig(self):
        n1 = self.nodeclass('n1', 11, 12)
        n2 = self.nodeclass('n2', 22, 23)
        n3 = self.nodeclass('n3', 33, 34)
        n1.builder = 1
        n1.kids = [n2, n3]

        assert self.calc.bsig(n1) == 55

        n1.ignore = [n2]

        assert self.calc.bsig(n1) == 33

    def test_Calc_bsig(self):
        n = self.nodeclass('n', 11, 12)

        assert self.calc.csig(n) == 12

    def test_Calc_current(self):
        class NN(self.nodeclass):
            always_build = 0
            def current(self):
                return None

        nn = NN('nn', 33, 34)
        assert not self.calc.current(nn, 30)
        assert self.calc.current(nn, 33)
        nn.always_build = 1
        assert not self.calc.current(nn, 33)

class SConsignEntryTestCase(unittest.TestCase):

    def runTest(self):
        e = SCons.Sig.SConsignEntry()
        assert e.timestamp == None
        assert e.csig == None
        assert e.bsig == None
        assert e.implicit == None

class _SConsignTestCase(unittest.TestCase):

    def runTest(self):
        class DummyModule:
            def to_string(self, sig):
                return str(sig)

            def from_string(self, sig):
                return int(sig)
            
        class DummyNode:
            path = 'not_a_valid_path'

        f = SCons.Sig._SConsign()
        f.set_bsig('foo', 1)
        assert f.get('foo') == (None, 1, None)
        f.set_csig('foo', 2)
        assert f.get('foo') == (None, 1, 2)
        f.set_timestamp('foo', 3)
        assert f.get('foo') == (3, 1, 2)
        f.set_implicit('foo', ['bar'])
        assert f.get('foo') == (3, 1, 2)
        assert f.get_implicit('foo') == ['bar']

        f = SCons.Sig._SConsign(DummyModule())
        f.set_bsig('foo', 1)
        assert f.get('foo') == (None, 1, None)
        f.set_csig('foo', 2)
        assert f.get('foo') == (None, 1, 2)
        f.set_timestamp('foo', 3)
        assert f.get('foo') == (3, 1, 2)
        f.set_implicit('foo', ['bar'])
        assert f.get('foo') == (3, 1, 2)
        assert f.get_implicit('foo') == ['bar']

class SConsignDBTestCase(unittest.TestCase):

    def runTest(self):
        class DummyNode:
            def __init__(self, path):
                self.path = path
        save_SConsign_db = SCons.Sig.SConsign_db
        SCons.Sig.SConsign_db = {}
        try:
            d1 = SCons.Sig.SConsignDB(DummyNode('dir1'))
            d1.set_timestamp('foo', 1)
            d1.set_bsig('foo', 2)
            d1.set_csig('foo', 3)
            d1.set_timestamp('bar', 4)
            d1.set_bsig('bar', 5)
            d1.set_csig('bar', 6)
            assert d1.get('foo') == (1, 2, 3)
            assert d1.get('bar') == (4, 5, 6)

            d2 = SCons.Sig.SConsignDB(DummyNode('dir1'))
            d2.set_timestamp('foo', 7)
            d2.set_bsig('foo', 8)
            d2.set_csig('foo', 9)
            d2.set_timestamp('bar', 10)
            d2.set_bsig('bar', 11)
            d2.set_csig('bar', 12)
            assert d2.get('foo') == (7, 8, 9)
            assert d2.get('bar') == (10, 11, 12)
        finally:
            SCons.Sig.SConsign_db = save_SConsign_db

class SConsignDirFileTestCase(unittest.TestCase):

    def runTest(self):
        class DummyModule:
            def to_string(self, sig):
                return str(sig)

            def from_string(self, sig):
                return int(sig)
            
        class DummyNode:
            path = 'not_a_valid_path'

        f = SCons.Sig.SConsignDirFile(DummyNode(), DummyModule())
        f.set_bsig('foo', 1)
        assert f.get('foo') == (None, 1, None)
        f.set_csig('foo', 2)
        assert f.get('foo') == (None, 1, 2)
        f.set_timestamp('foo', 3)
        assert f.get('foo') == (3, 1, 2)
        f.set_implicit('foo', ['bar'])
        assert f.get('foo') == (3, 1, 2)
        assert f.get_implicit('foo') == ['bar']

class SConsignFileTestCase(unittest.TestCase):

    def runTest(self):
        test = TestCmd.TestCmd(workdir = '')
        file = test.workpath('sconsign_file')

        assert SCons.Sig.SConsign_db is None, SCons.Sig.SConsign_db

        SCons.Sig.SConsignFile(file)

        assert not SCons.Sig.SConsign_db is None, SCons.Sig.SConsign_db

        class Fake_DBM:
            def open(self, name, mode):
                self.name = name
                self.mode = mode
                return self

        fake_dbm = Fake_DBM()

        SCons.Sig.SConsignFile(file, fake_dbm)

        assert not SCons.Sig.SConsign_db is None, SCons.Sig.SConsign_db
        assert not hasattr(fake_dbm, 'name'), fake_dbm
        assert not hasattr(fake_dbm, 'mode'), fake_dbm

        SCons.Sig.SConsign_db = None

        SCons.Sig.SConsignFile(file, fake_dbm)

        assert not SCons.Sig.SConsign_db is None, SCons.Sig.SConsign_db
        assert fake_dbm.name == file, fake_dbm.name
        assert fake_dbm.mode == "c", fake_dbm.mode



def suite():
    suite = unittest.TestSuite()
    suite.addTest(MD5TestCase())
    suite.addTest(TimeStampTestCase())
    suite.addTest(CalcTestCase())
    suite.addTest(SConsignEntryTestCase())
    suite.addTest(_SConsignTestCase())
    suite.addTest(SConsignDBTestCase())
    suite.addTest(SConsignDirFileTestCase())
    suite.addTest(SConsignFileTestCase())
    return suite

if __name__ == "__main__":
    runner = unittest.TextTestRunner()
    result = runner.run(suite())
    if not result.wasSuccessful():
        sys.exit(1)