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