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