71ff753fa2ef19582e3b9672f92dd2b5fda6aec7
[scons.git] / test / TEX.py
1 #!/usr/bin/env python
2 #
3 # Copyright (c) 2001, 2002 Steven Knight
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining
6 # a copy of this software and associated documentation files (the
7 # "Software"), to deal in the Software without restriction, including
8 # without limitation the rights to use, copy, modify, merge, publish,
9 # distribute, sublicense, and/or sell copies of the Software, and to
10 # permit persons to whom the Software is furnished to do so, subject to
11 # the following conditions:
12 #
13 # The above copyright notice and this permission notice shall be included
14 # in all copies or substantial portions of the Software.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
17 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
18 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 #
24
25 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
26
27 import os
28 import os.path
29 import string
30 import sys
31 import TestSCons
32
33 python = TestSCons.python
34
35 test = TestSCons.TestSCons()
36
37
38
39 test.write('mytex.py', r"""
40 import sys
41 import os
42 base_name = os.path.splitext(sys.argv[1])[0]
43 infile = open(sys.argv[1], 'rb')
44 out_file = open(base_name+'.dvi', 'wb')
45 for l in infile.readlines():
46     if l[0] != '\\':
47         out_file.write(l)
48 sys.exit(0)
49 """)
50
51 test.write('SConstruct', """
52 env = Environment(TEX = r'%s mytex.py', tools=['tex'])
53 env.DVI(target = 'test.dvi', source = 'test.tex')
54 """ % python)
55
56 test.write('test.tex', r"""This is a test.
57 \end
58 """)
59
60 test.run(arguments = 'test.dvi', stderr = None)
61
62 test.fail_test(test.read('test.dvi') != "This is a test.\n")
63
64
65
66 tex = test.where_is('tex')
67
68 if tex:
69
70     test.write("wrapper.py", """import os
71 import string
72 import sys
73 open('%s', 'wb').write("wrapper.py\\n")
74 os.system(string.join(sys.argv[1:], " "))
75 """ % string.replace(test.workpath('wrapper.out'), '\\', '\\\\'))
76
77     test.write('SConstruct', """
78 foo = Environment()
79 tex = foo.Dictionary('TEX')
80 bar = Environment(TEX = r'%s wrapper.py ' + tex)
81 foo.DVI(target = 'foo.dvi', source = 'foo.tex')
82 bar.DVI(target = 'bar', source = 'bar.tex')
83 """ % python)
84
85     tex = r"""
86 This is the %s TeX file.
87 \end
88 """
89
90     test.write('foo.tex', tex % 'foo.tex')
91
92     test.write('bar.tex', tex % 'bar.tex')
93
94     test.run(arguments = 'foo.dvi', stderr = None)
95
96     test.fail_test(os.path.exists(test.workpath('wrapper.out')))
97
98     test.fail_test(not os.path.exists(test.workpath('foo.dvi')))
99
100     test.run(arguments = 'bar.dvi', stderr = None)
101
102     test.fail_test(not os.path.exists(test.workpath('wrapper.out')))
103
104     test.fail_test(not os.path.exists(test.workpath('bar.dvi')))
105
106 test.pass_test()