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