]> git.lyx.org Git - lyx.git/blob - src/tex2lyx/test/runtests.py
French Additional manual, translation of chapter 4 up to sec. 4.3
[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                  'listpreamble.tex', \
82                  'tabular-x-test.tex', \
83                  'test-insets.tex', \
84                  'test-insets-basic.tex', \
85                  'test-memoir.tex', \
86                  'test-minted.tex', \
87                  'test-modules.tex', \
88                  'test-refstyle-theorems.tex', \
89                  'test-scr.tex', \
90                  'test-structure.tex', \
91                  'verbatim.tex', \
92                  'XeTeX-polyglossia.tex']
93
94     errors = []
95     overwrite = (outputdir == inputdir)
96     for f in files:
97         (base, ext) = os.path.splitext(f)
98         texfile = os.path.join(inputdir, f)
99         if overwrite:
100             # we are updating the test references, so use roundtrip to allow
101             # for checking the LyX export as well.
102             cmd = '%s -roundtrip -f %s' % (tex2lyx, texfile)
103         else:
104             lyxfile = os.path.join(outputdir, base + ".lyx")
105             cmd = '%s -roundtrip -copyfiles -f %s %s' % (tex2lyx, texfile, lyxfile)
106         print('Executing: ' + cmd + "\n")
107         proc = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
108         proc.wait()
109         err = proc.returncode
110         errorstring = proc.stderr.read()
111         if errorstring:
112             print(errorstring.decode(sys.getfilesystemencoding()))
113         if err != 0:
114             errors.append(f)
115         elif not overwrite:
116             lyxfile1 = getlyxinput(lyx,
117                         os.path.join(inputdir, base + ".lyx.lyx"),
118                         os.path.join(outputdir, base + ".lyx1.lyx") , uselyx2lyx)
119             if lyxfile1 is None:
120                 errors.append(f)
121             else:
122                 lyxfile2 = getlyxinput(lyx,
123                           os.path.join(outputdir, base + ".lyx"),
124                           os.path.join(outputdir, base + ".lyx2.lyx"), uselyx2lyx)
125                 if lyxfile2 is None:
126                     errors.append(f)
127                 else:
128                     t1 = time.ctime(os.path.getmtime(lyxfile1))
129                     t2 = time.ctime(os.path.getmtime(lyxfile2))
130                     f1 = open(lyxfile1, 'r', encoding="utf8")
131                     f2 = open(lyxfile2, 'r', encoding="utf8")
132                     lines1 = f1.readlines()
133                     i1 = 0
134                     for linex in lines1:
135                       if linex[:-1] == '\origin ' + inputdir + '/':
136                         lines1[i1] = '\origin ' + outputdir + '/' + "\n"
137                         break
138                       i1 = i1+1
139                     lines2 = f2.readlines()
140                     f1.close()
141                     f2.close()
142                     # ignore the first line e.g. the version of lyx
143                     if not compareLyx(lines1, lines2):
144                         diff = difflib.unified_diff(lines1, lines2, lyxfile1, lyxfile2, t1, t2)
145                         sys.stdout.writelines(diff)
146                         errors.append(f)
147
148
149     if len(errors) > 0:
150         error('Converting the following files failed: %s' % ', '.join(errors))
151
152 def getlyxinput(lyx, lyxfx, lyxf, uselyx2lyx):
153     if uselyx2lyx:
154         cmd = '%s -E lyx %s %s' % (lyx, lyxf, lyxfx)
155         sys.stdout.writelines(cmd)
156         sys.stdout.writelines("\n")
157         if os.system(cmd) != 0:
158             return None
159         return lyxf
160     else:
161         return lyxfx
162
163 if __name__ == "__main__":
164     main(sys.argv)