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