ee62ca750a82f851b41b22c9ece95e7b25df786d
[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 sys
28 import types
29 import unittest
30
31 import TestCmd
32 import SCons.Node.FS
33 import SCons.Scanner.Prog
34
35 test = TestCmd.TestCmd(workdir = '')
36
37 test.subdir('d1', ['d1', 'd2'], 'dir', ['dir', 'sub'])
38
39 libs = [ 'l1.lib', 'd1/l2.lib', 'd1/d2/l3.lib',
40          'dir/libfoo.a', 'dir/sub/libbar.a', 'dir/libxyz.other']
41
42 for h in libs:
43     test.write(h, "\n")
44
45 # define some helpers:
46
47 class DummyEnvironment:
48     def __init__(self, **kw):
49         self._dict = {'LIBSUFFIXES' : '.lib'}
50         self._dict.update(kw)
51         self.fs = SCons.Node.FS.FS(test.workpath(''))
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 [self._dict[x] for x in args]
60
61     def has_key(self, key):
62         return key in self.Dictionary()
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, target=None, source=None, conv=None):
74         try:
75             if s[0] == '$':
76                 return self._dict[s[1:]]
77         except IndexError:
78             return ''
79         return s
80
81     def subst_path(self, path, target=None, source=None, conv=None):
82         if type(path) != type([]):
83             path = [path]
84         return list(map(self.subst, path))
85
86     def get_factory(self, factory):
87         return factory or self.fs.File
88
89     def Dir(self, filename):
90         return self.fs.Dir(test.workpath(filename))
91
92     def File(self, filename):
93         return self.fs.File(test.workpath(filename))
94
95 class DummyNode:
96     def __init__(self, name):
97         self.name = name
98     def rexists(self):
99         return 1
100     def __str__(self):
101         return self.name
102     
103 def deps_match(deps, libs):
104     deps=list(map(str, deps))
105     deps.sort()
106     libs.sort()
107     return list(map(os.path.normpath, deps)) == list(map(os.path.normpath, libs))
108
109 # define some tests:
110
111 class ProgramScannerTestCase1(unittest.TestCase):
112     def runTest(self):
113         env = DummyEnvironment(LIBPATH=[ test.workpath("") ],
114                                LIBS=[ 'l1', 'l2', 'l3' ])
115         s = SCons.Scanner.Prog.ProgramScanner()
116         path = s.path(env)
117         deps = s(DummyNode('dummy'), env, path)
118         assert deps_match(deps, ['l1.lib']), list(map(str, deps))
119
120         env = DummyEnvironment(LIBPATH=[ test.workpath("") ],
121                                LIBS='l1')
122         s = SCons.Scanner.Prog.ProgramScanner()
123         path = s.path(env)
124         deps = s(DummyNode('dummy'), env, path)
125         assert deps_match(deps, ['l1.lib']), list(map(str, deps))
126
127         f1 = env.fs.File(test.workpath('f1'))
128         env = DummyEnvironment(LIBPATH=[ test.workpath("") ],
129                                LIBS=[f1])
130         s = SCons.Scanner.Prog.ProgramScanner()
131         path = s.path(env)
132         deps = s(DummyNode('dummy'), env, path)
133         assert deps[0] is f1, deps
134
135         f2 = env.fs.File(test.workpath('f1'))
136         env = DummyEnvironment(LIBPATH=[ test.workpath("") ],
137                                LIBS=f2)
138         s = SCons.Scanner.Prog.ProgramScanner()
139         path = s.path(env)
140         deps = s(DummyNode('dummy'), env, path)
141         assert deps[0] is f2, deps
142
143
144 class ProgramScannerTestCase2(unittest.TestCase):
145     def runTest(self):
146         env = DummyEnvironment(LIBPATH=list(map(test.workpath,
147                                            ["", "d1", "d1/d2" ])),
148                                LIBS=[ 'l1', 'l2', 'l3' ])
149         s = SCons.Scanner.Prog.ProgramScanner()
150         path = s.path(env)
151         deps = s(DummyNode('dummy'), env, path)
152         assert deps_match(deps, ['l1.lib', 'd1/l2.lib', 'd1/d2/l3.lib' ]), list(map(str, deps))
153
154 class ProgramScannerTestCase3(unittest.TestCase):
155     def runTest(self):
156         env = DummyEnvironment(LIBPATH=[test.workpath("d1/d2"),
157                                         test.workpath("d1")],
158                                LIBS='l2 l3'.split())
159         s = SCons.Scanner.Prog.ProgramScanner()
160         path = s.path(env)
161         deps = s(DummyNode('dummy'), env, path)
162         assert deps_match(deps, ['d1/l2.lib', 'd1/d2/l3.lib']), list(map(str, deps))
163
164 class ProgramScannerTestCase5(unittest.TestCase):
165     def runTest(self):
166         class SubstEnvironment(DummyEnvironment):
167             def subst(self, arg, target=None, source=None, conv=None, path=test.workpath("d1")):
168                 if arg == "$blah":
169                     return test.workpath("d1")
170                 else:
171                     return arg
172         env = SubstEnvironment(LIBPATH=[ "$blah" ],
173                                LIBS='l2 l3'.split())
174         s = SCons.Scanner.Prog.ProgramScanner()
175         path = s.path(env)
176         deps = s(DummyNode('dummy'), env, path)
177         assert deps_match(deps, [ 'd1/l2.lib' ]), list(map(str, deps))
178
179 class ProgramScannerTestCase6(unittest.TestCase):
180     def runTest(self):
181         env = DummyEnvironment(LIBPATH=[ test.workpath("dir") ],
182                                LIBS=['foo', 'sub/libbar', 'xyz.other'],
183                                LIBPREFIXES=['lib'],
184                                LIBSUFFIXES=['.a'])
185         s = SCons.Scanner.Prog.ProgramScanner()
186         path = s.path(env)
187         deps = s(DummyNode('dummy'), env, path)
188         assert deps_match(deps, ['dir/libfoo.a', 'dir/sub/libbar.a', 'dir/libxyz.other']), list(map(str, deps))
189
190 class ProgramScannerTestCase7(unittest.TestCase):
191     def runTest(self):
192         env = DummyEnvironment(LIBPATH=[ test.workpath("dir") ],
193                                LIBS=['foo', '$LIBBAR', '$XYZ'],
194                                LIBPREFIXES=['lib'],
195                                LIBSUFFIXES=['.a'],
196                                LIBBAR='sub/libbar',
197                                XYZ='xyz.other')
198         s = SCons.Scanner.Prog.ProgramScanner()
199         path = s.path(env)
200         deps = s(DummyNode('dummy'), env, path)
201         assert deps_match(deps, ['dir/libfoo.a', 'dir/sub/libbar.a', 'dir/libxyz.other']), list(map(str, deps))
202
203 class ProgramScannerTestCase8(unittest.TestCase):
204     def runTest(self):
205         
206         n1 = DummyNode('n1')
207         env = DummyEnvironment(LIBPATH=[ test.workpath("dir") ],
208                                LIBS=[n1],
209                                LIBPREFIXES=['p1-', 'p2-'],
210                                LIBSUFFIXES=['.1', '2'])
211         s = SCons.Scanner.Prog.ProgramScanner(node_class = DummyNode)
212         path = s.path(env)
213         deps = s(DummyNode('dummy'), env, path)
214         assert deps == [n1], deps
215
216         n2 = DummyNode('n2')
217         env = DummyEnvironment(LIBPATH=[ test.workpath("dir") ],
218                                LIBS=[n1, [n2]],
219                                LIBPREFIXES=['p1-', 'p2-'],
220                                LIBSUFFIXES=['.1', '2'])
221         s = SCons.Scanner.Prog.ProgramScanner(node_class = DummyNode)
222         path = s.path(env)
223         deps = s(DummyNode('dummy'), env, path)
224         assert deps == [n1, n2], deps
225
226 def suite():
227     suite = unittest.TestSuite()
228     suite.addTest(ProgramScannerTestCase1())
229     suite.addTest(ProgramScannerTestCase2())
230     suite.addTest(ProgramScannerTestCase3())
231     suite.addTest(ProgramScannerTestCase5())
232     suite.addTest(ProgramScannerTestCase6())
233     suite.addTest(ProgramScannerTestCase7())
234     suite.addTest(ProgramScannerTestCase8())
235     if hasattr(types, 'UnicodeType'):
236         code = """if 1:
237             class ProgramScannerTestCase4(unittest.TestCase):
238                 def runTest(self):
239                     env = DummyEnvironment(LIBPATH=[test.workpath("d1/d2"),
240                                                     test.workpath("d1")],
241                                            LIBS=u'l2 l3'.split())
242                     s = SCons.Scanner.Prog.ProgramScanner()
243                     path = s.path(env)
244                     deps = s(DummyNode('dummy'), env, path)
245                     assert deps_match(deps, ['d1/l2.lib', 'd1/d2/l3.lib']), map(str, deps)
246             suite.addTest(ProgramScannerTestCase4())
247             \n"""
248         exec code
249     return suite
250
251 if __name__ == "__main__":
252     runner = unittest.TextTestRunner()
253     result = runner.run(suite())
254     if not result.wasSuccessful():
255         sys.exit(1)
256
257 # Local Variables:
258 # tab-width:4
259 # indent-tabs-mode:nil
260 # End:
261 # vim: set expandtab tabstop=4 shiftwidth=4: