4 # lyx_batch.pl testname
10 use File::Slurp qw(read_dir read_file);
12 sub checkPrecondition();
17 my $builddir = "@CMAKE_BINARY_DIR@";
18 my $userdir = "$builddir/Testing/.lyxbatch";
19 my $workdir = "$builddir/autotests/out-home";
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@";
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@";
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
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",
52 "buffer-write-as save_as_saved2.lyx",
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",
62 "repeat 150 outline-down",
63 "repeat 150 outline-up",
64 "buffer-export pdflatex",
70 command => [$git_exe, "ls-files", "--error-unmatch", "vcs_info_export.lyx"],
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"],
80 src_files => ["ams-import.tex"],
82 check => [["ams-import.pdf", "ams-import.pdf"],
84 commands => ["buffer-new",
85 "buffer-import latex ams-import.tex",
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"]],
97 "dialog-show compare run-blocking $workdir/old.lyx $workdir/new.lyx",
98 "buffer-write-as $workdir/diffs.lyx",
104 die("Expected argument missing") if (! defined($ARGV[0]));
106 die("Invalid argument") if (! defined($Tests{$test}));
114 $ENV{LANGUAGE} = "en_US";
116 if (defined $Tests{$test}->{test_dir}) {
117 my @dirs = read_dir($Tests{$test}->{test_dir}, prefix => 1);
118 foreach my $dir (@dirs) {
120 print "--- Running tests in $dir\n";
132 my $test_dir = shift;
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";
140 my @expected = &addFiles($test_dir, $Tests{$test}->{check},1);
142 my @created = &addFiles($workdir, $Tests{$test}->{check}, 0);
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: $!");
148 print "Unlinking " . join(' ', @created) . "\n";
151 &checkPrecondition();
153 my @command = ($lyx_exe, "-userdir", $userdir);
154 if (defined($Tests{$test}->{command_line})) {
155 push(@command, @{$Tests{$test}->{command_line}});
157 if (defined($Tests{$test}->{commands}->[0])) {
158 if ($qt_version eq "QT5") {
159 push(@command, "-platform", "offscreen");
161 if (defined($Tests{$test}->{commands}->[1])) { # more than one command
162 push(@command, "-x", "command-sequence " . join(';', @{$Tests{$test}->{commands}}));
165 push(@command, "-x", $Tests{$test}->{commands}->[0]);
171 for (my $i = 0; defined($created[$i]); $i++) {
172 die("File \"$created[$i]\" not created") if (! -e "$created[$i]");
174 if (defined($expected[$i])) {
175 my $res = mycompare($Tests{$test}->{check_type}, $expected[$i], $created[$i], $Tests{$test}->{check_script});
177 die("Expected ($expected[$i]) and created ($created[$i]) files differ") if $res != 0;
182 sub checkPrecondition()
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});
190 my $result = &system1(@command);
191 print "Pre-condition result = $result\n";
192 die("Pre-condition error") if ($result != 0);
197 my ($exe, @params) = @_;
198 print "Executing:\n\t$exe '" . join("' '", @params) . "'\n";
199 return(system($exe, @params));
202 # Create a list of file paths
204 # rBases: List of base-names
207 my ($tdir, $rrBases, $idx) = @_;
209 if (defined($tdir)) {
216 for my $rf (@{$rrBases}) {
218 if (defined($rf) && defined($rf->[$idx])) {
219 $path = "$dir$rf->[$idx]";
221 push(@result, $path);
228 my ($check_type, $expected, $created, $check_script) = @_;
231 $check_type //= 'text';
233 if ($check_type eq 'pdf') {
234 my $cmd = $comparepdf;
236 if ($cmd =~ /NOTFOUND/) {
237 # no check is done due to missing executable
241 "-ca", "-v=1", $expected, $created
245 if (&system1($cmd, @params) != 0) {
247 $error = sprintf("failed to execute: $cmd");
250 $error = sprintf("$cmd with signal %d, %s coredump",
251 ($? & 127), ($? & 128) ? 'with' : 'without');
254 $error = sprintf("child $cmd exited with value %d", $? >> 8);
257 die($error) if ($error ne "");
261 elsif ($check_type eq 'custom') {
262 $result = system1(@$check_script, $expected, $created);
264 elsif ($check_type eq 'text') {
265 # defaut text comparision
266 $result = compare($created, $expected);
269 die "Unknown check type: $check_type";