summaryrefslogtreecommitdiffstats
path: root/scripts/t/Dpkg_Conf.t
blob: 940d18dd7f3d6bdc818284b931ce4221814b9770 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/perl
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

use strict;
use warnings;

use Test::More tests => 9;
use Test::Dpkg qw(:paths);

BEGIN {
    use_ok('Dpkg::Conf');
}

my $datadir = test_get_data_path();

my ($conf, $count, @opts);

my @expected_long_opts = (
'--option-double-quotes=value double quotes',
'--option-single-quotes=value single quotes',
'--option-space=value words space',
qw(
--option-dupe=value1
--option-name=value-name
--option-indent=value-indent
--option-equal=value-equal=subvalue-equal
--option-noequal=value-noequal
--option-dupe=value2
--option-simple
--option-dash=value-dash
--option-dupe=value3
--l=v
));
my @expected_short_opts = qw(
-o=vd
-s
);

$conf = Dpkg::Conf->new();
local $SIG{__WARN__} = sub { };
$count = $conf->load("$datadir/config-mixed");
delete $SIG{__WARN__};
is($count, 13, 'Load a config file, only long options');

@opts = $conf->get_options();
is_deeply(\@opts, \@expected_long_opts, 'Parse long options');

$conf = Dpkg::Conf->new(allow_short => 1);
$count = $conf->load("$datadir/config-mixed");
is($count, 15, 'Load a config file, mixed options');

@opts = $conf->get_options();
my @expected_mixed_opts = ( @expected_long_opts, @expected_short_opts );
is_deeply(\@opts, \@expected_mixed_opts, 'Parse mixed options');

my $expected_mixed_output = <<'MIXED';
option-double-quotes = "value double quotes"
option-single-quotes = "value single quotes"
option-space = "value words space"
option-dupe = "value1"
option-name = "value-name"
option-indent = "value-indent"
option-equal = "value-equal=subvalue-equal"
option-noequal = "value-noequal"
option-dupe = "value2"
option-simple
option-dash = "value-dash"
option-dupe = "value3"
l = "v"
-o = "vd"
-s
MIXED

is($conf->output, $expected_mixed_output, 'Output mixed options');

my $expected_filter;

$expected_filter = <<'FILTER';
l = "v"
-o = "vd"
-s
FILTER

$conf = Dpkg::Conf->new(allow_short => 1);
$conf->load("$datadir/config-mixed");
$conf->filter(remove => sub { $_[0] =~ m/^--option/ });
is($conf->output, $expected_filter, 'Filter remove');

$expected_filter = <<'FILTER';
option-double-quotes = "value double quotes"
option-single-quotes = "value single quotes"
FILTER

$conf = Dpkg::Conf->new(allow_short => 1);
$conf->load("$datadir/config-mixed");
$conf->filter(keep => sub { $_[0] =~ m/^--option-[a-z]+-quotes/ });
is($conf->output, $expected_filter, 'Filter keep');

$expected_filter = <<'FILTER';
l = "v"
FILTER

$conf = Dpkg::Conf->new(allow_short => 1);
$conf->load("$datadir/config-mixed");
$conf->filter(remove => sub { $_[0] =~ m/^--option/ },
              keep => sub { $_[0] =~ m/^--/ });
is($conf->output, $expected_filter, 'Filter keep and remove');

1;