summaryrefslogtreecommitdiffstats
path: root/src/engine/SCons/SConsignTests.py
blob: 16ef81640e1c19bd0e877a05431668b84c79322f (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
#
# __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 SCons.SConsign
import sys
import TestCmd
import unittest

class BuildInfo:
    def __init__(self, name):
        self.name = name

class BaseTestCase(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'

        aaa = BuildInfo('aaa')
        bbb = BuildInfo('bbb')
        bbb.arg1 = 'bbb arg1'
        ccc = BuildInfo('ccc')
        ccc.arg2 = 'ccc arg2'

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

        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 = BuildInfo('ddd')
        eee = BuildInfo('eee')
        fff = BuildInfo('fff')
        fff.arg = 'fff arg'

        f = SCons.SConsign.Base(DummyModule())
        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

class SConsignDBTestCase(unittest.TestCase):

    def runTest(self):
        class DummyNode:
            def __init__(self, path):
                self.path = path
        save_database = SCons.SConsign.database
        SCons.SConsign.database = {}
        try:
            d1 = SCons.SConsign.DB(DummyNode('dir1'))
            d1.set_entry('aaa', BuildInfo('aaa name'))
            d1.set_entry('bbb', BuildInfo('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('dir1'))
            d2.set_entry('ccc', BuildInfo('ccc name'))
            d2.set_entry('ddd', BuildInfo('ddd name'))
            ccc = d2.get_entry('ccc')
            assert ccc.name == 'ccc name'
            ddd = d2.get_entry('ddd')
            assert ddd.name == 'ddd name'
        finally:
            SCons.SConsign.database = save_database

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'

        foo = BuildInfo('foo')
        bar = BuildInfo('bar')

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

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

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

        bbb = BuildInfo('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(unittest.TestCase):

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

        assert SCons.SConsign.database is None, SCons.SConsign.database

        SCons.SConsign.File(file)

        assert not SCons.SConsign.database is SCons.dblite, SCons.SConsign.database

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

        fake_dbm = Fake_DBM()

        SCons.SConsign.File(file, fake_dbm)

        assert not SCons.SConsign.database is None, SCons.SConsign.database
        assert not hasattr(fake_dbm, 'name'), fake_dbm
        assert not hasattr(fake_dbm, 'mode'), fake_dbm

        SCons.SConsign.database = None

        SCons.SConsign.File(file, fake_dbm)

        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



def suite():
    suite = unittest.TestSuite()
    suite.addTest(BaseTestCase())
    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)