1#!/usr/bin/perl -w
2use strict;
3
4# Test ExtUtils::Install.
5
6BEGIN {
7    unshift @INC, 't/lib';
8}
9
10use TieOut;
11use File::Path;
12use File::Spec;
13use File::Temp qw[tempdir];
14use File::Compare;
15
16use Test::More tests => 70;
17
18use MakeMaker::Test::Setup::BFD;
19
20BEGIN {
21  local $ENV{PERL_INSTALL_QUIET};
22  use_ok('ExtUtils::Install');
23}
24
25# Check exports.
26foreach my $func (qw(install uninstall pm_to_blib install_default)) {
27    can_ok(__PACKAGE__, $func);
28}
29
30my $tmpdir = tempdir( DIR => 't', CLEANUP => 1 );
31chdir $tmpdir;
32
33ok( setup_recurs(), 'setup' );
34END {
35    ok( chdir File::Spec->updir );
36    ok( teardown_recurs(), 'teardown' );
37}
38# ensure the env doesn't pollute our tests
39local $ENV{EU_INSTALL_ALWAYS_COPY};
40local $ENV{EU_ALWAYS_COPY};
41
42chdir 'Big-Dummy';
43
44my $stdout = tie *STDOUT, 'TieOut';
45pm_to_blib( { 'lib/Big/Dummy.pm' => 'blib/lib/Big/Dummy.pm' },
46            'blib/lib/auto'
47          );
48END { rmtree 'blib' }
49
50ok( -d 'blib/lib',              'pm_to_blib created blib dir' );
51ok( -r 'blib/lib/Big/Dummy.pm', '  copied .pm file' );
52ok( -r 'blib/lib/auto',         '  created autosplit dir' );
53is( $stdout->read, "cp lib/Big/Dummy.pm blib/lib/Big/Dummy.pm\n" );
54
55pm_to_blib( { 'lib/Big/Dummy.pm' => 'blib/lib/Big/Dummy.pm' },
56            'blib/lib/auto'
57          );
58ok( -d 'blib/lib',              'second run, blib dir still there' );
59ok( -r 'blib/lib/Big/Dummy.pm', '  .pm file still there' );
60ok( -r 'blib/lib/auto',         '  autosplit still there' );
61is( $stdout->read, "Skip blib/lib/Big/Dummy.pm (unchanged)\n" );
62
63install( [
64    from_to=>{ 'blib/lib' => 'install-test/lib/perl',
65           read   => 'install-test/packlist',
66           write  => 'install-test/packlist'
67         },
68    dry_run=>1]);
69ok( ! -d 'install-test/lib/perl',        'install made dir (dry run)');
70ok( ! -r 'install-test/lib/perl/Big/Dummy.pm',
71                                         '  .pm file installed (dry run)');
72ok( ! -r 'install-test/packlist',        '  packlist exists (dry run)');
73
74install([ from_to=> { 'blib/lib' => 'install-test/lib/perl',
75           read   => 'install-test/packlist',
76           write  => 'install-test/packlist'
77         } ]);
78ok( -d 'install-test/lib/perl',                 'install made dir' );
79ok( -r 'install-test/lib/perl/Big/Dummy.pm',    '  .pm file installed' );
80ok(!-r 'install-test/lib/perl/Big/Dummy.SKIP',  '  ignored .SKIP file' );
81ok( -r 'install-test/packlist',                 '  packlist exists' );
82
83open(PACKLIST, 'install-test/packlist' );
84my %packlist = map { chomp;  ($_ => 1) } <PACKLIST>;
85close PACKLIST;
86
87# On case-insensitive filesystems (ie. VMS), the keys of the packlist might
88# be lowercase. :(
89my $native_dummy = File::Spec->catfile(qw(install-test lib perl Big Dummy.pm));
90is( keys %packlist, 1 );
91is( lc((keys %packlist)[0]), lc $native_dummy, 'packlist written' );
92
93
94# Test UNINST=1 preserving same versions in other dirs.
95install([from_to=> { 'blib/lib' => 'install-test/other_lib/perl',
96           read   => 'install-test/packlist',
97           write  => 'install-test/packlist'
98         },uninstall_shadows=>1]);
99ok( -d 'install-test/other_lib/perl',        'install made other dir' );
100ok( -r 'install-test/other_lib/perl/Big/Dummy.pm', '  .pm file installed' );
101ok( -r 'install-test/packlist',              '  packlist exists' );
102ok( -r 'install-test/lib/perl/Big/Dummy.pm', '  UNINST=1 preserved same' );
103
104
105chmod 0644, 'blib/lib/Big/Dummy.pm' or die $!;
106open(DUMMY, ">>blib/lib/Big/Dummy.pm") or die $!;
107print DUMMY "Extra stuff\n";
108close DUMMY;
109
110
111# Test UNINST=0 does not remove other versions in other dirs.
112{
113  ok( -r 'install-test/lib/perl/Big/Dummy.pm', 'different install exists' );
114
115  local @INC = ('install-test/lib/perl');
116  local $ENV{PERL5LIB} = '';
117  install([from_to=> { 'blib/lib' => 'install-test/other_lib/perl',
118           read   => 'install-test/packlist',
119           write  => 'install-test/packlist'
120         }]);
121  ok( -d 'install-test/other_lib/perl',        'install made other dir' );
122  ok( -r 'install-test/other_lib/perl/Big/Dummy.pm', '  .pm file installed' );
123  ok( -r 'install-test/packlist',              '  packlist exists' );
124  ok( -r 'install-test/lib/perl/Big/Dummy.pm',
125                                             '  UNINST=0 left different' );
126}
127
128# Test UNINST=1 only warning when failing to remove an irrelevant shadow file
129{
130  my $tfile='install-test/lib/perl/Big/Dummy.pm';
131  local $ExtUtils::Install::Testing = $tfile; 
132  local @INC = ('install-test/other_lib/perl','install-test/lib/perl');
133  local $ENV{PERL5LIB} = '';
134  ok( -r $tfile, 'different install exists' );
135  my @warn;
136  local $SIG{__WARN__}=sub { push @warn, @_; return };
137  my $ok=eval {
138    install([from_to=> { 'blib/lib' => 'install-test/other_lib/perl',
139           read   => 'install-test/packlist',
140           write  => 'install-test/packlist'
141         },
142       uninstall_shadows=>1]);
143    1
144  };
145  ok($ok,'  we didnt die');
146  ok(0+@warn,"  we did warn");
147  ok( -d 'install-test/other_lib/perl',        'install made other dir' );
148  ok( -r 'install-test/other_lib/perl/Big/Dummy.pm', '  .pm file installed' );
149  ok( -r 'install-test/packlist',              '  packlist exists' );
150  ok( -r $tfile, '  UNINST=1 failed to remove different' );
151  
152}
153
154# Test UNINST=1 dieing when failing to remove an relevant shadow file
155{
156  my $tfile='install-test/lib/perl/Big/Dummy.pm';
157  local $ExtUtils::Install::Testing = $tfile;
158  local @INC = ('install-test/lib/perl','install-test/other_lib/perl');
159  local $ENV{PERL5LIB} = '';
160  ok( -r $tfile, 'different install exists' );
161  my @warn;
162  local $SIG{__WARN__}=sub { push @warn,@_; return };
163  my $ok=eval {
164    install([from_to=> { 'blib/lib' => 'install-test/other_lib/perl',
165           read   => 'install-test/packlist',
166           write  => 'install-test/packlist'
167         },uninstall_shadows=>1]);
168    1
169  };
170  ok(!$ok,'  we did die');
171  ok(!@warn,"  we didnt warn");
172  ok( -d 'install-test/other_lib/perl',        'install made other dir' );
173  ok( -r 'install-test/other_lib/perl/Big/Dummy.pm', '  .pm file installed' );
174  ok( -r 'install-test/packlist',              '  packlist exists' );
175  ok( -r $tfile,'  UNINST=1 failed to remove different' );
176}
177
178# Test UNINST=1 removing other versions in other dirs.
179{
180  local @INC = ('install-test/lib/perl');
181  local $ENV{PERL5LIB} = '';
182  ok( -r 'install-test/lib/perl/Big/Dummy.pm','different install exists' );
183  install([from_to=>{ 'blib/lib' => 'install-test/other_lib/perl',
184           read   => 'install-test/packlist',
185           write  => 'install-test/packlist'
186         },uninstall_shadows=>1]);
187  ok( -d 'install-test/other_lib/perl',        'install made other dir' );
188  ok( -r 'install-test/other_lib/perl/Big/Dummy.pm', '  .pm file installed' );
189  ok( -r 'install-test/packlist',              '  packlist exists' );
190  ok( !-r 'install-test/lib/perl/Big/Dummy.pm',
191                                             '  UNINST=1 removed different' );
192}
193
194# Test EU_ALWAYS_COPY triggers copy.
195{
196  local @INC = ('install-test/lib/perl');
197  local $ENV{PERL5LIB} = '';
198  local $ENV{EU_INSTALL_ALWAYS_COPY}=1;
199  my $tfile='install-test/other_lib/perl/Big/Dummy.pm';
200  my $sfile='blib/lib/Big/Dummy.pm';
201  ok(-r $tfile,"install file already exists");
202  ok(-r $sfile,"source file already exists");
203  utime time-600, time-600, $sfile or die "utime '$sfile' failed:$!";   
204  ok( (stat $tfile)[9]!=(stat $sfile)[9],'  Times are different');
205  install([from_to=>{ 'blib/lib' => 'install-test/other_lib/perl',
206           read   => 'install-test/packlist',
207           write  => 'install-test/packlist'
208         },result=>\my %result]);
209  ok( -d 'install-test/other_lib/perl',        'install made other dir' );
210  ok( -r 'install-test/other_lib/perl/Big/Dummy.pm', '  .pm file installed' );
211  ok( -r 'install-test/packlist',              '  packlist exists' );
212SKIP: {
213  skip "Times not preserved during copy by default", 1 if $^O eq 'VMS';
214  ok( (stat $tfile)[9]==(stat $sfile)[9],'  Times are same');
215}
216  ok( !$result{install_unchanged},'  $result{install_unchanged} should be empty');
217}
218# Test nothing is copied.
219{
220  local @INC = ('install-test/lib/perl');
221  local $ENV{PERL5LIB} = '';
222  local $ENV{EU_INSTALL_ALWAYS_COPY}=0;
223  my $tfile='install-test/other_lib/perl/Big/Dummy.pm';
224  my $sfile='blib/lib/Big/Dummy.pm';
225  ok(-r $tfile,"install file already exists");
226  ok(-r $sfile,"source file already exists");
227  utime time-1200, time-1200, $sfile or die "utime '$sfile' failed:$!";   
228  ok( (stat $tfile)[9]!=(stat $sfile)[9],'  Times are different');
229  install([from_to=>{ 'blib/lib' => 'install-test/other_lib/perl',
230           read   => 'install-test/packlist',
231           write  => 'install-test/packlist'
232         },result=>\my %result]);
233  ok( -d 'install-test/other_lib/perl',        'install made other dir' );
234  ok( -r 'install-test/other_lib/perl/Big/Dummy.pm', '  .pm file installed' );
235  ok( -r 'install-test/packlist',              '  packlist exists' );
236  ok( (stat $tfile)[9]!=(stat$sfile)[9],'  Times are different');
237  ok( !$result{install},'  nothing should have been installed');
238  ok( $result{install_unchanged},'  install_unchanged should be populated');
239}
240