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