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