Test/Dpkg.pm

filter: Statementstmt 59.7% 92 / 154 Branchbran 32.8% 21 / 64 Conditioncond 55.0% 11 / 20 MC/DCmcdc 25.0% 3 / 12 Subroutinesub 74.2% 26 / 35 Podpod 0.0% 0 / 13 totaltotal 51.3% 153 / 298 CC 42 SCAR 25.9
CC
42
Cov
52%
test_neutralize_checksums
56.0
__ANON__
30.0
test_needs_openpgp_backend
20.6
CRAP 13.3
« Dpkg/Deps/KnownFacts.pm ↑ Summary Dpkg/Source/Package/V3/Native.pm »
1 # Copyright © 2015 Guillem Jover <guillem@debian.org>
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  
16 =encoding utf8
17  
18 =head1 NAME
19  
20 Test::Dpkg - helpers for test scripts for the dpkg suite
21  
22 =head1 DESCRIPTION
23  
24 This module provides helper functions to ease implementing test scripts
25 for the dpkg suite of tools.
26  
27 B<Note>: This is a private module, its API can change at any time.
28  
29 =cut
30  
31 package Test::Dpkg 0.00;
32  
33 51 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 49 our @EXPORT_OK = qw(
36 all_po_files
37 all_shell_files
38 all_perl_files
39 all_perl_modules
40 all_pod_modules
41 test_get_data_path
42 test_get_temp_path
43 test_chdir_srcdir
44 test_needs_author
45 test_needs_module
46 test_needs_command
47 test_needs_openpgp_backend
48 test_neutralize_checksums
49 );
50 49 our %EXPORT_TAGS = (
51 needs => [ qw(
52 test_needs_author
53 test_needs_module
54 test_needs_command
55 test_needs_openpgp_backend
56 ) ],
57 paths => [ qw(
58 all_po_files
59 all_shell_files
60 all_perl_files
61 all_perl_modules
62 all_pod_modules
63 test_get_data_path
64 test_get_temp_path
65 test_chdir_srcdir
66 ) ],
67 );
68  
69 51 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
70 51 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
71 51 ▶use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
72 51 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
73 51 ▶use File::Path qw(make_path rmtree);
Subroutine BEGIN: called CC=1 SCAR=0.0
74 51 ▶use IPC::Cmd qw(can_run);
Subroutine BEGIN: called CC=1 SCAR=0.0
75 51 ▶use Test::More;
Subroutine BEGIN: called CC=1 SCAR=0.0
76  
77 49 my $test_mode;
78  
79 51 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.6
80 51 ▶ $test_mode = $ENV{DPKG_TEST_MODE} // 'dpkg';
Condition50%1/2
$ENV{"DPKG_TEST_MODE"} // "dpkg"
l!l
510
MC/DC0%0/1
$ENV{"DPKG_TEST_MODE"} // "dpkg"
$ENV{"DPKG_TEST_MODE"}
Truth table50%1/2
A
A= $ENV{"DPKG_TEST_MODE"}
Aresult
11
00
81 }
82  
83 sub _test_get_caller_dir
84 55 ▶{
Subroutine _test_get_caller_dir: called CC=1 SCAR=0.0
85 55 my (undef, $path, undef) = caller 1;
86  
87 55 $path =~ s{\.t$}{};
88 55 $path =~ s{^\./}{};
89  
90 55 return $path;
91 }
92  
93 sub test_get_data_path
94 30 ▶{
Subroutine test_get_data_path: called CC=4 SCAR=14.0
Pod undocumented
95 30 my $path = shift;
96  
97 30 ▶ if (defined $path) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $path) { } 1 29 30
98 1 my $srcdir;
99 1 ▶ $srcdir = $ENV{srcdir} if $test_mode ne 'cpan';
Branch: 1 branch
BranchTrueFalseTotal
if $test_mode ne "cpan" 0 1 1
100 1 ▶ $srcdir ||= '.';
Condition50%1/2
$srcdir ||= "."
l!l
01
MC/DC0%0/1
$srcdir ||= "."
$srcdir
Truth table50%1/2
A
A= $srcdir
Aresult
11
00
101 1 return "$srcdir/$path";
102 } else {
103 29 return _test_get_caller_dir();
104 }
105 }
106  
107 sub test_get_temp_path
108 26 ▶{
Subroutine test_get_temp_path: called CC=2 SCAR=7.2
Pod undocumented
109 26 ▶ my $path = shift // _test_get_caller_dir();
Condition33%1/3
shift() // _test_get_caller_dir()
l!l&&r!l&&!r
0260
MC/DC0%0/2
shift() // _test_get_caller_dir()
shift() _test_get_caller_dir()
Truth table33%1/3
A // B
A= shift()B= _test_get_caller_dir()
ABresult
1X1
011
000
110 26 $path = 't.tmp/' . fileparse($path);
111  
112 26 rmtree($path);
113 26 make_path($path);
114 26 return $path;
115 }
116  
117 sub test_chdir_srcdir
118 6 ▶{
Subroutine test_chdir_srcdir: called CC=3 SCAR=17.3
Pod undocumented
119 6 ▶ if (defined $ENV{srcdir}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ENV{"srcdir"}) 0 6 6
120 0 ▶ chdir $ENV{srcdir} or BAIL_OUT("cannot chdir to source directory: $!");
Branch: 1 branch
BranchTrueFalseTotal
chdir $ENV{"srcdir"} or BAIL_OUT("cannot chdir to source directory: $!") 0 0 0
121 }
122 }
123  
124 sub _test_get_po_dirs
125 0 ▶{
Subroutine _test_get_po_dirs: not called CC=2 SCAR=17.9
126 0 ▶ if ($test_mode eq 'cpan') {
Branch: 1 branch
BranchTrueFalseTotal
if ($test_mode eq 'cpan') { } 0 0 0
127 0 return qw();
128 } else {
129 0 return qw(po scripts/po dselect/po man/po);
130 }
131 }
132  
133 sub _test_get_perl_dirs
134 4 ▶{
Subroutine _test_get_perl_dirs: called CC=2 SCAR=8.1
135 4 ▶ if ($test_mode eq 'cpan') {
Branch: 1 branch
BranchTrueFalseTotal
if ($test_mode eq 'cpan') { } 4 0 4
136 4 return qw(t lib);
137 } else {
138 0 return qw(t lib utils/t scripts dselect);
139 }
140 }
141  
142 4 ▶sub _test_scan_files($visit_func, $dirs)
Subroutine _test_scan_files: called CC=1 SCAR=0.0
143 {
144 4 my $scan_files = {
145 wanted => $visit_func,
146 no_chdir => 1,
147 };
148 4 find($scan_files, @{$dirs});
149 }
150  
151 4 ▶sub _test_get_files($keep_func, $dirs)
Subroutine _test_get_files: called CC=1 SCAR=0.0
152 {
153 4 my @files;
154 1220 ▶ my $visit_func = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
155 1220 ▶ push @files, $File::Find::name if $keep_func->($File::Find::name);
Branch: 1 branch
BranchTrueFalseTotal
if &$keep_func($File::Find::name) 558 662 1220
156 4 };
157  
158 4 _test_scan_files($visit_func, $dirs);
159  
160 4 return @files;
161 }
162  
163 sub all_po_files
164 0 ▶{
Subroutine all_po_files: not called CC=1 SCAR=6.9
Pod undocumented
165 0 ▶ my $keep_po_files = sub ($file) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
166 0 return $file =~ m{\.(?:po|pot)$};
167 0 };
168  
169 0 return _test_get_files($keep_po_files, [ _test_get_po_dirs() ]);
170 }
171  
172 0 ▶sub all_shell_files(@files_todo)
Subroutine all_shell_files: not called CC=1 SCAR=6.9
Pod undocumented
173 {
174 0 my %files_todo = map { $_ => 1 } @files_todo;
175 0 my @files = qw(
176 autogen
177 build-aux/gen-release
178 build-aux/get-vcs-id
179 build-aux/get-version
180 build-aux/run-script
181 debian/dpkg.cron.daily
182 debian/dpkg.postinst
183 debian/dpkg.postrm
184 debian/dselect.postrm
185 );
186  
187 0 ▶ my $keep_shell_file = sub ($file) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
188 0 ▶ return 0 if exists $files_todo{$file};
Branch: 1 branch
BranchTrueFalseTotal
if exists $files_todo{$file} 0 0 0
189 0 return $file =~ qr{\.(?:sh|bash)$};
190 0 };
191 0 ▶ my $keep_shebang_file = sub ($file) {
Subroutine __ANON__: not called CC=5 SCAR=34.0
192 0 ▶ return 0 if exists $files_todo{$file};
Branch: 1 branch
BranchTrueFalseTotal
if exists $files_todo{$file} 0 0 0
193  
194 0 ▶ return 0 if -z $file;
Branch: 1 branch
BranchTrueFalseTotal
if -z $file 0 0 0
195 0 ▶ return 0 unless -f $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 0 0 0
196  
197 0 ▶ open my $fh, '<', $file
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or die "error: cannot read file ${file}: $!\n" 0 0 0
198 or die "error: cannot read file $file: $!\n";
199 0 my $shebang = readline $fh;
200 0 close $fh;
201  
202 0 chomp $shebang;
203  
204 0 return $shebang =~ m{^#!/bin/(?:sh|bash)};
205 0 };
206  
207  
208 0 push @files, _test_get_files(
209 $keep_shell_file,
210 [ qw(
211 dselect/methods
212 src
213 ) ],
214 );
215 0 push @files, _test_get_files(
216 $keep_shebang_file,
217 [ qw(
218 debian/tests
219 lib/dpkg/t/data
220 scripts/t
221 tests
222 ) ],
223 );
224  
225 0 return @files;
226 }
227  
228 sub all_perl_files
229 3 ▶{
Subroutine all_perl_files: called CC=1 SCAR=0.0
Pod undocumented
230 915 ▶ my $keep_perl_files = sub ($file) {
Subroutine __ANON__: called CC=1 SCAR=0.0
231 915 return $file =~ m{\.(?:PL|pl|pm|t)$};
232 3 };
233  
234 3 return _test_get_files($keep_perl_files, [ _test_get_perl_dirs() ]);
235 }
236  
237 sub all_perl_modules
238 1 ▶{
Subroutine all_perl_modules: called CC=1 SCAR=0.0
Pod undocumented
239 305 ▶ my $keep_perl_modules = sub ($file) {
Subroutine __ANON__: called CC=1 SCAR=0.0
240 305 return $file =~ m{\.pm$};
241 1 };
242  
243 1 return _test_get_files($keep_perl_modules, [ _test_get_perl_dirs() ]);
244 }
245  
246 sub all_pod_modules
247 0 ▶{
Subroutine all_pod_modules: not called CC=1 SCAR=6.9
Pod undocumented
248 0 my @modules_todo = @_;
249 0 my %modules_todo = map { $_ => 1 } @modules_todo;
250 0 my @modules;
251  
252 0 require Module::Metadata;
253 0 ▶ my $scan_perl_modules = sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
254 0 my $module = $File::Find::name;
255  
256 # Only check modules, scripts are documented in man pages.
257 0 ▶ return unless $module =~ s/\.pm$//;
Branch: 1 branch
BranchTrueFalseTotal
unless $module =~ s/\.pm$//u 0 0 0
258  
259 0 my $mod = Module::Metadata->new_from_file($File::Find::name);
260  
261 # As a first step just check public modules (version > 0.xx).
262 0 ▶ return if $mod->version() =~ m/^0\.\d\d$/;
Branch: 1 branch
BranchTrueFalseTotal
if $mod->version =~ /^0\.\d\d$/u 0 0 0
263  
264 0 $module =~ s{^\Q$File::Find::topdir\E/}{};
265 0 $module =~ s{/}{::}g;
266  
267 0 ▶ return if exists $modules_todo{$module};
Branch: 1 branch
BranchTrueFalseTotal
if exists $modules_todo{$module} 0 0 0
268  
269 0 push @modules, $module;
270 0 };
271  
272 0 _test_scan_files($scan_perl_modules, [ _test_get_perl_dirs() ]);
273  
274 0 return @modules;
275 }
276  
277 sub test_needs_author
278 9 ▶{
Subroutine test_needs_author: called CC=2 SCAR=7.2
Pod undocumented
279 9 ▶ if (not $ENV{AUTHOR_TESTING}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $ENV{"AUTHOR_TESTING"}) 9 0 9
280 9 plan skip_all => 'author test';
281 }
282 }
283  
284 sub test_needs_module
285 2 ▶{
Subroutine test_needs_module: called CC=4 SCAR=13.9
Pod undocumented
286 2 my ($module, @imports) = @_;
287 2 my ($package) = caller;
288  
289 2 require version;
290 2 my $version = '';
291 2 ▶ if (@imports >= 1 and version::is_lax($imports[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (@imports >= 1 and version::is_lax($imports[0])) 1 1 2
Condition66%2/3
@imports >= 1 and version::is_lax($imports[0])
!ll&&!rl&&r
101
MC/DC50%1/2
@imports >= 1 and version::is_lax($imports[0])
@imports >= 1 version::is_lax($imports[0])
Truth table66%2/3
A and B
A= @imports >= 1B= version::is_lax($imports[0])
ABresult
0X0
100
111
292 1 $version = shift @imports;
293 }
294  
295 eval qq{
296 package $package;
297 use $module $version \@imports;
298 1;
299 2 ▶ } or do {
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
eval "\n package $package;\n use $module $version \@imports;\n 1;\n " or do { plan("skip_all", "requires module $module $version") } 1 1 2
300 1 plan skip_all => "requires module $module $version";
301 }
302 }
303  
304 sub test_needs_command
305 2 ▶{
Subroutine test_needs_command: called CC=2 SCAR=8.1
Pod undocumented
306 2 my $command = shift;
307  
308 2 ▶ if (not can_run($command)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not can_run($command)) 0 2 2
309 0 plan skip_all => "requires command $command";
310 }
311 }
312  
313 49 my @openpgp_backends = (
314 {
315 backend => 'gpg',
316 cmd => 'gpg-sq',
317 cmdv => 'gpgv-sq',
318 },
319 {
320 backend => 'gpg',
321 cmd => 'gpg',
322 cmdv => 'gpgv',
323 },
324 {
325 backend => 'sq',
326 cmd => 'sq',
327 cmdv => 'sqv',
328 },
329 {
330 backend => 'sop',
331 cmd => 'sop',
332 cmdv => 'sopv',
333 },
334 {
335 backend => 'sop',
336 cmd => 'sqop',
337 cmdv => 'sqopv',
338 },
339 {
340 backend => 'sop',
341 cmd => 'rsop',
342 cmdv => 'rsopv',
343 },
344 {
345 backend => 'sop',
346 cmd => 'gosop',
347 },
348 {
349 backend => 'sop',
350 cmd => 'hop',
351 },
352 {
353 backend => 'sop',
354 cmd => 'pgpainless-cli',
355 },
356 );
357  
358 sub test_needs_openpgp_backend
359 6 ▶{
Subroutine test_needs_openpgp_backend: called CC=13 SCAR=30.3
Pod undocumented
360 6 my @have_backends;
361 6 foreach my $backend (@openpgp_backends) {
362 54 my $name = $backend->{backend};
363 54 ▶ my $cmd = $backend->{cmd} // q();
Condition50%1/2
$backend->{"cmd"} // ""
l!l
540
MC/DC0%0/1
$backend->{"cmd"} // ""
$backend->{"cmd"}
Truth table50%1/2
A
A= $backend->{"cmd"}
Aresult
11
00
364 54 ▶ my $cmdv = $backend->{cmdv} // q();
Condition100%2/2
$backend->{"cmdv"} // ""
l!l
3618
MC/DC100%1/1
$backend->{"cmdv"} // ""
$backend->{"cmdv"}
Truth table100%2/2
A
A= $backend->{"cmdv"}
Aresult
11
00
365  
366 54 ▶ my $have_cmd = $cmd eq 'none' ? 0 : can_run($cmd);
Branch: 1 branch
BranchTrueFalseTotal
$cmd eq 'none' ? : 0 54 54
367 54 ▶ my $have_cmdv = $cmdv eq 'none' ? 0 : can_run($cmdv);
Branch: 1 branch
BranchTrueFalseTotal
$cmdv eq 'none' ? : 0 54 54
368  
369 54 ▶ next unless ($have_cmd || $have_cmdv);
Branch: 1 branch
BranchTrueFalseTotal
unless $have_cmd or $have_cmdv 48 6 54
Condition66%2/3
$have_cmd or $have_cmdv
l!l&&r!l&&!r
0648
MC/DC50%1/2
$have_cmd or $have_cmdv
$have_cmd $have_cmdv
Truth table66%2/3
A or B
A= $have_cmdB= $have_cmdv
ABresult
1X1
011
000
370  
371 6 my $have_backend = {
372 backend => $name,
373 };
374 6 ▶ $have_backend->{cmd} = $cmd if $have_cmd;
Branch: 1 branch
BranchTrueFalseTotal
if $have_cmd 0 6 6
375 6 ▶ $have_backend->{cmdv} = $cmdv if $have_cmdv;
Branch: 1 branch
BranchTrueFalseTotal
if $have_cmdv 6 0 6
376  
377 6 push @have_backends, $have_backend;
378  
379 6 ▶ if ($have_cmd && $have_cmdv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($have_cmd and $have_cmdv) 0 6 6
Condition33%1/3
$have_cmd and $have_cmdv
!ll&&!rl&&r
600
MC/DC0%0/2
$have_cmd and $have_cmdv
$have_cmd $have_cmdv
Truth table33%1/3
A and B
A= $have_cmdB= $have_cmdv
ABresult
0X0
100
111
380 0 push @have_backends, {
381 backend => $name,
382 cmd => $cmd,
383 cmdv => 'none',
384 };
385 0 push @have_backends, {
386 backend => $name,
387 cmd => 'none',
388 cmdv => $cmdv,
389 };
390 }
391 }
392 6 ▶ if (@have_backends == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@have_backends == 0) 0 6 6
393 my @cmds = grep {
394 0 $_ ne 'none'
395 } map {
396 0 ($_->{cmd}, $_->{cmdv})
397 0 } @openpgp_backends;
398 0 plan skip_all => "requires >= 1 openpgp command: @cmds";
399 }
400  
401 6 return @have_backends;
402 }
403  
404 sub test_neutralize_checksums
405 0 ▶{
Subroutine test_neutralize_checksums: not called CC=7 SCAR=40.3
Pod undocumented
406 0 my $filename = shift;
407 0 my $filenamenew = "$filename.new";
408  
409 0 my $cwd = getcwd();
410 0 ▶ open my $fhnew, '>', $filenamenew or die "cannot open new $filenamenew in $cwd: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fhnew, ">", $filenamenew or die "cannot open new $filenamenew in ${cwd}: $!" 0 0 0
411 0 ▶ open my $fh, '<', $filename or die "cannot open $filename in $cwd: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $filename or die "cannot open $filename in ${cwd}: $!" 0 0 0
412 0 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
413 0 s/^ ([0-9a-f]{32,}) [1-9][0-9]* /q{ } . $1 =~ tr{0-9a-f}{0}r . q{ 0 }/e;
414 0 print { $fhnew } $_;
415 }
416 0 ▶ close $fh or die "cannot close $filename";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "cannot close $filename" 0 0 0
417 0 ▶ close $fhnew or die "cannot close $filenamenew";
Branch: 1 branch
BranchTrueFalseTotal
close $fhnew or die "cannot close $filenamenew" 0 0 0
418  
419 0 ▶ rename $filenamenew, $filename or die "cannot rename $filenamenew to $filename";
Branch: 1 branch
BranchTrueFalseTotal
rename $filenamenew, $filename or die "cannot rename $filenamenew to $filename" 0 0 0
420 }
421  
422 =head1 CHANGES
423  
424 =head2 Version 0.xx
425  
426 This is a private module.
427  
428 =cut
429  
430 49 1;
« Dpkg/Deps/KnownFacts.pm ↑ Summary Dpkg/Source/Package/V3/Native.pm »