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