Use App::Prove::State to store the timings for the tests, and if
[p5sagit/p5-mst-13.2.git] / lib / ExtUtils / t / Manifest.t
CommitLineData
f6d6199c 1#!/usr/bin/perl -w
0300da75 2
3BEGIN {
39234879 4 if( $ENV{PERL_CORE} ) {
5 chdir 't' if -d 't';
6 unshift @INC, '../lib';
7 }
f6d6199c 8 else {
9 unshift @INC, 't/lib';
10 }
0300da75 11}
39234879 12chdir 't';
0300da75 13
f6d6199c 14use strict;
15
1c14aae0 16use Test::More tests => 66;
0300da75 17use Cwd;
18
0300da75 19use File::Spec;
20use File::Path;
a7d1454b 21use File::Find;
1c14aae0 22use Config;
a7d1454b 23
24my $Is_VMS = $^O eq 'VMS';
57b1a898 25
26# We're going to be chdir'ing and modules are sometimes loaded on the
27# fly in this test, so we need an absolute @INC.
28@INC = map { File::Spec->rel2abs($_) } @INC;
0300da75 29
30# keep track of everything added so it can all be deleted
2530b651 31my %Files;
0300da75 32sub add_file {
479d2113 33 my ($file, $data) = @_;
34 $data ||= 'foo';
2530b651 35 1 while unlink $file; # or else we'll get multiple versions on VMS
479d2113 36 open( T, '>'.$file) or return;
37 print T $data;
2530b651 38 ++$Files{$file};
57b1a898 39 close T;
0300da75 40}
41
42sub read_manifest {
a7d1454b 43 open( M, 'MANIFEST' ) or return;
44 chomp( my @files = <M> );
57b1a898 45 close M;
a7d1454b 46 return @files;
0300da75 47}
48
49sub catch_warning {
b3217f3b 50 my $warn = '';
a7d1454b 51 local $SIG{__WARN__} = sub { $warn .= $_[0] };
52 return join('', $_[0]->() ), $warn;
0300da75 53}
54
55sub remove_dir {
a7d1454b 56 ok( rmdir( $_ ), "remove $_ directory" ) for @_;
0300da75 57}
58
59# use module, import functions
f6d6199c 60BEGIN {
61 use_ok( 'ExtUtils::Manifest',
62 qw( mkmanifest manicheck filecheck fullcheck
479d2113 63 maniread manicopy skipcheck maniadd) );
f6d6199c 64}
0300da75 65
66my $cwd = Cwd::getcwd();
67
68# Just in case any old files were lying around.
69rmtree('mantest');
70
71ok( mkdir( 'mantest', 0777 ), 'make mantest directory' );
72ok( chdir( 'mantest' ), 'chdir() to mantest' );
73ok( add_file('foo'), 'add a temporary file' );
74
1c14aae0 75# This ensures the -x check for manicopy means something
76# Some platforms don't have chmod or an executable bit, in which case
77# this call will do nothing or fail, but on the platforms where chmod()
78# works, we test the executable bit is copied
79chmod( 0744, 'foo') if $Config{'chmod'};
80
0300da75 81# there shouldn't be a MANIFEST there
b3217f3b 82my ($res, $warn) = catch_warning( \&mkmanifest );
f2e6bef3 83# Canonize the order.
f6d6199c 84$warn = join("", map { "$_|" }
85 sort { lc($a) cmp lc($b) } split /\r?\n/, $warn);
f2e6bef3 86is( $warn, "Added to MANIFEST: foo|Added to MANIFEST: MANIFEST|",
f6d6199c 87 "mkmanifest() displayed its additions" );
0300da75 88
89# and now you see it
90ok( -e 'MANIFEST', 'create MANIFEST file' );
91
92my @list = read_manifest();
93is( @list, 2, 'check files in MANIFEST' );
94ok( ! ExtUtils::Manifest::filecheck(), 'no additional files in directory' );
95
96# after adding bar, the MANIFEST is out of date
97ok( add_file( 'bar' ), 'add another file' );
98ok( ! manicheck(), 'MANIFEST now out of sync' );
99
100# it reports that bar has been added and throws a warning
101($res, $warn) = catch_warning( \&filecheck );
102
103like( $warn, qr/^Not in MANIFEST: bar/, 'warning that bar has been added' );
104is( $res, 'bar', 'bar reported as new' );
105
106# now quiet the warning that bar was added and test again
b3217f3b 107($res, $warn) = do { local $ExtUtils::Manifest::Quiet = 1;
108 catch_warning( \&skipcheck )
f6d6199c 109 };
b3217f3b 110is( $warn, '', 'disabled warnings' );
0300da75 111
f6d6199c 112# add a skip file with a rule to skip itself (and the nonexistent glob '*baz*')
0300da75 113add_file( 'MANIFEST.SKIP', "baz\n.SKIP" );
114
115# this'll skip the new file
f6d6199c 116($res, $warn) = catch_warning( \&skipcheck );
117like( $warn, qr/^Skipping MANIFEST\.SKIP/i, 'got skipping warning' );
0300da75 118
45bc4d3a 119my @skipped;
0300da75 120catch_warning( sub {
4c857482 121 @skipped = skipcheck()
0300da75 122});
123
45bc4d3a 124is( join( ' ', @skipped ), 'MANIFEST.SKIP', 'listed skipped files' );
0300da75 125
f6d6199c 126{
127 local $ExtUtils::Manifest::Quiet = 1;
128 is( join(' ', filecheck() ), 'bar', 'listing skipped with filecheck()' );
129}
0300da75 130
131# add a subdirectory and a file there that should be found
132ok( mkdir( 'moretest', 0777 ), 'created moretest directory' );
f6d6199c 133add_file( File::Spec->catfile('moretest', 'quux'), 'quux' );
134ok( exists( ExtUtils::Manifest::manifind()->{'moretest/quux'} ),
135 "manifind found moretest/quux" );
0300da75 136
137# only MANIFEST and foo are in the manifest
2530b651 138$_ = 'foo';
0300da75 139my $files = maniread();
140is( keys %$files, 2, 'two files found' );
f6d6199c 141is( join(' ', sort { lc($a) cmp lc($b) } keys %$files), 'foo MANIFEST',
142 'both files found' );
2530b651 143is( $_, 'foo', q{maniread() doesn't clobber $_} );
0300da75 144
a7d1454b 145ok( mkdir( 'copy', 0777 ), 'made copy directory' );
146
147# Check that manicopy copies files.
148manicopy( $files, 'copy', 'cp' );
149my @copies = ();
150find( sub { push @copies, $_ if -f }, 'copy' );
151@copies = map { s/\.$//; $_ } @copies if $Is_VMS; # VMS likes to put dots on
152 # the end of files.
153# Have to compare insensitively for non-case preserving VMS
154is_deeply( [sort map { lc } @copies], [sort map { lc } keys %$files] );
155
156# cp would leave files readonly, so check permissions.
157foreach my $orig (@copies) {
158 my $copy = "copy/$orig";
159 ok( -r $copy, "$copy: must be readable" );
4c857482 160 is( -w $copy, -w $orig, " writable if original was" );
161 is( -x $copy, -x $orig, " executable if original was" );
a7d1454b 162}
163rmtree('copy');
164
165
0300da75 166# poison the manifest, and add a comment that should be reported
167add_file( 'MANIFEST', 'none #none' );
f6d6199c 168is( ExtUtils::Manifest::maniread()->{none}, '#none',
169 'maniread found comment' );
0300da75 170
171ok( mkdir( 'copy', 0777 ), 'made copy directory' );
0300da75 172$files = maniread();
173eval { (undef, $warn) = catch_warning( sub {
b3217f3b 174 manicopy( $files, 'copy', 'cp' ) })
0300da75 175};
57b1a898 176like( $@, qr/^Can't read none: /, 'croaked about none' );
0300da75 177
178# a newline comes through, so get rid of it
179chomp($warn);
180
181# the copy should have given one warning and one error
f6d6199c 182like($warn, qr/^Skipping MANIFEST.SKIP/i, 'warned about MANIFEST.SKIP' );
0300da75 183
184# tell ExtUtils::Manifest to use a different file
f6d6199c 185{
186 local $ExtUtils::Manifest::MANIFEST = 'albatross';
187 ($res, $warn) = catch_warning( \&mkmanifest );
188 like( $warn, qr/Added to albatross: /, 'using a new manifest file' );
b3217f3b 189
f6d6199c 190 # add the new file to the list of files to be deleted
2530b651 191 $Files{'albatross'}++;
39234879 192}
0300da75 193
0300da75 194
f6d6199c 195# Make sure MANIFEST.SKIP is using complete relative paths
196add_file( 'MANIFEST.SKIP' => "^moretest/q\n" );
197
198# This'll skip moretest/quux
199($res, $warn) = catch_warning( \&skipcheck );
45bc4d3a 200like( $warn, qr{^Skipping moretest/quux$}i, 'got skipping warning again' );
201
202
203# There was a bug where entries in MANIFEST would be blotted out
204# by MANIFEST.SKIP rules.
205add_file( 'MANIFEST.SKIP' => 'foo' );
479d2113 206add_file( 'MANIFEST' => "foobar\n" );
45bc4d3a 207add_file( 'foobar' => '123' );
208($res, $warn) = catch_warning( \&manicheck );
4c857482 209is( $res, '', 'MANIFEST overrides MANIFEST.SKIP' );
b3217f3b 210is( $warn, '', 'MANIFEST overrides MANIFEST.SKIP, no warnings' );
f6d6199c 211
479d2113 212$files = maniread;
213ok( !$files->{wibble}, 'MANIFEST in good state' );
214maniadd({ wibble => undef });
215maniadd({ yarrow => "hock" });
216$files = maniread;
217is( $files->{wibble}, '', 'maniadd() with undef comment' );
218is( $files->{yarrow}, 'hock',' with comment' );
219is( $files->{foobar}, '', ' preserved old entries' );
5ca25ae7 220
1c14aae0 221# test including an external manifest.skip file in MANIFEST.SKIP
222{
223 maniadd({ foo => undef , albatross => undef,
224 'mymanifest.skip' => undef, 'mydefault.skip' => undef});
225 add_file('mymanifest.skip' => "^foo\n");
226 add_file('mydefault.skip' => "^my\n");
227 $ExtUtils::Manifest::DEFAULT_MSKIP =
228 File::Spec->catfile($cwd, qw(mantest mydefault.skip));
229 my $skip = File::Spec->catfile($cwd, qw(mantest mymanifest.skip));
230 add_file('MANIFEST.SKIP' =>
231 "albatross\n#!include $skip\n#!include_default");
232 my ($res, $warn) = catch_warning( \&skipcheck );
233 for (qw(albatross foo foobar mymanifest.skip mydefault.skip)) {
234 like( $warn, qr/Skipping \b$_\b/,
235 "Skipping $_" );
236 }
237 ($res, $warn) = catch_warning( \&mkmanifest );
238 for (qw(albatross foo foobar mymanifest.skip mydefault.skip)) {
239 like( $warn, qr/Removed from MANIFEST: \b$_\b/,
240 "Removed $_ from MANIFEST" );
241 }
242 my $files = maniread;
243 ok( ! exists $files->{albatross}, 'albatross excluded via MANIFEST.SKIP' );
244 ok( exists $files->{yarrow}, 'yarrow included in MANIFEST' );
245 ok( exists $files->{bar}, 'bar included in MANIFEST' );
246 ok( ! exists $files->{foobar}, 'foobar excluded via mymanifest.skip' );
247 ok( ! exists $files->{foo}, 'foo excluded via mymanifest.skip' );
248 ok( ! exists $files->{'mymanifest.skip'},
249 'mymanifest.skip excluded via mydefault.skip' );
250 ok( ! exists $files->{'mydefault.skip'},
251 'mydefault.skip excluded via mydefault.skip' );
a2fa79ff 252 my $extsep = $Is_VMS ? '_' : '.';
253 $Files{"$_.bak"}++ for ('MANIFEST', "MANIFEST${extsep}SKIP");
1c14aae0 254}
255
2530b651 256add_file('MANIFEST' => 'Makefile.PL');
9d058bf8 257maniadd({ foo => 'bar' });
2530b651 258$files = maniread;
259# VMS downcases the MANIFEST. We normalize it here to match.
260%$files = map { (lc $_ => $files->{$_}) } keys %$files;
261my %expect = ( 'makefile.pl' => '',
5ca25ae7 262 'foo' => 'bar'
263 );
2530b651 264is_deeply( $files, \%expect, 'maniadd() vs MANIFEST without trailing newline');
0300da75 265
1c14aae0 266#add_file('MANIFEST' => 'Makefile.PL');
267#maniadd({ foo => 'bar' });
5ca25ae7 268
2c91f887 269SKIP: {
270 chmod( 0400, 'MANIFEST' );
271 skip "Can't make MANIFEST read-only", 2 if -w 'MANIFEST';
272
380d5532 273 eval {
274 maniadd({ 'foo' => 'bar' });
275 };
2c91f887 276 is( $@, '', "maniadd() won't open MANIFEST if it doesn't need to" );
277
278 eval {
279 maniadd({ 'grrrwoof' => 'yippie' });
280 };
30361541 281 like( $@, qr/^\Qmaniadd() could not open MANIFEST:\E/,
2c91f887 282 "maniadd() dies if it can't open the MANIFEST" );
283
0aa703b2 284 chmod( 0600, 'MANIFEST' );
2c91f887 285}
a7d1454b 286
2c91f887 287
0300da75 288END {
2530b651 289 is( unlink( keys %Files ), keys %Files, 'remove all added files' );
0300da75 290 remove_dir( 'moretest', 'copy' );
291
292 # now get rid of the parent directory
293 ok( chdir( $cwd ), 'return to parent directory' );
294 remove_dir( 'mantest' );
295}
349e1be1 296