]> git.lyx.org Git - lyx.git/blob - lib/scripts/lyx_batch.pl.in
Disable GUI for our batch tests if using QT5
[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 check_precondition();
12 sub system1(@);
13 sub add_files($$);
14
15 my $builddir = "@CMAKE_BINARY_DIR@";
16 my $userdir = "$builddir/Testing/.lyxbatch";
17 my $workdir = "$builddir/autotests/out-home";
18
19 my $vsuffix = "@PROGRAM_SUFFIX@";
20 my $lyx_exe = "$builddir/bin/lyx$vsuffix";
21 my $git_exe = "@LYX_GITVERSION@";
22 my $qt_version = "@LYX_USE_QT@";
23
24 my $lyxsource = "@LYX_ABS_TOP_SRCDIR@";
25 my $data = "$lyxsource/development/batchtests";
26
27 # src_files := Files to be copied from lyx-source to build-dir
28 # create := Files which are expected to be created in the build-dir
29 # original := Files in the lyx-source, corresponding to the created ones,
30 #             which we provide for comparison
31 # commands := List of commands (lyx-functions) to be executed by lyx in a batch
32 # precondition: system commands to be executed prior to the test
33 # command_line: List of parameters to be used on the lyx-command-line
34 my %Tests = (
35   beamer_test => {
36     src_files => ["beamer_test.lyx"],
37     create => ["beamer_test.tex"],
38     original => ["beamer_test.tex.orig"],
39     commands => ["file-open beamer_test.lyx",
40                  "buffer-begin",
41                  "repeat 150 outline-down",
42                  "repeat 150 outline-up",
43                  "buffer-export pdflatex",
44                  "buffer-reload dump",
45                  "lyx-quit"],
46   },
47   vcs_info_export => {
48     precondition => {
49       command => [$git_exe, "ls-files", "--error-unmatch", "vcs_info_export.lyx"],
50       workdir => "$data",
51     },
52     src_files => ["vcs_info_export.lyx"],
53     create => ["vcs_info_export.tex"],
54     original => ["vcs_info_export.tex.orig"],
55     command_line => ["-E", "pdflatex", "vcs_info_export.tex", "$data/vcs_info_export.lyx"],
56   },
57   "ams-import" => {
58     src_files => ["ams-import.tex"],
59     create => ["ams-import.pdf", "ams-import.lyx"],
60     original => [undef, undef],
61     commands => ["buffer-new",
62                  "buffer-import latex ams-import.tex",
63                  "buffer-write",
64                  "buffer-export pdf2",
65                  "lyx-quit"],
66   },
67 );
68
69 die("Expected argument missing") if (! defined($ARGV[0]));
70 my $test = $ARGV[0];
71 die("Invalid argument") if (! defined($Tests{$test}));
72
73 if (! -e $userdir) {
74   mkdir($userdir);
75 }
76 my @expected = &add_files($data, $Tests{$test}->{original});
77
78 my @created = &add_files($workdir, $Tests{$test}->{create});
79
80 # Copy src-files to work with
81 for my $f (@{$Tests{$test}->{src_files}}) {
82   copy("$data/$f", "$workdir/$f") or die("Copy failed: $!");
83 }
84 print "Unlinking " . join(' ', @created) . "\n";
85 unlink(@created);
86
87 $ENV{LANG} = "en";
88 $ENV{LC_ALL} = "C";
89 $ENV{LANGUAGE} = "en_US";
90
91 check_precondition();
92 chdir($workdir);
93 my @command = ($lyx_exe, "-userdir", $userdir);
94 if (defined($Tests{$test}->{command_line})) {
95   push(@command, @{$Tests{$test}->{command_line}});
96 }
97 if (defined($Tests{$test}->{commands})) {
98   if ($qt_version eq "QT5") {
99     push(@command, "-platform", "offscreen");
100   }
101   push(@command, "-x", "command-sequence " . join(';', @{$Tests{$test}->{commands}}));
102 }
103
104 system1(@command);
105 for my $f (@created) {
106   die("File \"$f\" not created") if (! -e "$f");
107 }
108 for (my $i = 0; defined($created[$i]); $i++) {
109   if (defined($expected[$i])) {
110     die("Expected ($expected[$i]) and created ($created[$i]) files differ") if (compare($expected[$i], $created[$i]) != 0);
111   }
112 }
113 exit(0);
114
115 sub check_precondition()
116 {
117   return if (! defined($Tests{$test}->{precondition}));
118   my $rPrecond = $Tests{$test}->{precondition};
119   my @command = @{$rPrecond->{command}};
120   if (defined($rPrecond->{workdir})) {
121     chdir($rPrecond->{workdir});
122   }
123   my $result = system1(@command);
124   print "Pre-condition result = $result\n";
125   die("Pre-condition error") if ($result != 0);
126 }
127
128 sub system1(@)
129 {
130   my ($exe, @params) = @_;
131   print "Executing:\n\t$exe '" . join("' '", @params) . "'\n";
132   system($exe, @params);
133 }
134
135 # Create a list of file paths
136 # dir: result-dir
137 # rBases: List of base-names
138 sub add_files($$)
139 {
140   my ($dir, $rBases) = @_;
141   my @result = ();
142   for my $f (@{$rBases}) {
143     if (defined($f)) {
144       push(@result, "$dir/$f");
145     }
146     else {
147       push(@result, undef);
148     }
149   }
150   return(@result);
151 }