]> git.lyx.org Git - lyx.git/blob - src/tex2lyx/test/runtests.py
Add make target to update tex2lyx test references
[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
19
20
21 def usage(prog_name):
22   return "Usage: %s [<tex2lyx binary> [<script dir>] [<output dir>]]" % prog_name
23
24
25 def main(argv):
26     # Parse and manipulate the command line arguments.
27     if len(argv) >= 3:
28         sys.path.append(os.path.join(sys.argv[2]))
29     else:
30         sys.path.append(os.path.join(os.path.dirname(sys.argv[0]), '../../../lib/scripts'))
31
32     from lyxpreview_tools import error
33
34     if len(argv) < 2:
35         tex2lyx = './tex2lyx'
36     elif len(argv) <= 4:
37         tex2lyx = argv[1]
38     else:
39         error(usage(argv[0]))
40
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     files = ['test.ltx', 'test-structure.tex', 'test-insets.tex', \
49              'box-color-size-space-align.tex', 'CJK.tex', \
50              'XeTeX-polyglossia.tex']
51
52     errors = []
53     overwrite = (outputdir == inputdir)
54     for f in files:
55         (base, ext) = os.path.splitext(f)
56         texfile = os.path.join(inputdir, f)
57         if overwrite:
58             cmd = '%s -roundtrip -f %s' % (tex2lyx, texfile)
59         else:
60             lyxfile = os.path.join(outputdir, base + ".lyx")
61             cmd = '%s -roundtrip -copyfiles -f %s %s' % (tex2lyx, texfile, lyxfile)
62         if os.system(cmd) != 0:
63             errors.append(f)
64         elif not overwrite:
65             lyxfile1 = os.path.join(inputdir, base + ".lyx.lyx")
66             lyxfile2 = os.path.join(outputdir, base + ".lyx")
67             if not filecmp.cmp(lyxfile1, lyxfile2, False):
68                 t1 = time.ctime(os.path.getmtime(lyxfile1))
69                 t2 = time.ctime(os.path.getmtime(lyxfile2))
70                 f1 = open(lyxfile1, 'r')
71                 f2 = open(lyxfile2, 'r')
72                 lines1 = f1.readlines()
73                 lines2 = f2.readlines()
74                 diff = difflib.unified_diff(lines1, lines2, lyxfile1, lyxfile2, t1, t2)
75                 f1.close()
76                 f2.close()
77                 sys.stdout.writelines(diff)
78                 errors.append(f)
79
80     if len(errors) > 0:
81         error('Converting the following files failed: %s' % ', '.join(errors))
82
83 if __name__ == "__main__":
84     main(sys.argv)
85