Upgrade to MakeMaker 5.91_02, from Michael Schwern.
[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 # these files help the test run
17 use Test::More tests => 33;
18 use Cwd;
19
20 # these files are needed for the module itself
21 use File::Spec;
22 use File::Path;
23 use Carp::Heavy;
24
25 # keep track of everything added so it can all be deleted
26 my %files;
27 sub add_file {
28         my ($file, $data) = @_;
29         $data ||= 'foo';
30     unlink $file;  # or else we'll get multiple versions on VMS
31         open( my $T, '>', $file) or return;
32         print $T $data;
33         ++$files{$file};
34 }
35
36 sub read_manifest {
37         open( my $M, 'MANIFEST' ) or return;
38         chomp( my @files = <$M> );
39         return @files;
40 }
41
42 sub catch_warning {
43         my $warn;
44         local $SIG{__WARN__} = sub { $warn .= $_[0] };
45         return join('', $_[0]->() ), $warn;
46 }
47
48 sub remove_dir {
49         ok( rmdir( $_ ), "remove $_ directory" ) for @_;
50 }
51
52 # use module, import functions
53 BEGIN { 
54     use_ok( 'ExtUtils::Manifest', 
55             qw( mkmanifest manicheck filecheck fullcheck 
56                 maniread manicopy skipcheck ) ); 
57 }
58
59 my $cwd = Cwd::getcwd();
60
61 # Just in case any old files were lying around.
62 rmtree('mantest');
63
64 ok( mkdir( 'mantest', 0777 ), 'make mantest directory' );
65 ok( chdir( 'mantest' ), 'chdir() to mantest' );
66 ok( add_file('foo'), 'add a temporary file' );
67
68 # there shouldn't be a MANIFEST there
69 my ($res, $warn) = catch_warning( \&mkmanifest ); 
70 # Canonize the order.
71 $warn = join("", map { "$_|" } 
72                  sort { lc($a) cmp lc($b) } split /\r?\n/, $warn);
73 is( $warn, "Added to MANIFEST: foo|Added to MANIFEST: MANIFEST|",
74     "mkmanifest() displayed its additions" );
75
76 # and now you see it
77 ok( -e 'MANIFEST', 'create MANIFEST file' );
78
79 my @list = read_manifest();
80 is( @list, 2, 'check files in MANIFEST' );
81 ok( ! ExtUtils::Manifest::filecheck(), 'no additional files in directory' );
82
83 # after adding bar, the MANIFEST is out of date
84 ok( add_file( 'bar' ), 'add another file' );
85 ok( ! manicheck(), 'MANIFEST now out of sync' );
86
87 # it reports that bar has been added and throws a warning
88 ($res, $warn) = catch_warning( \&filecheck );
89
90 like( $warn, qr/^Not in MANIFEST: bar/, 'warning that bar has been added' );
91 is( $res, 'bar', 'bar reported as new' );
92
93 # now quiet the warning that bar was added and test again
94 ($res, $warn) = do { local $ExtUtils::Manifest::Quiet = 1; 
95                      catch_warning( \&skipcheck ) 
96                 };
97 cmp_ok( $warn, 'eq', '', 'disabled warnings' );
98
99 # add a skip file with a rule to skip itself (and the nonexistent glob '*baz*')
100 add_file( 'MANIFEST.SKIP', "baz\n.SKIP" );
101
102 # this'll skip the new file
103 ($res, $warn) = catch_warning( \&skipcheck );
104 like( $warn, qr/^Skipping MANIFEST\.SKIP/i, 'got skipping warning' );
105
106 my @skipped;
107 catch_warning( sub {
108         @skipped = skipcheck()
109 });
110
111 is( join( ' ', @skipped ), 'MANIFEST.SKIP', 'listed skipped files' );
112
113 {
114         local $ExtUtils::Manifest::Quiet = 1;
115         is( join(' ', filecheck() ), 'bar', 'listing skipped with filecheck()' );
116 }
117
118 # add a subdirectory and a file there that should be found
119 ok( mkdir( 'moretest', 0777 ), 'created moretest directory' );
120 add_file( File::Spec->catfile('moretest', 'quux'), 'quux' );
121 ok( exists( ExtUtils::Manifest::manifind()->{'moretest/quux'} ), 
122                                         "manifind found moretest/quux" );
123
124 # only MANIFEST and foo are in the manifest
125 my $files = maniread();
126 is( keys %$files, 2, 'two files found' );
127 is( join(' ', sort { lc($a) cmp lc($b) } keys %$files), 'foo MANIFEST', 
128                                         'both files found' );
129
130 # poison the manifest, and add a comment that should be reported
131 add_file( 'MANIFEST', 'none #none' );
132 is( ExtUtils::Manifest::maniread()->{none}, '#none', 
133                                         'maniread found comment' );
134
135 ok( mkdir( 'copy', 0777 ), 'made copy directory' );
136
137 $files = maniread();
138 eval { (undef, $warn) = catch_warning( sub {
139                 manicopy( $files, 'copy', 'cp' ) }) 
140 };
141 like( $@, qr/^Can't read none: /, 'carped about none' );
142
143 # a newline comes through, so get rid of it
144 chomp($warn);
145
146 # the copy should have given one warning and one error
147 like($warn, qr/^Skipping MANIFEST.SKIP/i, 'warned about MANIFEST.SKIP' );
148
149 # tell ExtUtils::Manifest to use a different file
150 {
151         local $ExtUtils::Manifest::MANIFEST = 'albatross'; 
152         ($res, $warn) = catch_warning( \&mkmanifest );
153         like( $warn, qr/Added to albatross: /, 'using a new manifest file' );
154         
155         # add the new file to the list of files to be deleted
156         $files{'albatross'}++;
157 }
158
159
160 # Make sure MANIFEST.SKIP is using complete relative paths
161 add_file( 'MANIFEST.SKIP' => "^moretest/q\n" );
162
163 # This'll skip moretest/quux
164 ($res, $warn) = catch_warning( \&skipcheck );
165 like( $warn, qr{^Skipping moretest/quux$}i, 'got skipping warning again' );
166
167
168 # There was a bug where entries in MANIFEST would be blotted out
169 # by MANIFEST.SKIP rules.
170 add_file( 'MANIFEST.SKIP' => 'foo' );
171 add_file( 'MANIFEST'      => 'foobar'   );
172 add_file( 'foobar'        => '123' );
173 ($res, $warn) = catch_warning( \&manicheck );
174 is( $res,  '',      'MANIFEST overrides MANIFEST.SKIP' );
175 is( $warn, undef,   'MANIFEST overrides MANIFEST.SKIP, no warnings' );
176
177
178 END {
179         # the args are evaluated in scalar context
180         is( unlink( keys %files ), keys %files, 'remove all added files' );
181         remove_dir( 'moretest', 'copy' );
182
183         # now get rid of the parent directory
184         ok( chdir( $cwd ), 'return to parent directory' );
185         unlink('mantest/MANIFEST');
186         remove_dir( 'mantest' );
187 }
188