]> git.lyx.org Git - lyx.git/blob - lib/scripts/lyx_batch.pl.in
Remove profiling.py
[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_type => 'custom',
58     check_script => ["$perl","$test_bin/compare_exported_tex.pl"],
59     check => [["beamer_test.tex", "beamer_test.tex.orig"]],
60     commands => ["file-open beamer_test.lyx",
61                  "buffer-begin",
62                  "repeat 150 outline-down",
63                  "repeat 150 outline-up",
64                  "buffer-export pdflatex",
65                  "buffer-reload dump",
66                  "lyx-quit"],
67   },
68   vcs_info_export => {
69     precondition => {
70       command => [$git_exe, "ls-files", "--error-unmatch", "vcs_info_export.lyx"],
71       workdir => "$data",
72     },
73     src_files => ["vcs_info_export.lyx"],
74     check_type => 'custom',
75     check_script => ["$perl","$test_bin/compare_exported_tex.pl"],
76     check => [["vcs_info_export.tex", "vcs_info_export.tex.orig"]],
77     command_line => ["-E", "pdflatex", "vcs_info_export.tex", "$data/vcs_info_export.lyx"],
78   },
79   "ams-import" => {
80     src_files => ["ams-import.tex"],
81     check_type => 'pdf',
82     check => [["ams-import.pdf", "ams-import.pdf"],
83               ["ams-import.lyx"]],
84     commands => ["buffer-new",
85                  "buffer-import latex ams-import.tex",
86                  "buffer-write",
87                  "buffer-export pdf2",
88                  "lyx-quit"],
89   },
90   "compare_test" => {
91       src_files => ["old.lyx", "new.lyx"],
92       check_type => 'custom',
93       check_script => ["$perl","$test_bin/compare_custom.pl"],
94       test_dir => "$lyxsource/development/batchtests/compare_tests/",
95       check => [["diffs.lyx", "diffs.expected.lyx"]],
96       commands => [
97           "dialog-show compare run-blocking $workdir/old.lyx $workdir/new.lyx",
98           "buffer-write-as $workdir/diffs.lyx",
99           "lyx-quit"
100       ],
101   },
102 );
103
104 die("Expected argument missing") if (! defined($ARGV[0]));
105 my $test = $ARGV[0];
106 die("Invalid argument") if (! defined($Tests{$test}));
107
108 if (! -e $userdir) {
109   mkdir($userdir);
110 }
111
112 $ENV{LANG} = "en";
113 $ENV{LC_ALL} = "C";
114 $ENV{LANGUAGE} = "en_US";
115
116 if (defined $Tests{$test}->{test_dir}) {
117   my @dirs = read_dir($Tests{$test}->{test_dir}, prefix => 1);
118   foreach my $dir (@dirs) {
119     next unless -d $dir;
120     print "--- Running tests in $dir\n";
121     run_tests($dir);
122   }
123 }
124 else {
125   run_tests($data);
126 }
127
128 exit(0);
129
130
131 sub run_tests {
132   my $test_dir = shift;
133
134   if (-e "$test_dir/skip.test") {
135     my $skip_msg = read_file("$test_dir/skip.test");
136     print "TEST SKIPPED.\n$skip_msg\n";
137     return;
138   }
139
140   my @expected = &addFiles($test_dir, $Tests{$test}->{check},1);
141
142   my @created = &addFiles($workdir, $Tests{$test}->{check}, 0);
143
144   # Copy src-files to work with
145   for my $f (@{$Tests{$test}->{src_files}}) {
146     copy("$test_dir/$f", "$workdir/$f") or die("Copy failed: $!");
147   }
148   print "Unlinking " . join(' ', @created) . "\n";
149   unlink(@created);
150
151   &checkPrecondition();
152   chdir($workdir);
153   my @command = ($lyx_exe, "-userdir", $userdir);
154   if (defined($Tests{$test}->{command_line})) {
155     push(@command, @{$Tests{$test}->{command_line}});
156   }
157   if (defined($Tests{$test}->{commands}->[0])) {
158     if ($qt_version eq "QT5") {
159       push(@command, "-platform", "offscreen");
160     }
161     if (defined($Tests{$test}->{commands}->[1])) { # more than one command
162       push(@command, "-x", "command-sequence " . join(';', @{$Tests{$test}->{commands}}));
163     }
164     else {
165       push(@command, "-x", $Tests{$test}->{commands}->[0]);
166     }
167   }
168
169   &system1(@command);
170
171   for (my $i = 0; defined($created[$i]); $i++) {
172     die("File \"$created[$i]\" not created") if (! -e "$created[$i]");
173
174     if (defined($expected[$i])) {
175       my $res = mycompare($Tests{$test}->{check_type}, $expected[$i], $created[$i], $Tests{$test}->{check_script});
176
177       die("Expected ($expected[$i]) and created ($created[$i]) files differ") if $res != 0;
178     }
179   }
180 }
181
182 sub checkPrecondition()
183 {
184   return if (! defined($Tests{$test}->{precondition}));
185   my $rPrecond = $Tests{$test}->{precondition};
186   my @command = @{$rPrecond->{command}};
187   if (defined($rPrecond->{workdir})) {
188     chdir($rPrecond->{workdir});
189   }
190   my $result = &system1(@command);
191   print "Pre-condition result = $result\n";
192   die("Pre-condition error") if ($result != 0);
193 }
194
195 sub system1(@)
196 {
197   my ($exe, @params) = @_;
198   print "Executing:\n\t$exe '" . join("' '", @params) . "'\n";
199   return(system($exe, @params));
200 }
201
202 # Create a list of file paths
203 # dir: result-dir
204 # rBases: List of base-names
205 sub addFiles($$$)
206 {
207   my ($tdir, $rrBases, $idx) = @_;
208   my $dir;
209   if (defined($tdir)) {
210     $dir = "$tdir/";
211   }
212   else {
213     $dir = "";
214   }
215   my @result = ();
216   for my $rf (@{$rrBases}) {
217     my $path = undef;
218     if (defined($rf) && defined($rf->[$idx])) {
219       $path = "$dir$rf->[$idx]";
220     }
221     push(@result, $path);
222   }
223   return(@result);
224 }
225
226 sub mycompare($$$$)
227 {
228   my ($check_type, $expected, $created, $check_script) = @_;
229   my $result;
230
231   $check_type //= 'text';
232
233 if ($check_type eq 'pdf') {
234     my $cmd = $comparepdf;
235
236     if ($cmd =~ /NOTFOUND/) {
237       # no check is done due to missing executable
238     }
239     else {
240       my @params = (
241         "-ca", "-v=1", $expected, $created
242       );
243
244       my $error = "";
245       if (&system1($cmd, @params) != 0) {
246         if ($? == -1) {
247           $error = sprintf("failed to execute: $cmd");
248         }
249         elsif ($? & 127) {
250           $error = sprintf("$cmd with signal %d, %s coredump",
251                            ($? & 127),  ($? & 128) ? 'with' : 'without');
252         }
253         else {
254           $error = sprintf("child $cmd exited with value %d", $? >> 8);
255         }
256       }
257       die($error) if ($error ne "");
258     }
259     $result = 0;
260   }
261   elsif ($check_type eq 'custom') {
262     $result = system1(@$check_script, $expected, $created);
263   }
264   elsif ($check_type eq 'text') {
265     # defaut text comparision
266     $result = compare($created, $expected);
267   }
268   else {
269     die "Unknown check type: $check_type";
270   }
271   return($result);
272 }