]> git.lyx.org Git - features.git/blob - lib/scripts/lyx_batch.pl.in
482924897940241217603eeebe95520daa902525
[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 use File::Slurp qw(read_dir);
11
12 sub checkPrecondition();
13 sub system1(@);
14 sub addFiles($$$);
15 sub mycompare($$$$);
16
17 my $builddir = "@CMAKE_BINARY_DIR@";
18 my $userdir = "$builddir/Testing/.lyxbatch";
19 my $workdir = "$builddir/autotests/out-home";
20
21 my $vsuffix = "@PROGRAM_SUFFIX@";
22 my $lyx_exe = "$builddir/bin/lyx$vsuffix";
23 my $git_exe = "@LYX_GITVERSION@";
24 my $qt_version = "@LYX_USE_QT@";
25
26 my $lyxsource = "@LYX_ABS_TOP_SRCDIR@";
27 my $data = "$lyxsource/development/batchtests";
28 my $test_bin = "$lyxsource/development/batchtests/bin";
29 my $comparepdf = "@COMPAREPDF_EXECUTABLE@";
30 my $perl = "@PERL_EXECUTABLE@";
31
32 # src_files := Files to be copied from lyx-source to build-dir
33 # check     := List of pairs of files to check
34 #                  created file (in build-dir)
35 #                  expected file (in source dir, to be compared with the created one)
36 # check_type: Type of check to perform, can be either:
37 #                  * text (default) - compares using File::Compare (i.e. full comparison)
38 #                  * pdf - compares using comparepdf (must be installed)
39 #                  * custom - compares using a custom script
40 # check_script: For check_type==custom, this is the script that's executed to do the comparison
41 # commands  := List of commands (lyx-functions) to be executed by lyx in a batch
42 # precondition: system commands to be executed prior to the test
43 # command_line: List of parameters to be used on the lyx-command-line
44 my %Tests = (
45   save_as_test => {
46     src_files => ["save_as.lyx"],
47     check => [["save_as_saved.lyx"],
48               ["save_as_saved2.lyx"]],
49     commands => ["file-open beamer_test.lyx",
50                  "buffer-write-as save_as_saved.lyx",
51                  "buffer-reload dump",
52                  "buffer-write-as save_as_saved2.lyx",
53                  "lyx-quit"],
54   },
55   beamer_test => {
56     src_files => ["beamer_test.lyx"],
57     check => [["beamer_test.tex", "beamer_test.tex.orig"]],
58     commands => ["file-open beamer_test.lyx",
59                  "buffer-begin",
60                  "repeat 150 outline-down",
61                  "repeat 150 outline-up",
62                  "buffer-export pdflatex",
63                  "buffer-reload dump",
64                  "lyx-quit"],
65   },
66   vcs_info_export => {
67     precondition => {
68       command => [$git_exe, "ls-files", "--error-unmatch", "vcs_info_export.lyx"],
69       workdir => "$data",
70     },
71     src_files => ["vcs_info_export.lyx"],
72     check => [["vcs_info_export.tex", "vcs_info_export.tex.orig"]],
73     command_line => ["-E", "pdflatex", "vcs_info_export.tex", "$data/vcs_info_export.lyx"],
74   },
75   "ams-import" => {
76     src_files => ["ams-import.tex"],
77     check_type => 'pdf',
78     check => [["ams-import.pdf", "ams-import.pdf"],
79               ["ams-import.lyx"]],
80     commands => ["buffer-new",
81                  "buffer-import latex ams-import.tex",
82                  "buffer-write",
83                  "buffer-export pdf2",
84                  "lyx-quit"],
85   },
86   "compare_test" => {
87       src_files => ["old.lyx", "new.lyx"],
88       check_type => 'custom',
89       check_script => ["$perl","$test_bin/compare_custom.pl"],
90       test_dir => "$lyxsource/development/batchtests/compare_tests/",
91       check => [["diffs.lyx", "diffs.expected.lyx"]],
92       commands => [
93           "dialog-show compare run-blocking $workdir/old.lyx $workdir/new.lyx",
94           "buffer-write-as $workdir/diffs.lyx",
95           "lyx-quit"
96       ],
97   },
98 );
99
100 die("Expected argument missing") if (! defined($ARGV[0]));
101 my $test = $ARGV[0];
102 die("Invalid argument") if (! defined($Tests{$test}));
103
104 if (! -e $userdir) {
105   mkdir($userdir);
106 }
107
108 $ENV{LANG} = "en";
109 $ENV{LC_ALL} = "C";
110 $ENV{LANGUAGE} = "en_US";
111
112 if (defined $Tests{$test}->{test_dir}) {
113   my @dirs = read_dir($Tests{$test}->{test_dir}, prefix => 1);
114   foreach my $dir (@dirs) {
115     next unless -d $dir;
116     print "--- Running tests in $dir\n";
117     run_tests($dir);
118   }
119 }
120 else {
121   run_tests($data);
122 }
123
124 exit(0);
125
126
127 sub run_tests {
128   my $test_dir = shift;
129
130   my @expected = &addFiles($test_dir, $Tests{$test}->{check},1);
131
132   my @created = &addFiles($workdir, $Tests{$test}->{check}, 0);
133
134   # Copy src-files to work with
135   for my $f (@{$Tests{$test}->{src_files}}) {
136     copy("$test_dir/$f", "$workdir/$f") or die("Copy failed: $!");
137   }
138   print "Unlinking " . join(' ', @created) . "\n";
139   unlink(@created);
140
141   &checkPrecondition();
142   chdir($workdir);
143   my @command = ($lyx_exe, "-userdir", $userdir);
144   if (defined($Tests{$test}->{command_line})) {
145     push(@command, @{$Tests{$test}->{command_line}});
146   }
147   if (defined($Tests{$test}->{commands}->[0])) {
148     if ($qt_version eq "QT5") {
149       push(@command, "-platform", "offscreen");
150     }
151     if (defined($Tests{$test}->{commands}->[1])) { # more than one command
152       push(@command, "-x", "command-sequence " . join(';', @{$Tests{$test}->{commands}}));
153     }
154     else {
155       push(@command, "-x", $Tests{$test}->{commands}->[0]);
156     }
157   }
158
159   &system1(@command);
160
161   for (my $i = 0; defined($created[$i]); $i++) {
162     die("File \"$created[$i]\" not created") if (! -e "$created[$i]");
163
164     if (defined($expected[$i])) {
165       my $res = mycompare($Tests{$test}->{check_type}, $expected[$i], $created[$i], $Tests{$test}->{check_script});
166
167       die("Expected ($expected[$i]) and created ($created[$i]) files differ") if $res != 0;
168     }
169   }
170 }
171
172 sub checkPrecondition()
173 {
174   return if (! defined($Tests{$test}->{precondition}));
175   my $rPrecond = $Tests{$test}->{precondition};
176   my @command = @{$rPrecond->{command}};
177   if (defined($rPrecond->{workdir})) {
178     chdir($rPrecond->{workdir});
179   }
180   my $result = &system1(@command);
181   print "Pre-condition result = $result\n";
182   die("Pre-condition error") if ($result != 0);
183 }
184
185 sub system1(@)
186 {
187   my ($exe, @params) = @_;
188   print "Executing:\n\t$exe '" . join("' '", @params) . "'\n";
189   return(system($exe, @params));
190 }
191
192 # Create a list of file paths
193 # dir: result-dir
194 # rBases: List of base-names
195 sub addFiles($$$)
196 {
197   my ($tdir, $rrBases, $idx) = @_;
198   my $dir;
199   if (defined($tdir)) {
200     $dir = "$tdir/";
201   }
202   else {
203     $dir = "";
204   }
205   my @result = ();
206   for my $rf (@{$rrBases}) {
207     my $path = undef;
208     if (defined($rf) && defined($rf->[$idx])) {
209       $path = "$dir$rf->[$idx]";
210     }
211     push(@result, $path);
212   }
213   return(@result);
214 }
215
216 sub mycompare($$$$)
217 {
218   my ($check_type, $expected, $created, $check_script) = @_;
219   my $result;
220
221   $check_type //= 'text';
222
223 if ($check_type eq 'pdf') {
224     my $cmd = $comparepdf;
225
226     if ($cmd =~ /NOTFOUND/) {
227       # no check is done due to missing executable
228     }
229     else {
230       my @params = (
231         "-ca", "-v=1", $expected, $created
232       );
233
234       my $error = "";
235       if (&system1($cmd, @params) != 0) {
236         if ($? == -1) {
237           $error = sprintf("failed to execute: $cmd");
238         }
239         elsif ($? & 127) {
240           $error = sprintf("$cmd with signal %d, %s coredump",
241                            ($? & 127),  ($? & 128) ? 'with' : 'without');
242         }
243         else {
244           $error = sprintf("child $cmd exited with value %d", $? >> 8);
245         }
246       }
247       die($error) if ($error ne "");
248     }
249     $result = 0;
250   }
251   elsif ($check_type eq 'custom') {
252     $result = system1(@$check_script, $expected, $created);
253   }
254   elsif ($check_type eq 'text') {
255     # defaut text comparision
256     $result = compare($created, $expected);
257   }
258   else {
259     die "Unknown check type: $check_type";
260   }
261   return($result);
262 }