Fix an exception caused by a null-string CPPPATH.
[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
102         if os.sep == '/':
103             def cvt(str):
104                 return str
105         else:
106             def cvt(str):
107                 return string.replace(str, '/', os.sep)
108
109
110         newcom = scons_subst("test $TARGETS $SOURCES", loc, {})
111         assert newcom == cvt("test foo/bar.exe /bar/baz.obj ../foo/baz.obj foo/blah.cpp /bar/ack.cpp ../foo/ack.c")
112
113         newcom = scons_subst("test ${TARGETS[:]} ${SOURCES[0]}", loc, {})
114         assert newcom == cvt("test foo/bar.exe /bar/baz.obj ../foo/baz.obj foo/blah.cpp")
115
116         newcom = scons_subst("test ${TARGETS[1:]}v", loc, {})
117         assert newcom == cvt("test /bar/baz.obj ../foo/baz.objv")
118
119         newcom = scons_subst("test $TARGET", loc, {})
120         assert newcom == cvt("test foo/bar.exe")
121
122         newcom = scons_subst("test $TARGET$SOURCE[0]", loc, {})
123         assert newcom == cvt("test foo/bar.exe[0]")
124
125         newcom = scons_subst("test ${TARGET.file}", loc, {})
126         assert newcom == cvt("test bar.exe")
127
128         newcom = scons_subst("test ${TARGET.filebase}", loc, {})
129         assert newcom == cvt("test bar")
130
131         newcom = scons_subst("test ${TARGET.suffix}", loc, {})
132         assert newcom == cvt("test .exe")
133
134         newcom = scons_subst("test ${TARGET.base}", loc, {})
135         assert newcom == cvt("test foo/bar")
136
137         newcom = scons_subst("test ${TARGET.dir}", loc, {})
138         assert newcom == cvt("test foo")
139
140         newcom = scons_subst("test $xxx", loc, {})
141         assert newcom == cvt("test"), newcom
142
143         newcom = scons_subst("test $($xxx$)", loc, {})
144         assert newcom == cvt("test $($)"), newcom
145
146         newcom = scons_subst("test $( $xxx $)", loc, {})
147         assert newcom == cvt("test $( $)"), newcom
148
149         newcom = scons_subst("test $($xxx$)", loc, {}, re.compile('\$[()]'))
150         assert newcom == cvt("test"), newcom
151
152         newcom = scons_subst("test $( $xxx $)", loc, {}, re.compile('\$[()]'))
153         assert newcom == cvt("test"), newcom
154
155         newcom = scons_subst("test aXbXcXd", loc, {}, re.compile('X'))
156         assert newcom == cvt("test abcd"), newcom
157
158     def test_subst_list(self):
159         """Testing the scons_subst_list() method..."""
160         loc = {}
161         loc['TARGETS'] = PathList(map(os.path.normpath, [ "./foo/bar.exe",
162                                                           "/bar/baz with spaces.obj",
163                                                           "../foo/baz.obj" ]))
164         loc['TARGET'] = loc['TARGETS'][0]
165         loc['SOURCES'] = PathList(map(os.path.normpath, [ "./foo/blah with spaces.cpp",
166                                                           "/bar/ack.cpp",
167                                                           "../foo/ack.c" ]))
168         loc['xxx'] = None
169         loc['NEWLINE'] = 'before\nafter'
170
171         if os.sep == '/':
172             def cvt(str):
173                 return str
174         else:
175             def cvt(str):
176                 return string.replace(str, '/', os.sep)
177
178         cmd_list = scons_subst_list("$TARGETS", loc, {})
179         assert cmd_list[0][1] == cvt("/bar/baz with spaces.obj"), cmd_list[0][1]
180
181         cmd_list = scons_subst_list("$SOURCES $NEWLINE $TARGETS", loc, {})
182         assert len(cmd_list) == 2, cmd_list
183         assert cmd_list[0][0] == cvt('foo/blah with spaces.cpp'), cmd_list[0][0]
184         assert cmd_list[1][2] == cvt("/bar/baz with spaces.obj"), cmd_list[1]
185
186         cmd_list = scons_subst_list("$SOURCES$NEWLINE", loc, {})
187         assert len(cmd_list) == 2, cmd_list
188         assert cmd_list[1][0] == 'after', cmd_list[1][0]
189         assert cmd_list[0][2] == cvt('../foo/ack.cbefore'), cmd_list[0][2]
190
191     def test_find_file(self):
192         """Testing find_file function."""
193         test = TestCmd.TestCmd(workdir = '')
194         test.write('./foo', 'Some file\n')
195         fs = SCons.Node.FS.FS(test.workpath(""))
196         os.chdir(test.workpath("")) # FS doesn't like the cwd to be something other than it's root
197         node_derived = fs.File(test.workpath('bar/baz'))
198         node_derived.builder_set(1) # Any non-zero value.
199         paths = map(fs.Dir, ['.', './bar'])
200         nodes = [find_file('foo', paths, fs.File), 
201                  find_file('baz', paths, fs.File)] 
202         file_names = map(str, nodes)
203         file_names = map(os.path.normpath, file_names)
204         assert os.path.normpath('./foo') in file_names, file_names
205         assert os.path.normpath('./bar/baz') in file_names, file_names
206
207     def test_autogenerate(dict):
208         """Test autogenerating variables in a dictionary."""
209         dict = {'LIBS'          : [ 'foo', 'bar', 'baz' ],
210                 'LIBLINKPREFIX' : 'foo',
211                 'LIBLINKSUFFIX' : 'bar'}
212         autogenerate(dict, dir = SCons.Node.FS.default_fs.Dir('/xx'))
213         assert len(dict['_LIBFLAGS']) == 3, dict('_LIBFLAGS')
214         assert dict['_LIBFLAGS'][0] == 'foofoobar', \
215                dict['_LIBFLAGS'][0]
216         assert dict['_LIBFLAGS'][1] == 'foobarbar', \
217                dict['_LIBFLAGS'][1]
218         assert dict['_LIBFLAGS'][2] == 'foobazbar', \
219                dict['_LIBFLAGS'][2]
220
221         blat = SCons.Node.FS.default_fs.File('blat')
222         dict = {'CPPPATH'   : [ 'foo', '$FOO/bar', blat],
223                 'INCPREFIX' : 'foo ',
224                 'INCSUFFIX' : 'bar',
225                 'FOO'       : 'baz' }
226         autogenerate(dict, dir = SCons.Node.FS.default_fs.Dir('/xx'))
227         assert len(dict['_INCFLAGS']) == 8, dict['_INCFLAGS']
228         assert dict['_INCFLAGS'][0] == '$(', \
229                dict['_INCFLAGS'][0]
230         assert dict['_INCFLAGS'][1] == os.path.normpath('foo'), \
231                dict['_INCFLAGS'][1]
232         assert dict['_INCFLAGS'][2] == os.path.normpath('/xx/foobar'), \
233                dict['_INCFLAGS'][2]
234         assert dict['_INCFLAGS'][3] == os.path.normpath('foo'), \
235                dict['_INCFLAGS'][3]
236         assert dict['_INCFLAGS'][4] == os.path.normpath('/xx/baz/barbar'), \
237                dict['_INCFLAGS'][4]
238         assert dict['_INCFLAGS'][5] == os.path.normpath('foo'), \
239                dict['_INCFLAGS'][5]
240         assert dict['_INCFLAGS'][6] == os.path.normpath('blatbar'), \
241                dict['_INCFLAGS'][6]
242         assert dict['_INCFLAGS'][7] == '$)', \
243                dict['_INCFLAGS'][7]
244
245         dict = {'CPPPATH'  : '',
246                 'LIBPATH'  : '' }
247         autogenerate(dict, dir = SCons.Node.FS.default_fs.Dir('/yy'))
248         assert len(dict['_INCFLAGS']) == 0, dict['_INCFLAGS']
249         assert len(dict['_LIBDIRFLAGS']) == 0, dict['_LIBDIRFLAGS']
250
251     def test_render_tree(self):
252         class Node:
253             def __init__(self, name, children=[]):
254                 self.children = children
255                 self.name = name
256             def __str__(self):
257                 return self.name
258
259         def get_children(node):
260             return node.children
261
262         windows_h = Node("windows.h")
263         stdlib_h = Node("stdlib.h")
264         stdio_h = Node("stdio.h")
265         bar_c = Node("bar.c", [stdlib_h, windows_h])
266         bar_o = Node("bar.o", [bar_c])
267         foo_c = Node("foo.c", [stdio_h])
268         foo_o = Node("foo.o", [foo_c])
269         foo = Node("foo", [foo_o, bar_o])
270
271         expect = """\
272 +-foo
273   +-foo.o
274   | +-foo.c
275   |   +-stdio.h
276   +-bar.o
277     +-bar.c
278       +-stdlib.h
279       +-windows.h
280 """
281
282         actual = render_tree(foo, get_children)
283         assert expect == actual, (expect, actual)
284
285     def test_is_Dict(self):
286         assert is_Dict({})
287         import UserDict
288         assert is_Dict(UserDict.UserDict())
289         assert not is_Dict([])
290         assert not is_Dict("")
291         if hasattr(types, 'UnicodeType'):
292             exec "assert not is_Dict(u'')"
293
294     def test_is_List(self):
295         assert is_List([])
296         import UserList
297         assert is_List(UserList.UserList())
298         assert not is_List({})
299         assert not is_List("")
300         if hasattr(types, 'UnicodeType'):
301             exec "assert not is_List(u'')"
302
303     def test_is_String(self):
304         assert is_String("")
305         if hasattr(types, 'UnicodeType'):
306             exec "assert is_String(u'')"
307         try:
308             import UserString
309         except:
310             pass
311         else:
312             assert is_String(UserString.UserString(''))
313         assert not is_String({})
314         assert not is_String([])
315
316 if __name__ == "__main__":
317     suite = unittest.makeSuite(UtilTestCase, 'test_')
318     if not unittest.TextTestRunner().run(suite).wasSuccessful():
319         sys.exit(1)