summaryrefslogtreecommitdiffstats
path: root/src/engine/SCons/Sig/SigTests.py
blob: 3a8e0b3ee9703b286b02955b5756112ffed3a652 (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
__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, derived):
        self.path = path
        self.contents = contents
        self.timestamp = timestamp
        self.derived = derived

    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.derived = file.derived
	self.depends = []
        
    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 has_signature(self):
        return hasattr(self, "sig")

    def set_signature(self, sig):
        self.sig = sig

    def get_signature(self):
        return self.sig


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]]
    nodes[3].sources = []
    nodes[4].sources = [nodes[5]]
    nodes[5].sources = [nodes[6]]
    nodes[6].sources = [nodes[5]]
    nodes[7].sources = [nodes[2], nodes[4]]
    nodes[8].sources = []
    nodes[9].sources = [nodes[8]]
    nodes[10].sources = [nodes[9]]

    return nodes
        

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_delete()
        
    def test_initial(self):
        
        nodes = create_nodes(self.files)
        calc = SCons.Sig.Calculator(self.module)

        for node in nodes:
            self.failUnless(not calc.current(node), "none of the nodes should be current")

        # 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)
        
        calc.write(nodes)

    def test_built(self):

        nodes = create_nodes(self.files)

        calc = SCons.Sig.Calculator(self.module)
        
        for node in nodes:
            self.failUnless(calc.current(node), "all of the nodes should be current")

        calc.write(nodes)

    def test_modify(self):

        nodes = create_nodes(self.files)

        #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)

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

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

        calc.write(nodes)

    def test_delete(self):
        
        nodes = create_nodes(self.files)

        #simulate the deletion of some files
        self.files[1].modify(None, 0)
        self.files[7].modify(None, 0)
        self.files[9].modify(None, 0)
        
        calc = SCons.Sig.Calculator(self.module)

        self.failUnless(calc.current(nodes[0]))
        self.failUnless(not calc.current(nodes[1]), "deleted")
        self.failUnless(calc.current(nodes[2]))
        self.failUnless(calc.current(nodes[3]))
        self.failUnless(calc.current(nodes[4]))
        self.failUnless(calc.current(nodes[5]))
        self.failUnless(calc.current(nodes[6]))
        self.failUnless(not calc.current(nodes[7]), "deleted")
        self.failUnless(calc.current(nodes[8]))
        self.failUnless(not calc.current(nodes[9]), "deleted")
        self.failUnless(calc.current(nodes[10]),
                        "current even though it's source was deleted") 

        calc.write(nodes)

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

    module = SCons.Sig.MD5

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

    module = SCons.Sig.TimeStamp

def suite():
    suite = unittest.TestSuite()
    suite.addTest(MD5TestCase())
    suite.addTest(TimeStampTestCase())
    return suite

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