This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Fix warning.
[perl5.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 => 66;
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
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;
29
30 # keep track of everything added so it can all be deleted
31 my %Files;
32 sub add_file {
33     my ($file, $data) = @_;
34     $data ||= 'foo';
35     1 while unlink $file;  # or else we'll get multiple versions on VMS
36     open( T, '>'.$file) or return;
37     print T $data;
38     ++$Files{$file};
39     close T;
40 }
41
42 sub read_manifest {
43     open( M, 'MANIFEST' ) or return;
44     chomp( my @files = <M> );
45     close M;
46     return @files;
47 }
48
49 sub catch_warning {
50     my $warn = '';
51     local $SIG{__WARN__} = sub { $warn .= $_[0] };
52     return join('', $_[0]->() ), $warn;
53 }
54
55 sub remove_dir {
56     ok( rmdir( $_ ), "remove $_ directory" ) for @_;
57 }
58
59 # use module, import functions
60 BEGIN { 
61     use_ok( 'ExtUtils::Manifest', 
62             qw( mkmanifest manicheck filecheck fullcheck 
63                 maniread manicopy skipcheck maniadd) ); 
64 }
65
66 my $cwd = Cwd::getcwd();
67
68 # Just in case any old files were lying around.
69 rmtree('mantest');
70
71 ok( mkdir( 'mantest', 0777 ), 'make mantest directory' );
72 ok( chdir( 'mantest' ), 'chdir() to mantest' );
73 ok( add_file('foo'), 'add a temporary file' );
74
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
79 chmod( 0744, 'foo') if $Config{'chmod'};
80
81 # there shouldn't be a MANIFEST there
82 my ($res, $warn) = catch_warning( \&mkmanifest );
83 # Canonize the order.
84 $warn = join("", map { "$_|" } 
85                  sort { lc($a) cmp lc($b) } split /\r?\n/, $warn);
86 is( $warn, "Added to MANIFEST: foo|Added to MANIFEST: MANIFEST|",
87     "mkmanifest() displayed its additions" );
88
89 # and now you see it
90 ok( -e 'MANIFEST', 'create MANIFEST file' );
91
92 my @list = read_manifest();
93 is( @list, 2, 'check files in MANIFEST' );
94 ok( ! ExtUtils::Manifest::filecheck(), 'no additional files in directory' );
95
96 # after adding bar, the MANIFEST is out of date
97 ok( add_file( 'bar' ), 'add another file' );
98 ok( ! 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
103 like( $warn, qr/^Not in MANIFEST: bar/, 'warning that bar has been added' );
104 is( $res, 'bar', 'bar reported as new' );
105
106 # now quiet the warning that bar was added and test again
107 ($res, $warn) = do { local $ExtUtils::Manifest::Quiet = 1;
108                      catch_warning( \&skipcheck )
109                 };
110 is( $warn, '', 'disabled warnings' );
111
112 # add a skip file with a rule to skip itself (and the nonexistent glob '*baz*')
113 add_file( 'MANIFEST.SKIP', "baz\n.SKIP" );
114
115 # this'll skip the new file
116 ($res, $warn) = catch_warning( \&skipcheck );
117 like( $warn, qr/^Skipping MANIFEST\.SKIP/i, 'got skipping warning' );
118
119 my @skipped;
120 catch_warning( sub {
121         @skipped = skipcheck()
122 });
123
124 is( join( ' ', @skipped ), 'MANIFEST.SKIP', 'listed skipped files' );
125
126 {
127         local $ExtUtils::Manifest::Quiet = 1;
128         is( join(' ', filecheck() ), 'bar', 'listing skipped with filecheck()' );
129 }
130
131 # add a subdirectory and a file there that should be found
132 ok( mkdir( 'moretest', 0777 ), 'created moretest directory' );
133 add_file( File::Spec->catfile('moretest', 'quux'), 'quux' );
134 ok( exists( ExtUtils::Manifest::manifind()->{'moretest/quux'} ), 
135                                         "manifind found moretest/quux" );
136
137 # only MANIFEST and foo are in the manifest
138 $_ = 'foo';
139 my $files = maniread();
140 is( keys %$files, 2, 'two files found' );
141 is( join(' ', sort { lc($a) cmp lc($b) } keys %$files), 'foo MANIFEST', 
142                                         'both files found' );
143 is( $_, 'foo', q{maniread() doesn't clobber $_} );
144
145 ok( mkdir( 'copy', 0777 ), 'made copy directory' );
146
147 # Check that manicopy copies files.
148 manicopy( $files, 'copy', 'cp' );
149 my @copies = ();
150 find( 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
154 is_deeply( [sort map { lc } @copies], [sort map { lc } keys %$files] );
155
156 # cp would leave files readonly, so check permissions.
157 foreach my $orig (@copies) {
158     my $copy = "copy/$orig";
159     ok( -r $copy,               "$copy: must be readable" );
160     is( -w $copy, -w $orig,     "       writable if original was" );
161     is( -x $copy, -x $orig,     "       executable if original was" );
162 }
163 rmtree('copy');
164
165
166 # poison the manifest, and add a comment that should be reported
167 add_file( 'MANIFEST', 'none #none' );
168 is( ExtUtils::Manifest::maniread()->{none}, '#none', 
169                                         'maniread found comment' );
170
171 ok( mkdir( 'copy', 0777 ), 'made copy directory' );
172 $files = maniread();
173 eval { (undef, $warn) = catch_warning( sub {
174                 manicopy( $files, 'copy', 'cp' ) })
175 };
176 like( $@, qr/^Can't read none: /, 'croaked about none' );
177
178 # a newline comes through, so get rid of it
179 chomp($warn);
180
181 # the copy should have given one warning and one error
182 like($warn, qr/^Skipping MANIFEST.SKIP/i, 'warned about MANIFEST.SKIP' );
183
184 # tell ExtUtils::Manifest to use a different file
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' );
189
190         # add the new file to the list of files to be deleted
191         $Files{'albatross'}++;
192 }
193
194
195 # Make sure MANIFEST.SKIP is using complete relative paths
196 add_file( 'MANIFEST.SKIP' => "^moretest/q\n" );
197
198 # This'll skip moretest/quux
199 ($res, $warn) = catch_warning( \&skipcheck );
200 like( $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.
205 add_file( 'MANIFEST.SKIP' => 'foo' );
206 add_file( 'MANIFEST'      => "foobar\n"   );
207 add_file( 'foobar'        => '123' );
208 ($res, $warn) = catch_warning( \&manicheck );
209 is( $res,  '',      'MANIFEST overrides MANIFEST.SKIP' );
210 is( $warn, '',   'MANIFEST overrides MANIFEST.SKIP, no warnings' );
211
212 $files = maniread;
213 ok( !$files->{wibble},     'MANIFEST in good state' );
214 maniadd({ wibble => undef });
215 maniadd({ yarrow => "hock" });
216 $files = maniread;
217 is( $files->{wibble}, '',    'maniadd() with undef comment' );
218 is( $files->{yarrow}, 'hock','          with comment' );
219 is( $files->{foobar}, '',    '          preserved old entries' );
220
221 # test including an external manifest.skip file in MANIFEST.SKIP
222 {
223     maniadd({ foo => undef , albatross => undef,
224               'mymanifest.skip' => undef, 'mydefault.skip' => undef});
225     add_file('mymanifest.skip' => "^foo\n");
226     add_file('mydefault.skip'  => "^my\n");
227     $ExtUtils::Manifest::DEFAULT_MSKIP =
228          File::Spec->catfile($cwd, qw(mantest mydefault.skip));
229     my $skip = File::Spec->catfile($cwd, qw(mantest mymanifest.skip));
230     add_file('MANIFEST.SKIP' =>
231              "albatross\n#!include $skip\n#!include_default");
232     my ($res, $warn) = catch_warning( \&skipcheck );
233     for (qw(albatross foo foobar mymanifest.skip mydefault.skip)) {
234         like( $warn, qr/Skipping \b$_\b/,
235               "Skipping $_" );
236     }
237     ($res, $warn) = catch_warning( \&mkmanifest );
238     for (qw(albatross foo foobar mymanifest.skip mydefault.skip)) {
239         like( $warn, qr/Removed from MANIFEST: \b$_\b/,
240               "Removed $_ from MANIFEST" );
241     }
242     my $files = maniread;
243     ok( ! exists $files->{albatross}, 'albatross excluded via MANIFEST.SKIP' );
244     ok( exists $files->{yarrow},      'yarrow included in MANIFEST' );
245     ok( exists $files->{bar},         'bar included in MANIFEST' );
246     ok( ! exists $files->{foobar},    'foobar excluded via mymanifest.skip' );
247     ok( ! exists $files->{foo},       'foo excluded via mymanifest.skip' );
248     ok( ! exists $files->{'mymanifest.skip'},
249         'mymanifest.skip excluded via mydefault.skip' );
250     ok( ! exists $files->{'mydefault.skip'},
251         'mydefault.skip excluded via mydefault.skip' );
252     my $extsep = $Is_VMS ? '_' : '.';
253     $Files{"$_.bak"}++ for ('MANIFEST', "MANIFEST${extsep}SKIP");
254 }
255
256 add_file('MANIFEST'   => 'Makefile.PL');
257 maniadd({ foo  => 'bar' });
258 $files = maniread;
259 # VMS downcases the MANIFEST.  We normalize it here to match.
260 %$files = map { (lc $_ => $files->{$_}) } keys %$files;
261 my %expect = ( 'makefile.pl' => '',
262                'foo'    => 'bar'
263              );
264 is_deeply( $files, \%expect, 'maniadd() vs MANIFEST without trailing newline');
265
266 #add_file('MANIFEST'   => 'Makefile.PL');
267 #maniadd({ foo => 'bar' });
268
269 SKIP: {
270     chmod( 0400, 'MANIFEST' );
271     skip "Can't make MANIFEST read-only", 2 if -w 'MANIFEST';
272
273     eval {
274         maniadd({ 'foo' => 'bar' });
275     };
276     is( $@, '',  "maniadd() won't open MANIFEST if it doesn't need to" );
277
278     eval {
279         maniadd({ 'grrrwoof' => 'yippie' });
280     };
281     like( $@, qr/^\Qmaniadd() could not open MANIFEST:\E/,  
282                  "maniadd() dies if it can't open the MANIFEST" );
283
284     chmod( 0600, 'MANIFEST' );
285 }
286
287
288 END {
289         is( unlink( keys %Files ), keys %Files, 'remove all added files' );
290         remove_dir( 'moretest', 'copy' );
291
292         # now get rid of the parent directory
293         ok( chdir( $cwd ), 'return to parent directory' );
294         remove_dir( 'mantest' );
295 }
296