Debian Codegraph @

lib/Test/CheckManifest.pm

raw symbols SCIP
1 package Test::CheckManifest;
2
3 # ABSTRACT: Check if your Manifest matches your distro
4
5 use strict;
6 use warnings;
7 use 5.008;
8
9 use Cwd;
10 use Carp;
11 use File::Spec;
12 use File::Basename;
13 use Test::Builder;
14 use File::Find;
15 use Scalar::Util qw(blessed);
16
17 our $VERSION = '1.43';
18 our $VERBOSE = 1;
19 our $HOME;
20 our $test_bool = 1;
21
22 my $test = Test::Builder->new();
23 my $plan = 0;
24 my $counter = 0;
25
26 my @excluded_files = qw(
27 pm_to_blib Makefile META.yml Build pod2htmd.tmp META.json
28 pod2htmi.tmp Build.bat .cvsignore MYMETA.json MYMETA.yml
29 );
30
31 sub import {
32 my $self = shift;
33 my $caller = caller;
34 my %plan = @_;
35
36 for my $func ( qw( ok_manifest ) ) {
37 no strict 'refs'; ## no critic
38 *{$caller."::".$func} = \&$func;
39 }
40
41 $test->exported_to($caller);
42 $test->plan(%plan);
43
44 $plan = 1 if(exists $plan{tests});
45 }
46
47 sub _validate_args {
48 my ($hashref, $msg) = @_;
49
50 my $ref = ref $hashref;
51 if ( !$ref || 'HASH' ne $ref ) {
52 $msg = $hashref if !$ref;
53 $hashref = {};
54 }
55
56 my $ref_filter = ref $hashref->{filter};
57 $hashref->{filter} = [] if !$ref_filter || 'ARRAY' ne $ref_filter;
58 $hashref->{filter} = [ grep{ blessed $_ && $_->isa('Regexp') } @{ $hashref->{filter} } ];
59
60 my $ref_exclude = ref $hashref->{exclude};
61 $hashref->{exclude} = [] if !$ref_exclude || 'ARRAY' ne $ref_exclude;
62 push @{$hashref->{exclude}}, qw!/blib /_blib! if $test_bool;
63
64 for my $excluded_path ( @{ $hashref->{exclude} } ) {
65 croak 'path in excluded array must be "absolute"' if $excluded_path !~ m!^/!;
66 }
67
68 my $bool = lc( $hashref->{bool} || '' );
69 $hashref->{bool} = $bool && $bool eq 'and' ? 'and' : 'or';
70
71 return $hashref, $msg;
72 }
73
74 sub _check_excludes {
75 my ($hashref, $home) = @_;
76
77 my @excluded;
78
79 EXCLUDED_PATH:
80 for my $excluded_path ( @{ $hashref->{exclude} } ) {
81 next EXCLUDED_PATH if !defined $excluded_path;
82 next EXCLUDED_PATH if !length $excluded_path;
83
84 my $path = File::Spec->catdir($home, $excluded_path);
85
86 $path = File::Spec->rel2abs( $path ) if !File::Spec->file_name_is_absolute( $path );
87
88 next if !-e $path;
89
90 push @excluded, $path;
91 }
92
93 return \@excluded;
94 }
95
96 sub _find_home {
97 my ($params) = @_;
98
99 my $tmp_path = File::Spec->rel2abs( $0 );
100 my ($home, $volume, $dirs, $file, @dirs);
101
102 if ( $params->{file} ) {
103 $tmp_path = $params->{file};
104 }
105 elsif ( $params->{dir} ) {
106 $tmp_path = File::Spec->catfile( $params->{dir}, 'test' );
107 }
108
109 ($volume,$dirs,$file) = File::Spec->splitpath($tmp_path);
110 $home = File::Spec->catdir($volume, $dirs);
111
112 my $counter = 0;
113 while ( 1 ) {
114 last if -f File::Spec->catfile( $home, 'MANIFEST' );
115
116 my $tmp_home = Cwd::realpath( File::Spec->catdir( $home, '..' ) );
117
118 last if !$tmp_home || $counter++ == 5;
119 $home = $tmp_home;
120 }
121
122 return $HOME if $HOME;
123 return $home;
124 }
125
126 sub _manifest_files {
127 my ($home, $manifest) = @_;
128
129 my @files = _read_file( $manifest );
130
131 for my $tfile ( @files ) {
132 $tfile = ( split /\s{2,}/, $tfile, 2 )[0];
133
134 next if !-e $home . '/' . $tfile;
135
136 $tfile = File::Spec->rel2abs($home . '/' . $tfile);
137 }
138
139 return @files;
140 }
141
142 sub ok_manifest {
143 my ($hashref,$msg) = _validate_args( @_ );
144
145 $test->plan(tests => 1) if !$plan;
146
147 my $home = _find_home( $hashref );
148 my $manifest = File::Spec->catfile( $home, 'MANIFEST' );
149
150 if ( !-f $manifest ) {
151 $test->BAILOUT( 'Cannot find a MANIFEST. Please check!' );
152 }
153
154 my @files = _manifest_files( $home, $manifest );
155 if ( !@files ) {
156 $test->diag( "No files in MANIFEST found (is it readable?)" );
157 return;
158 }
159
160 my $skip_path = File::Spec->catfile( $home, 'MANIFEST.SKIP' );
161 my @skip_files = _read_file( $skip_path );
162 my @skip_rx = map{ qr/$_/ }@skip_files;
163 my $excluded = _check_excludes( $hashref, $home );
164
165 my (@dir_files, %excluded);
166
167 find({
168 no_chdir => 1,
169 follow => 0,
170 wanted => sub {
171 my $file = $File::Find::name;
172 return if !-f $file;
173
174 my $is_excluded = _is_excluded(
175 $file,
176 $excluded,
177 $hashref->{filter},
178 $hashref->{bool},
179 \@skip_rx,
180 $home,
181 );
182
183 my $abs = File::Spec->rel2abs($file);
184
185 $is_excluded ?
186 ( $excluded{$abs} = 1 ) :
187 ( push @dir_files, $abs );
188 }
189 },$home);
190
191 my $success = _check_manifest( \@dir_files, \@files, \%excluded, $msg, $manifest );
192
193 return $success;
194 }
195
196 sub _check_manifest {
197 my ($existing_files, $manifest_files, $excluded, $msg, $manifest) = @_;
198
199 my @existing = @{ $existing_files || [] };
200 my @manifest = @{ $manifest_files || [] };
201
202 my $bool = 1;
203
204 my %files_hash;
205 @files_hash{@manifest} = ();
206 my %missing_files;
207
208 SFILE:
209 for my $file ( @existing ) {
210 for my $check ( @manifest ) {
211 if ( $file eq $check ) {
212 delete $files_hash{$check};
213 next SFILE;
214 }
215 }
216
217 $missing_files{$file} = 1;
218 }
219
220 my @dup_files = ();
221 my @files_plus = ();
222
223 delete @files_hash{ keys %{$excluded || {}} };
224 delete @missing_files{ keys %{$excluded || {}} };
225
226 @files_plus = sort keys %files_hash;
227 $bool = 0 if scalar @files_plus > 0;
228 $bool = 0 if %missing_files;
229
230 my %seen_files = ();
231 @dup_files = map { $seen_files{$_}++ ? $_ : () } @manifest;
232 $bool = 0 if scalar @dup_files > 0;
233
234 my $diag = 'The following files are not named in the MANIFEST file: '.
235 join(', ', sort keys %missing_files);
236 my $plus = 'The following files are not part of distro but named in the MANIFEST file: '.
237 join(', ',@files_plus);
238 my $dup = 'The following files appeared more than once in the MANIFEST file: '.
239 join(', ',@dup_files);
240
241 my $success;
242
243 if ( !$ENV{NO_MANIFEST_CHECK} ) {
244 $success = $test->is_num($bool,$test_bool,$msg);
245 }
246 else {
247 $success = $bool == $test_bool;
248 }
249
250 $test->diag($diag) if keys %missing_files >= 1 and $test_bool == 1 and $VERBOSE;
251 $test->diag($plus) if scalar @files_plus >= 1 and $test_bool == 1 and $VERBOSE;
252 $test->diag($dup) if scalar @dup_files >= 1 and $test_bool == 1 and $VERBOSE;
253
254 $test->diag( "MANIFEST: $manifest" ) if !$success;
255
256 return $success;
257 }
258
259 sub _read_file {
260 my ($path) = @_;
261
262 return if !-r $path;
263
264 my @files;
265
266 open my $fh, '<', $path;
267 while( my $fh_line = <$fh> ){
268 chomp $fh_line;
269
270 next if $fh_line =~ m{ \A \s* \# }x;
271
272 my ($file);
273
274 if ( ($file) = $fh_line =~ /^'(\\[\\']|.+)+'\s*/) {
275 $file =~ s/\\([\\'])/$1/g;
276 }
277 else {
278 ($file) = $fh_line =~ /^(\S+)\s*/;
279 }
280
281 next unless $file;
282
283 push @files, $file;
284 }
285
286 close $fh;
287
288 chomp @files;
289
290 {
291 local $/ = "\r";
292 chomp @files;
293 }
294
295 return @files;
296 }
297
298 sub _not_ok_manifest {
299 $test_bool = 0;
300 ok_manifest(@_);
301 $test_bool = 1;
302 }
303
304 sub _is_excluded {
305 my ($file,$dirref,$filter,$bool,$files_in_skip,$home) = @_;
306
307 $home = '' if !defined $home;
308
309 return 0 if $files_in_skip and 'ARRAY' ne ref $files_in_skip;
310
311 if ( $files_in_skip ) {
312
313 # $home is usually given without trailing slash,
314 # the $files_in_skip is taken from MANIFEST.SKIP which usually contain regexes
315 # for files relative the $home. Therefore the remaining leading slashes in $localfile
316 my $separator = $^O eq 'MSWin32' ? '[/\\\\]' : '/';
317 (my $local_file = $file) =~ s{\Q$home\E$separator*}{};
318 for my $rx ( @{$files_in_skip} ) {
319 return 1 if $local_file =~ $rx;
320 }
321 }
322
323 my $basename = basename $file;
324 my @matches = grep{ $basename eq $_ }@excluded_files;
325
326 return 1 if @matches;
327
328 my $is_in_dir = _is_in_dir( $file, $dirref );
329
330 $bool ||= 'or';
331 if ( $bool eq 'or' ) {
332 push @matches, $file if grep{ $file =~ /$_/ }@$filter;
333 push @matches, $file if $is_in_dir;
334 }
335 else{
336 if( grep{ $file =~ /$_/ }@$filter and $is_in_dir ) {
337 push @matches, $file;
338 }
339 }
340
341 return scalar @matches;
342 }
343
344 sub _is_in_dir {
345 my ($file, $excludes) = @_;
346
347 return if !defined $file;
348 return if !length $file;
349
350 my (undef, $path) = File::Spec->splitpath( $file );
351 my @file_parts = File::Spec->splitdir( $path );
352 my $is_in_dir;
353
354 EXCLUDE:
355 for my $exclude ( @{ $excludes || [] } ) {
356
357 next EXCLUDE if !defined $exclude;
358 next EXCLUDE if !length $exclude;
359
360 my (undef, $exclude_dir, $efile) = File::Spec->splitpath( $exclude );
361 my @exclude_parts = File::Spec->splitdir( $exclude_dir . $efile );
362
363 pop @exclude_parts if $exclude_parts[-1] eq '';
364
365 next EXCLUDE if @exclude_parts > @file_parts;
366
367 my @subparts = @file_parts[ 0 .. $#exclude_parts ];
368
369 my $exclude_join = join '/', @exclude_parts;
370 my $sub_join = join '/', @subparts;
371
372 next EXCLUDE if $exclude_join ne $sub_join;
373
374 $is_in_dir = 1;
375 last EXCLUDE;
376 }
377
378 return $is_in_dir;
379 }
380
381 1;
382
383 __END__
384
385 =pod
386
387 =encoding UTF-8
388
389 =head1 NAME
390
391 Test::CheckManifest - Check if your Manifest matches your distro
392
393 =head1 VERSION
394
395 version 1.43
396
397 =head1 SYNOPSIS
398
399 use Test::CheckManifest;
400 ok_manifest();
401
402 =head2 EXPORT
403
404 There is only one method exported: C<ok_manifest>
405
406 =head1 METHODS
407
408 =head2 ok_manifest [{exclude => $arref}][$msg]
409
410 checks whether the Manifest file matches the distro or not. To match a distro
411 the Manifest has to name all files that come along with the distribution.
412
413 To check the Manifest file, this module searches for a file named C<MANIFEST>.
414
415 To exclude some directories from this test, you can specify these dirs in the
416 hashref.
417
418 ok_manifest({exclude => ['/var/test/']});
419
420 is ok if the files in C</path/to/your/dist/var/test/> are not named in the
421 C<MANIFEST> file. That means that the paths in the exclude array must be
422 "pseudo-absolute" (absolute to your distribution).
423
424 To use a "filter" you can use the key "filter"
425
426 ok_manifest({filter => [qr/\.svn/]});
427
428 With that you can exclude all files with an '.svn' in the filename or in the
429 path from the test.
430
431 These files would be excluded (as examples):
432
433 =over 4
434
435 =item * /dist/var/.svn/test
436
437 =item * /dist/lib/test.svn
438
439 =back
440
441 You can also combine "filter" and "exclude" with 'and' or 'or' default is 'or':
442
443 ok_manifest({exclude => ['/var/test'],
444 filter => [qr/\.svn/],
445 bool => 'and'});
446
447 These files have to be named in the C<MANIFEST>:
448
449 =over 4
450
451 =item * /var/foo/.svn/any.file
452
453 =item * /dist/t/file.svn
454
455 =item * /var/test/test.txt
456
457 =back
458
459 These files not:
460
461 =over 4
462
463 =item * /var/test/.svn/*
464
465 =item * /var/test/file.svn
466
467 =back
468
469 By default, C<ok_manifest> will look for the file C<MANIFEST> in the current working directory (which is how tests are traditionally run). If you wish to specify a different directory, you may pass the C<file> or C<dir> parameters, for example:
470
471 ok_manifest({dir => '/path/to/my/dist/'});
472
473 =head1 EXCLUDING FILES
474
475 Beside C<filter> and C<exclude> there is another way to exclude files:
476 C<MANIFEST.SKIP>. This is a file with filenames that should be excluded:
477
478 t/my_very_own.t
479 file_to.skip
480
481 =head1 REPLACE THIS MODULE
482
483 You can replace the test scripts using C<Test::CheckManifest> with this one
484 using L<ExtUtils::Manifest>.
485
486 use Test::More tests => 2;
487 use ExtUtils::Manifest;
488
489 is_deeply [ ExtUtils::Manifest::manicheck() ], [], 'missing';
490 is_deeply [ ExtUtils::Manifest::filecheck() ], [], 'extra';
491
492 (L<thanks to @mohawk2|https://github.com/reneeb/Test-CheckManifest/issues/20>).
493
494 =head1 ACKNOWLEDGEMENT
495
496 Great thanks to Christopher H. Laco, who did a lot of testing stuff for me and
497 he reported some bugs to RT.
498
499 =head1 AUTHOR
500
501 Renee Baecker <[email protected]>
502
503 =head1 COPYRIGHT AND LICENSE
504
505 This software is Copyright (c) 2018 by Renee Baecker.
506
507 This is free software, licensed under:
508
509 The Artistic License 2.0 (GPL Compatible)
510
511 =cut