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