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