Upgrade to ExtUtils::Command 1.15
[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
6dbcfe36 16use Test::More tests => 94;
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
6dbcfe36 36 open( T, '> '.$file) or return;
479d2113 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
6dbcfe36 63 maniread manicopy skipcheck maniadd maniskip) );
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};
176
177# a newline comes through, so get rid of it
178chomp($warn);
6dbcfe36 179# the copy should have given a warning
180like($warn, qr/^none not found/, 'carped about none' );
181($res, $warn) = catch_warning( \&skipcheck );
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
6dbcfe36 221my %funky_files;
222# test including a filename with a space
223SKIP: {
224 add_file( 'foo bar' => "space" )
225 or skip "couldn't create spaced test file", 2;
226 local $ExtUtils::Manifest::MANIFEST = "albatross";
227 maniadd({ 'foo bar' => "contains space"});
228 is( maniread()->{'foo bar'}, "contains space",
229 'spaced manifest filename' );
230 add_file( 'albatross.bak', '' );
231 ($res, $warn) = catch_warning( \&mkmanifest );
232 like( $warn, qr/\A(Added to.*\n)+\z/m,
233 'no warnings about funky filename' );
234 $funky_files{'space'} = 'foo bar';
235}
236
237# test including a filename with a space and a quote
238SKIP: {
239 add_file( 'foo\' baz\'quux' => "quote" )
240 or skip "couldn't create quoted test file", 1;
241 local $ExtUtils::Manifest::MANIFEST = "albatross";
242 maniadd({ 'foo\' baz\'quux' => "contains quote"});
243 is( maniread()->{'foo\' baz\'quux'}, "contains quote",
244 'quoted manifest filename' );
245 $funky_files{'space_quote'} = 'foo\' baz\'quux';
246}
247
248# test including a filename with a space and a backslash
249SKIP: {
250 add_file( 'foo bar\\baz' => "backslash" )
251 or skip "couldn't create backslash test file", 1;
252 local $ExtUtils::Manifest::MANIFEST = "albatross";
253 maniadd({ 'foo bar\\baz' => "contains backslash"});
254 is( maniread()->{'foo bar\\baz'}, "contains backslash",
255 'backslashed manifest filename' );
256 $funky_files{'space_backslash'} = 'foo bar\\baz';
257}
258
259# test including a filename with a space, quote, and a backslash
260SKIP: {
261 add_file( 'foo bar\\baz\'quux' => "backslash/quote" )
262 or skip "couldn't create backslash/quote test file", 1;
263 local $ExtUtils::Manifest::MANIFEST = "albatross";
264 maniadd({ 'foo bar\\baz\'quux' => "backslash and quote"});
265 is( maniread()->{'foo bar\\baz\'quux'}, "backslash and quote",
266 'backslashed and quoted manifest filename' );
267 $funky_files{'space_quote_backslash'} = 'foo bar\\baz\'quux';
268}
269
270my @funky_keys = qw(space space_quote space_backslash space_quote_backslash);
1c14aae0 271# test including an external manifest.skip file in MANIFEST.SKIP
272{
273 maniadd({ foo => undef , albatross => undef,
274 'mymanifest.skip' => undef, 'mydefault.skip' => undef});
6dbcfe36 275 for (@funky_keys) {
276 maniadd( {$funky_files{$_} => $_} ) if defined $funky_files{$_};
277 }
278
1c14aae0 279 add_file('mymanifest.skip' => "^foo\n");
280 add_file('mydefault.skip' => "^my\n");
6dbcfe36 281 local $ExtUtils::Manifest::DEFAULT_MSKIP =
1c14aae0 282 File::Spec->catfile($cwd, qw(mantest mydefault.skip));
283 my $skip = File::Spec->catfile($cwd, qw(mantest mymanifest.skip));
284 add_file('MANIFEST.SKIP' =>
285 "albatross\n#!include $skip\n#!include_default");
286 my ($res, $warn) = catch_warning( \&skipcheck );
287 for (qw(albatross foo foobar mymanifest.skip mydefault.skip)) {
288 like( $warn, qr/Skipping \b$_\b/,
289 "Skipping $_" );
290 }
6dbcfe36 291 for my $funky_key (@funky_keys) {
292 SKIP: {
293 my $funky_file = $funky_files{$funky_key};
294 skip "'$funky_key' not created", 1 unless $funky_file;
295 like( $warn, qr/Skipping \b\Q$funky_file\E\b/,
296 "Skipping $funky_file");
297 }
298 }
1c14aae0 299 ($res, $warn) = catch_warning( \&mkmanifest );
300 for (qw(albatross foo foobar mymanifest.skip mydefault.skip)) {
301 like( $warn, qr/Removed from MANIFEST: \b$_\b/,
302 "Removed $_ from MANIFEST" );
303 }
6dbcfe36 304 for my $funky_key (@funky_keys) {
305 SKIP: {
306 my $funky_file = $funky_files{$funky_key};
307 skip "'$funky_key' not created", 1 unless $funky_file;
308 like( $warn, qr/Removed from MANIFEST: \b\Q$funky_file\E\b/,
309 "Removed $funky_file from MANIFEST");
310 }
311 }
1c14aae0 312 my $files = maniread;
313 ok( ! exists $files->{albatross}, 'albatross excluded via MANIFEST.SKIP' );
314 ok( exists $files->{yarrow}, 'yarrow included in MANIFEST' );
315 ok( exists $files->{bar}, 'bar included in MANIFEST' );
316 ok( ! exists $files->{foobar}, 'foobar excluded via mymanifest.skip' );
317 ok( ! exists $files->{foo}, 'foo excluded via mymanifest.skip' );
318 ok( ! exists $files->{'mymanifest.skip'},
319 'mymanifest.skip excluded via mydefault.skip' );
320 ok( ! exists $files->{'mydefault.skip'},
321 'mydefault.skip excluded via mydefault.skip' );
6dbcfe36 322
323 # test exclusion of funky files
324 for my $funky_key (@funky_keys) {
325 SKIP: {
326 my $funky_file = $funky_files{$funky_key};
327 skip "'$funky_key' not created", 1 unless $funky_file;
328 ok( ! exists $files->{$funky_file},
329 "'$funky_file' excluded via mymanifest.skip" );
330 }
331 }
332
333 # tests for maniskip
334 my $skipchk = maniskip();
335 is ( $skipchk->('albatross'), 1,
336 'albatross excluded via MANIFEST.SKIP' );
337 is( $skipchk->('yarrow'), '',
338 'yarrow included in MANIFEST' );
339 is( $skipchk->('bar'), '',
340 'bar included in MANIFEST' );
341 $skipchk = maniskip('mymanifest.skip');
342 is( $skipchk->('foobar'), 1,
343 'foobar excluded via mymanifest.skip' );
344 is( $skipchk->('foo'), 1,
345 'foo excluded via mymanifest.skip' );
346 is( $skipchk->('mymanifest.skip'), '',
347 'mymanifest.skip included via mydefault.skip' );
348 is( $skipchk->('mydefault.skip'), '',
349 'mydefault.skip included via mydefault.skip' );
350 $skipchk = maniskip('mydefault.skip');
351 is( $skipchk->('foobar'), '',
352 'foobar included via mydefault.skip' );
353 is( $skipchk->('foo'), '',
354 'foo included via mydefault.skip' );
355 is( $skipchk->('mymanifest.skip'), 1,
356 'mymanifest.skip excluded via mydefault.skip' );
357 is( $skipchk->('mydefault.skip'), 1,
358 'mydefault.skip excluded via mydefault.skip' );
359
a2fa79ff 360 my $extsep = $Is_VMS ? '_' : '.';
361 $Files{"$_.bak"}++ for ('MANIFEST', "MANIFEST${extsep}SKIP");
1c14aae0 362}
363
2530b651 364add_file('MANIFEST' => 'Makefile.PL');
9d058bf8 365maniadd({ foo => 'bar' });
2530b651 366$files = maniread;
367# VMS downcases the MANIFEST. We normalize it here to match.
368%$files = map { (lc $_ => $files->{$_}) } keys %$files;
369my %expect = ( 'makefile.pl' => '',
5ca25ae7 370 'foo' => 'bar'
371 );
2530b651 372is_deeply( $files, \%expect, 'maniadd() vs MANIFEST without trailing newline');
0300da75 373
1c14aae0 374#add_file('MANIFEST' => 'Makefile.PL');
375#maniadd({ foo => 'bar' });
5ca25ae7 376
2c91f887 377SKIP: {
378 chmod( 0400, 'MANIFEST' );
379 skip "Can't make MANIFEST read-only", 2 if -w 'MANIFEST';
380
380d5532 381 eval {
382 maniadd({ 'foo' => 'bar' });
383 };
2c91f887 384 is( $@, '', "maniadd() won't open MANIFEST if it doesn't need to" );
385
386 eval {
387 maniadd({ 'grrrwoof' => 'yippie' });
388 };
30361541 389 like( $@, qr/^\Qmaniadd() could not open MANIFEST:\E/,
2c91f887 390 "maniadd() dies if it can't open the MANIFEST" );
391
0aa703b2 392 chmod( 0600, 'MANIFEST' );
2c91f887 393}
a7d1454b 394
2c91f887 395
0300da75 396END {
2530b651 397 is( unlink( keys %Files ), keys %Files, 'remove all added files' );
0300da75 398 remove_dir( 'moretest', 'copy' );
399
400 # now get rid of the parent directory
401 ok( chdir( $cwd ), 'return to parent directory' );
402 remove_dir( 'mantest' );
403}
349e1be1 404