summaryrefslogtreecommitdiffstats
path: root/src/engine/SCons/BuilderTests.py
blob: e24784d38c35cce7b47dd66b7c11aaf4b878d135 (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
#
# Copyright (c) 2001 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__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"

import sys
import unittest

import TestCmd
import SCons.Builder


# Initial setup of the common environment for all tests,
# a temporary working directory containing a
# script for writing arguments to an output file.
#
# We don't do this as a setUp() method because it's
# unnecessary to create a separate directory and script
# for each test, they can just use the one.
test = TestCmd.TestCmd(workdir = '')

test.write('act.py', """import os, string, sys
f = open(sys.argv[1], 'w')
f.write("act.py: " + string.join(sys.argv[2:]) + "\\n")
try:
    if sys.argv[3]:
        f.write("act.py: " + os.environ[sys.argv[3]] + "\\n")
except:
    pass
f.close()
sys.exit(0)
""")

act_py = test.workpath('act.py')
outfile = test.workpath('outfile')

class Environment:
            def subst(self, s):
                return s
env = Environment()

class BuilderTestCase(unittest.TestCase):

    def test__call__(self):
	"""Test calling a builder to establish source dependencies
	"""
	class Node:
	    def __init__(self, name):
		self.name = name
		self.sources = []
		self.builder = None
	    def __str__(self):
	        return self.name
	    def builder_set(self, builder):
		self.builder = builder
	    def env_set(self, env):
		self.env = env
	    def add_source(self, source):
		self.sources.extend(source)
	builder = SCons.Builder.Builder(action = "foo")
	n1 = Node("n1");
	n2 = Node("n2");
	builder(env, target = n1, source = n2)
	assert n1.env == env
	assert n1.builder == builder
	assert n1.sources == [n2]

    def test_action(self):
	"""Test Builder creation

	Verify that we can retrieve the supplied action attribute.
	"""
	builder = SCons.Builder.Builder(action = "foo")
	assert builder.action.command == "foo"

    def test_cmp(self):
	"""Test simple comparisons of Builder objects
	"""
	b1 = SCons.Builder.Builder(src_suffix = '.o')
	b2 = SCons.Builder.Builder(src_suffix = '.o')
	assert b1 == b2
	b3 = SCons.Builder.Builder(src_suffix = '.x')
	assert b1 != b3
	assert b2 != b3

    def test_execute(self):
	"""Test execution of simple Builder objects
	
	One Builder is a string that executes an external command,
	one is an internal Python function, one is a list
	containing one of each.
	"""

	python = sys.executable

	cmd1 = r'%s %s %s xyzzy' % (python, act_py, outfile)

	builder = SCons.Builder.Builder(action = cmd1)
	r = builder.execute()
	assert r == 0
	c = test.read(outfile, 'r')
	assert c == "act.py: xyzzy\n", c

	cmd2 = r'%s %s %s $TARGET' % (python, act_py, outfile)

	builder = SCons.Builder.Builder(action = cmd2)
	r = builder.execute(target = 'foo')
	assert r == 0
	c = test.read(outfile, 'r')
	assert c == "act.py: foo\n", c

	cmd3 = r'%s %s %s ${TARGETS}' % (python, act_py, outfile)

	builder = SCons.Builder.Builder(action = cmd3)
	r = builder.execute(target = ['aaa', 'bbb'])
	assert r == 0
	c = test.read(outfile, 'r')
	assert c == "act.py: aaa bbb\n", c

	cmd4 = r'%s %s %s $SOURCES' % (python, act_py, outfile)

	builder = SCons.Builder.Builder(action = cmd4)
	r = builder.execute(source = ['one', 'two'])
	assert r == 0
	c = test.read(outfile, 'r')
	assert c == "act.py: one two\n", c

	cmd4 = r'%s %s %s ${SOURCES[:2]}' % (python, act_py, outfile)

	builder = SCons.Builder.Builder(action = cmd4)
	r = builder.execute(source = ['three', 'four', 'five'])
	assert r == 0
	c = test.read(outfile, 'r')
	assert c == "act.py: three four\n", c

	cmd5 = r'%s %s %s $TARGET XYZZY' % (python, act_py, outfile)

	builder = SCons.Builder.Builder(action = cmd5)
	r = builder.execute(target = 'out5', env = {'ENV' : {'XYZZY' : 'xyzzy'}})
	assert r == 0
	c = test.read(outfile, 'r')
	assert c == "act.py: out5 XYZZY\nact.py: xyzzy\n", c

	def function1(kw):
	    open(kw['out'], 'w').write("function1\n")
	    return 1

	builder = SCons.Builder.Builder(action = function1)
	r = builder.execute(out = outfile)
	assert r == 1
	c = test.read(outfile, 'r')
	assert c == "function1\n", c

	class class1a:
	    def __init__(self, kw):
		open(kw['out'], 'w').write("class1a\n")

	builder = SCons.Builder.Builder(action = class1a)
	r = builder.execute(out = outfile)
	assert r.__class__ == class1a
	c = test.read(outfile, 'r')
	assert c == "class1a\n", c

	class class1b:
	    def __call__(self, kw):
		open(kw['out'], 'w').write("class1b\n")
		return 2

	builder = SCons.Builder.Builder(action = class1b())
	r = builder.execute(out = outfile)
	assert r == 2
	c = test.read(outfile, 'r')
	assert c == "class1b\n", c

	cmd2 = r'%s %s %s syzygy' % (python, act_py, outfile)

	def function2(kw):
	    open(kw['out'], 'a').write("function2\n")
	    return 0

	class class2a:
	    def __call__(self, kw):
		open(kw['out'], 'a').write("class2a\n")
		return 0

	class class2b:
	    def __init__(self, kw):
		open(kw['out'], 'a').write("class2b\n")

	builder = SCons.Builder.Builder(action = [cmd2, function2, class2a(), class2b])
	r = builder.execute(out = outfile)
	assert r.__class__ == class2b
	c = test.read(outfile, 'r')
	assert c == "act.py: syzygy\nfunction2\nclass2a\nclass2b\n", c

    def test_name(self):
	"""Test Builder creation with a specified name
	"""
	builder = SCons.Builder.Builder(name = 'foo')
	assert builder.name == 'foo'

    def test_node_factory(self):
	"""Test a Builder that creates nodes of a specified class
	"""
	class Foo:
	    pass
	def FooFactory(target):
	    return Foo(target)
	builder = SCons.Builder.Builder(node_factory = FooFactory)
	assert builder.node_factory is FooFactory

    def test_prefix(self):
	"""Test Builder creation with a specified target prefix

	Make sure that there is no '.' separator appended.
	"""
	builder = SCons.Builder.Builder(prefix = 'lib.')
	assert builder.prefix == 'lib.'
	builder = SCons.Builder.Builder(prefix = 'lib')
	assert builder.prefix == 'lib'
	tgt = builder(env, target = 'tgt1', source = 'src1')
	assert tgt.path == 'libtgt1', \
	        "Target has unexpected name: %s" % tgt.path

    def test_src_suffix(self):
	"""Test Builder creation with a specified source file suffix
	
	Make sure that the '.' separator is appended to the
	beginning if it isn't already present.
	"""
	builder = SCons.Builder.Builder(src_suffix = '.c')
	assert builder.src_suffix == '.c'
	builder = SCons.Builder.Builder(src_suffix = 'c')
	assert builder.src_suffix == '.c'
	tgt = builder(env, target = 'tgt2', source = 'src2')
	assert tgt.sources[0].path == 'src2.c', \
	        "Source has unexpected name: %s" % tgt.sources[0].path

    def test_suffix(self):
	"""Test Builder creation with a specified target suffix

	Make sure that the '.' separator is appended to the
	beginning if it isn't already present.
	"""
	builder = SCons.Builder.Builder(suffix = '.o')
	assert builder.suffix == '.o'
	builder = SCons.Builder.Builder(suffix = 'o')
	assert builder.suffix == '.o'
	tgt = builder(env, target = 'tgt3', source = 'src3')
	assert tgt.path == 'tgt3.o', \
	        "Target has unexpected name: %s" % tgt[0].path

    def test_MultiStepBuilder(self):
        """Testing MultiStepBuilder class."""
        builder1 = SCons.Builder.Builder(action='foo',
                                        src_suffix='.bar',
                                        suffix='.foo')
        builder2 = SCons.Builder.MultiStepBuilder(action='foo',
                                                  src_builder = builder1)
        tgt = builder2(env, target='baz', source='test.bar test2.foo test3.txt')
        flag = 0
        for snode in tgt.sources:
            if snode.path == 'test.foo':
                flag = 1
                assert snode.sources[0].path == 'test.bar'
        assert flag

    def test_CompositeBuilder(self):
        """Testing CompositeBuilder class."""
        builder = SCons.Builder.Builder(action={ '.foo' : 'foo',
                                                 '.bar' : 'bar' })
        
        assert isinstance(builder, SCons.Builder.CompositeBuilder)
        tgt = builder(env, target='test1', source='test1.foo')
        assert isinstance(tgt.builder, SCons.Builder.BuilderBase)
        assert tgt.builder.action.command == 'foo'
        tgt = builder(env, target='test2', source='test2.bar')
        assert tgt.builder.action.command == 'bar'
        flag = 0
        try:
            tgt = builder(env, target='test2', source='test2.bar test1.foo')
        except SCons.Errors.UserError:
            flag = 1
        assert flag, "UserError should be thrown when we build targets with files of different suffixes."


if __name__ == "__main__":
    suite = unittest.makeSuite(BuilderTestCase, 'test_')
    if not unittest.TextTestRunner().run(suite).wasSuccessful():
	sys.exit(1)