This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Move ExtUtils-Manifest to cpan/
[perl5.git] / cpan / ExtUtils-Manifest / t / Manifest.t
CommitLineData
f6d6199c 1#!/usr/bin/perl -w
0300da75
MS
2
3BEGIN {
39234879
MS
4 if( $ENV{PERL_CORE} ) {
5 chdir 't' if -d 't';
6 unshift @INC, '../lib';
7 }
f6d6199c
MS
8 else {
9 unshift @INC, 't/lib';
10 }
0300da75 11}
39234879 12chdir 't';
0300da75 13
f6d6199c
MS
14use strict;
15
e4ac890e 16use Test::More tests => 96;
0300da75
MS
17use Cwd;
18
0300da75
MS
19use File::Spec;
20use File::Path;
a7d1454b 21use File::Find;
1c14aae0 22use Config;
a7d1454b
RGS
23
24my $Is_VMS = $^O eq 'VMS';
7e4d7138
SH
25my $Is_VMS_noefs = $Is_VMS;
26if ($Is_VMS) {
27 my $vms_efs = 0;
28 if (eval 'require VMS::Feature') {
29 $vms_efs = VMS::Feature::current("efs_charset");
30 } else {
31 my $efs_charset = $ENV{'DECC$EFS_CHARSET'} || '';
04721f73 32 $vms_efs = $efs_charset =~ /^[ET1]/i;
7e4d7138
SH
33 }
34 $Is_VMS_noefs = 0 if $vms_efs;
35}
36
57b1a898
MS
37
38# We're going to be chdir'ing and modules are sometimes loaded on the
39# fly in this test, so we need an absolute @INC.
40@INC = map { File::Spec->rel2abs($_) } @INC;
0300da75
MS
41
42# keep track of everything added so it can all be deleted
2530b651 43my %Files;
0300da75 44sub add_file {
479d2113
MS
45 my ($file, $data) = @_;
46 $data ||= 'foo';
8a753a87 47 $file =~ s/ /^_/g if $Is_VMS_noefs; # escape spaces
2530b651 48 1 while unlink $file; # or else we'll get multiple versions on VMS
6dbcfe36 49 open( T, '> '.$file) or return;
479d2113 50 print T $data;
57b1a898 51 close T;
7a5ea4ae
NC
52 return 0 unless -e $file; # exists under the name we gave it ?
53 ++$Files{$file};
0300da75
MS
54}
55
56sub read_manifest {
a7d1454b
RGS
57 open( M, 'MANIFEST' ) or return;
58 chomp( my @files = <M> );
57b1a898 59 close M;
a7d1454b 60 return @files;
0300da75
MS
61}
62
63sub catch_warning {
b3217f3b 64 my $warn = '';
a7d1454b
RGS
65 local $SIG{__WARN__} = sub { $warn .= $_[0] };
66 return join('', $_[0]->() ), $warn;
0300da75
MS
67}
68
69sub remove_dir {
a7d1454b 70 ok( rmdir( $_ ), "remove $_ directory" ) for @_;
0300da75
MS
71}
72
73# use module, import functions
04721f73
FR
74BEGIN {
75 use_ok( 'ExtUtils::Manifest',
76 qw( mkmanifest manicheck filecheck fullcheck
77 maniread manicopy skipcheck maniadd maniskip) );
f6d6199c 78}
0300da75
MS
79
80my $cwd = Cwd::getcwd();
81
82# Just in case any old files were lying around.
83rmtree('mantest');
84
85ok( mkdir( 'mantest', 0777 ), 'make mantest directory' );
86ok( chdir( 'mantest' ), 'chdir() to mantest' );
87ok( add_file('foo'), 'add a temporary file' );
88
1c14aae0
RGS
89# This ensures the -x check for manicopy means something
90# Some platforms don't have chmod or an executable bit, in which case
91# this call will do nothing or fail, but on the platforms where chmod()
92# works, we test the executable bit is copied
93chmod( 0744, 'foo') if $Config{'chmod'};
94
0300da75 95# there shouldn't be a MANIFEST there
b3217f3b 96my ($res, $warn) = catch_warning( \&mkmanifest );
f2e6bef3 97# Canonize the order.
04721f73 98$warn = join("", map { "$_|" }
f6d6199c 99 sort { lc($a) cmp lc($b) } split /\r?\n/, $warn);
f2e6bef3 100is( $warn, "Added to MANIFEST: foo|Added to MANIFEST: MANIFEST|",
f6d6199c 101 "mkmanifest() displayed its additions" );
0300da75
MS
102
103# and now you see it
104ok( -e 'MANIFEST', 'create MANIFEST file' );
105
106my @list = read_manifest();
107is( @list, 2, 'check files in MANIFEST' );
108ok( ! ExtUtils::Manifest::filecheck(), 'no additional files in directory' );
109
110# after adding bar, the MANIFEST is out of date
111ok( add_file( 'bar' ), 'add another file' );
112ok( ! manicheck(), 'MANIFEST now out of sync' );
113
114# it reports that bar has been added and throws a warning
115($res, $warn) = catch_warning( \&filecheck );
116
117like( $warn, qr/^Not in MANIFEST: bar/, 'warning that bar has been added' );
118is( $res, 'bar', 'bar reported as new' );
119
120# now quiet the warning that bar was added and test again
b3217f3b
SP
121($res, $warn) = do { local $ExtUtils::Manifest::Quiet = 1;
122 catch_warning( \&skipcheck )
f6d6199c 123 };
b3217f3b 124is( $warn, '', 'disabled warnings' );
0300da75 125
f6d6199c 126# add a skip file with a rule to skip itself (and the nonexistent glob '*baz*')
0300da75
MS
127add_file( 'MANIFEST.SKIP', "baz\n.SKIP" );
128
129# this'll skip the new file
f6d6199c
MS
130($res, $warn) = catch_warning( \&skipcheck );
131like( $warn, qr/^Skipping MANIFEST\.SKIP/i, 'got skipping warning' );
0300da75 132
45bc4d3a 133my @skipped;
0300da75 134catch_warning( sub {
4c857482 135 @skipped = skipcheck()
0300da75
MS
136});
137
45bc4d3a 138is( join( ' ', @skipped ), 'MANIFEST.SKIP', 'listed skipped files' );
0300da75 139
f6d6199c
MS
140{
141 local $ExtUtils::Manifest::Quiet = 1;
142 is( join(' ', filecheck() ), 'bar', 'listing skipped with filecheck()' );
143}
0300da75
MS
144
145# add a subdirectory and a file there that should be found
146ok( mkdir( 'moretest', 0777 ), 'created moretest directory' );
f6d6199c 147add_file( File::Spec->catfile('moretest', 'quux'), 'quux' );
04721f73 148ok( exists( ExtUtils::Manifest::manifind()->{'moretest/quux'} ),
f6d6199c 149 "manifind found moretest/quux" );
0300da75
MS
150
151# only MANIFEST and foo are in the manifest
2530b651 152$_ = 'foo';
0300da75
MS
153my $files = maniread();
154is( keys %$files, 2, 'two files found' );
04721f73 155is( join(' ', sort { lc($a) cmp lc($b) } keys %$files), 'foo MANIFEST',
f6d6199c 156 'both files found' );
2530b651 157is( $_, 'foo', q{maniread() doesn't clobber $_} );
0300da75 158
a7d1454b
RGS
159ok( mkdir( 'copy', 0777 ), 'made copy directory' );
160
161# Check that manicopy copies files.
162manicopy( $files, 'copy', 'cp' );
163my @copies = ();
164find( sub { push @copies, $_ if -f }, 'copy' );
165@copies = map { s/\.$//; $_ } @copies if $Is_VMS; # VMS likes to put dots on
166 # the end of files.
167# Have to compare insensitively for non-case preserving VMS
168is_deeply( [sort map { lc } @copies], [sort map { lc } keys %$files] );
169
170# cp would leave files readonly, so check permissions.
171foreach my $orig (@copies) {
172 my $copy = "copy/$orig";
173 ok( -r $copy, "$copy: must be readable" );
4c857482
SP
174 is( -w $copy, -w $orig, " writable if original was" );
175 is( -x $copy, -x $orig, " executable if original was" );
a7d1454b
RGS
176}
177rmtree('copy');
178
179
0300da75
MS
180# poison the manifest, and add a comment that should be reported
181add_file( 'MANIFEST', 'none #none' );
04721f73 182is( ExtUtils::Manifest::maniread()->{none}, '#none',
f6d6199c 183 'maniread found comment' );
0300da75
MS
184
185ok( mkdir( 'copy', 0777 ), 'made copy directory' );
0300da75
MS
186$files = maniread();
187eval { (undef, $warn) = catch_warning( sub {
04721f73 188 manicopy( $files, 'copy', 'cp' ) })
0300da75
MS
189};
190
191# a newline comes through, so get rid of it
192chomp($warn);
6dbcfe36
SP
193# the copy should have given a warning
194like($warn, qr/^none not found/, 'carped about none' );
195($res, $warn) = catch_warning( \&skipcheck );
f6d6199c 196like($warn, qr/^Skipping MANIFEST.SKIP/i, 'warned about MANIFEST.SKIP' );
0300da75
MS
197
198# tell ExtUtils::Manifest to use a different file
f6d6199c 199{
04721f73 200 local $ExtUtils::Manifest::MANIFEST = 'albatross';
f6d6199c
MS
201 ($res, $warn) = catch_warning( \&mkmanifest );
202 like( $warn, qr/Added to albatross: /, 'using a new manifest file' );
b3217f3b 203
f6d6199c 204 # add the new file to the list of files to be deleted
2530b651 205 $Files{'albatross'}++;
39234879 206}
0300da75 207
0300da75 208
f6d6199c
MS
209# Make sure MANIFEST.SKIP is using complete relative paths
210add_file( 'MANIFEST.SKIP' => "^moretest/q\n" );
211
212# This'll skip moretest/quux
213($res, $warn) = catch_warning( \&skipcheck );
45bc4d3a
JH
214like( $warn, qr{^Skipping moretest/quux$}i, 'got skipping warning again' );
215
216
217# There was a bug where entries in MANIFEST would be blotted out
218# by MANIFEST.SKIP rules.
219add_file( 'MANIFEST.SKIP' => 'foo' );
479d2113 220add_file( 'MANIFEST' => "foobar\n" );
45bc4d3a
JH
221add_file( 'foobar' => '123' );
222($res, $warn) = catch_warning( \&manicheck );
4c857482 223is( $res, '', 'MANIFEST overrides MANIFEST.SKIP' );
b3217f3b 224is( $warn, '', 'MANIFEST overrides MANIFEST.SKIP, no warnings' );
f6d6199c 225
479d2113
MS
226$files = maniread;
227ok( !$files->{wibble}, 'MANIFEST in good state' );
228maniadd({ wibble => undef });
229maniadd({ yarrow => "hock" });
230$files = maniread;
231is( $files->{wibble}, '', 'maniadd() with undef comment' );
232is( $files->{yarrow}, 'hock',' with comment' );
233is( $files->{foobar}, '', ' preserved old entries' );
5ca25ae7 234
e4ac890e
SA
235{
236 # EOL normalization in maniadd()
237
238 # move manifest away:
239 rename "MANIFEST", "MANIFEST.bak" or die "Could not rename MANIFEST to MANIFEST.bak: $!";
240 my $prev_maniaddresult;
241 my @eol = ("\012","\015","\015\012");
242 # for all line-endings:
243 for my $i (0..$#eol) {
244 my $eol = $eol[$i];
245 # cp the backup of the manifest to MANIFEST, line-endings adjusted
246 my $content = do { local $/; open my $fh, "MANIFEST.bak" or die; <$fh> };
247 SPLITTER: for my $eol2 (@eol) {
248 if ( index($content, $eol2) > -1 ) {
249 my @lines = split /$eol2/, $content;
250 pop @lines while $lines[-1] eq "";
251 open my $fh, ">", "MANIFEST" or die "Could not open >MANIFEST: $!";
252 print $fh map { "$_$eol" } @lines;
253 close $fh or die "Could not close: $!";
254 last SPLITTER;
255 }
256 }
257 # try maniadd
258 maniadd({eoltest => "end of line normalization test"});
259 # slurp result and compare to previous result
260 my $maniaddresult = do { local $/; open my $fh, "MANIFEST" or die; <$fh> };
261 if ($prev_maniaddresult) {
262 if ( $maniaddresult eq $prev_maniaddresult ) {
263 pass "normalization success with i=$i";
264 } else {
265 require Data::Dumper;
97bae9c5 266 no warnings "once";
e4ac890e
SA
267 local $Data::Dumper::Useqq = 1;
268 local $Data::Dumper::Terse = 1;
269 is Data::Dumper::Dumper($maniaddresult), Data::Dumper::Dumper($prev_maniaddresult), "eol normalization failed with i=$i";
270 }
271 }
272 $prev_maniaddresult = $maniaddresult;
273 }
274 # move backup over MANIFEST
275 rename "MANIFEST.bak", "MANIFEST" or die "Could not rename MANIFEST.bak to MANIFEST: $!";
276}
277
6dbcfe36
SP
278my %funky_files;
279# test including a filename with a space
280SKIP: {
281 add_file( 'foo bar' => "space" )
282 or skip "couldn't create spaced test file", 2;
283 local $ExtUtils::Manifest::MANIFEST = "albatross";
284 maniadd({ 'foo bar' => "contains space"});
285 is( maniread()->{'foo bar'}, "contains space",
286 'spaced manifest filename' );
287 add_file( 'albatross.bak', '' );
288 ($res, $warn) = catch_warning( \&mkmanifest );
289 like( $warn, qr/\A(Added to.*\n)+\z/m,
290 'no warnings about funky filename' );
291 $funky_files{'space'} = 'foo bar';
292}
293
294# test including a filename with a space and a quote
295SKIP: {
296 add_file( 'foo\' baz\'quux' => "quote" )
297 or skip "couldn't create quoted test file", 1;
298 local $ExtUtils::Manifest::MANIFEST = "albatross";
299 maniadd({ 'foo\' baz\'quux' => "contains quote"});
300 is( maniread()->{'foo\' baz\'quux'}, "contains quote",
301 'quoted manifest filename' );
302 $funky_files{'space_quote'} = 'foo\' baz\'quux';
303}
304
305# test including a filename with a space and a backslash
306SKIP: {
307 add_file( 'foo bar\\baz' => "backslash" )
308 or skip "couldn't create backslash test file", 1;
309 local $ExtUtils::Manifest::MANIFEST = "albatross";
310 maniadd({ 'foo bar\\baz' => "contains backslash"});
311 is( maniread()->{'foo bar\\baz'}, "contains backslash",
312 'backslashed manifest filename' );
313 $funky_files{'space_backslash'} = 'foo bar\\baz';
314}
315
316# test including a filename with a space, quote, and a backslash
317SKIP: {
318 add_file( 'foo bar\\baz\'quux' => "backslash/quote" )
319 or skip "couldn't create backslash/quote test file", 1;
320 local $ExtUtils::Manifest::MANIFEST = "albatross";
321 maniadd({ 'foo bar\\baz\'quux' => "backslash and quote"});
322 is( maniread()->{'foo bar\\baz\'quux'}, "backslash and quote",
323 'backslashed and quoted manifest filename' );
324 $funky_files{'space_quote_backslash'} = 'foo bar\\baz\'quux';
325}
326
327my @funky_keys = qw(space space_quote space_backslash space_quote_backslash);
1c14aae0
RGS
328# test including an external manifest.skip file in MANIFEST.SKIP
329{
330 maniadd({ foo => undef , albatross => undef,
331 'mymanifest.skip' => undef, 'mydefault.skip' => undef});
6dbcfe36
SP
332 for (@funky_keys) {
333 maniadd( {$funky_files{$_} => $_} ) if defined $funky_files{$_};
334 }
335
1c14aae0
RGS
336 add_file('mymanifest.skip' => "^foo\n");
337 add_file('mydefault.skip' => "^my\n");
6dbcfe36 338 local $ExtUtils::Manifest::DEFAULT_MSKIP =
1c14aae0
RGS
339 File::Spec->catfile($cwd, qw(mantest mydefault.skip));
340 my $skip = File::Spec->catfile($cwd, qw(mantest mymanifest.skip));
341 add_file('MANIFEST.SKIP' =>
342 "albatross\n#!include $skip\n#!include_default");
343 my ($res, $warn) = catch_warning( \&skipcheck );
344 for (qw(albatross foo foobar mymanifest.skip mydefault.skip)) {
345 like( $warn, qr/Skipping \b$_\b/,
346 "Skipping $_" );
347 }
6dbcfe36
SP
348 for my $funky_key (@funky_keys) {
349 SKIP: {
350 my $funky_file = $funky_files{$funky_key};
351 skip "'$funky_key' not created", 1 unless $funky_file;
352 like( $warn, qr/Skipping \b\Q$funky_file\E\b/,
353 "Skipping $funky_file");
354 }
355 }
1c14aae0
RGS
356 ($res, $warn) = catch_warning( \&mkmanifest );
357 for (qw(albatross foo foobar mymanifest.skip mydefault.skip)) {
358 like( $warn, qr/Removed from MANIFEST: \b$_\b/,
359 "Removed $_ from MANIFEST" );
360 }
6dbcfe36
SP
361 for my $funky_key (@funky_keys) {
362 SKIP: {
363 my $funky_file = $funky_files{$funky_key};
364 skip "'$funky_key' not created", 1 unless $funky_file;
365 like( $warn, qr/Removed from MANIFEST: \b\Q$funky_file\E\b/,
366 "Removed $funky_file from MANIFEST");
367 }
368 }
1c14aae0
RGS
369 my $files = maniread;
370 ok( ! exists $files->{albatross}, 'albatross excluded via MANIFEST.SKIP' );
371 ok( exists $files->{yarrow}, 'yarrow included in MANIFEST' );
372 ok( exists $files->{bar}, 'bar included in MANIFEST' );
373 ok( ! exists $files->{foobar}, 'foobar excluded via mymanifest.skip' );
374 ok( ! exists $files->{foo}, 'foo excluded via mymanifest.skip' );
375 ok( ! exists $files->{'mymanifest.skip'},
376 'mymanifest.skip excluded via mydefault.skip' );
377 ok( ! exists $files->{'mydefault.skip'},
378 'mydefault.skip excluded via mydefault.skip' );
6dbcfe36
SP
379
380 # test exclusion of funky files
381 for my $funky_key (@funky_keys) {
382 SKIP: {
383 my $funky_file = $funky_files{$funky_key};
384 skip "'$funky_key' not created", 1 unless $funky_file;
385 ok( ! exists $files->{$funky_file},
386 "'$funky_file' excluded via mymanifest.skip" );
387 }
388 }
389
390 # tests for maniskip
391 my $skipchk = maniskip();
392 is ( $skipchk->('albatross'), 1,
393 'albatross excluded via MANIFEST.SKIP' );
394 is( $skipchk->('yarrow'), '',
395 'yarrow included in MANIFEST' );
396 is( $skipchk->('bar'), '',
397 'bar included in MANIFEST' );
398 $skipchk = maniskip('mymanifest.skip');
399 is( $skipchk->('foobar'), 1,
400 'foobar excluded via mymanifest.skip' );
401 is( $skipchk->('foo'), 1,
402 'foo excluded via mymanifest.skip' );
403 is( $skipchk->('mymanifest.skip'), '',
404 'mymanifest.skip included via mydefault.skip' );
405 is( $skipchk->('mydefault.skip'), '',
406 'mydefault.skip included via mydefault.skip' );
407 $skipchk = maniskip('mydefault.skip');
408 is( $skipchk->('foobar'), '',
409 'foobar included via mydefault.skip' );
410 is( $skipchk->('foo'), '',
411 'foo included via mydefault.skip' );
412 is( $skipchk->('mymanifest.skip'), 1,
413 'mymanifest.skip excluded via mydefault.skip' );
414 is( $skipchk->('mydefault.skip'), 1,
415 'mydefault.skip excluded via mydefault.skip' );
416
7e4d7138 417 my $extsep = $Is_VMS_noefs ? '_' : '.';
a2fa79ff 418 $Files{"$_.bak"}++ for ('MANIFEST', "MANIFEST${extsep}SKIP");
1c14aae0
RGS
419}
420
2530b651 421add_file('MANIFEST' => 'Makefile.PL');
9d058bf8 422maniadd({ foo => 'bar' });
2530b651
MS
423$files = maniread;
424# VMS downcases the MANIFEST. We normalize it here to match.
425%$files = map { (lc $_ => $files->{$_}) } keys %$files;
426my %expect = ( 'makefile.pl' => '',
5ca25ae7
JH
427 'foo' => 'bar'
428 );
2530b651 429is_deeply( $files, \%expect, 'maniadd() vs MANIFEST without trailing newline');
0300da75 430
1c14aae0
RGS
431#add_file('MANIFEST' => 'Makefile.PL');
432#maniadd({ foo => 'bar' });
5ca25ae7 433
2c91f887
JH
434SKIP: {
435 chmod( 0400, 'MANIFEST' );
436 skip "Can't make MANIFEST read-only", 2 if -w 'MANIFEST';
437
380d5532
MS
438 eval {
439 maniadd({ 'foo' => 'bar' });
440 };
2c91f887
JH
441 is( $@, '', "maniadd() won't open MANIFEST if it doesn't need to" );
442
443 eval {
444 maniadd({ 'grrrwoof' => 'yippie' });
445 };
04721f73 446 like( $@, qr/^\Qmaniadd() could not open MANIFEST:\E/,
2c91f887
JH
447 "maniadd() dies if it can't open the MANIFEST" );
448
0aa703b2 449 chmod( 0600, 'MANIFEST' );
2c91f887 450}
a7d1454b 451
2c91f887 452
0300da75 453END {
2530b651 454 is( unlink( keys %Files ), keys %Files, 'remove all added files' );
bf081550 455 for my $file ( keys %Files ) { 1 while unlink $file; } # all versions
0300da75
MS
456 remove_dir( 'moretest', 'copy' );
457
458 # now get rid of the parent directory
459 ok( chdir( $cwd ), 'return to parent directory' );
460 remove_dir( 'mantest' );
461}