Move CPANPLUS from ext/ to cpan/
[p5sagit/p5-mst-13.2.git] / cpan / CPANPLUS / t / inc / conf.pl
1 ### On VMS, the ENV is not reset after the program terminates.
2 ### So reset it here explicitly
3 my ($old_env_path, $old_env_perl5lib);
4 BEGIN {
5     use FindBin; 
6     use File::Spec;
7     
8     ### paths to our own 'lib' and 'inc' dirs
9     ### include them, relative from t/
10     my @paths   = map { "$FindBin::Bin/$_" } qw[../lib inc];
11
12     ### absolute'ify the paths in @INC;
13     my @rel2abs = map { File::Spec->rel2abs( $_ ) }
14                     grep { not File::Spec->file_name_is_absolute( $_ ) } @INC;
15     
16     ### use require to make devel::cover happy
17     require lib;
18     for ( @paths, @rel2abs ) { 
19         my $l = 'lib'; 
20         $l->import( $_ ) 
21     }
22
23     use Config;
24
25     ### and add them to the environment, so shellouts get them
26     $old_env_perl5lib = $ENV{'PERL5LIB'};
27     $ENV{'PERL5LIB'}  = join $Config{'path_sep'}, 
28                         grep { defined } $ENV{'PERL5LIB'}, @paths, @rel2abs;
29     
30     ### add our own path to the front of $ENV{PATH}, so that cpanp-run-perl
31     ### and friends get picked up
32     $old_env_path = $ENV{PATH};
33     $ENV{'PATH'}  = join $Config{'path_sep'}, 
34                     grep { defined } "$FindBin::Bin/../bin", $ENV{'PATH'};
35
36     ### Fix up the path to perl, as we're about to chdir
37     ### but only under perlcore, or if the path contains delimiters,
38     ### meaning it's relative, but not looked up in your $PATH
39     $^X = File::Spec->rel2abs( $^X ) 
40         if $ENV{PERL_CORE} or ( $^X =~ m|[/\\]| );
41
42     ### chdir to our own test dir, so we know all files are relative 
43     ### to this point, no matter whether run from perlcore tests or
44     ### regular CPAN installs
45     chdir "$FindBin::Bin" if -d "$FindBin::Bin"
46 }
47
48 BEGIN {
49     use IPC::Cmd;
50    
51     ### Win32 has issues with redirecting FD's properly in IPC::Run:
52     ### Can't redirect fd #4 on Win32 at IPC/Run.pm line 2801
53     $IPC::Cmd::USE_IPC_RUN = 0 if $^O eq 'MSWin32';
54     $IPC::Cmd::USE_IPC_RUN = 0 if $^O eq 'MSWin32';
55 }
56
57 ### Use a $^O comparison, as depending on module at this time
58 ### may cause weird errors/warnings
59 END {
60     if ($^O eq 'VMS') {
61         ### VMS environment variables modified by this test need to be put back
62         ### path is "magic" on VMS, we can not tell if it really existed before
63         ### this was run, because VMS will magically pretend that a PATH
64         ### environment variable exists set to the current working directory
65         $ENV{PATH} = $old_env_path;
66
67         if (defined $old_env_perl5lib) {
68             $ENV{PERL5LIB} = $old_env_perl5lib;
69         } else {
70             delete $ENV{PERL5LIB};
71         }
72     }
73 }
74
75 use strict;
76 use CPANPLUS::Configure;
77 use CPANPLUS::Error ();
78
79 use File::Path      qw[rmtree];
80 use FileHandle;
81 use File::Basename  qw[basename];
82
83 {   ### Force the ignoring of .po files for L::M::S
84     $INC{'Locale::Maketext::Lexicon.pm'} = __FILE__;
85     $Locale::Maketext::Lexicon::VERSION = 0;
86 }
87
88 my $Env = 'PERL5_CPANPLUS_TEST_VERBOSE';
89
90 # prereq has to be in our package file && core!
91 use constant TEST_CONF_PREREQ           => 'Cwd';   
92 use constant TEST_CONF_MODULE           => 'Foo::Bar::EU::NOXS';
93 use constant TEST_CONF_MODULE_SUB       => 'Foo::Bar::EU::NOXS::Sub';
94 use constant TEST_CONF_AUTHOR           => 'EUNOXS';
95 use constant TEST_CONF_INST_MODULE      => 'Foo::Bar';
96 use constant TEST_CONF_INVALID_MODULE   => 'fnurk';
97 use constant TEST_CONF_MIRROR_DIR       => 'dummy-localmirror';
98 use constant TEST_CONF_CPAN_DIR         => 'dummy-CPAN';
99 use constant TEST_CONF_CPANPLUS_DIR     => 'dummy-cpanplus';
100 use constant TEST_CONF_INSTALL_DIR      => File::Spec->rel2abs(
101                                                 File::Spec->catdir(      
102                                                     TEST_CONF_CPANPLUS_DIR,
103                                                     'install'
104                                                 )
105                                             );
106
107 sub dummy_cpan_dir {
108     ### VMS needs this in directory format for rel2abs
109     my $test_dir = $^O eq 'VMS'
110                     ? File::Spec->catdir(TEST_CONF_CPAN_DIR)
111                     : TEST_CONF_CPAN_DIR;
112
113     ### Convert to an absolute file specification
114     my $abs_test_dir = File::Spec->rel2abs($test_dir);
115     
116     ### According to John M: the hosts path needs to be in UNIX format.  
117     ### File::Spec::Unix->rel2abs does not work at all on VMS
118     $abs_test_dir    = VMS::Filespec::unixify( $abs_test_dir ) if $^O eq 'VMS';
119
120     return $abs_test_dir;
121 }
122
123 sub gimme_conf { 
124
125     ### don't load any other configs than the heuristic one
126     ### during tests. They might hold broken/incorrect data
127     ### for our test suite. Bug [perl #43629] showed this.
128     my $conf = CPANPLUS::Configure->new( load_configs => 0 );
129
130     my $dummy_cpan = dummy_cpan_dir();
131     
132     $conf->set_conf( hosts  => [ { 
133                         path        => $dummy_cpan,
134                         scheme      => 'file',
135                     } ],      
136     );
137     $conf->set_conf( base       => File::Spec->rel2abs(TEST_CONF_CPANPLUS_DIR));
138     $conf->set_conf( dist_type  => '' );
139     $conf->set_conf( signature  => 0 );
140     $conf->set_conf( verbose    => 1 ) if $ENV{ $Env };
141     
142     ### never use a pager in the test suite
143     $conf->set_program( pager   => '' );
144
145     ### dmq tells us that we should run with /nologo
146     ### if using nmake, as it's very noisy otherwise.
147     {   my $make = $conf->get_program('make');
148         if( $make and basename($make) =~ /^nmake/i ) {
149             $conf->set_conf( makeflags => '/nologo' );
150         }
151     }
152
153     $conf->set_conf( source_engine =>  $ENV{CPANPLUS_SOURCE_ENGINE} )
154         if $ENV{CPANPLUS_SOURCE_ENGINE};
155     
156     _clean_test_dir( [
157         $conf->get_conf('base'),     
158         TEST_CONF_MIRROR_DIR,
159 #         TEST_INSTALL_DIR_LIB,
160 #         TEST_INSTALL_DIR_BIN,
161 #         TEST_INSTALL_DIR_MAN1, 
162 #         TEST_INSTALL_DIR_MAN3,
163     ], (  $ENV{PERL_CORE} ? 0 : 1 ) );
164         
165     return $conf;
166 };
167
168 {
169     my $fh;
170     my $file = ".".basename($0).".output";
171     sub output_handle {
172         return $fh if $fh;
173         
174         $fh = FileHandle->new(">$file")
175                     or warn "Could not open output file '$file': $!";
176        
177         $fh->autoflush(1);
178         return $fh;
179     }
180     
181     sub output_file { return $file }
182     
183     
184     
185     ### redirect output from msg() and error() output to file
186     unless( $ENV{$Env} ) {
187     
188         print "# To run tests in verbose mode, set ".
189               "\$ENV{$Env} = 1\n" unless $ENV{PERL_CORE};
190     
191         1 while unlink $file;   # just in case
192     
193         $CPANPLUS::Error::ERROR_FH  =
194         $CPANPLUS::Error::ERROR_FH  = output_handle();
195         
196         $CPANPLUS::Error::MSG_FH    =
197         $CPANPLUS::Error::MSG_FH    = output_handle();
198         
199     }        
200 }
201
202
203 ### clean these files if we're under perl core
204 END { 
205     if ( $ENV{PERL_CORE} ) {
206         close output_handle(); 1 while unlink output_file();
207
208         _clean_test_dir( [
209             gimme_conf->get_conf('base'),   
210             TEST_CONF_MIRROR_DIR,
211     #         TEST_INSTALL_DIR_LIB,
212     #         TEST_INSTALL_DIR_BIN,
213     #         TEST_INSTALL_DIR_MAN1, 
214     #         TEST_INSTALL_DIR_MAN3,
215         ], 0 ); # DO NOT be verbose under perl core -- makes tests fail
216     }
217 }
218
219 ### whenever we start a new script, we want to clean out our
220 ### old files from the test '.cpanplus' dir..
221 sub _clean_test_dir {
222     my $dirs    = shift || [];
223     my $verbose = shift || 0;
224
225     for my $dir ( @$dirs ) {
226
227         ### no point if it doesn't exist;
228         next unless -d $dir;
229
230         my $dh;
231         opendir $dh, $dir or die "Could not open basedir '$dir': $!";
232         while( my $file = readdir $dh ) { 
233             next if $file =~ /^\./;  # skip dot files
234             
235             my $path = File::Spec->catfile( $dir, $file );
236             
237             ### directory, rmtree it
238             if( -d $path ) {
239
240                 ### John Malmberg reports yet another VMS issue:
241                 ### A directory name on VMS in VMS format ends with .dir 
242                 ### when it is referenced as a file.
243                 ### In UNIX format traditionally PERL on VMS does not remove the
244                 ### '.dir', however the VMS C library conversion routines do
245                 ### remove the '.dir' and the VMS C library routines can not 
246                 ### handle the '.dir' being present on UNIX format filenames.
247                 ### So code doing the fixup has on VMS has to be able to handle 
248                 ### both UNIX format names and VMS format names. 
249                 
250                 ### XXX See http://www.xray.mpe.mpg.de/
251                 ### mailing-lists/perl5-porters/2007-10/msg00064.html
252                 ### for details -- the below regex could use some touchups
253                 ### according to John. M.            
254                 $file =~ s/\.dir$//i if $^O eq 'VMS';
255                 
256                 my $dirpath = File::Spec->catdir( $dir, $file );
257
258                 print "# Deleting directory '$dirpath'\n" if $verbose;
259                 eval { rmtree( $dirpath ) };
260                 warn "Could not delete '$dirpath' while cleaning up '$dir'" 
261                     if $@;
262            
263             ### regular file
264             } else {
265                 print "# Deleting file '$path'\n" if $verbose;
266                 1 while unlink $path;
267             }            
268         }       
269     
270         close $dh;
271     }
272     
273     return 1;
274 }
275 1;