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 => 26;
20use Test::Dpkg qw(:paths);
21
22use_ok('Dpkg::Dist::Files');
23
24my $datadir = test_get_data_path();
25
26my $expected;
27my %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 => {},
33    },
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 => {},
39    },
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 => {},
49    },
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 => {},
59    },
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 => {},
69    },
70    'other_0.txt' => {
71        filename => 'other_0.txt',
72        section => 'text',
73        priority => 'optional',
74        attrs => {
75            'mime-type' => 'text/plain',
76        },
77    },
78    'BY-HAND-file' => {
79        filename => 'BY-HAND-file',
80        section => 'webdocs',
81        priority => 'optional',
82        attrs => {
83            'by-hand' => 'true'
84        },
85    },
86    'another:filename' => {
87        filename => 'another:filename',
88        section => 'by-hand',
89        priority => 'extra',
90        attrs => {},
91    },
92    'added-on-the-fly' => {
93        filename => 'added-on-the-fly',
94        section => 'void',
95        priority => 'wish',
96        attrs => {},
97    },
98);
99
100my $dist = Dpkg::Dist::Files->new();
101$dist->load("$datadir/files-byhand") or error('cannot parse file');
102
103$expected = <<'FILES';
104BY-HAND-file webdocs optional by-hand=true
105other_0.txt text optional mime-type=text/plain
106pkg-arch_2.0.0_amd64.deb admin required
107pkg-indep_0.0.1-2_all.deb net standard
108pkg-templ_1.2.3_arch.type section priority
109FILES
110
111is($dist->output(), $expected, 'Parsed dist file');
112foreach my $f ($dist->get_files()) {
113    my $filename = $f->{filename};
114
115    is_deeply($f, $expected{$filename},
116              "Detail for individual dist file $filename, via get_files()");
117
118    my $fs = $dist->get_file($filename);
119    is_deeply($fs, $expected{$filename},
120              "Detail for individual dist file $filename, via get_file()");
121}
122
123is($dist->parse_filename('file%invalid'), undef, 'invalid filename');
124
125$expected = <<'FILES';
126BY-HAND-file webdocs optional by-hand=true
127added-on-the-fly void wish
128other_0.txt text optional mime-type=text/plain
129pkg-arch_2.0.0_amd64.deb void imperative
130pkg-templ_1.2.3_arch.type section priority
131FILES
132
133$dist->add_file('added-on-the-fly', 'void', 'wish');
134is_deeply($dist->get_file('added-on-the-fly'), $expected{'added-on-the-fly'},
135    'Get added file added-on-the-fly');
136
137$dist->add_file('pkg-arch_2.0.0_amd64.deb', 'void', 'imperative');
138my %expected_pkg_arch = %{$expected{'pkg-arch_2.0.0_amd64.deb'}};
139$expected_pkg_arch{section} = 'void';
140$expected_pkg_arch{priority} = 'imperative';
141is_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');
143
144$dist->del_file('pkg-indep_0.0.1-2_all.deb');
145is($dist->get_file('unknown'), undef, 'Get unknown file');
146is($dist->get_file('pkg-indep_0.0.1-2_all.deb'), undef, 'Get deleted file');
147is($dist->output(), $expected, 'Modified dist object');
148
149$expected = <<'FILES';
150another:filename by-hand extra
151pkg-src_4:2.0+1A~rc1-1.dsc source extra
152pkg-src_4:2.0+1A~rc1-1.tar.xz source extra
153FILES
154
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');
159
160is_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');
163is_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');
166is_deeply($dist->get_file('another:filename'),
167          $expected{'another:filename'},
168          'Get added file another:filename');
169is($dist->output, $expected, 'Added source files');
170
171$expected = <<'FILES';
172BY-HAND-file webdocs optional by-hand=true
173other_0.txt text optional mime-type=text/plain
174pkg-arch_2.0.0_amd64.deb admin required
175pkg-frag-a_0.0_arch.type section priority
176pkg-frag-b_0.0_arch.type section priority
177pkg-indep_0.0.1-2_all.deb net standard
178pkg-templ_1.2.3_arch.type section priority
179FILES
180
181$dist->reset();
182$dist->load_dir($datadir) or error('cannot parse fragment files');
183is($dist->output(), $expected, 'Parse fragment directory');
184
185$expected = <<'FILES';
186pkg-arch_2.0.0_amd64.deb admin required
187pkg-indep_0.0.1-2_all.deb net standard
188pkg-templ_1.2.3_arch.type section priority
189FILES
190
191$dist->reset();
192$dist->load("$datadir/files-byhand") or error('cannot parse file');
193$dist->filter(remove => sub { $_[0]->{priority} eq 'optional' });
194is($dist->output(), $expected, 'Filter remove priority optional');
195
196$expected = <<'FILES';
197BY-HAND-file webdocs optional by-hand=true
198other_0.txt text optional mime-type=text/plain
199FILES
200
201$dist->reset();
202$dist->load("$datadir/files-byhand") or error('cannot parse file');
203$dist->filter(keep => sub { $_[0]->{priority} eq 'optional' });
204is($dist->output(), $expected, 'Filter keep priority optional');
205
206$expected = <<'FILES';
207BY-HAND-file webdocs optional by-hand=true
208FILES
209
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' });
214is($dist->output(), $expected, 'Filter remove section text, keep priority optional');
215
2161;
217