]> git.lyx.org Git - lyx.git/blob - src/tex2lyx/test/runtests.py
Update tex2lyx tests.
[lyx.git] / src / tex2lyx / test / runtests.py
1 #! /usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 # file src/tex2lyx/test/runtests.py
5 # This file is part of LyX, the document processor.
6 # Licence details can be found in the file COPYING.
7
8 # author Georg Baum
9
10 # Full author contact details are available in file CREDITS
11
12 # This script is a very basic test suite runner for tex2lyx
13 # The defaults for optional command line arguments are tailored to the
14 # standard use case of testing without special build settings like a version
15 # suffix, since I don't know how to transport command line arguments through
16 # the autotools "make check" mechanism.
17
18 import os, string, sys, time, difflib, filecmp, subprocess, re
19
20 def usage(prog_name):
21   return "Usage: %s [uselyx2lyx] [<tex2lyx binary> [[<script dir>] [[<output dir>] [testfile]]]]" % prog_name
22
23 pat_fl1 = re.compile(r'^#LyX file created by tex2lyx .*$')
24 pat_fl2 = re.compile(r'^#LyX \d+\.\d+ created this file.*$')
25
26 def compareLyx(lines1, lines2):
27     if lines1[1:] != lines2[1:]:
28         return False
29     if not pat_fl1.match(lines1[0]) and not pat_fl2.match(lines1[0]):
30         return False
31     if not pat_fl1.match(lines2[0]) and not pat_fl2.match(lines2[0]):
32         return False
33     return True
34
35 def main(argv):
36     # Parse and manipulate the command line arguments.
37     skipcount = 0
38     uselyx2lyx = False
39     if len(argv) > 1:
40         if argv[1] == "uselyx2lyx":
41             uselyx2lyx = True
42             skipcount = 1
43     if len(argv) >= 3+skipcount:
44         sys.path.append(os.path.join(sys.argv[2+skipcount]))
45     else:
46         sys.path.append(os.path.join(os.path.dirname(sys.argv[0]), '../../../lib/scripts'))
47
48     from lyxpreview_tools import error
49
50     if len(argv) < 2+skipcount:
51         tex2lyx = './tex2lyx'
52     elif len(argv) <= 5+skipcount:
53         tex2lyx = argv[1+skipcount]
54     else:
55         error(usage(argv[0]))
56
57     suffixre = re.search(r'\d+\.\d+$', tex2lyx)
58     if suffixre:
59         suffix = suffixre.group()
60     else:
61         suffix = ""
62     lyx = os.path.join(os.path.dirname(tex2lyx), "lyx" + suffix)
63     inputdir = os.path.dirname(argv[0])
64     if len(argv) >= 4+skipcount:
65         outputdir = sys.argv[3+skipcount]
66     else:
67 #        outputdir = inputdir
68         outputdir = os.path.join(os.path.dirname(tex2lyx), "test")
69
70     if len(argv) >= 5+skipcount:
71         files = [sys.argv[4+skipcount]]
72     else:
73         files = ['test.ltx', \
74                  'algo2e.tex', \
75                  'box-color-size-space-align.tex', \
76                  'CJK.tex', \
77                  'CJKutf8.tex', \
78                  'test-insets.tex', \
79                  'test-insets-basic.tex', \
80                  'test-memoir.tex', \
81                  'test-modules.tex', \
82                  'test-refstyle-theorems.tex', \
83                  'test-scr.tex', \
84                  'test-structure.tex', \
85                  'verbatim.tex', \
86                  'XeTeX-polyglossia.tex']
87
88     errors = []
89     overwrite = (outputdir == inputdir)
90     for f in files:
91         (base, ext) = os.path.splitext(f)
92         texfile = os.path.join(inputdir, f)
93         if overwrite:
94             # we are updating the test references, so use roundtrip to allow
95             # for checking the LyX export as well.
96             cmd = '%s -roundtrip -f %s' % (tex2lyx, texfile)
97         else:
98             lyxfile = os.path.join(outputdir, base + ".lyx")
99             cmd = '%s -roundtrip -copyfiles -f %s %s' % (tex2lyx, texfile, lyxfile)
100         print 'Executing: ' + cmd + "\n"
101         proc = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
102         proc.wait()
103         err = proc.returncode
104         errorstring = proc.stderr.read()
105         if not errorstring is None:
106             print errorstring
107         if err != 0:
108             errors.append(f)
109         elif not overwrite:
110             lyxfile1 = getlyxinput(lyx,
111                         os.path.join(inputdir, base + ".lyx.lyx"),
112                         os.path.join(outputdir, base + ".lyx1.lyx") , uselyx2lyx)
113             if lyxfile1 is None:
114                 errors.append(f)
115             else:
116                 lyxfile2 = getlyxinput(lyx,
117                           os.path.join(outputdir, base + ".lyx"),
118                           os.path.join(outputdir, base + ".lyx2.lyx"), uselyx2lyx)
119                 if lyxfile2 is None:
120                     errors.append(f)
121                 else:
122                     t1 = time.ctime(os.path.getmtime(lyxfile1))
123                     t2 = time.ctime(os.path.getmtime(lyxfile2))
124                     f1 = open(lyxfile1, 'r')
125                     f2 = open(lyxfile2, 'r')
126                     lines1 = f1.readlines()
127                     i1 = 0
128                     for linex in lines1:
129                       if linex[:-1] == '\origin ' + inputdir + '/':
130                         lines1[i1] = '\origin ' + outputdir + '/' + "\n"
131                         break
132                       i1 = i1+1
133                     lines2 = f2.readlines()
134                     f1.close()
135                     f2.close()
136                     # ignore the first line e.g. the version of lyx
137                     if not compareLyx(lines1, lines2):
138                         diff = difflib.unified_diff(lines1, lines2, lyxfile1, lyxfile2, t1, t2)
139                         sys.stdout.writelines(diff)
140                         errors.append(f)
141
142
143     if len(errors) > 0:
144         error('Converting the following files failed: %s' % ', '.join(errors))
145
146 def getlyxinput(lyx, lyxfx, lyxf, uselyx2lyx):
147     if uselyx2lyx:
148         cmd = '%s -E lyx %s %s' % (lyx, lyxf, lyxfx)
149         sys.stdout.writelines(cmd)
150         sys.stdout.writelines("\n")
151         if os.system(cmd) != 0:
152             return None
153         return lyxf
154     else:
155         return lyxfx
156
157 if __name__ == "__main__":
158     main(sys.argv)
159