Upgrade to ExtUtils::MakeMaker 6.52
[p5sagit/p5-mst-13.2.git] / lib / ExtUtils / t / Manifest.t
1 #!/usr/bin/perl -w
2
3 BEGIN {
4     if( $ENV{PERL_CORE} ) {
5         chdir 't' if -d 't';
6         unshift @INC, '../lib';
7     }
8     else {
9         unshift @INC, 't/lib';
10     }
11 }
12 chdir 't';
13
14 use strict;
15
16 use Test::More tests => 94;
17 use Cwd;
18
19 use File::Spec;
20 use File::Path;
21 use File::Find;
22 use Config;
23
24 my $Is_VMS = $^O eq 'VMS';
25 my $Is_VMS_noefs = $Is_VMS;
26 if ($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'} || '';
32         $vms_efs = $efs_charset =~ /^[ET1]/i; 
33     }
34     $Is_VMS_noefs = 0 if $vms_efs;
35 }
36
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;
41
42 # keep track of everything added so it can all be deleted
43 my %Files;
44 sub add_file {
45     my ($file, $data) = @_;
46     $data ||= 'foo';
47     1 while unlink $file;  # or else we'll get multiple versions on VMS
48     open( T, '> '.$file) or return;
49     print T $data;
50     close T;
51     return 0 unless -e $file;  # exists under the name we gave it ?
52     ++$Files{$file};
53 }
54
55 sub read_manifest {
56     open( M, 'MANIFEST' ) or return;
57     chomp( my @files = <M> );
58     close M;
59     return @files;
60 }
61
62 sub catch_warning {
63     my $warn = '';
64     local $SIG{__WARN__} = sub { $warn .= $_[0] };
65     return join('', $_[0]->() ), $warn;
66 }
67
68 sub remove_dir {
69     ok( rmdir( $_ ), "remove $_ directory" ) for @_;
70 }
71
72 # use module, import functions
73 BEGIN { 
74     use_ok( 'ExtUtils::Manifest', 
75             qw( mkmanifest manicheck filecheck fullcheck 
76                 maniread manicopy skipcheck maniadd maniskip) ); 
77 }
78
79 my $cwd = Cwd::getcwd();
80
81 # Just in case any old files were lying around.
82 rmtree('mantest');
83
84 ok( mkdir( 'mantest', 0777 ), 'make mantest directory' );
85 ok( chdir( 'mantest' ), 'chdir() to mantest' );
86 ok( add_file('foo'), 'add a temporary file' );
87
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
92 chmod( 0744, 'foo') if $Config{'chmod'};
93
94 # there shouldn't be a MANIFEST there
95 my ($res, $warn) = catch_warning( \&mkmanifest );
96 # Canonize the order.
97 $warn = join("", map { "$_|" } 
98                  sort { lc($a) cmp lc($b) } split /\r?\n/, $warn);
99 is( $warn, "Added to MANIFEST: foo|Added to MANIFEST: MANIFEST|",
100     "mkmanifest() displayed its additions" );
101
102 # and now you see it
103 ok( -e 'MANIFEST', 'create MANIFEST file' );
104
105 my @list = read_manifest();
106 is( @list, 2, 'check files in MANIFEST' );
107 ok( ! ExtUtils::Manifest::filecheck(), 'no additional files in directory' );
108
109 # after adding bar, the MANIFEST is out of date
110 ok( add_file( 'bar' ), 'add another file' );
111 ok( ! 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
116 like( $warn, qr/^Not in MANIFEST: bar/, 'warning that bar has been added' );
117 is( $res, 'bar', 'bar reported as new' );
118
119 # now quiet the warning that bar was added and test again
120 ($res, $warn) = do { local $ExtUtils::Manifest::Quiet = 1;
121                      catch_warning( \&skipcheck )
122                 };
123 is( $warn, '', 'disabled warnings' );
124
125 # add a skip file with a rule to skip itself (and the nonexistent glob '*baz*')
126 add_file( 'MANIFEST.SKIP', "baz\n.SKIP" );
127
128 # this'll skip the new file
129 ($res, $warn) = catch_warning( \&skipcheck );
130 like( $warn, qr/^Skipping MANIFEST\.SKIP/i, 'got skipping warning' );
131
132 my @skipped;
133 catch_warning( sub {
134         @skipped = skipcheck()
135 });
136
137 is( join( ' ', @skipped ), 'MANIFEST.SKIP', 'listed skipped files' );
138
139 {
140         local $ExtUtils::Manifest::Quiet = 1;
141         is( join(' ', filecheck() ), 'bar', 'listing skipped with filecheck()' );
142 }
143
144 # add a subdirectory and a file there that should be found
145 ok( mkdir( 'moretest', 0777 ), 'created moretest directory' );
146 add_file( File::Spec->catfile('moretest', 'quux'), 'quux' );
147 ok( exists( ExtUtils::Manifest::manifind()->{'moretest/quux'} ), 
148                                         "manifind found moretest/quux" );
149
150 # only MANIFEST and foo are in the manifest
151 $_ = 'foo';
152 my $files = maniread();
153 is( keys %$files, 2, 'two files found' );
154 is( join(' ', sort { lc($a) cmp lc($b) } keys %$files), 'foo MANIFEST', 
155                                         'both files found' );
156 is( $_, 'foo', q{maniread() doesn't clobber $_} );
157
158 ok( mkdir( 'copy', 0777 ), 'made copy directory' );
159
160 # Check that manicopy copies files.
161 manicopy( $files, 'copy', 'cp' );
162 my @copies = ();
163 find( 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
167 is_deeply( [sort map { lc } @copies], [sort map { lc } keys %$files] );
168
169 # cp would leave files readonly, so check permissions.
170 foreach my $orig (@copies) {
171     my $copy = "copy/$orig";
172     ok( -r $copy,               "$copy: must be readable" );
173     is( -w $copy, -w $orig,     "       writable if original was" );
174     is( -x $copy, -x $orig,     "       executable if original was" );
175 }
176 rmtree('copy');
177
178
179 # poison the manifest, and add a comment that should be reported
180 add_file( 'MANIFEST', 'none #none' );
181 is( ExtUtils::Manifest::maniread()->{none}, '#none', 
182                                         'maniread found comment' );
183
184 ok( mkdir( 'copy', 0777 ), 'made copy directory' );
185 $files = maniread();
186 eval { (undef, $warn) = catch_warning( sub {
187                 manicopy( $files, 'copy', 'cp' ) })
188 };
189
190 # a newline comes through, so get rid of it
191 chomp($warn);
192 # the copy should have given a warning
193 like($warn, qr/^none not found/, 'carped about none' );
194 ($res, $warn) = catch_warning( \&skipcheck );
195 like($warn, qr/^Skipping MANIFEST.SKIP/i, 'warned about MANIFEST.SKIP' );
196
197 # tell ExtUtils::Manifest to use a different file
198 {
199         local $ExtUtils::Manifest::MANIFEST = 'albatross'; 
200         ($res, $warn) = catch_warning( \&mkmanifest );
201         like( $warn, qr/Added to albatross: /, 'using a new manifest file' );
202
203         # add the new file to the list of files to be deleted
204         $Files{'albatross'}++;
205 }
206
207
208 # Make sure MANIFEST.SKIP is using complete relative paths
209 add_file( 'MANIFEST.SKIP' => "^moretest/q\n" );
210
211 # This'll skip moretest/quux
212 ($res, $warn) = catch_warning( \&skipcheck );
213 like( $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.
218 add_file( 'MANIFEST.SKIP' => 'foo' );
219 add_file( 'MANIFEST'      => "foobar\n"   );
220 add_file( 'foobar'        => '123' );
221 ($res, $warn) = catch_warning( \&manicheck );
222 is( $res,  '',      'MANIFEST overrides MANIFEST.SKIP' );
223 is( $warn, '',   'MANIFEST overrides MANIFEST.SKIP, no warnings' );
224
225 $files = maniread;
226 ok( !$files->{wibble},     'MANIFEST in good state' );
227 maniadd({ wibble => undef });
228 maniadd({ yarrow => "hock" });
229 $files = maniread;
230 is( $files->{wibble}, '',    'maniadd() with undef comment' );
231 is( $files->{yarrow}, 'hock','          with comment' );
232 is( $files->{foobar}, '',    '          preserved old entries' );
233
234 my %funky_files;
235 # test including a filename with a space
236 SKIP: {
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
251 SKIP: {
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
262 SKIP: {
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
273 SKIP: {
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
283 my @funky_keys = qw(space space_quote space_backslash space_quote_backslash);
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});
288     for (@funky_keys) {
289         maniadd( {$funky_files{$_} => $_} ) if defined $funky_files{$_};
290     }
291
292     add_file('mymanifest.skip' => "^foo\n");
293     add_file('mydefault.skip'  => "^my\n");
294     local $ExtUtils::Manifest::DEFAULT_MSKIP =
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     }
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     }
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     }
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     }
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' );
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
373     my $extsep = $Is_VMS_noefs ? '_' : '.';
374     $Files{"$_.bak"}++ for ('MANIFEST', "MANIFEST${extsep}SKIP");
375 }
376
377 add_file('MANIFEST'   => 'Makefile.PL');
378 maniadd({ foo  => 'bar' });
379 $files = maniread;
380 # VMS downcases the MANIFEST.  We normalize it here to match.
381 %$files = map { (lc $_ => $files->{$_}) } keys %$files;
382 my %expect = ( 'makefile.pl' => '',
383                'foo'    => 'bar'
384              );
385 is_deeply( $files, \%expect, 'maniadd() vs MANIFEST without trailing newline');
386
387 #add_file('MANIFEST'   => 'Makefile.PL');
388 #maniadd({ foo => 'bar' });
389
390 SKIP: {
391     chmod( 0400, 'MANIFEST' );
392     skip "Can't make MANIFEST read-only", 2 if -w 'MANIFEST';
393
394     eval {
395         maniadd({ 'foo' => 'bar' });
396     };
397     is( $@, '',  "maniadd() won't open MANIFEST if it doesn't need to" );
398
399     eval {
400         maniadd({ 'grrrwoof' => 'yippie' });
401     };
402     like( $@, qr/^\Qmaniadd() could not open MANIFEST:\E/,  
403                  "maniadd() dies if it can't open the MANIFEST" );
404
405     chmod( 0600, 'MANIFEST' );
406 }
407
408
409 END {
410         is( unlink( keys %Files ), keys %Files, 'remove all added files' );
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 }
417