dpkg (1.18.25) stretch; urgency=medium
[dpkg] / scripts / t / Dpkg_Conf.t
CommitLineData
1479465f
GJ
1#!/usr/bin/perl
2#
3# This program is free software; you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
5# the Free Software Foundation; either version 2 of the License, or
6# (at your option) any later version.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License
14# along with this program. If not, see <https://www.gnu.org/licenses/>.
15
16use strict;
17use warnings;
18
19use Test::More tests => 9;
20use Test::Dpkg qw(:paths);
21
22BEGIN {
23 use_ok('Dpkg::Conf');
24}
25
26my $datadir = test_get_data_path('t/Dpkg_Conf');
27
28my ($conf, $count, @opts);
29
30my @expected_long_opts = (
31'--option-double-quotes=value double quotes',
32'--option-single-quotes=value single quotes',
33'--option-space=value words space',
34qw(
35--option-dupe=value1
36--option-name=value-name
37--option-indent=value-indent
38--option-equal=value-equal=subvalue-equal
39--option-noequal=value-noequal
40--option-dupe=value2
41--option-simple
42--option-dash=value-dash
43--option-dupe=value3
44--l=v
45));
46my @expected_short_opts = qw(
47-o=vd
48-s
49);
50
51$conf = Dpkg::Conf->new();
52local $SIG{__WARN__} = sub { };
53$count = $conf->load("$datadir/config-mixed");
54delete $SIG{__WARN__};
55is($count, 13, 'Load a config file, only long options');
56
57@opts = $conf->get_options();
58is_deeply(\@opts, \@expected_long_opts, 'Parse long options');
59
60$conf = Dpkg::Conf->new(allow_short => 1);
61$count = $conf->load("$datadir/config-mixed");
62is($count, 15, 'Load a config file, mixed options');
63
64@opts = $conf->get_options();
65my @expected_mixed_opts = ( @expected_long_opts, @expected_short_opts );
66is_deeply(\@opts, \@expected_mixed_opts, 'Parse mixed options');
67
68my $expected_mixed_output = <<'MIXED';
69option-double-quotes = "value double quotes"
70option-single-quotes = "value single quotes"
71option-space = "value words space"
72option-dupe = "value1"
73option-name = "value-name"
74option-indent = "value-indent"
75option-equal = "value-equal=subvalue-equal"
76option-noequal = "value-noequal"
77option-dupe = "value2"
78option-simple
79option-dash = "value-dash"
80option-dupe = "value3"
81l = "v"
82-o = "vd"
83-s
84MIXED
85
86is($conf->output, $expected_mixed_output, 'Output mixed options');
87
88my $expected_filter;
89
90$expected_filter = <<'FILTER';
91l = "v"
92-o = "vd"
93-s
94FILTER
95
96$conf = Dpkg::Conf->new(allow_short => 1);
97$conf->load("$datadir/config-mixed");
98$conf->filter(remove => sub { $_[0] =~ m/^--option/ });
99is($conf->output, $expected_filter, 'Filter remove');
100
101$expected_filter = <<'FILTER';
102option-double-quotes = "value double quotes"
103option-single-quotes = "value single quotes"
104FILTER
105
106$conf = Dpkg::Conf->new(allow_short => 1);
107$conf->load("$datadir/config-mixed");
108$conf->filter(keep => sub { $_[0] =~ m/^--option-[a-z]+-quotes/ });
109is($conf->output, $expected_filter, 'Filter keep');
110
111$expected_filter = <<'FILTER';
112l = "v"
113FILTER
114
115$conf = Dpkg::Conf->new(allow_short => 1);
116$conf->load("$datadir/config-mixed");
117$conf->filter(remove => sub { $_[0] =~ m/^--option/ },
118 keep => sub { $_[0] =~ m/^--/ });
119is($conf->output, $expected_filter, 'Filter keep and remove');
120
1211;