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