| 1 | #!/usr/bin/perl -w |
| 2 | |
| 3 | # Copyright 2011, 2012, 2013, 2015 Kevin Ryde |
| 4 | |
| 5 | # 0-file-is-part-of.t is shared by several distributions. |
| 6 | # |
| 7 | # 0-file-is-part-of.t is free software; you can redistribute it and/or |
| 8 | # modify it under the terms of the GNU General Public License as published |
| 9 | # by the Free Software Foundation; either version 3, or (at your option) any |
| 10 | # later version. |
| 11 | # |
| 12 | # 0-file-is-part-of.t is distributed in the hope that it will be useful, |
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
| 15 | # Public License for more details. |
| 16 | # |
| 17 | # You should have received a copy of the GNU General Public License along |
| 18 | # with this file. If not, see <http://www.gnu.org/licenses/>. |
| 19 | |
| 20 | requirescip-perl cpan Perl-Critic-Pulp 100 main/require(). 5; |
| 21 | use strictscip-perl cpan Perl-Critic-Pulp 100 strict/; |
| 22 | use Test::Morescip-perl cpan Perl-Critic-Pulp 100 Test/More/ tests => 1; |
| 23 | |
| 24 | use libscip-perl cpan Perl-Critic-Pulp 100 lib/ 't'; |
| 25 | use MyTestHelpersscip-perl cpan Perl-Critic-Pulp 100 MyTestHelpers/; |
| 26 | BEGIN { MyTestHelpers::nowarningsscip-perl cpan Perl-Critic-Pulp 100 MyTestHelpers/nowarnings().(); } |
| 27 | |
| 28 | okscip-perl cpan Perl-Critic-Pulp 100 main/ok(). (Test::FileIsPartOfDist->checkscip-perl cpan Perl-Critic-Pulp 100 Test/FileIsPartOfDist/check().(verbose=>1), |
| 29 | 'Test::FileIsPartOfDist'); |
| 30 | exitscip-perl cpan Perl-Critic-Pulp 100 main/exit(). 0; |
| 31 | |
| 32 | |
| 33 | |
| 34 | package Test::FileIsPartOfDistscip-perl cpan Perl-Critic-Pulp 100 Test/FileIsPartOfDist/; |
| 35 | BEGIN { requirescip-perl cpan Perl-Critic-Pulp 100 Test/FileIsPartOfDist/require(). 5 } |
| 36 | use strictscip-perl cpan Perl-Critic-Pulp 100 strict/; |
| 37 | use ExtUtils::Manifestscip-perl cpan Perl-Critic-Pulp 100 ExtUtils/Manifest/; |
| 38 | use File::Slurpscip-perl cpan Perl-Critic-Pulp 100 File/Slurp/; |
| 39 | |
| 40 | # uncomment this to run the ### lines |
| 41 | # use Smart::Comments; |
| 42 | |
| 43 | sub importscip-perl cpan Perl-Critic-Pulp 100 Test/FileIsPartOfDist/import(). { |
| 44 | my $class = shiftscip-perl cpan Perl-Critic-Pulp 100 Test/FileIsPartOfDist/shift().; |
| 45 | my $arg; |
| 46 | foreach $arg (@_scip-perl cpan Perl-Critic-Pulp 100 Test/FileIsPartOfDist/`@_`.) { |
| 47 | if ($arg eq '-test') { |
| 48 | requirescip-perl cpan Perl-Critic-Pulp 100 Test/FileIsPartOfDist/require(). Test; |
| 49 | Test::planscip-perl cpan Perl-Critic-Pulp 100 Test/plan().(tests=>1); |
| 50 | isscip-perl cpan Perl-Critic-Pulp 100 Test/FileIsPartOfDist/is(). ($class->checkscip-perl cpan Perl-Critic-Pulp 100 Test/FileIsPartOfDist/check()., 1, 'Test::FileIsPartOfDist'); |
| 51 | } |
| 52 | } |
| 53 | return 1; |
| 54 | } |
| 55 | |
| 56 | sub newscip-perl cpan Perl-Critic-Pulp 100 Test/FileIsPartOfDist/new(). { |
| 57 | my $class = shiftscip-perl cpan Perl-Critic-Pulp 100 Test/FileIsPartOfDist/shift().; |
| 58 | return blessscip-perl cpan Perl-Critic-Pulp 100 Test/FileIsPartOfDist/bless(). { @_scip-perl cpan Perl-Critic-Pulp 100 Test/FileIsPartOfDist/`@_`. }, $class; |
| 59 | } |
| 60 | |
| 61 | sub checkscip-perl cpan Perl-Critic-Pulp 100 Test/FileIsPartOfDist/check(). { |
| 62 | my $class = shiftscip-perl cpan Perl-Critic-Pulp 100 Test/FileIsPartOfDist/shift().; |
| 63 | my $self = $class->newscip-perl cpan Perl-Critic-Pulp 100 Test/FileIsPartOfDist/new().(@_scip-perl cpan Perl-Critic-Pulp 100 Test/FileIsPartOfDist/`@_`.); |
| 64 | |
| 65 | my $manifest = ExtUtils::Manifest::manireadscip-perl cpan Perl-Critic-Pulp 100 ExtUtils/Manifest/maniread().(); |
| 66 | if (! $manifest) { |
| 67 | $self->diagscip-perl cpan Perl-Critic-Pulp 100 Test/FileIsPartOfDist/diag().("no MANIFEST perhaps"); |
| 68 | return 0; |
| 69 | } |
| 70 | my @filenames = keysscip-perl cpan Perl-Critic-Pulp 100 Test/FileIsPartOfDist/keys(). %$manifest; |
| 71 | |
| 72 | my $distname = $self->makefile_distnamescip-perl cpan Perl-Critic-Pulp 100 Test/FileIsPartOfDist/makefile_distname().; |
| 73 | if (! definedscip-perl cpan Perl-Critic-Pulp 100 Test/FileIsPartOfDist/defined(). $distname) { |
| 74 | $self->diagscip-perl cpan Perl-Critic-Pulp 100 Test/FileIsPartOfDist/diag().("Oops, DISTNAME not found in Makefile"); |
| 75 | return 0; |
| 76 | } |
| 77 | if ($self->{'verbose'}) { |
| 78 | $self->diagscip-perl cpan Perl-Critic-Pulp 100 Test/FileIsPartOfDist/diag().("DISTNAME $distname"); |
| 79 | } |
| 80 | |
| 81 | my $good = 1; |
| 82 | my $filename; |
| 83 | foreach $filename (@filenames) { |
| 84 | if (! $self->check_file_is_part_ofscip-perl cpan Perl-Critic-Pulp 100 Test/FileIsPartOfDist/check_file_is_part_of().($filename,$distname)) { |
| 85 | $good = 0; |
| 86 | } |
| 87 | } |
| 88 | return $good; |
| 89 | } |
| 90 | |
| 91 | sub makefile_distnamescip-perl cpan Perl-Critic-Pulp 100 Test/FileIsPartOfDist/makefile_distname(). { |
| 92 | my ($self) = @_scip-perl cpan Perl-Critic-Pulp 100 Test/FileIsPartOfDist/`@_`.; |
| 93 | my $filename = "Makefile"; |
| 94 | my $content = File::Slurp::read_filescip-perl cpan Perl-Critic-Pulp 100 File/Slurp/read_file(). ($filename); |
| 95 | if (! definedscip-perl cpan Perl-Critic-Pulp 100 Test/FileIsPartOfDist/defined(). $content) { |
| 96 | $self->diagscip-perl cpan Perl-Critic-Pulp 100 Test/FileIsPartOfDist/diag().("Cannot read $filename: $!"); |
| 97 | return undef; |
| 98 | } |
| 99 | my $distname; |
| 100 | if ($content =~ /^DISTNAME\s*=\s*([^#\n]*)/m) { |
| 101 | $distname = $1scip-perl cpan Perl-Critic-Pulp 100 Test/FileIsPartOfDist/$1.; |
| 102 | $distname =~ s/\s+$//; |
| 103 | ### $distname |
| 104 | if ($distname eq 'App-Chart') { $distname = 'Chart'; } # hack |
| 105 | } |
| 106 | return $distname; |
| 107 | } |
| 108 | |
| 109 | sub check_file_is_part_ofscip-perl cpan Perl-Critic-Pulp 100 Test/FileIsPartOfDist/check_file_is_part_of(). { |
| 110 | my ($self, $filename, $distname) = @_scip-perl cpan Perl-Critic-Pulp 100 Test/FileIsPartOfDist/`@_`.; |
| 111 | |
| 112 | my $content = File::Slurp::read_filescip-perl cpan Perl-Critic-Pulp 100 File/Slurp/read_file(). ($filename); |
| 113 | if (! definedscip-perl cpan Perl-Critic-Pulp 100 Test/FileIsPartOfDist/defined(). $content) { |
| 114 | $self->diagscip-perl cpan Perl-Critic-Pulp 100 Test/FileIsPartOfDist/diag().("Cannot read $filename: $!"); |
| 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | $content =~ /([T]his file is part of[^\n]*)/i |
| 119 | or return 1; |
| 120 | my $got = $1scip-perl cpan Perl-Critic-Pulp 100 Test/FileIsPartOfDist/$1.; |
| 121 | if ($got =~ /[T]his file is part of \Q$distname\E\b/i) { |
| 122 | return 1; |
| 123 | } |
| 124 | $self->diagscip-perl cpan Perl-Critic-Pulp 100 Test/FileIsPartOfDist/diag().("$filename: $got"); |
| 125 | $self->diagscip-perl cpan Perl-Critic-Pulp 100 Test/FileIsPartOfDist/diag().("expected DISTNAME: $distname"); |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | sub diagscip-perl cpan Perl-Critic-Pulp 100 Test/FileIsPartOfDist/diag(). { |
| 130 | my $self = shiftscip-perl cpan Perl-Critic-Pulp 100 Test/FileIsPartOfDist/shift().; |
| 131 | my $func = $self->{'diag_func'} |
| 132 | || eval { Test::More->canscip-perl cpan Perl-Critic-Pulp 100 Test/More/can().('diag') } |
| 133 | || \&_diagscip-perl cpan Perl-Critic-Pulp 100 Test/FileIsPartOfDist/_diag().; |
| 134 | &$funcscip-perl cpan Perl-Critic-Pulp 100 Test/FileIsPartOfDist/$func().(@_scip-perl cpan Perl-Critic-Pulp 100 Test/FileIsPartOfDist/`@_`.); |
| 135 | } |
| 136 | sub _diagscip-perl cpan Perl-Critic-Pulp 100 Test/FileIsPartOfDist/_diag(). { |
| 137 | my $msg = joinscip-perl cpan Perl-Critic-Pulp 100 Test/FileIsPartOfDist/join().('', mapscip-perl cpan Perl-Critic-Pulp 100 Test/FileIsPartOfDist/map(). {definedscip-perl cpan Perl-Critic-Pulp 100 Test/FileIsPartOfDist/defined().($_scip-perl cpan Perl-Critic-Pulp 100 Test/FileIsPartOfDist/$_.)?$_scip-perl cpan Perl-Critic-Pulp 100 Test/FileIsPartOfDist/$_.:'[undef]'} @_scip-perl cpan Perl-Critic-Pulp 100 Test/FileIsPartOfDist/`@_`.)."\n"; |
| 138 | $msg =~ s/^/# /mg; |
| 139 | print STDERR $msg; |
| 140 | } |