summaryrefslogtreecommitdiffstats
path: root/src/engine/SCons/ExecutorTests.py
blob: 62c1eab854a32bbe44953a9dc3261b8e3e8418d5 (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
#
# __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 string
import sys
import unittest

import SCons.Executor


class MyEnvironment:
    def __init__(self, **kw):
        self._dict = {}
        self._dict.update(kw)
    def __getitem__(self, key):
        return self._dict[key]
    def Override(self, overrides):
        d = self._dict.copy()
        d.update(overrides)
        return apply(MyEnvironment, (), d)
    def _update(self, dict):
        self._dict.update(dict)

class MyAction:
    def __init__(self, actions=['action1', 'action2']):
        self.actions = actions
    def __call__(self, target, source, env, errfunc, **kw):
        for action in self.actions:
            apply(action, (target, source, env, errfunc), kw)
    def genstring(self, target, source, env):
        return string.join(['GENSTRING'] + map(str, self.actions) + target + source)
    def get_contents(self, target, source, env):
        return string.join(self.actions + target + source)

class MyBuilder:
    def __init__(self, env, overrides):
        self.env = env
        self.overrides = overrides
        self.action = MyAction()

class MyNode:
    def __init__(self, name=None, pre=[], post=[]):
        self.name = name
        self.implicit = []
        self.pre_actions = pre
        self.post_actions = post
        self.missing_val = None
    def __str__(self):
        return self.name
    def build(self, errfunc=None):
        executor = SCons.Executor.Executor(MyAction(self.pre_actions +
                                                    [self.builder.action] +
                                                    self.post_actions),
                                           self.builder.env,
                                           [],
                                           [self],
                                           ['s1', 's2'])
        apply(executor, (self, errfunc), {})
    def get_scanner(self, env, kw):
        return MyScanner('dep-')
    def get_implicit_deps(self, env, scanner, path):
        return [scanner.prefix + str(self)]
    def add_to_implicit(self, deps):
        self.implicit.extend(deps)
    def missing(self):
        return self.missing_val
    def calc_signature(self, calc):
        return 'cs-'+calc+'-'+self.name

class MyScanner:
    def __init__(self, prefix):
        self.prefix = prefix
    def path(self, env, cwd, target, source):
        return ()
    def select(self, node):
        return self

class ExecutorTestCase(unittest.TestCase):

    def test__init__(self):
        """Test creating an Executor"""
        source_list = ['s1', 's2']
        x = SCons.Executor.Executor('a', 'e', ['o'], 't', source_list)
        assert x.action == 'a', x.builder
        assert x.env == 'e', x.env
        assert x.overridelist == ['o'], x.overridelist
        assert x.targets == 't', x.targets
        source_list.append('s3')
        assert x.sources == ['s1', 's2'], x.sources
        try:
            x = SCons.Executor.Executor(None, 'e', ['o'], 't', source_list)
        except SCons.Errors.UserError:
            pass
        else:
            raise "Did not catch expected UserError"

    def test_get_build_env(self):
        """Test fetching and generating a build environment"""
        x = SCons.Executor.Executor(MyAction(), MyEnvironment(e=1), [],
                                    't', ['s1', 's2'])
        x.env = MyEnvironment(eee=1)
        be = x.get_build_env()
        assert be['eee'] == 1, be

        env = MyEnvironment(X='xxx')
        x = SCons.Executor.Executor(MyAction(),
                                    env,
                                    [{'O':'o2'}],
                                    't',
                                    ['s1', 's2'])
        be = x.get_build_env()
        assert be['O'] == 'o2', be['O']
        assert be['X'] == 'xxx', be['X']

        env = MyEnvironment(Y='yyy')
        overrides = [{'O':'ob3'}, {'O':'oo3'}]
        x = SCons.Executor.Executor(MyAction(), env, overrides, 't', 's')
        be = x.get_build_env()
        assert be['O'] == 'oo3', be['O']
        assert be['Y'] == 'yyy', be['Y']
        overrides = [{'O':'ob3'}]
        x = SCons.Executor.Executor(MyAction(), env, overrides, 't', 's')
        be = x.get_build_env()
        assert be['O'] == 'ob3', be['O']
        assert be['Y'] == 'yyy', be['Y']

    def test_get_build_scanner_path(self):
        """Test fetching the path for the specified scanner."""
        t = MyNode('t')
        t.cwd = 'here'
        x = SCons.Executor.Executor(MyAction(),
                                    MyEnvironment(SCANNERVAL='sss'),
                                    [],
                                    [t],
                                    ['s1', 's2'])

        class LocalScanner:
            def path(self, env, dir, target, source):
                target = map(str, target)
                source = map(str, source)
                return "scanner: %s, %s, %s, %s" % (env['SCANNERVAL'], dir, target, source)
        s = LocalScanner()

        p = x.get_build_scanner_path(s)
        assert p == "scanner: sss, here, ['t'], ['s1', 's2']", p

    def test_get_kw(self):
        """Test the get_kw() method"""
        t = MyNode('t')
        x = SCons.Executor.Executor(MyAction(),
                                    MyEnvironment(),
                                    [],
                                    [t],
                                    ['s1', 's2'],
                                    builder_kw={'X':1, 'Y':2})
        kw = x.get_kw()
        assert kw == {'X':1, 'Y':2}, kw
        kw = x.get_kw({'Z':3})
        assert kw == {'X':1, 'Y':2, 'Z':3}, kw
        kw = x.get_kw({'X':4})
        assert kw == {'X':4, 'Y':2}, kw

    def test__call__(self):
        """Test calling an Executor"""
        result = []
        def pre(target, source, env, errfunc, result=result, **kw):
            result.append('pre')
        def action1(target, source, env, errfunc, result=result, **kw):
            result.append('action1')
        def action2(target, source, env, errfunc, result=result, **kw):
            result.append('action2')
        def post(target, source, env, errfunc, result=result, **kw):
            result.append('post')

        env = MyEnvironment()
        a = MyAction([action1, action2])
        b = MyBuilder(env, {})
        b.action = a
        n = MyNode('n', [pre], [post])
        n.builder = b
        n.build()
        assert result == ['pre', 'action1', 'action2', 'post'], result
        del result[:]

        def pre_err(target, source, env, errfunc, result=result, **kw):
            result.append('pre_err')
            if errfunc:
                errfunc(1)
            return 1

        n = MyNode('n', [pre_err], [post])
        n.builder = b
        n.build()
        assert result == ['pre_err', 'action1', 'action2', 'post'], result
        del result[:]

        def errfunc(stat):
            raise "errfunc %s" % stat

        try:
            n.build(errfunc)
        except:
            assert sys.exc_type == "errfunc 1", sys.exc_type
        else:
            assert 0, "did not catch expected exception"
        assert result == ['pre_err'], result
        del result[:]

    def test_cleanup(self):
        """Test cleaning up an Executor"""
        orig_env = MyEnvironment(e=1)
        x = SCons.Executor.Executor('b', orig_env, [{'o':1}],
                                    't', ['s1', 's2'])

        be = x.get_build_env()
        assert be['e'] == 1, be['e']
        
        x.cleanup()

        x.env = MyEnvironment(eee=1)
        be = x.get_build_env()
        assert be['eee'] == 1, be['eee']

        x.cleanup()

        be = x.get_build_env()
        assert be['eee'] == 1, be['eee']

    def test_add_sources(self):
        """Test adding sources to an Executor"""
        x = SCons.Executor.Executor('b', 'e', 'o', 't', ['s1', 's2'])
        assert x.sources == ['s1', 's2'], x.sources
        x.add_sources(['s1', 's2'])
        assert x.sources == ['s1', 's2'], x.sources
        x.add_sources(['s3', 's1', 's4'])
        assert x.sources == ['s1', 's2', 's3', 's4'], x.sources

    def test___str__(self):
        """Test the __str__() method"""
        env = MyEnvironment(S='string')

        x = SCons.Executor.Executor(MyAction(), env, [], ['t'], ['s'])
        c = str(x)
        assert c == 'GENSTRING action1 action2 t s', c

    def test_nullify(self):
        """Test the nullify() method"""
        env = MyEnvironment(S='string')

        result = []
        def action1(target, source, env, errfunc, result=result, **kw):
            result.append('action1')

        env = MyEnvironment()
        a = MyAction([action1])
        x = SCons.Executor.Executor(a, env, [], ['t1', 't2'], ['s1', 's2'])

        x(MyNode('', [], []), None)
        assert result == ['action1'], result
        s = str(x)
        assert s[:10] == 'GENSTRING ', s

        del result[:]
        x.nullify()

        assert result == [], result
        x(MyNode('', [], []), None)
        assert result == [], result
        s = str(x)
        assert s == '', s

    def test_get_contents(self):
        """Test fetching the signatures contents"""
        env = MyEnvironment(C='contents')

        x = SCons.Executor.Executor(MyAction(), env, [], ['t'], ['s'])
        c = x.get_contents()
        assert c == 'action1 action2 t s', c

        x = SCons.Executor.Executor(MyAction(actions=['grow']), env, [],
                                    ['t'], ['s'])
        c = x.get_contents()
        assert c == 'grow t s', c

    def test_get_timestamp(self):
        """Test fetching the "timestamp" """
        x = SCons.Executor.Executor('b', 'e', 'o', 't', ['s1', 's2'])
        ts = x.get_timestamp()
        assert ts == 0, ts

    def test_scan_targets(self):
        """Test scanning the targets for implicit dependencies"""
        env = MyEnvironment(S='string')
        t1 = MyNode('t1')
        t2 = MyNode('t2')
        sources = [MyNode('s1'), MyNode('s2')]
        x = SCons.Executor.Executor('b', env, [{}], [t1, t2], sources)

        deps = x.scan_targets(None)
        assert t1.implicit == ['dep-t1', 'dep-t2'], t1.implicit
        assert t2.implicit == ['dep-t1', 'dep-t2'], t2.implicit

        t1.implicit = []
        t2.implicit = []

        deps = x.scan_targets(MyScanner('scanner-'))
        assert t1.implicit == ['scanner-t1', 'scanner-t2'], t1.implicit
        assert t2.implicit == ['scanner-t1', 'scanner-t2'], t2.implicit

    def test_scan_sources(self):
        """Test scanning the sources for implicit dependencies"""
        env = MyEnvironment(S='string')
        t1 = MyNode('t1')
        t2 = MyNode('t2')
        sources = [MyNode('s1'), MyNode('s2')]
        x = SCons.Executor.Executor('b', env, [{}], [t1, t2], sources)

        deps = x.scan_sources(None)
        assert t1.implicit == ['dep-s1', 'dep-s2'], t1.implicit
        assert t2.implicit == ['dep-s1', 'dep-s2'], t2.implicit

        t1.implicit = []
        t2.implicit = []

        deps = x.scan_sources(MyScanner('scanner-'))
        assert t1.implicit == ['scanner-s1', 'scanner-s2'], t1.implicit
        assert t2.implicit == ['scanner-s1', 'scanner-s2'], t2.implicit

    def test_get_missing_sources(self):
        """Test the ability to check if any sources are missing"""
        env = MyEnvironment()
        targets = [MyNode('t')]
        sources = [MyNode('s1'), MyNode('s2')]
        x = SCons.Executor.Executor('b', env, [{}], targets, sources)
        sources[0].missing_val = 1
        missing = x.get_missing_sources()
        assert missing == [sources[0]], missing

    def test_get_source_binfo(self):
        """Test fetching the build signature info for the sources"""
        env = MyEnvironment()
        t1 = MyNode('t')
        s1 = MyNode('s1')
        s2 = MyNode('s2')
        x = SCons.Executor.Executor('b', env, [{}], [t1], [s1, s2])

        b = x.get_source_binfo('C')
        assert b == ([s1, s2],
                     ['cs-C-s1', 'cs-C-s2'],
                     ['s1', 's2']), b

        b = x.get_source_binfo('C', [s1])
        assert b == ([s2],
                     ['cs-C-s2'],
                     ['s2']), b

        b = x.get_source_binfo('C', [s2])
        assert b == ([s1],
                     ['cs-C-s1'],
                     ['s1']), b



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