Debian Codegraph @

texmf-dist/doc/latex/cals/test/support/LatexTest.py

raw
1 # Running a latex
2 import unittest, os, shutil, re, glob
3
4 settings = None # will be set from the main script
5
6 re_macro_start = re.compile(r'''^> \\[^=]+=macro:$''')
7 re_box_start = re.compile(r'''^> \\box\d+=''')
8 re_some_show = re.compile(r'''^> .*\.$''')
9 re_message = re.compile(r'''^! ''')
10 re_comment = re.compile(r'''^%[^\r\n]*[\r\n]*''', re.S|re.M)
11
12 class LatexTestCase(unittest.TestCase):
13
14 def __init__(self, *ls):
15 unittest.TestCase.__init__(self, *ls)
16 tpl_file = 'template.txt'
17 h = open(tpl_file)
18 self.template = h.read()
19 h.close()
20
21 def get_settings(self):
22 return settings
23
24 def setUp(self):
25 tmp_dir = settings.get_tmp_dir()
26 for fname in os.listdir(tmp_dir):
27 full_name = os.path.join(tmp_dir, fname)
28 os.unlink(full_name)
29
30 def run_latex(self, module, test_name):
31 tmp_dir = settings.get_tmp_dir()
32 src_latex_file = os.path.join(module, test_name + '.tex')
33 tmp_latex_file = os.path.join(tmp_dir, test_name + '.tex')
34 tmp_log_file = os.path.join(tmp_dir, test_name + '.log')
35 #
36 # Create a TeX file from a tempate
37 #
38 h = open(src_latex_file)
39 s = h.read()
40 h.close()
41 s = self.template.replace('##CODE##', s)
42 h = open(tmp_latex_file, 'w')
43 h.write(s)
44 h.close()
45 #
46 # Run LaTeX
47 #
48 cmd = 'cd %s; %s -interaction batchmode %s.tex >/dev/null' % (tmp_dir, settings.get_latex(), test_name)
49 os.system(cmd)
50 return tmp_log_file
51
52 # -------------------------------------------------------
53 # Log-file works
54
55 #
56 # Comparing expected and the got results
57 #
58 def check_log(self, module, test_name, log_file):
59 s_got = self.collect_log(log_file)
60 chk_file = os.path.join(module, test_name + '.chk')
61 h = open(chk_file)
62 s_expected = h.read()
63 h.close()
64 s_expected = re_comment.sub('', s_expected)
65 s_expected = s_expected.strip()
66 s_got = s_got.strip()
67 #
68 # It is convenient to know where exactly start difference
69 #
70 if s_expected != s_got:
71 pos = 0
72 maxpos = min(len(s_expected), len(s_got))
73 while (pos < maxpos) and (s_expected[pos] == s_got[pos]):
74 pos = pos +1
75 s_expected = s_expected[:pos] + '---->' + s_expected[pos:]
76 s_got = s_got[:pos] + '---->' + s_got[pos:]
77 self.assertEqual(s_got, s_expected)
78
79 #
80 # The test case: run latex and check log
81 #
82 def run_test_case(self, module, test_name):
83 log_file = self.run_latex(module, test_name)
84 self.check_log(module, test_name, log_file)
85 self.check_pdf(module, test_name)
86
87 #
88 # Log parsing
89 #
90 def collect_macro(self, h, macro_dump):
91 for l in h:
92 l = l.strip()
93 macro_dump = macro_dump + "\n" + l
94 if (len(l) > 0) and ('.' == l[-1]):
95 break
96 return macro_dump
97
98 def collect_box(self, h, box_dump):
99 box_dump = re.sub('[0-9]+', 'XX', box_dump)
100 for l in h:
101 l = l.strip()
102 if '' == l:
103 break
104 box_dump = box_dump + "\n" + l
105 return box_dump
106
107 def collect_log(self, log_file):
108 s = ''
109 h = open(log_file)
110 for l in h:
111 l = l.strip()
112 if re_macro_start.match(l):
113 macro_dump = self.collect_macro(h, l)
114 s = s + macro_dump + "\n"
115 if re_box_start.match(l):
116 box_dump = self.collect_box(h, l)
117 s = s + box_dump + "\n"
118 if re_some_show.match(l):
119 s = s + l + "\n"
120 if re_message.match(l):
121 if l != '! OK.':
122 s = s + l + "\n"
123 if l.startswith('Package test Info: '):
124 pos = l.index('on input line ')
125 s = s + l[19:pos]
126 s = s.rstrip()
127 s = s + "\n"
128 return s
129
130 # -------------------------------------------------------
131 # Output works
132
133 def check_pdf(self, module, test_name):
134 #
135 # First, check if output testing is required
136 #
137 expected_pngs = glob.glob(os.path.join(module, test_name+'*.png'))
138 if not len(expected_pngs):
139 return
140 expected_pngs = [os.path.basename(x) for x in expected_pngs]
141 #
142 # Generate PNGs and check we got the same number of files
143 #
144 tmp_dir = settings.get_tmp_dir()
145 cmd = "cd %s; convert -density 300x300 %s.pdf %s.png" % (tmp_dir, test_name, test_name)
146 os.system(cmd)
147 got_pngs = glob.glob(os.path.join(tmp_dir, test_name+'*.png'))
148 got_pngs = [os.path.basename(x) for x in got_pngs]
149 self.assertEqual(expected_pngs, got_pngs)
150 #
151 # Compare the PNGs
152 #
153 for png in expected_pngs:
154 expected_png = os.path.join(module, png)
155 got_png = os.path.join(tmp_dir, png)
156 diff_png = os.path.join(tmp_dir, 'diff.png')
157 cmd = "compare compare -metric RMSE %s %s %s 2>%s" % (expected_png, got_png, diff_png, os.path.join(tmp_dir, 'compare-stdout'))
158 os.system(cmd)
159 h = open(os.path.join(tmp_dir, 'compare-stdout'))
160 s = h.read()
161 h.close()
162 s = s.strip()
163 self.assertEqual('0 (0)', s)