http://scons.tigris.org/issues/show_bug.cgi?id=2329
[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 unittest
29
30 import TestCmd
31 import SCons.Node.FS
32 import SCons.Scanner.Prog
33
34 test = TestCmd.TestCmd(workdir = '')
35
36 test.subdir('d1', ['d1', 'd2'], 'dir', ['dir', 'sub'])
37
38 libs = [ 'l1.lib', 'd1/l2.lib', 'd1/d2/l3.lib',
39          'dir/libfoo.a', 'dir/sub/libbar.a', 'dir/libxyz.other']
40
41 for h in libs:
42     test.write(h, "\n")
43
44 # define some helpers:
45
46 class DummyEnvironment:
47     def __init__(self, **kw):
48         self._dict = {'LIBSUFFIXES' : '.lib'}
49         self._dict.update(kw)
50         self.fs = SCons.Node.FS.FS(test.workpath(''))
51
52     def Dictionary(self, *args):
53         if not args:
54             return self._dict
55         elif len(args) == 1:
56             return self._dict[args[0]]
57         else:
58             return [self._dict[x] for x in args]
59
60     def has_key(self, key):
61         return key in self.Dictionary()
62
63     def __getitem__(self,key):
64         return self.Dictionary()[key]
65
66     def __setitem__(self,key,value):
67         self.Dictionary()[key] = value
68
69     def __delitem__(self,key):
70         del self.Dictionary()[key]
71
72     def subst(self, s, target=None, source=None, conv=None):
73         try:
74             if s[0] == '$':
75                 return self._dict[s[1:]]
76         except IndexError:
77             return ''
78         return s
79
80     def subst_path(self, path, target=None, source=None, conv=None):
81         if not isinstance(path, list):
82             path = [path]
83         return list(map(self.subst, path))
84
85     def get_factory(self, factory):
86         return factory or self.fs.File
87
88     def Dir(self, filename):
89         return self.fs.Dir(test.workpath(filename))
90
91     def File(self, filename):
92         return self.fs.File(test.workpath(filename))
93
94 class DummyNode:
95     def __init__(self, name):
96         self.name = name
97     def rexists(self):
98         return 1
99     def __str__(self):
100         return self.name
101     
102 def deps_match(deps, libs):
103     deps=sorted(map(str, deps))
104     libs.sort()
105     return list(map(os.path.normpath, deps)) == list(map(os.path.normpath, libs))
106
107 # define some tests:
108
109 class ProgramScannerTestCase1(unittest.TestCase):
110     def runTest(self):
111         env = DummyEnvironment(LIBPATH=[ test.workpath("") ],
112                                LIBS=[ 'l1', 'l2', 'l3' ])
113         s = SCons.Scanner.Prog.ProgramScanner()
114         path = s.path(env)
115         deps = s(DummyNode('dummy'), env, path)
116         assert deps_match(deps, ['l1.lib']), list(map(str, deps))
117
118         env = DummyEnvironment(LIBPATH=[ test.workpath("") ],
119                                LIBS='l1')
120         s = SCons.Scanner.Prog.ProgramScanner()
121         path = s.path(env)
122         deps = s(DummyNode('dummy'), env, path)
123         assert deps_match(deps, ['l1.lib']), list(map(str, deps))
124
125         f1 = env.fs.File(test.workpath('f1'))
126         env = DummyEnvironment(LIBPATH=[ test.workpath("") ],
127                                LIBS=[f1])
128         s = SCons.Scanner.Prog.ProgramScanner()
129         path = s.path(env)
130         deps = s(DummyNode('dummy'), env, path)
131         assert deps[0] is f1, deps
132
133         f2 = env.fs.File(test.workpath('f1'))
134         env = DummyEnvironment(LIBPATH=[ test.workpath("") ],
135                                LIBS=f2)
136         s = SCons.Scanner.Prog.ProgramScanner()
137         path = s.path(env)
138         deps = s(DummyNode('dummy'), env, path)
139         assert deps[0] is f2, deps
140
141
142 class ProgramScannerTestCase2(unittest.TestCase):
143     def runTest(self):
144         env = DummyEnvironment(LIBPATH=list(map(test.workpath,
145                                            ["", "d1", "d1/d2" ])),
146                                LIBS=[ 'l1', 'l2', 'l3' ])
147         s = SCons.Scanner.Prog.ProgramScanner()
148         path = s.path(env)
149         deps = s(DummyNode('dummy'), env, path)
150         assert deps_match(deps, ['l1.lib', 'd1/l2.lib', 'd1/d2/l3.lib' ]), list(map(str, deps))
151
152 class ProgramScannerTestCase3(unittest.TestCase):
153     def runTest(self):
154         env = DummyEnvironment(LIBPATH=[test.workpath("d1/d2"),
155                                         test.workpath("d1")],
156                                LIBS='l2 l3'.split())
157         s = SCons.Scanner.Prog.ProgramScanner()
158         path = s.path(env)
159         deps = s(DummyNode('dummy'), env, path)
160         assert deps_match(deps, ['d1/l2.lib', 'd1/d2/l3.lib']), list(map(str, deps))
161
162 class ProgramScannerTestCase5(unittest.TestCase):
163     def runTest(self):
164         class SubstEnvironment(DummyEnvironment):
165             def subst(self, arg, target=None, source=None, conv=None, path=test.workpath("d1")):
166                 if arg == "$blah":
167                     return test.workpath("d1")
168                 else:
169                     return arg
170         env = SubstEnvironment(LIBPATH=[ "$blah" ],
171                                LIBS='l2 l3'.split())
172         s = SCons.Scanner.Prog.ProgramScanner()
173         path = s.path(env)
174         deps = s(DummyNode('dummy'), env, path)
175         assert deps_match(deps, [ 'd1/l2.lib' ]), list(map(str, deps))
176
177 class ProgramScannerTestCase6(unittest.TestCase):
178     def runTest(self):
179         env = DummyEnvironment(LIBPATH=[ test.workpath("dir") ],
180                                LIBS=['foo', 'sub/libbar', 'xyz.other'],
181                                LIBPREFIXES=['lib'],
182                                LIBSUFFIXES=['.a'])
183         s = SCons.Scanner.Prog.ProgramScanner()
184         path = s.path(env)
185         deps = s(DummyNode('dummy'), env, path)
186         assert deps_match(deps, ['dir/libfoo.a', 'dir/sub/libbar.a', 'dir/libxyz.other']), list(map(str, deps))
187
188 class ProgramScannerTestCase7(unittest.TestCase):
189     def runTest(self):
190         env = DummyEnvironment(LIBPATH=[ test.workpath("dir") ],
191                                LIBS=['foo', '$LIBBAR', '$XYZ'],
192                                LIBPREFIXES=['lib'],
193                                LIBSUFFIXES=['.a'],
194                                LIBBAR='sub/libbar',
195                                XYZ='xyz.other')
196         s = SCons.Scanner.Prog.ProgramScanner()
197         path = s.path(env)
198         deps = s(DummyNode('dummy'), env, path)
199         assert deps_match(deps, ['dir/libfoo.a', 'dir/sub/libbar.a', 'dir/libxyz.other']), list(map(str, deps))
200
201 class ProgramScannerTestCase8(unittest.TestCase):
202     def runTest(self):
203         
204         n1 = DummyNode('n1')
205         env = DummyEnvironment(LIBPATH=[ test.workpath("dir") ],
206                                LIBS=[n1],
207                                LIBPREFIXES=['p1-', 'p2-'],
208                                LIBSUFFIXES=['.1', '2'])
209         s = SCons.Scanner.Prog.ProgramScanner(node_class = DummyNode)
210         path = s.path(env)
211         deps = s(DummyNode('dummy'), env, path)
212         assert deps == [n1], deps
213
214         n2 = DummyNode('n2')
215         env = DummyEnvironment(LIBPATH=[ test.workpath("dir") ],
216                                LIBS=[n1, [n2]],
217                                LIBPREFIXES=['p1-', 'p2-'],
218                                LIBSUFFIXES=['.1', '2'])
219         s = SCons.Scanner.Prog.ProgramScanner(node_class = DummyNode)
220         path = s.path(env)
221         deps = s(DummyNode('dummy'), env, path)
222         assert deps == [n1, n2], deps
223
224 def suite():
225     suite = unittest.TestSuite()
226     suite.addTest(ProgramScannerTestCase1())
227     suite.addTest(ProgramScannerTestCase2())
228     suite.addTest(ProgramScannerTestCase3())
229     suite.addTest(ProgramScannerTestCase5())
230     suite.addTest(ProgramScannerTestCase6())
231     suite.addTest(ProgramScannerTestCase7())
232     suite.addTest(ProgramScannerTestCase8())
233     try: unicode
234     except NameError: pass
235     else:
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: