]> git.lyx.org Git - features.git/blob - lib/scripts/lyx_batch.pl.in
Cmake tests: Added check to inspect created pdf in test "AMS-import"
[features.git] / lib / scripts / lyx_batch.pl.in
1 #! /usr/bin/env perl
2 # -*- mode: perl; -*-
3
4 # lyx_batch.pl testname
5
6 use strict;
7 use warnings;
8 use File::Copy;
9 use File::Compare;
10
11 sub checkPrecondition();
12 sub system1(@);
13 sub addFiles($$$);
14 sub mycompare($$$);
15
16 my $builddir = "@CMAKE_BINARY_DIR@";
17 my $userdir = "$builddir/Testing/.lyxbatch";
18 my $workdir = "$builddir/autotests/out-home";
19
20 my $vsuffix = "@PROGRAM_SUFFIX@";
21 my $lyx_exe = "$builddir/bin/lyx$vsuffix";
22 my $git_exe = "@LYX_GITVERSION@";
23 my $qt_version = "@LYX_USE_QT@";
24
25 my $lyxsource = "@LYX_ABS_TOP_SRCDIR@";
26 my $data = "$lyxsource/development/batchtests";
27 my $comparepdf = "@COMPAREPDF_EXECUTABLE@";
28
29 # src_files := Files to be copied from lyx-source to build-dir
30 # check     := List of tripples
31 #                  created file (in build-dir)
32 #                  expected file (in source dir, to be compared with the created one)
33 #                  eventually system command to compare files
34 # commands  := List of commands (lyx-functions) to be executed by lyx in a batch
35 # precondition: system commands to be executed prior to the test
36 # command_line: List of parameters to be used on the lyx-command-line
37 my %Tests = (
38   beamer_test => {
39     src_files => ["beamer_test.lyx"],
40     check => [["beamer_test.tex", "beamer_test.tex.orig"]],
41     commands => ["file-open beamer_test.lyx",
42                  "buffer-begin",
43                  "repeat 150 outline-down",
44                  "repeat 150 outline-up",
45                  "buffer-export pdflatex",
46                  "buffer-reload dump",
47                  "lyx-quit"],
48   },
49   vcs_info_export => {
50     precondition => {
51       command => [$git_exe, "ls-files", "--error-unmatch", "vcs_info_export.lyx"],
52       workdir => "$data",
53     },
54     src_files => ["vcs_info_export.lyx"],
55     check => [["vcs_info_export.tex", "vcs_info_export.tex.orig"]],
56     command_line => ["-E", "pdflatex", "vcs_info_export.tex", "$data/vcs_info_export.lyx"],
57   },
58   "ams-import" => {
59     src_files => ["ams-import.tex"],
60     check => [["ams-import.pdf", "ams-import.pdf", $comparepdf],
61               ["ams-import.lyx"]],
62     commands => ["buffer-new",
63                  "buffer-import latex ams-import.tex",
64                  "buffer-write",
65                  "buffer-export pdf2",
66                  "lyx-quit"],
67   },
68 );
69
70 die("Expected argument missing") if (! defined($ARGV[0]));
71 my $test = $ARGV[0];
72 die("Invalid argument") if (! defined($Tests{$test}));
73
74 if (! -e $userdir) {
75   mkdir($userdir);
76 }
77 my @expected = &addFiles($data, $Tests{$test}->{check},1);
78
79 my @created = &addFiles($workdir, $Tests{$test}->{check}, 0);
80
81 my @comparecommand = &addFiles(undef, $Tests{$test}->{check}, 2);
82
83 # Copy src-files to work with
84 for my $f (@{$Tests{$test}->{src_files}}) {
85   copy("$data/$f", "$workdir/$f") or die("Copy failed: $!");
86 }
87 print "Unlinking " . join(' ', @created) . "\n";
88 unlink(@created);
89
90 $ENV{LANG} = "en";
91 $ENV{LC_ALL} = "C";
92 $ENV{LANGUAGE} = "en_US";
93
94 &checkPrecondition();
95 chdir($workdir);
96 my @command = ($lyx_exe, "-userdir", $userdir);
97 if (defined($Tests{$test}->{command_line})) {
98   push(@command, @{$Tests{$test}->{command_line}});
99 }
100 if (defined($Tests{$test}->{commands}->[0])) {
101   if ($qt_version eq "QT5") {
102     push(@command, "-platform", "offscreen");
103   }
104   if (defined($Tests{$test}->{commands}->[1])) { # more than one command
105     push(@command, "-x", "command-sequence " . join(';', @{$Tests{$test}->{commands}}));
106   }
107   else {
108     push(@command, "-x", $Tests{$test}->{commands}->[0]);
109   }
110 }
111
112 &system1(@command);
113
114 for (my $i = 0; defined($created[$i]); $i++) {
115   die("File \"$created[$i]\" not created") if (! -e "$created[$i]");
116 if (defined($expected[$i])) {
117   die("Expected ($expected[$i]) and created ($created[$i]) files differ") if (&mycompare($comparecommand[$i], $expected[$i], $created[$i]) != 0);
118   }
119 }
120 exit(0);
121
122 sub checkPrecondition()
123 {
124   return if (! defined($Tests{$test}->{precondition}));
125   my $rPrecond = $Tests{$test}->{precondition};
126   my @command = @{$rPrecond->{command}};
127   if (defined($rPrecond->{workdir})) {
128     chdir($rPrecond->{workdir});
129   }
130   my $result = &system1(@command);
131   print "Pre-condition result = $result\n";
132   die("Pre-condition error") if ($result != 0);
133 }
134
135 sub system1(@)
136 {
137   my ($exe, @params) = @_;
138   print "Executing:\n\t$exe '" . join("' '", @params) . "'\n";
139   return(system($exe, @params));
140 }
141
142 # Create a list of file paths
143 # dir: result-dir
144 # rBases: List of base-names
145 sub addFiles($$$)
146 {
147   my ($tdir, $rrBases, $idx) = @_;
148   my $dir;
149   if (defined($tdir)) {
150     $dir = "$tdir/";
151   }
152   else {
153     $dir = "";
154   }
155   my @result = ();
156   for my $rf (@{$rrBases}) {
157     my $path = undef;
158     if (defined($rf) && defined($rf->[$idx])) {
159       $path = "$dir$rf->[$idx]";
160     }
161     push(@result, $path);
162   }
163   return(@result);
164 }
165
166 sub mycompare($$$)
167 {
168   my ($cmd, $expected, $created) = @_;
169   my $result;
170   if (defined($cmd)) {          # system command desired
171     my @params = ();
172     if ($cmd =~ /NOTFOUND/) {
173       # no check is done due to missing executable
174     }
175     else {
176       if ($cmd =~ /comparepdf/) {
177         push(@params, "-ca", "-v=1", $expected, $created);
178       }
179       else {
180         die("unknown system command $cmd");
181       }
182       my $error = "";
183       if (&system1($cmd, @params) != 0) {
184         if ($? == -1) {
185           $error = sprintf("failed to execute: $cmd");
186         }
187         elsif ($? & 127) {
188           $error = sprintf("$cmd with signal %d, %s coredump",
189                            ($? & 127),  ($? & 128) ? 'with' : 'without');
190         }
191         else {
192           $error = sprintf("child $cmd exited with value %d", $? >> 8);
193         }
194       }
195       die($error) if ($error ne "");
196     }
197     $result = 0;
198   }
199   else {
200     # defaut text comparision
201     $result = compare($created, $expected);
202   }
203   return($result);
204 }