]> git.lyx.org Git - lyx.git/blob - lib/scripts/prefTest.pl.in
Cmake tests: Add a script to modify preferences prior to export testing
[lyx.git] / lib / scripts / prefTest.pl.in
1 #! /usr/bin/env perl
2 # -*- mode: perl; -*-
3
4 use strict;
5
6 # Syntax: ReplaceValuesprefTest.pl [<var1>=<Subst1> [<var2>=<Subst> ...]] [[ctest parameters]]
7
8 my $bindir = "@CMAKE_BINARY_DIR@";
9
10 my $userdir = "$bindir/Testing/.lyx";
11
12 my %allowedKeys = (
13   "use_converter_needauth_forbidden" => ["true", "false"],
14   "use_converter_needauth" => ["true", "false"],
15     );
16
17 chdir($bindir);
18
19 # Parse Arguments for strings to substitute
20
21 my %Subst = ();
22
23 my $ctestparams = 0;
24 my @ctestpars = ();
25 for my $arg (@ARGV) {
26   if ($ctestparams) {
27     push(@ctestpars, $arg);
28   }
29   else {
30     if ($arg =~ /^([^=]+)=(.*)$/) {
31       my $key = $1;
32       my $value = $2;
33       my $valid = 0;
34       if (defined($allowedKeys{$key})) {
35         for my $val (@{$allowedKeys{$key}}) {
36           if ($val eq $value) {
37             $valid = 1;
38             last;
39           }
40         }
41       }
42       if ($valid) {
43         $Subst{$key} = [$value, 0];
44       }
45       else {
46         die("invalid key or value specified in \"$arg\"");
47       }
48     }
49     else {
50       $ctestparams = 1;
51       push(@ctestpars, $arg);
52     }
53   }
54 }
55 if (%Subst) { # Try to do something only if a substitute is requested
56   if (open(FO, '>', "$userdir/preferences.tmp")) {
57     if (open(FI, "$userdir/preferences")) {
58       while (my $l = <FI>) {
59         for my $k (keys %Subst) {
60           if ($l =~ /^\\$k\b/) {
61             $l = "\\$k $Subst{$k}->[0]\n";
62             $Subst{$k}->[1] = 1;
63           }
64         }
65         print FO $l;
66       }
67     }
68     for my $k (keys %Subst) {
69       if ($Subst{$k}->[1] == 0) {
70         print FO "\\$k $Subst{$k}->[0]\n";
71       }
72     }
73     rename("$userdir/preferences.tmp", "$userdir/preferences");
74   }
75 }
76
77 my $res = 0;
78 if (@ctestpars) {
79   $res = system("ctest", @ctestpars);
80 }
81
82 exit($res);