2 # -*- coding: utf-8 -*-
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.
10 # Full author contact details are available in file CREDITS
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.
18 import os, string, sys, time, difflib, filecmp, subprocess, re
21 return "Usage: %s [uselyx2lyx] [<tex2lyx binary> [[<script dir>] [[<output dir>] [testfile]]]]" % prog_name
23 pat_fl = re.compile(r'^#LyX file created by tex2lyx .*$')
25 def compareLyx(lines1, lines2):
26 if lines1[1:] != lines2[1:]:
28 if not pat_fl.match(lines1[0]):
30 if not pat_fl.match(lines2[0]):
35 # Parse and manipulate the command line arguments.
39 if argv[1] == "uselyx2lyx":
42 if len(argv) >= 3+skipcount:
43 sys.path.append(os.path.join(sys.argv[2+skipcount]))
45 sys.path.append(os.path.join(os.path.dirname(sys.argv[0]), '../../../lib/scripts'))
47 from lyxpreview_tools import error
49 if len(argv) < 2+skipcount:
51 elif len(argv) <= 5+skipcount:
52 tex2lyx = argv[1+skipcount]
56 suffixre = re.search(r'\d+\.\d+$', tex2lyx)
58 suffix = suffixre.group()
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]
66 # outputdir = inputdir
67 outputdir = os.path.join(os.path.dirname(tex2lyx), "test")
69 if len(argv) >= 5+skipcount:
70 files = [sys.argv[4+skipcount]]
72 files = ['test.ltx', \
73 'box-color-size-space-align.tex', \
78 'test-refstyle-theorems.tex', \
79 'test-structure.tex', \
81 'XeTeX-polyglossia.tex']
84 overwrite = (outputdir == inputdir)
86 (base, ext) = os.path.splitext(f)
87 texfile = os.path.join(inputdir, f)
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)
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)
98 errorstring = proc.stderr.read()
99 if not errorstring is None:
104 lyxfile1 = getlyxinput(lyx,
105 os.path.join(inputdir, base + ".lyx.lyx"),
106 os.path.join(outputdir, base + ".lyx1.lyx") , uselyx2lyx)
110 lyxfile2 = getlyxinput(lyx,
111 os.path.join(outputdir, base + ".lyx"),
112 os.path.join(outputdir, base + ".lyx2.lyx"), uselyx2lyx)
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()
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)
132 error('Converting the following files failed: %s' % ', '.join(errors))
134 def getlyxinput(lyx, lyxfx, lyxf, uselyx2lyx):
136 cmd = '%s -E lyx %s %s' % (lyx, lyxf, lyxfx)
137 sys.stdout.writelines(cmd)
138 if os.system(cmd) != 0:
144 if __name__ == "__main__":