Make construction variables with a value of 0 work (Anthony Roach)
[scons.git] / src / engine / SCons / UtilTests.py
1 #
2 # Copyright (c) 2001, 2002 Steven Knight
3 #
4 # Permission is hereby granted, free of charge, to any person obtaining
5 # a copy of this software and associated documentation files (the
6 # "Software"), to deal in the Software without restriction, including
7 # without limitation the rights to use, copy, modify, merge, publish,
8 # distribute, sublicense, and/or sell copies of the Software, and to
9 # permit persons to whom the Software is furnished to do so, subject to
10 # the following conditions:
11 #
12 # The above copyright notice and this permission notice shall be included
13 # in all copies or substantial portions of the Software.
14 #
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
16 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
17 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 #
23
24 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
25
26 import os
27 import os.path
28 import re
29 import string
30 import sys
31 import types
32 import unittest
33 import SCons.Node
34 import SCons.Node.FS
35 from SCons.Util import *
36 import TestCmd
37
38 class UtilTestCase(unittest.TestCase):
39     def test_str2nodes(self):
40         """Test the str2nodes function."""
41         nodes = scons_str2nodes("Util.py UtilTests.py")
42         assert len(nodes) == 2, nodes
43         assert isinstance(nodes[0], SCons.Node.FS.File)
44         assert isinstance(nodes[1], SCons.Node.FS.File)
45         assert nodes[0].path == "Util.py"
46         assert nodes[1].path == "UtilTests.py"
47
48         if hasattr(types, 'UnicodeType'):
49             code = """if 1:
50                 nodes = scons_str2nodes(u"Util.py UtilTests.py")
51                 assert len(nodes) == 2, nodes
52                 assert isinstance(nodes[0], SCons.Node.FS.File)
53                 assert isinstance(nodes[1], SCons.Node.FS.File)
54                 assert nodes[0].path == u"Util.py"
55                 assert nodes[1].path == u"UtilTests.py"
56                 \n"""
57             exec code
58
59         nodes = scons_str2nodes("Util.py UtilTests.py", SCons.Node.FS.FS().File)
60         assert len(nodes) == 2, nodes
61         assert isinstance(nodes[0], SCons.Node.FS.File)
62         assert isinstance(nodes[1], SCons.Node.FS.File)
63         assert nodes[0].path == "Util.py"
64         assert nodes[1].path == "UtilTests.py"
65
66         nodes = scons_str2nodes(["Util.py", "UtilTests.py"])
67         assert len(nodes) == 2, nodes
68         assert isinstance(nodes[0], SCons.Node.FS.File)
69         assert isinstance(nodes[1], SCons.Node.FS.File)
70         assert nodes[0].path == "Util.py"
71         assert nodes[1].path == "UtilTests.py"
72
73         n1 = SCons.Node.FS.default_fs.File("Util.py")
74         nodes = scons_str2nodes([n1, "UtilTests.py"])
75         assert len(nodes) == 2, nodes
76         assert isinstance(nodes[0], SCons.Node.FS.File)
77         assert isinstance(nodes[1], SCons.Node.FS.File)
78         assert nodes[0].path == "Util.py"
79         assert nodes[1].path == "UtilTests.py"
80
81         class SConsNode(SCons.Node.Node):
82             pass
83         node = scons_str2nodes(SConsNode())
84
85         class OtherNode:
86             pass
87         node = scons_str2nodes(OtherNode())
88
89
90     def test_subst(self):
91         """Test the subst function."""
92         loc = {}
93         loc['TARGETS'] = PathList(map(os.path.normpath, [ "./foo/bar.exe",
94                                                           "/bar/baz.obj",
95                                                           "../foo/baz.obj" ]))
96         loc['TARGET'] = loc['TARGETS'][0]
97         loc['SOURCES'] = PathList(map(os.path.normpath, [ "./foo/blah.cpp",
98                                                           "/bar/ack.cpp",
99                                                           "../foo/ack.c" ]))
100         loc['xxx'] = None
101         loc['zero'] = 0
102         loc['one'] = 1
103
104         if os.sep == '/':
105             def cvt(str):
106                 return str
107         else:
108             def cvt(str):
109                 return string.replace(str, '/', os.sep)
110
111
112         newcom = scons_subst("test $TARGETS $SOURCES", loc, {})
113         assert newcom == cvt("test foo/bar.exe /bar/baz.obj ../foo/baz.obj foo/blah.cpp /bar/ack.cpp ../foo/ack.c")
114
115         newcom = scons_subst("test ${TARGETS[:]} ${SOURCES[0]}", loc, {})
116         assert newcom == cvt("test foo/bar.exe /bar/baz.obj ../foo/baz.obj foo/blah.cpp")
117
118         newcom = scons_subst("test ${TARGETS[1:]}v", loc, {})
119         assert newcom == cvt("test /bar/baz.obj ../foo/baz.objv")
120
121         newcom = scons_subst("test $TARGET", loc, {})
122         assert newcom == cvt("test foo/bar.exe")
123
124         newcom = scons_subst("test $TARGET$SOURCE[0]", loc, {})
125         assert newcom == cvt("test foo/bar.exe[0]")
126
127         newcom = scons_subst("test ${TARGET.file}", loc, {})
128         assert newcom == cvt("test bar.exe")
129
130         newcom = scons_subst("test ${TARGET.filebase}", loc, {})
131         assert newcom == cvt("test bar")
132
133         newcom = scons_subst("test ${TARGET.suffix}", loc, {})
134         assert newcom == cvt("test .exe")
135
136         newcom = scons_subst("test ${TARGET.base}", loc, {})
137         assert newcom == cvt("test foo/bar")
138
139         newcom = scons_subst("test ${TARGET.dir}", loc, {})
140         assert newcom == cvt("test foo")
141
142         newcom = scons_subst("test $xxx", loc, {})
143         assert newcom == cvt("test"), newcom
144
145         newcom = scons_subst("test $($xxx$)", loc, {})
146         assert newcom == cvt("test $($)"), newcom
147
148         newcom = scons_subst("test $( $xxx $)", loc, {})
149         assert newcom == cvt("test $( $)"), newcom
150
151         newcom = scons_subst("test $($xxx$)", loc, {}, re.compile('\$[()]'))
152         assert newcom == cvt("test"), newcom
153
154         newcom = scons_subst("test $( $xxx $)", loc, {}, re.compile('\$[()]'))
155         assert newcom == cvt("test"), newcom
156
157         newcom = scons_subst("test $zero", loc, {})
158         assert newcom == cvt("test 0"), newcom
159
160         newcom = scons_subst("test $one", loc, {})
161         assert newcom == cvt("test 1"), newcom
162
163         newcom = scons_subst("test aXbXcXd", loc, {}, re.compile('X'))
164         assert newcom == cvt("test abcd"), newcom
165
166         glob = { 'a' : 1, 'b' : 2 }
167         loc = {'a' : 3, 'c' : 4 }
168         newcom = scons_subst("test $a $b $c $d test", glob, loc)
169         assert newcom == "test 3 2 4 test", newcom
170
171     def test_subst_list(self):
172         """Testing the scons_subst_list() method..."""
173         loc = {}
174         loc['TARGETS'] = PathList(map(os.path.normpath, [ "./foo/bar.exe",
175                                                           "/bar/baz with spaces.obj",
176                                                           "../foo/baz.obj" ]))
177         loc['TARGET'] = loc['TARGETS'][0]
178         loc['SOURCES'] = PathList(map(os.path.normpath, [ "./foo/blah with spaces.cpp",
179                                                           "/bar/ack.cpp",
180                                                           "../foo/ack.c" ]))
181         loc['xxx'] = None
182         loc['NEWLINE'] = 'before\nafter'
183
184         if os.sep == '/':
185             def cvt(str):
186                 return str
187         else:
188             def cvt(str):
189                 return string.replace(str, '/', os.sep)
190
191         cmd_list = scons_subst_list("$TARGETS", loc, {})
192         assert cmd_list[0][1] == cvt("/bar/baz with spaces.obj"), cmd_list[0][1]
193
194         cmd_list = scons_subst_list("$SOURCES $NEWLINE $TARGETS", loc, {})
195         assert len(cmd_list) == 2, cmd_list
196         assert cmd_list[0][0] == cvt('foo/blah with spaces.cpp'), cmd_list[0][0]
197         assert cmd_list[1][2] == cvt("/bar/baz with spaces.obj"), cmd_list[1]
198
199         cmd_list = scons_subst_list("$SOURCES$NEWLINE", loc, {})
200         assert len(cmd_list) == 2, cmd_list
201         assert cmd_list[1][0] == 'after', cmd_list[1][0]
202         assert cmd_list[0][2] == cvt('../foo/ack.cbefore'), cmd_list[0][2]
203
204         glob = { 'a' : 1, 'b' : 2 }
205         loc = {'a' : 3, 'c' : 4 }
206         cmd_list = scons_subst_list("test $a $b $c $d test", glob, loc)
207         assert len(cmd_list) == 1, cmd_list
208         assert cmd_list[0] == ['test', '3', '2', '4', 'test'], cmd_list
209
210     def test_autogenerate(dict):
211         """Test autogenerating variables in a dictionary."""
212         dict = {'LIBS'          : [ 'foo', 'bar', 'baz' ],
213                 'LIBLINKPREFIX' : 'foo',
214                 'LIBLINKSUFFIX' : 'bar'}
215         autogenerate(dict, dir = SCons.Node.FS.default_fs.Dir('/xx'))
216         assert len(dict['_LIBFLAGS']) == 3, dict('_LIBFLAGS')
217         assert dict['_LIBFLAGS'][0] == 'foofoobar', \
218                dict['_LIBFLAGS'][0]
219         assert dict['_LIBFLAGS'][1] == 'foobarbar', \
220                dict['_LIBFLAGS'][1]
221         assert dict['_LIBFLAGS'][2] == 'foobazbar', \
222                dict['_LIBFLAGS'][2]
223
224         blat = SCons.Node.FS.default_fs.File('blat')
225         dict = {'CPPPATH'   : [ 'foo', '$FOO/bar', blat],
226                 'INCPREFIX' : 'foo ',
227                 'INCSUFFIX' : 'bar',
228                 'FOO'       : 'baz' }
229         autogenerate(dict, dir = SCons.Node.FS.default_fs.Dir('/xx'))
230         assert len(dict['_INCFLAGS']) == 8, dict['_INCFLAGS']
231         assert dict['_INCFLAGS'][0] == '$(', \
232                dict['_INCFLAGS'][0]
233         assert dict['_INCFLAGS'][1] == os.path.normpath('foo'), \
234                dict['_INCFLAGS'][1]
235         assert dict['_INCFLAGS'][2] == os.path.normpath('/xx/foobar'), \
236                dict['_INCFLAGS'][2]
237         assert dict['_INCFLAGS'][3] == os.path.normpath('foo'), \
238                dict['_INCFLAGS'][3]
239         assert dict['_INCFLAGS'][4] == os.path.normpath('/xx/baz/barbar'), \
240                dict['_INCFLAGS'][4]
241         assert dict['_INCFLAGS'][5] == os.path.normpath('foo'), \
242                dict['_INCFLAGS'][5]
243         assert dict['_INCFLAGS'][6] == os.path.normpath('blatbar'), \
244                dict['_INCFLAGS'][6]
245         assert dict['_INCFLAGS'][7] == '$)', \
246                dict['_INCFLAGS'][7]
247
248         dict = {'CPPPATH'  : '',
249                 'LIBPATH'  : '' }
250         autogenerate(dict, dir = SCons.Node.FS.default_fs.Dir('/yy'))
251         assert len(dict['_INCFLAGS']) == 0, dict['_INCFLAGS']
252         assert len(dict['_LIBDIRFLAGS']) == 0, dict['_LIBDIRFLAGS']
253
254     def test_render_tree(self):
255         class Node:
256             def __init__(self, name, children=[]):
257                 self.children = children
258                 self.name = name
259             def __str__(self):
260                 return self.name
261
262         def get_children(node):
263             return node.children
264
265         windows_h = Node("windows.h")
266         stdlib_h = Node("stdlib.h")
267         stdio_h = Node("stdio.h")
268         bar_c = Node("bar.c", [stdlib_h, windows_h])
269         bar_o = Node("bar.o", [bar_c])
270         foo_c = Node("foo.c", [stdio_h])
271         foo_o = Node("foo.o", [foo_c])
272         foo = Node("foo", [foo_o, bar_o])
273
274         expect = """\
275 +-foo
276   +-foo.o
277   | +-foo.c
278   |   +-stdio.h
279   +-bar.o
280     +-bar.c
281       +-stdlib.h
282       +-windows.h
283 """
284
285         actual = render_tree(foo, get_children)
286         assert expect == actual, (expect, actual)
287
288     def test_is_Dict(self):
289         assert is_Dict({})
290         import UserDict
291         assert is_Dict(UserDict.UserDict())
292         assert not is_Dict([])
293         assert not is_Dict("")
294         if hasattr(types, 'UnicodeType'):
295             exec "assert not is_Dict(u'')"
296
297     def test_is_List(self):
298         assert is_List([])
299         import UserList
300         assert is_List(UserList.UserList())
301         assert not is_List({})
302         assert not is_List("")
303         if hasattr(types, 'UnicodeType'):
304             exec "assert not is_List(u'')"
305
306     def test_is_String(self):
307         assert is_String("")
308         if hasattr(types, 'UnicodeType'):
309             exec "assert is_String(u'')"
310         try:
311             import UserString
312         except:
313             pass
314         else:
315             assert is_String(UserString.UserString(''))
316         assert not is_String({})
317         assert not is_String([])
318
319 if __name__ == "__main__":
320     suite = unittest.makeSuite(UtilTestCase, 'test_')
321     if not unittest.TextTestRunner().run(suite).wasSuccessful():
322         sys.exit(1)