summaryrefslogtreecommitdiff
path: root/src/engine/SCons/ActionTests.py
blob: cda565d3dff8d80f758a3d3cac404d40662ea1a0 (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
#
# Copyright (c) 2001, 2002 Steven Knight
#
# 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/ActionTests.py __REVISION__ __DATE__ __DEVELOPER__"

# Define a null function for use as a builder action.
# Where this is defined in the file seems to affect its
# byte-code contents, so try to minimize changes by
# defining it here, before we even import anything.
def Func():
    pass

import sys
import types
import unittest

import SCons.Action
import TestCmd

class ActionTestCase(unittest.TestCase):

    def runTest(self):
        """Test the Action factory
        """
        def foo():
            pass
        a1 = SCons.Action.Action(foo)
        assert isinstance(a1, SCons.Action.FunctionAction), a1

        a2 = SCons.Action.Action("string")
        assert isinstance(a2, SCons.Action.CommandAction), a2

        if hasattr(types, 'UnicodeType'):
            exec "a3 = SCons.Action.Action(u'string')"
            exec "assert isinstance(a3, SCons.Action.CommandAction), a3"

        a4 = SCons.Action.Action(["x", "y", "z", [ "a", "b", "c"]])
        assert isinstance(a4, SCons.Action.ListAction), a4
        assert isinstance(a4.list[0], SCons.Action.CommandAction), a4.list[0]
        assert isinstance(a4.list[1], SCons.Action.CommandAction), a4.list[1]
        assert isinstance(a4.list[2], SCons.Action.CommandAction), a4.list[2]
        assert isinstance(a4.list[3], SCons.Action.CommandAction), a4.list[3]
        assert a4.list[3].cmd_list == [ "a", "b", "c" ], a4.list[3].cmd_list

        a5 = SCons.Action.Action(1)
        assert a5 is None, a5

        a6 = SCons.Action.Action(a1)
        assert a6 is a1, a6

        a7 = SCons.Action.Action([[ "explicit", "command", "line" ]])
        assert isinstance(a7, SCons.Action.CommandAction), a7
        assert a7.cmd_list == [ "explicit", "command", "line" ], a7.cmd_list

        a8 = SCons.Action.Action(["a8"])
        assert isinstance(a8, SCons.Action.CommandAction), a8
        assert a8.cmd_list == [ "a8" ], a8.cmd_list

class ActionBaseTestCase(unittest.TestCase):

    def test_cmp(self):
        """Test Action comparison
        """
        a1 = SCons.Action.Action("x")
        a2 = SCons.Action.Action("x")
        assert a1 == a2
        a3 = SCons.Action.Action("y")
        assert a1 != a3
        assert a2 != a3

    def test_subst_dict(self):
        """Test substituting dictionary values in an Action
        """
        a = SCons.Action.Action("x")

        d = a.subst_dict(env = {'a' : 'A', 'b' : 'B'})
        assert d['a'] == 'A', d
        assert d['b'] == 'B', d

        d = a.subst_dict(target = 't', source = 's')
        assert str(d['TARGETS']) == 't', d['TARGETS']
        assert str(d['TARGET']) == 't', d['TARGET']
        assert str(d['SOURCES']) == 's', d['SOURCES']
        assert str(d['SOURCE']) == 's', d['SOURCE']


        d = a.subst_dict(target = ['t1', 't2'], source = ['s1', 's2'])
        TARGETS = map(lambda x: str(x), d['TARGETS'])
        TARGETS.sort()
        assert TARGETS == ['t1', 't2'], d['TARGETS']
        assert str(d['TARGET']) == 't1', d['TARGET']
        SOURCES = map(lambda x: str(x), d['SOURCES'])
        SOURCES.sort()
        assert SOURCES == ['s1', 's2'], d['SOURCES']
        assert str(d['SOURCE']) == 's1', d['SOURCE']

class CommandActionTestCase(unittest.TestCase):

    def test_init(self):
        """Test creation of a command Action
        """
        a = SCons.Action.CommandAction(["xyzzy"])
        assert a.cmd_list == [ "xyzzy" ], a.cmd_list

    def test_execute(self):
        """Test executing a command Action
        """
        self.test_set_handler()
        pass

    def test_set_handler(self):
        """Test setting the command handler...
        """
        class Test:
            def __init__(self):
                self.executed = 0
        t=Test()
        def func(cmd, args, env, test=t):
            test.executed = 1
            return 0
        SCons.Action.SetCommandHandler(func)
        assert SCons.Action.spawn is func
        a = SCons.Action.CommandAction(["xyzzy"])
        a.execute()
        assert t.executed == 1

    def test_get_raw_contents(self):
        """Test fetching the contents of a command Action
        """
        a = SCons.Action.CommandAction(["|", "$(", "$foo", "|", "$bar",
                                        "$)", "|"])
        c = a.get_contents(target=[], source=[],
                           foo = 'FFF', bar = 'BBB')
        assert c == "| $( FFF | BBB $) |"

    def test_get_contents(self):
        """Test fetching the contents of a command Action
        """
        a = SCons.Action.CommandAction(["|", "$(", "$foo", "|", "$bar",
                                        "$)", "|"])
        c = a.get_contents(target=[], source=[],
                           foo = 'FFF', bar = 'BBB')
        assert c == "| |", c

class CommandGeneratorActionTestCase(unittest.TestCase):

    def test_init(self):
        """Test creation of a command generator Action
        """
        def f(target, source, env):
            pass
        a = SCons.Action.CommandGeneratorAction(f)
        assert a.generator == f

    def test_execute(self):
        """Test executing a command generator Action
        """

        def f(dummy, env, self=self):
            self.dummy = dummy
            assert env.subst("$FOO $( bar $) baz") == 'foo baz\nbar ack bar baz', env.subst("$FOO $( bar $) baz")
            assert env.subst("$FOO $( bar $) baz", raw=1) == 'foo baz\nbar ack $( bar $) baz', env.subst("$FOO $( bar $) baz", raw=1)
            assert env.subst_list("$FOO $( bar $) baz") == [ [ 'foo', 'baz' ],
                                                             [ 'bar', 'ack', 'bar', 'baz' ] ], env.subst_list("$FOO $( bar $) baz")
            assert env.subst_list("$FOO $( bar $) baz",
                                  raw=1) == [ [ 'foo', 'baz' ],
                                              [ 'bar', 'ack', '$(', 'bar', '$)', 'baz' ] ], env.subst_list("$FOO $( bar $) baz", raw=1)
            return "$FOO"
        def func_action(env, dummy, self=self):
            assert env.subst('$foo $( bar $)') == 'bar bar', env.subst('$foo $( bar $)')
            assert env.subst('$foo $( bar $)',
                             raw=1) == 'bar $( bar $)', env.subst('$foo $( bar $)', raw=1)
            assert env.subst_list([ '$foo', '$(', 'bar', '$)' ]) == [[ 'bar', 'bar' ]], env.subst_list([ '$foo', '$(', 'bar', '$)' ])
            assert env.subst_list([ '$foo', '$(', 'bar', '$)' ],
                                  raw=1) == [[ 'bar', '$(', 'bar', '$)' ]], env.subst_list([ '$foo', '$(', 'bar', '$)' ], raw=1)
            self.dummy=dummy
        def f2(dummy, env, f=func_action):
            return f
        def ch(cmd, args, env, self=self):
            self.cmd.append(cmd)
            self.args.append(args)

        a = SCons.Action.CommandGeneratorAction(f)
        self.dummy = 0
        old_hdl = SCons.Action.GetCommandHandler()
        self.cmd = []
        self.args = []
        try:
            SCons.Action.SetCommandHandler(ch)
            a.execute(dummy=1, env={ 'FOO' : 'foo baz\nbar ack' })
        finally:
            SCons.Action.SetCommandHandler(old_hdl)
        assert self.dummy == 1
        assert self.cmd == [ 'foo', 'bar'], self.cmd
        assert self.args == [ [ 'foo', 'baz' ], [ 'bar', 'ack' ] ], self.args

        b=SCons.Action.CommandGeneratorAction(f2)
        self.dummy = 0
        b.execute(dummy=2, env={ 'foo' : 'bar' })
        assert self.dummy==2, self.dummy
        del self.dummy

    def test_get_contents(self):
        """Test fetching the contents of a command generator Action
        """
        def f(target, source, foo, bar):
            return [["guux", foo, "$(", "ignore", "$)", bar]]

        a = SCons.Action.CommandGeneratorAction(f)
        c = a.get_contents(target=[], source=[],
                           foo = 'FFF', bar = 'BBB')
        assert c == "guux FFF BBB", c


class FunctionActionTestCase(unittest.TestCase):

    def test_init(self):
        """Test creation of a function Action
        """
        def func():
            pass
        a = SCons.Action.FunctionAction(func)
        assert a.function == func

    def test_execute(self):
        """Test executing a function Action
        """
        self.inc = 0
        def f(s, target, source, env):
            s.inc = s.inc + 1
            s.target = target
            s.source=source
            assert env.subst("foo$BAR") == 'foofoo bar', env.subst("foo$BAR")
            assert env.subst_list("foo$BAR") == [ [ 'foofoo', 'bar' ] ], \
                   env.subst_list("foo$BAR")
            return 0
        a = SCons.Action.FunctionAction(f)
        a.execute(s = self, target=1, source=2, env={'BAR':'foo bar'})
        assert self.inc == 1, self.inc
        assert self.source == [2], self.source
        assert self.target == [1], self.target

    def test_get_contents(self):
        """Test fetching the contents of a function Action
        """
        a = SCons.Action.FunctionAction(Func)
        c = a.get_contents(target=[], source=[])
        assert c == "\177\036\000\177\037\000d\000\000S", repr(c)

class ListActionTestCase(unittest.TestCase):

    def test_init(self):
        """Test creation of a list of subsidiary Actions
        """
        def func():
            pass
        a = SCons.Action.ListAction(["x", func, ["y", "z"]])
        assert isinstance(a.list[0], SCons.Action.CommandAction)
        assert isinstance(a.list[1], SCons.Action.FunctionAction)
        assert isinstance(a.list[2], SCons.Action.ListAction)
        assert a.list[2].list[0].cmd_list == [ 'y' ]

    def test_execute(self):
        """Test executing a list of subsidiary Actions
        """
        self.inc = 0
        def f(s):
            s.inc = s.inc + 1
            return 0
        a = SCons.Action.ListAction([f, f, f])
        a.execute(s = self)
        assert self.inc == 3, self.inc

    def test_get_contents(self):
        """Test fetching the contents of a list of subsidiary Actions
        """
        a = SCons.Action.ListAction(["x", "y", "z"])
        c = a.get_contents(target=[], source=[])
        assert c == "xyz", c

class LazyActionTestCase(unittest.TestCase):
    def test_init(self):
        """Test creation of a lazy-evaluation Action
        """
        # Environment variable references should create a special
        # type of CommandGeneratorAction that lazily evaluates the
        # variable.
        a9 = SCons.Action.Action('$FOO')
        assert isinstance(a9, SCons.Action.CommandGeneratorAction), a9
        assert a9.generator.var == 'FOO', a9.generator.var

        a10 = SCons.Action.Action('${FOO}')
        assert isinstance(a9, SCons.Action.CommandGeneratorAction), a10
        assert a10.generator.var == 'FOO', a10.generator.var

    def test_execute(self):
        """Test executing a lazy-evalueation Action
        """
        def f(s, env):
            s.test=1
            return 0
        a = SCons.Action.Action('$BAR')
        a.execute(s = self, env={'BAR':f})
        assert self.test == 1, self.test

    def test_get_contents(self):
        """Test fetching the contents of a lazy-evaluation Action
        """
        a = SCons.Action.Action("${FOO}")
        c = a.get_contents(target=[], source=[],
                           env={'FOO':[["This", "is", "$(", "a", "$)", "test"]]})
        assert c == "This is test", c


if __name__ == "__main__":
    suite = unittest.TestSuite()
    suite.addTest(ActionTestCase())
    suite.addTest(ActionBaseTestCase("test_cmp"))
    suite.addTest(ActionBaseTestCase("test_subst_dict"))
    for tclass in [CommandActionTestCase,
                   CommandGeneratorActionTestCase,
                   FunctionActionTestCase,
                   ListActionTestCase,
                   LazyActionTestCase]:
        for func in ["test_init", "test_execute", "test_get_contents"]:
            suite.addTest(tclass(func))
    if not unittest.TextTestRunner().run(suite).wasSuccessful():
        sys.exit(1)