ac978cf9637fbab7d7caeb8a214bd378921acb74
[scons.git] / src / engine / SCons / Scanner / LaTeXTests.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 import UserDict
31
32 import TestCmd
33 import SCons.Node.FS
34 import SCons.Scanner.LaTeX
35
36 test = TestCmd.TestCmd(workdir = '')
37
38 test.write('test1.latex',"""
39 \include{inc1}
40 \input{inc2}
41 include{incNO}
42 %\include{incNO}
43 xyzzy \include{inc6}
44 """)
45
46 test.write('test2.latex',"""
47 \include{inc1}
48 \include{inc3}
49 """)
50
51 test.write('test3.latex',"""
52 \includegraphics{inc4.eps}
53 \includegraphics[width=60mm]{inc5.xyz}
54 """)
55
56 test.subdir('subdir')
57
58 test.write('inc1.tex',"\n")
59 test.write('inc2.tex',"\n")
60 test.write(['subdir', 'inc3.tex'], "\n")
61 test.write(['subdir', 'inc4.eps'], "\n")
62 test.write('inc5.xyz', "\n")
63 test.write('inc6.tex', "\n")
64 test.write('incNO.tex', "\n")
65
66 # define some helpers:
67 #   copied from CTest.py
68 class DummyEnvironment(UserDict.UserDict):
69     def __init__(self, **kw):
70         UserDict.UserDict.__init__(self)
71         self.data.update(kw)
72         self.fs = SCons.Node.FS.FS(test.workpath(''))
73
74     def Dictionary(self, *args):
75         return self.data
76
77     def subst(self, strSubst, target=None, source=None, conv=None):
78         if strSubst[0] == '$':
79             return self.data[strSubst[1:]]
80         return strSubst
81
82     def subst_list(self, strSubst, target=None, source=None, conv=None):
83         if strSubst[0] == '$':
84             return [self.data[strSubst[1:]]]
85         return [[strSubst]]
86
87     def subst_path(self, path, target=None, source=None, conv=None):
88         if type(path) != type([]):
89             path = [path]
90         return list(map(self.subst, path))
91
92     def get_calculator(self):
93         return None
94
95     def get_factory(self, factory):
96         return factory or self.fs.File
97
98     def Dir(self, filename):
99         return self.fs.Dir(filename)
100
101     def File(self, filename):
102         return self.fs.File(filename)
103
104 if os.path.normcase('foo') == os.path.normcase('FOO'):
105     my_normpath = os.path.normcase
106 else:
107     my_normpath = os.path.normpath
108
109 def deps_match(self, deps, headers):
110     global my_normpath
111     scanned = list(map(my_normpath, list(map(str, deps))))
112     expect = list(map(my_normpath, headers))
113     self.failUnless(scanned == expect, "expect %s != scanned %s" % (expect, scanned))
114
115
116 class LaTeXScannerTestCase1(unittest.TestCase):
117     def runTest(self):
118         env = DummyEnvironment(LATEXSUFFIXES = [".tex", ".ltx", ".latex"])
119         s = SCons.Scanner.LaTeX.LaTeXScanner()
120         path = s.path(env)
121         deps = s(env.File('test1.latex'), env, path)
122         headers = ['inc1.tex', 'inc2.tex', 'inc6.tex']
123         deps_match(self, deps, headers)
124
125 class LaTeXScannerTestCase2(unittest.TestCase):
126      def runTest(self):
127          env = DummyEnvironment(TEXINPUTS=[test.workpath("subdir")],LATEXSUFFIXES = [".tex", ".ltx", ".latex"])
128          s = SCons.Scanner.LaTeX.LaTeXScanner()
129          path = s.path(env)
130          deps = s(env.File('test2.latex'), env, path)
131          headers = ['inc1.tex', 'subdir/inc3.tex']
132          deps_match(self, deps, headers)
133
134 class LaTeXScannerTestCase3(unittest.TestCase):
135      def runTest(self):
136          env = DummyEnvironment(TEXINPUTS=[test.workpath("subdir")],LATEXSUFFIXES = [".tex", ".ltx", ".latex"])
137          s = SCons.Scanner.LaTeX.LaTeXScanner()
138          path = s.path(env)
139          deps = s(env.File('test3.latex'), env, path)
140          files = ['inc5.xyz', 'subdir/inc4.eps']
141          deps_match(self, deps, files)
142
143
144 def suite():
145     suite = unittest.TestSuite()
146     suite.addTest(LaTeXScannerTestCase1())
147     suite.addTest(LaTeXScannerTestCase2())
148     suite.addTest(LaTeXScannerTestCase3())
149     return suite
150
151 if __name__ == "__main__":
152     runner = unittest.TextTestRunner()
153     result = runner.run(suite())
154     if not result.wasSuccessful():
155         sys.exit(1)
156
157 # Local Variables:
158 # tab-width:4
159 # indent-tabs-mode:nil
160 # End:
161 # vim: set expandtab tabstop=4 shiftwidth=4: