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 from __future__ import print_function
20 import os, string, sys, time, difflib, filecmp, subprocess, re
23 return "Usage: %s [uselyx2lyx] [<tex2lyx binary> [[<script dir>] [[<output dir>] [testfile]]]]" % prog_name
25 pat_fl1 = re.compile(r'^#LyX file created by tex2lyx .*$')
26 pat_fl2 = re.compile(r'^#LyX \d+\.\d+ created this file.*$')
28 def compareLyx(lines1, lines2):
29 if lines1[1:] != lines2[1:]:
31 if not pat_fl1.match(lines1[0]) and not pat_fl2.match(lines1[0]):
33 if not pat_fl1.match(lines2[0]) and not pat_fl2.match(lines2[0]):
38 # Parse and manipulate the command line arguments.
42 if argv[1] == "uselyx2lyx":
45 if len(argv) >= 3+skipcount:
46 sys.path.append(os.path.join(sys.argv[2+skipcount]))
48 sys.path.append(os.path.join(os.path.dirname(sys.argv[0]), '../../../lib/scripts'))
50 from lyxpreview_tools import error
52 if len(argv) < 2+skipcount:
54 elif len(argv) <= 5+skipcount:
55 tex2lyx = argv[1+skipcount]
59 suffixre = re.search(r'\d+\.\d+$', tex2lyx)
61 suffix = suffixre.group()
64 lyx = os.path.join(os.path.dirname(tex2lyx), "lyx" + suffix)
65 inputdir = os.path.realpath(os.path.dirname(argv[0]))
66 if len(argv) >= 4+skipcount:
67 outputdir = os.path.realpath(sys.argv[3+skipcount])
69 # outputdir = inputdir
70 outputdir = os.path.realpath(os.path.join(os.path.dirname(tex2lyx), "test"))
72 if len(argv) >= 5+skipcount:
73 files = [sys.argv[4+skipcount]]
75 files = ['test.ltx', \
77 'box-color-size-space-align.tex', \
81 'test-insets-basic.tex', \
84 'test-refstyle-theorems.tex', \
86 'test-structure.tex', \
88 'XeTeX-polyglossia.tex']
91 overwrite = (outputdir == inputdir)
93 (base, ext) = os.path.splitext(f)
94 texfile = os.path.join(inputdir, f)
96 # we are updating the test references, so use roundtrip to allow
97 # for checking the LyX export as well.
98 cmd = '%s -roundtrip -f %s' % (tex2lyx, texfile)
100 lyxfile = os.path.join(outputdir, base + ".lyx")
101 cmd = '%s -roundtrip -copyfiles -f %s %s' % (tex2lyx, texfile, lyxfile)
102 print('Executing: ' + cmd + "\n")
103 proc = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
105 err = proc.returncode
106 errorstring = proc.stderr.read()
107 if not errorstring is None:
112 lyxfile1 = getlyxinput(lyx,
113 os.path.join(inputdir, base + ".lyx.lyx"),
114 os.path.join(outputdir, base + ".lyx1.lyx") , uselyx2lyx)
118 lyxfile2 = getlyxinput(lyx,
119 os.path.join(outputdir, base + ".lyx"),
120 os.path.join(outputdir, base + ".lyx2.lyx"), uselyx2lyx)
124 t1 = time.ctime(os.path.getmtime(lyxfile1))
125 t2 = time.ctime(os.path.getmtime(lyxfile2))
126 f1 = open(lyxfile1, 'r')
127 f2 = open(lyxfile2, 'r')
128 lines1 = f1.readlines()
131 if linex[:-1] == '\origin ' + inputdir + '/':
132 lines1[i1] = '\origin ' + outputdir + '/' + "\n"
135 lines2 = f2.readlines()
138 # ignore the first line e.g. the version of lyx
139 if not compareLyx(lines1, lines2):
140 diff = difflib.unified_diff(lines1, lines2, lyxfile1, lyxfile2, t1, t2)
141 sys.stdout.writelines(diff)
146 error('Converting the following files failed: %s' % ', '.join(errors))
148 def getlyxinput(lyx, lyxfx, lyxf, uselyx2lyx):
150 cmd = '%s -E lyx %s %s' % (lyx, lyxf, lyxfx)
151 sys.stdout.writelines(cmd)
152 sys.stdout.writelines("\n")
153 if os.system(cmd) != 0:
159 if __name__ == "__main__":