]> git.lyx.org Git - lyx.git/blob - lib/scripts/lyx_batch.pl.in
Update RELEASE NOTES
[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
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   save_as_test => {
39     src_files => ["save_as.lyx"],
40     check => [["save_as_saved.lyx"],
41               ["save_as_saved2.lyx"]],
42     commands => ["file-open beamer_test.lyx",
43                  "buffer-write-as save_as_saved.lyx",
44                  "buffer-reload dump",
45                  "buffer-write-as save_as_saved2.lyx",
46                  "lyx-quit"],
47   },
48   beamer_test => {
49     src_files => ["beamer_test.lyx"],
50     check => [["beamer_test.tex", "beamer_test.tex.orig"]],
51     commands => ["file-open beamer_test.lyx",
52                  "buffer-begin",
53                  "repeat 150 outline-down",
54                  "repeat 150 outline-up",
55                  "buffer-export pdflatex",
56                  "buffer-reload dump",
57                  "lyx-quit"],
58   },
59   vcs_info_export => {
60     precondition => {
61       command => [$git_exe, "ls-files", "--error-unmatch", "vcs_info_export.lyx"],
62       workdir => "$data",
63     },
64     src_files => ["vcs_info_export.lyx"],
65     check => [["vcs_info_export.tex", "vcs_info_export.tex.orig"]],
66     command_line => ["-E", "pdflatex", "vcs_info_export.tex", "$data/vcs_info_export.lyx"],
67   },
68   "ams-import" => {
69     src_files => ["ams-import.tex"],
70     check => [["ams-import.pdf", "ams-import.pdf", $comparepdf],
71               ["ams-import.lyx"]],
72     commands => ["buffer-new",
73                  "buffer-import latex ams-import.tex",
74                  "buffer-write",
75                  "buffer-export pdf2",
76                  "lyx-quit"],
77   },
78 );
79
80 die("Expected argument missing") if (! defined($ARGV[0]));
81 my $test = $ARGV[0];
82 die("Invalid argument") if (! defined($Tests{$test}));
83
84 if (! -e $userdir) {
85   mkdir($userdir);
86 }
87 my @expected = &addFiles($data, $Tests{$test}->{check},1);
88
89 my @created = &addFiles($workdir, $Tests{$test}->{check}, 0);
90
91 my @comparecommand = &addFiles(undef, $Tests{$test}->{check}, 2);
92
93 # Copy src-files to work with
94 for my $f (@{$Tests{$test}->{src_files}}) {
95   copy("$data/$f", "$workdir/$f") or die("Copy failed: $!");
96 }
97 print "Unlinking " . join(' ', @created) . "\n";
98 unlink(@created);
99
100 $ENV{LANG} = "en";
101 $ENV{LC_ALL} = "C";
102 $ENV{LANGUAGE} = "en_US";
103
104 &checkPrecondition();
105 chdir($workdir);
106 my @command = ($lyx_exe, "-userdir", $userdir);
107 if (defined($Tests{$test}->{command_line})) {
108   push(@command, @{$Tests{$test}->{command_line}});
109 }
110 if (defined($Tests{$test}->{commands}->[0])) {
111   if ($qt_version eq "QT5") {
112     push(@command, "-platform", "offscreen");
113   }
114   if (defined($Tests{$test}->{commands}->[1])) { # more than one command
115     push(@command, "-x", "command-sequence " . join(';', @{$Tests{$test}->{commands}}));
116   }
117   else {
118     push(@command, "-x", $Tests{$test}->{commands}->[0]);
119   }
120 }
121
122 &system1(@command);
123
124 for (my $i = 0; defined($created[$i]); $i++) {
125   die("File \"$created[$i]\" not created") if (! -e "$created[$i]");
126 if (defined($expected[$i])) {
127   die("Expected ($expected[$i]) and created ($created[$i]) files differ") if (&mycompare($comparecommand[$i], $expected[$i], $created[$i]) != 0);
128   }
129 }
130 exit(0);
131
132 sub checkPrecondition()
133 {
134   return if (! defined($Tests{$test}->{precondition}));
135   my $rPrecond = $Tests{$test}->{precondition};
136   my @command = @{$rPrecond->{command}};
137   if (defined($rPrecond->{workdir})) {
138     chdir($rPrecond->{workdir});
139   }
140   my $result = &system1(@command);
141   print "Pre-condition result = $result\n";
142   die("Pre-condition error") if ($result != 0);
143 }
144
145 sub system1(@)
146 {
147   my ($exe, @params) = @_;
148   print "Executing:\n\t$exe '" . join("' '", @params) . "'\n";
149   return(system($exe, @params));
150 }
151
152 # Create a list of file paths
153 # dir: result-dir
154 # rBases: List of base-names
155 sub addFiles($$$)
156 {
157   my ($tdir, $rrBases, $idx) = @_;
158   my $dir;
159   if (defined($tdir)) {
160     $dir = "$tdir/";
161   }
162   else {
163     $dir = "";
164   }
165   my @result = ();
166   for my $rf (@{$rrBases}) {
167     my $path = undef;
168     if (defined($rf) && defined($rf->[$idx])) {
169       $path = "$dir$rf->[$idx]";
170     }
171     push(@result, $path);
172   }
173   return(@result);
174 }
175
176 sub mycompare($$$)
177 {
178   my ($cmd, $expected, $created) = @_;
179   my $result;
180   if (defined($cmd)) {          # system command desired
181     my @params = ();
182     if ($cmd =~ /NOTFOUND/) {
183       # no check is done due to missing executable
184     }
185     else {
186       if ($cmd =~ /comparepdf/) {
187         push(@params, "-ca", "-v=1", $expected, $created);
188       }
189       else {
190         die("unknown system command $cmd");
191       }
192       my $error = "";
193       if (&system1($cmd, @params) != 0) {
194         if ($? == -1) {
195           $error = sprintf("failed to execute: $cmd");
196         }
197         elsif ($? & 127) {
198           $error = sprintf("$cmd with signal %d, %s coredump",
199                            ($? & 127),  ($? & 128) ? 'with' : 'without');
200         }
201         else {
202           $error = sprintf("child $cmd exited with value %d", $? >> 8);
203         }
204       }
205       die($error) if ($error ne "");
206     }
207     $result = 0;
208   }
209   else {
210     # defaut text comparision
211     $result = compare($created, $expected);
212   }
213   return($result);
214 }