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_fl1 = re.compile(r'^#LyX file created by tex2lyx .*$')
24 pat_fl2 = re.compile(r'^#LyX \d+\.\d+ created this file.*$')
26 def compareLyx(lines1, lines2):
27 if lines1[1:] != lines2[1:]:
29 if not pat_fl1.match(lines1[0]) and not pat_fl2.match(lines1[0]):
31 if not pat_fl1.match(lines2[0]) and not pat_fl2.match(lines2[0]):
36 # Parse and manipulate the command line arguments.
40 if argv[1] == "uselyx2lyx":
43 if len(argv) >= 3+skipcount:
44 sys.path.append(os.path.join(sys.argv[2+skipcount]))
46 sys.path.append(os.path.join(os.path.dirname(sys.argv[0]), '../../../lib/scripts'))
48 from lyxpreview_tools import error
50 if len(argv) < 2+skipcount:
52 elif len(argv) <= 5+skipcount:
53 tex2lyx = argv[1+skipcount]
57 suffixre = re.search(r'\d+\.\d+$', tex2lyx)
59 suffix = suffixre.group()
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]
67 # outputdir = inputdir
68 outputdir = os.path.join(os.path.dirname(tex2lyx), "test")
70 if len(argv) >= 5+skipcount:
71 files = [sys.argv[4+skipcount]]
73 files = ['test.ltx', \
74 'box-color-size-space-align.tex', \
80 'test-refstyle-theorems.tex', \
82 'test-structure.tex', \
84 'XeTeX-polyglossia.tex', \
88 overwrite = (outputdir == inputdir)
90 (base, ext) = os.path.splitext(f)
91 texfile = os.path.join(inputdir, f)
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)
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)
102 err = proc.returncode
103 errorstring = proc.stderr.read()
104 if not errorstring is None:
109 lyxfile1 = getlyxinput(lyx,
110 os.path.join(inputdir, base + ".lyx.lyx"),
111 os.path.join(outputdir, base + ".lyx1.lyx") , uselyx2lyx)
115 lyxfile2 = getlyxinput(lyx,
116 os.path.join(outputdir, base + ".lyx"),
117 os.path.join(outputdir, base + ".lyx2.lyx"), uselyx2lyx)
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 lines2 = f2.readlines()
129 # ignore the first line e.g. the version of lyx
130 if not compareLyx(lines1, lines2):
131 diff = difflib.unified_diff(lines1, lines2, lyxfile1, lyxfile2, t1, t2)
132 sys.stdout.writelines(diff)
137 error('Converting the following files failed: %s' % ', '.join(errors))
139 def getlyxinput(lyx, lyxfx, lyxf, uselyx2lyx):
141 cmd = '%s -E lyx %s %s' % (lyx, lyxf, lyxfx)
142 sys.stdout.writelines(cmd)
143 sys.stdout.writelines("\n")
144 if os.system(cmd) != 0:
150 if __name__ == "__main__":