doc: Move Perl version baseline as the first perl coding style subsection
[dpkg.git] / scripts / t / Dpkg_Dist_Files.t
blob7e820b55f630c19c0325773e776330f2c4b4a680
1 #!/usr/bin/perl
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.
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.
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/>.
16 use strict;
17 use warnings;
19 use Test::More tests => 26;
20 use Test::Dpkg qw(:paths);
22 use_ok('Dpkg::Dist::Files');
24 my $datadir = test_get_data_path();
26 my $expected;
27 my %expected = (
28 'pkg-src_4:2.0+1A~rc1-1.dsc' => {
29 filename => 'pkg-src_4:2.0+1A~rc1-1.dsc',
30 section => 'source',
31 priority => 'extra',
32 attrs => {},
34 'pkg-src_4:2.0+1A~rc1-1.tar.xz' => {
35 filename => 'pkg-src_4:2.0+1A~rc1-1.tar.xz',
36 section => 'source',
37 priority => 'extra',
38 attrs => {},
40 'pkg-templ_1.2.3_arch.type' => {
41 filename => 'pkg-templ_1.2.3_arch.type',
42 package => 'pkg-templ',
43 package_type => 'type',
44 version => '1.2.3',
45 arch => 'arch',
46 section => 'section',
47 priority => 'priority',
48 attrs => {},
50 'pkg-arch_2.0.0_amd64.deb' => {
51 filename => 'pkg-arch_2.0.0_amd64.deb',
52 package => 'pkg-arch',
53 package_type => 'deb',
54 version => '2.0.0',
55 arch => 'amd64',
56 section => 'admin',
57 priority => 'required',
58 attrs => {},
60 'pkg-indep_0.0.1-2_all.deb' => {
61 filename => 'pkg-indep_0.0.1-2_all.deb',
62 package => 'pkg-indep',
63 package_type => 'deb',
64 version => '0.0.1-2',
65 arch => 'all',
66 section => 'net',
67 priority => 'standard',
68 attrs => {},
70 'other_0.txt' => {
71 filename => 'other_0.txt',
72 section => 'text',
73 priority => 'optional',
74 attrs => {
75 'mime-type' => 'text/plain',
78 'BY-HAND-file' => {
79 filename => 'BY-HAND-file',
80 section => 'webdocs',
81 priority => 'optional',
82 attrs => {
83 'by-hand' => 'true'
86 'another:filename' => {
87 filename => 'another:filename',
88 section => 'by-hand',
89 priority => 'extra',
90 attrs => {},
92 'added-on-the-fly' => {
93 filename => 'added-on-the-fly',
94 section => 'void',
95 priority => 'wish',
96 attrs => {},
100 my $dist = Dpkg::Dist::Files->new();
101 $dist->load("$datadir/files-byhand") or error('cannot parse file');
103 $expected = <<'FILES';
104 BY-HAND-file webdocs optional by-hand=true
105 other_0.txt text optional mime-type=text/plain
106 pkg-arch_2.0.0_amd64.deb admin required
107 pkg-indep_0.0.1-2_all.deb net standard
108 pkg-templ_1.2.3_arch.type section priority
109 FILES
111 is($dist->output(), $expected, 'Parsed dist file');
112 foreach my $f ($dist->get_files()) {
113 my $filename = $f->{filename};
115 is_deeply($f, $expected{$filename},
116 "Detail for individual dist file $filename, via get_files()");
118 my $fs = $dist->get_file($filename);
119 is_deeply($fs, $expected{$filename},
120 "Detail for individual dist file $filename, via get_file()");
123 is($dist->parse_filename('file%invalid'), undef, 'invalid filename');
125 $expected = <<'FILES';
126 BY-HAND-file webdocs optional by-hand=true
127 added-on-the-fly void wish
128 other_0.txt text optional mime-type=text/plain
129 pkg-arch_2.0.0_amd64.deb void imperative
130 pkg-templ_1.2.3_arch.type section priority
131 FILES
133 $dist->add_file('added-on-the-fly', 'void', 'wish');
134 is_deeply($dist->get_file('added-on-the-fly'), $expected{'added-on-the-fly'},
135 'Get added file added-on-the-fly');
137 $dist->add_file('pkg-arch_2.0.0_amd64.deb', 'void', 'imperative');
138 my %expected_pkg_arch = %{$expected{'pkg-arch_2.0.0_amd64.deb'}};
139 $expected_pkg_arch{section} = 'void';
140 $expected_pkg_arch{priority} = 'imperative';
141 is_deeply($dist->get_file('pkg-arch_2.0.0_amd64.deb'), \%expected_pkg_arch,
142 'Get modified file pkg-arch_2.0.0_amd64.deb');
144 $dist->del_file('pkg-indep_0.0.1-2_all.deb');
145 is($dist->get_file('unknown'), undef, 'Get unknown file');
146 is($dist->get_file('pkg-indep_0.0.1-2_all.deb'), undef, 'Get deleted file');
147 is($dist->output(), $expected, 'Modified dist object');
149 $expected = <<'FILES';
150 another:filename by-hand extra
151 pkg-src_4:2.0+1A~rc1-1.dsc source extra
152 pkg-src_4:2.0+1A~rc1-1.tar.xz source extra
153 FILES
155 $dist->reset();
156 $dist->add_file('pkg-src_4:2.0+1A~rc1-1.dsc', 'source', 'extra');
157 $dist->add_file('pkg-src_4:2.0+1A~rc1-1.tar.xz', 'source', 'extra');
158 $dist->add_file('another:filename', 'by-hand', 'extra');
160 is_deeply($dist->get_file('pkg-src_4:2.0+1A~rc1-1.dsc'),
161 $expected{'pkg-src_4:2.0+1A~rc1-1.dsc'},
162 'Get added file pkg-src_4:2.0+1A~rc1-1.dsc');
163 is_deeply($dist->get_file('pkg-src_4:2.0+1A~rc1-1.tar.xz'),
164 $expected{'pkg-src_4:2.0+1A~rc1-1.tar.xz'},
165 'Get added file pkg-src_4:2.0+1A~rc1-1.tar.xz');
166 is_deeply($dist->get_file('another:filename'),
167 $expected{'another:filename'},
168 'Get added file another:filename');
169 is($dist->output, $expected, 'Added source files');
171 $expected = <<'FILES';
172 BY-HAND-file webdocs optional by-hand=true
173 other_0.txt text optional mime-type=text/plain
174 pkg-arch_2.0.0_amd64.deb admin required
175 pkg-frag-a_0.0_arch.type section priority
176 pkg-frag-b_0.0_arch.type section priority
177 pkg-indep_0.0.1-2_all.deb net standard
178 pkg-templ_1.2.3_arch.type section priority
179 FILES
181 $dist->reset();
182 $dist->load_dir($datadir) or error('cannot parse fragment files');
183 is($dist->output(), $expected, 'Parse fragment directory');
185 $expected = <<'FILES';
186 pkg-arch_2.0.0_amd64.deb admin required
187 pkg-indep_0.0.1-2_all.deb net standard
188 pkg-templ_1.2.3_arch.type section priority
189 FILES
191 $dist->reset();
192 $dist->load("$datadir/files-byhand") or error('cannot parse file');
193 $dist->filter(remove => sub { $_[0]->{priority} eq 'optional' });
194 is($dist->output(), $expected, 'Filter remove priority optional');
196 $expected = <<'FILES';
197 BY-HAND-file webdocs optional by-hand=true
198 other_0.txt text optional mime-type=text/plain
199 FILES
201 $dist->reset();
202 $dist->load("$datadir/files-byhand") or error('cannot parse file');
203 $dist->filter(keep => sub { $_[0]->{priority} eq 'optional' });
204 is($dist->output(), $expected, 'Filter keep priority optional');
206 $expected = <<'FILES';
207 BY-HAND-file webdocs optional by-hand=true
208 FILES
210 $dist->reset();
211 $dist->load("$datadir/files-byhand") or error('cannot parse file');
212 $dist->filter(remove => sub { $_[0]->{section} eq 'text' },
213 keep => sub { $_[0]->{priority} eq 'optional' });
214 is($dist->output(), $expected, 'Filter remove section text, keep priority optional');