Fix stripping the library prefix.
[scons.git] / src / engine / SCons / Scanner / ProgTests.py
1 #
2 # __COPYRIGHT__
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.path
27 import string
28 import sys
29 import types
30 import unittest
31
32 import TestCmd
33 import SCons.Node.FS
34 import SCons.Scanner.Prog
35
36 test = TestCmd.TestCmd(workdir = '')
37
38 test.subdir('d1', ['d1', 'd2'], 'dir', ['dir', 'sub'])
39
40 libs = [ 'l1.lib', 'd1/l2.lib', 'd1/d2/l3.lib',
41          'dir/libfoo.a', 'dir/sub/libbar.a', 'dir/libxyz.other']
42
43 for h in libs:
44     test.write(h, "\n")
45
46 # define some helpers:
47
48 class DummyEnvironment:
49     def __init__(self, **kw):
50         self._dict = {'LIBSUFFIXES' : '.lib'}
51         self._dict.update(kw)
52
53     def Dictionary(self, *args):
54         if not args:
55             return self._dict
56         elif len(args) == 1:
57             return self._dict[args[0]]
58         else:
59             return map(lambda x, s=self: s._dict[x], args)
60
61     def has_key(self, key):
62         return self.Dictionary().has_key(key)
63
64     def __getitem__(self,key):
65         return self.Dictionary()[key]
66
67     def __setitem__(self,key,value):
68         self.Dictionary()[key] = value
69
70     def __delitem__(self,key):
71         del self.Dictionary()[key]
72
73     def subst(self, s):
74         return s
75
76 def deps_match(deps, libs):
77     deps=map(str, deps)
78     deps.sort()
79     libs.sort()
80     return map(os.path.normpath, deps) == \
81            map(os.path.normpath,
82                map(test.workpath, libs))
83
84 # define some tests:
85
86 class ProgScanTestCase1(unittest.TestCase):
87     def runTest(self):
88         env = DummyEnvironment(LIBPATH=[ test.workpath("") ],
89                                LIBS=[ 'l1', 'l2', 'l3' ])
90         s = SCons.Scanner.Prog.ProgScan()
91         path = s.path(env)
92         deps = s('dummy', env, path)
93         assert deps_match(deps, ['l1.lib']), map(str, deps)
94
95         env = DummyEnvironment(LIBPATH=[ test.workpath("") ],
96                                LIBS='l1')
97         s = SCons.Scanner.Prog.ProgScan()
98         path = s.path(env)
99         deps = s('dummy', env, path)
100         assert deps_match(deps, ['l1.lib']), map(str, deps)
101
102         f1 = SCons.Node.FS.default_fs.File(test.workpath('f1'))
103         env = DummyEnvironment(LIBPATH=[ test.workpath("") ],
104                                LIBS=[f1])
105         s = SCons.Scanner.Prog.ProgScan()
106         path = s.path(env)
107         deps = s('dummy', env, path)
108         assert deps[0] is f1, deps
109
110         f2 = SCons.Node.FS.default_fs.File(test.workpath('f1'))
111         env = DummyEnvironment(LIBPATH=[ test.workpath("") ],
112                                LIBS=f2)
113         s = SCons.Scanner.Prog.ProgScan()
114         path = s.path(env)
115         deps = s('dummy', env, path)
116         assert deps[0] is f2, deps
117
118
119 class ProgScanTestCase2(unittest.TestCase):
120     def runTest(self):
121         env = DummyEnvironment(LIBPATH=map(test.workpath,
122                                            ["", "d1", "d1/d2" ]),
123                                LIBS=[ 'l1', 'l2', 'l3' ])
124         s = SCons.Scanner.Prog.ProgScan()
125         path = s.path(env)
126         deps = s('dummy', env, path)
127         assert deps_match(deps, ['l1.lib', 'd1/l2.lib', 'd1/d2/l3.lib' ]), map(str, deps)
128
129 class ProgScanTestCase3(unittest.TestCase):
130     def runTest(self):
131         env = DummyEnvironment(LIBPATH=[test.workpath("d1/d2"),
132                                         test.workpath("d1")],
133                                LIBS=string.split('l2 l3'))
134         s = SCons.Scanner.Prog.ProgScan()
135         path = s.path(env)
136         deps = s('dummy', env, path)
137         assert deps_match(deps, ['d1/l2.lib', 'd1/d2/l3.lib']), map(str, deps)
138
139 class ProgScanTestCase5(unittest.TestCase):
140     def runTest(self):
141         class SubstEnvironment(DummyEnvironment):
142             def subst(self, arg, path=test.workpath("d1")):
143                 if arg == "blah":
144                     return test.workpath("d1")
145                 else:
146                     return arg
147         env = SubstEnvironment(LIBPATH=[ "blah" ],
148                                LIBS=string.split('l2 l3'))
149         s = SCons.Scanner.Prog.ProgScan()
150         path = s.path(env)
151         deps = s('dummy', env, path)
152         assert deps_match(deps, [ 'd1/l2.lib' ]), map(str, deps)
153
154 class ProgScanTestCase6(unittest.TestCase):
155     def runTest(self):
156         env = DummyEnvironment(LIBPATH=[ test.workpath("dir") ],
157                                LIBS=['foo', 'sub/libbar', 'xyz.other'],
158                                LIBPREFIXES=['lib'],
159                                LIBSUFFIXES=['.a'])
160         s = SCons.Scanner.Prog.ProgScan()
161         path = s.path(env)
162         deps = s('dummy', env, path)
163         assert deps_match(deps, ['dir/libfoo.a', 'dir/sub/libbar.a', 'dir/libxyz.other']), map(str, deps)
164
165 def suite():
166     suite = unittest.TestSuite()
167     suite.addTest(ProgScanTestCase1())
168     suite.addTest(ProgScanTestCase2())
169     suite.addTest(ProgScanTestCase3())
170     suite.addTest(ProgScanTestCase5())
171     suite.addTest(ProgScanTestCase6())
172     if hasattr(types, 'UnicodeType'):
173         code = """if 1:
174             class ProgScanTestCase4(unittest.TestCase):
175                 def runTest(self):
176                     env = DummyEnvironment(LIBPATH=[test.workpath("d1/d2"),
177                                                     test.workpath("d1")],
178                                            LIBS=string.split(u'l2 l3'))
179                     s = SCons.Scanner.Prog.ProgScan()
180                     path = s.path(env)
181                     deps = s('dummy', env, path)
182                     assert deps_match(deps, ['d1/l2.lib', 'd1/d2/l3.lib']), map(str, deps)
183             suite.addTest(ProgScanTestCase4())
184             \n"""
185         exec code
186     return suite
187
188 if __name__ == "__main__":
189     runner = unittest.TextTestRunner()
190     result = runner.run(suite())
191     if not result.wasSuccessful():
192         sys.exit(1)