]> git.lyx.org Git - lyx.git/blob - src/tex2lyx/test/runtests.py
Fix writer2latex quote handling (bug #8903)
[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                  'box-color-size-space-align.tex', \
75                  'CJK.tex', \
76                  'CJKutf8.tex', \
77                  'test-insets.tex', \
78                  'test-modules.tex', \
79                  'test-refstyle-theorems.tex', \
80                  'test-structure.tex', \
81                  'verbatim.tex', \
82                  'XeTeX-polyglossia.tex']
83
84     errors = []
85     overwrite = (outputdir == inputdir)
86     for f in files:
87         (base, ext) = os.path.splitext(f)
88         texfile = os.path.join(inputdir, f)
89         if overwrite:
90             # we are updating the test references, so use roundtrip to allow
91             # for checking the LyX export as well.
92             cmd = '%s -roundtrip -f %s' % (tex2lyx, texfile)
93         else:
94             lyxfile = os.path.join(outputdir, base + ".lyx")
95             cmd = '%s -roundtrip -copyfiles -f %s %s' % (tex2lyx, texfile, lyxfile)
96         print 'Executing: ' + cmd + "\n"
97         proc = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
98         proc.wait()
99         err = proc.returncode
100         errorstring = proc.stderr.read()
101         if not errorstring is None:
102             print errorstring
103         if err != 0:
104             errors.append(f)
105         elif not overwrite:
106             lyxfile1 = getlyxinput(lyx,
107                         os.path.join(inputdir, base + ".lyx.lyx"),
108                         os.path.join(outputdir, base + ".lyx1.lyx") , uselyx2lyx)
109             if lyxfile1 is None:
110                 errors.append(f)
111             else:
112                 lyxfile2 = getlyxinput(lyx,
113                           os.path.join(outputdir, base + ".lyx"),
114                           os.path.join(outputdir, base + ".lyx2.lyx"), uselyx2lyx)
115                 if lyxfile2 is None:
116                     errors.append(f)
117                 else:
118                     t1 = time.ctime(os.path.getmtime(lyxfile1))
119                     t2 = time.ctime(os.path.getmtime(lyxfile2))
120                     f1 = open(lyxfile1, 'r')
121                     f2 = open(lyxfile2, 'r')
122                     lines1 = f1.readlines()
123                     lines2 = f2.readlines()
124                     f1.close()
125                     f2.close()
126                     # ignore the first line e.g. the version of lyx
127                     if not compareLyx(lines1, lines2):
128                         diff = difflib.unified_diff(lines1, lines2, lyxfile1, lyxfile2, t1, t2)
129                         sys.stdout.writelines(diff)
130                         errors.append(f)
131
132
133     if len(errors) > 0:
134         error('Converting the following files failed: %s' % ', '.join(errors))
135
136 def getlyxinput(lyx, lyxfx, lyxf, uselyx2lyx):
137     if uselyx2lyx:
138         cmd = '%s -E lyx %s %s' % (lyx, lyxf, lyxfx)
139         sys.stdout.writelines(cmd)
140         if os.system(cmd) != 0:
141             return None
142         return lyxf
143     else:
144         return lyxfx
145
146 if __name__ == "__main__":
147     main(sys.argv)
148