]> git.lyx.org Git - lyx.git/blob - lib/scripts/lyx_batch.pl.in
Create Chapter 6 Bullets in Additional.lyx and move the bullet section into it; this...
[lyx.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 read_file);
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   if (-e "$test_dir/skip.test") {
131     my $skip_msg = read_file("$test_dir/skip.test");
132     print "TEST SKIPPED.\n$skip_msg\n";
133     return;
134   }
135
136   my @expected = &addFiles($test_dir, $Tests{$test}->{check},1);
137
138   my @created = &addFiles($workdir, $Tests{$test}->{check}, 0);
139
140   # Copy src-files to work with
141   for my $f (@{$Tests{$test}->{src_files}}) {
142     copy("$test_dir/$f", "$workdir/$f") or die("Copy failed: $!");
143   }
144   print "Unlinking " . join(' ', @created) . "\n";
145   unlink(@created);
146
147   &checkPrecondition();
148   chdir($workdir);
149   my @command = ($lyx_exe, "-userdir", $userdir);
150   if (defined($Tests{$test}->{command_line})) {
151     push(@command, @{$Tests{$test}->{command_line}});
152   }
153   if (defined($Tests{$test}->{commands}->[0])) {
154     if ($qt_version eq "QT5") {
155       push(@command, "-platform", "offscreen");
156     }
157     if (defined($Tests{$test}->{commands}->[1])) { # more than one command
158       push(@command, "-x", "command-sequence " . join(';', @{$Tests{$test}->{commands}}));
159     }
160     else {
161       push(@command, "-x", $Tests{$test}->{commands}->[0]);
162     }
163   }
164
165   &system1(@command);
166
167   for (my $i = 0; defined($created[$i]); $i++) {
168     die("File \"$created[$i]\" not created") if (! -e "$created[$i]");
169
170     if (defined($expected[$i])) {
171       my $res = mycompare($Tests{$test}->{check_type}, $expected[$i], $created[$i], $Tests{$test}->{check_script});
172
173       die("Expected ($expected[$i]) and created ($created[$i]) files differ") if $res != 0;
174     }
175   }
176 }
177
178 sub checkPrecondition()
179 {
180   return if (! defined($Tests{$test}->{precondition}));
181   my $rPrecond = $Tests{$test}->{precondition};
182   my @command = @{$rPrecond->{command}};
183   if (defined($rPrecond->{workdir})) {
184     chdir($rPrecond->{workdir});
185   }
186   my $result = &system1(@command);
187   print "Pre-condition result = $result\n";
188   die("Pre-condition error") if ($result != 0);
189 }
190
191 sub system1(@)
192 {
193   my ($exe, @params) = @_;
194   print "Executing:\n\t$exe '" . join("' '", @params) . "'\n";
195   return(system($exe, @params));
196 }
197
198 # Create a list of file paths
199 # dir: result-dir
200 # rBases: List of base-names
201 sub addFiles($$$)
202 {
203   my ($tdir, $rrBases, $idx) = @_;
204   my $dir;
205   if (defined($tdir)) {
206     $dir = "$tdir/";
207   }
208   else {
209     $dir = "";
210   }
211   my @result = ();
212   for my $rf (@{$rrBases}) {
213     my $path = undef;
214     if (defined($rf) && defined($rf->[$idx])) {
215       $path = "$dir$rf->[$idx]";
216     }
217     push(@result, $path);
218   }
219   return(@result);
220 }
221
222 sub mycompare($$$$)
223 {
224   my ($check_type, $expected, $created, $check_script) = @_;
225   my $result;
226
227   $check_type //= 'text';
228
229 if ($check_type eq 'pdf') {
230     my $cmd = $comparepdf;
231
232     if ($cmd =~ /NOTFOUND/) {
233       # no check is done due to missing executable
234     }
235     else {
236       my @params = (
237         "-ca", "-v=1", $expected, $created
238       );
239
240       my $error = "";
241       if (&system1($cmd, @params) != 0) {
242         if ($? == -1) {
243           $error = sprintf("failed to execute: $cmd");
244         }
245         elsif ($? & 127) {
246           $error = sprintf("$cmd with signal %d, %s coredump",
247                            ($? & 127),  ($? & 128) ? 'with' : 'without');
248         }
249         else {
250           $error = sprintf("child $cmd exited with value %d", $? >> 8);
251         }
252       }
253       die($error) if ($error ne "");
254     }
255     $result = 0;
256   }
257   elsif ($check_type eq 'custom') {
258     $result = system1(@$check_script, $expected, $created);
259   }
260   elsif ($check_type eq 'text') {
261     # defaut text comparision
262     $result = compare($created, $expected);
263   }
264   else {
265     die "Unknown check type: $check_type";
266   }
267   return($result);
268 }