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
25 # Parse and manipulate the command line arguments.
29 if argv[1] == "uselyx2lyx":
32 if len(argv) >= 3+skipcount:
33 sys.path.append(os.path.join(sys.argv[2+skipcount]))
35 sys.path.append(os.path.join(os.path.dirname(sys.argv[0]), '../../../lib/scripts'))
37 from lyxpreview_tools import error
39 if len(argv) < 2+skipcount:
41 elif len(argv) <= 5+skipcount:
42 tex2lyx = argv[1+skipcount]
46 lyx = os.path.join(os.path.dirname(tex2lyx), "lyx")
47 inputdir = os.path.dirname(argv[0])
48 if len(argv) >= 4+skipcount:
49 outputdir = sys.argv[3+skipcount]
51 # outputdir = inputdir
52 outputdir = os.path.join(os.path.dirname(tex2lyx), "test")
54 if len(argv) >= 5+skipcount:
55 files = [sys.argv[4+skipcount]]
57 files = ['test.ltx', 'test-structure.tex', 'test-insets.tex', \
58 'test-modules.tex', 'box-color-size-space-align.tex', \
59 'CJK.tex', 'CJKutf8.tex', 'XeTeX-polyglossia.tex']
62 overwrite = (outputdir == inputdir)
64 (base, ext) = os.path.splitext(f)
65 texfile = os.path.join(inputdir, f)
67 # we are updating the test references, so use roundtrip to allow
68 # for checking the LyX export as well.
69 cmd = '%s -roundtrip -f %s' % (tex2lyx, texfile)
71 lyxfile = os.path.join(outputdir, base + ".lyx")
72 cmd = '%s -roundtrip -copyfiles -f %s %s' % (tex2lyx, texfile, lyxfile)
73 proc = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
76 errorstring = proc.stderr.read()
77 if not errorstring is None:
82 lyxfile1 = getlyxinput(lyx,
83 os.path.join(inputdir, base + ".lyx.lyx"),
84 os.path.join(outputdir, base + ".lyx1.lyx") , uselyx2lyx)
88 lyxfile2 = getlyxinput(lyx,
89 os.path.join(outputdir, base + ".lyx"),
90 os.path.join(outputdir, base + ".lyx2.lyx"), uselyx2lyx)
93 elif not filecmp.cmp(lyxfile1, lyxfile2, False):
94 t1 = time.ctime(os.path.getmtime(lyxfile1))
95 t2 = time.ctime(os.path.getmtime(lyxfile2))
96 f1 = open(lyxfile1, 'r')
97 f2 = open(lyxfile2, 'r')
98 lines1 = f1.readlines()
99 lines2 = f2.readlines()
100 diff = difflib.unified_diff(lines1, lines2, lyxfile1, lyxfile2, t1, t2)
103 sys.stdout.writelines(diff)
107 error('Converting the following files failed: %s' % ', '.join(errors))
109 def getlyxinput(lyx, lyxfx, lyxf, uselyx2lyx):
111 cmd = '%s -E lyx %s %s' % (lyx, lyxf, lyxfx)
112 sys.stdout.writelines(cmd)
113 if os.system(cmd) != 0:
119 if __name__ == "__main__":