]> git.lyx.org Git - lyx.git/blob - src/tex2lyx/test/runtests.py
Do not call tex2lyx in roundtrip mode for tests
[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 [<tex2lyx binary> [[<script dir>] [[<output dir>] [testfile]]]]" % prog_name
22
23
24 def main(argv):
25     # Parse and manipulate the command line arguments.
26     if len(argv) >= 3:
27         sys.path.append(os.path.join(sys.argv[2]))
28     else:
29         sys.path.append(os.path.join(os.path.dirname(sys.argv[0]), '../../../lib/scripts'))
30
31     from lyxpreview_tools import error
32
33     if len(argv) < 2:
34         tex2lyx = './tex2lyx'
35     elif len(argv) <= 5:
36         tex2lyx = argv[1]
37     else:
38         error(usage(argv[0]))
39
40     lyx = os.path.join(os.path.dirname(tex2lyx), "lyx")
41     inputdir = os.path.dirname(argv[0])
42     if len(argv) >= 4:
43         outputdir = sys.argv[3]
44     else:
45 #        outputdir = inputdir
46         outputdir = os.path.join(os.path.dirname(tex2lyx), "test")
47
48     if len(argv) >= 5:
49         files = [sys.argv[4]]
50     else:
51         files = ['test.ltx', 'test-structure.tex', 'test-insets.tex', \
52              'test-modules.tex', 'box-color-size-space-align.tex', \
53              'CJK.tex', 'XeTeX-polyglossia.tex']
54
55     errors = []
56     overwrite = (outputdir == inputdir)
57     for f in files:
58         (base, ext) = os.path.splitext(f)
59         texfile = os.path.join(inputdir, f)
60         if overwrite:
61             # we are updating the test references, so use roundtrip to allow
62             # for checking the LyX export as well.
63             cmd = '%s -roundtrip -f %s' % (tex2lyx, texfile)
64         else:
65             lyxfile = os.path.join(outputdir, base + ".lyx")
66             cmd = '%s -copyfiles -f %s %s' % (tex2lyx, texfile, lyxfile)
67         proc = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
68         proc.wait()
69         err = proc.returncode
70         errorstring = proc.stderr.read()
71         if not errorstring is None:
72             print errorstring
73         if err != 0:
74             errors.append(f)
75         elif not overwrite:
76             lyxfile1 = os.path.join(inputdir, base + ".lyx.lyx")
77             lyxfile2 = os.path.join(outputdir, base + ".lyx")
78             if not filecmp.cmp(lyxfile1, lyxfile2, False):
79                 t1 = time.ctime(os.path.getmtime(lyxfile1))
80                 t2 = time.ctime(os.path.getmtime(lyxfile2))
81                 f1 = open(lyxfile1, 'r')
82                 f2 = open(lyxfile2, 'r')
83                 lines1 = f1.readlines()
84                 lines2 = f2.readlines()
85                 diff = difflib.unified_diff(lines1, lines2, lyxfile1, lyxfile2, t1, t2)
86                 f1.close()
87                 f2.close()
88                 sys.stdout.writelines(diff)
89                 errors.append(f)
90
91     if len(errors) > 0:
92         error('Converting the following files failed: %s' % ', '.join(errors))
93
94 if __name__ == "__main__":
95     main(sys.argv)
96