Re: [PATCH@32279] Upgrade File::Fetch to 0.13_04 - fixed for VMS.
[p5sagit/p5-mst-13.2.git] / lib / CPANPLUS / t / 05_CPANPLUS-Internals-Fetch.t
1 ### make sure we can find our conf.pl file
2 BEGIN { 
3     use FindBin; 
4     require "$FindBin::Bin/inc/conf.pl";
5 }
6
7 use strict;
8
9 use CPANPLUS::Backend;
10
11 use Test::More 'no_plan';
12 use Data::Dumper;
13 use File::Spec;
14 use Cwd;
15 use File::Basename;
16 use CPANPLUS::Internals::Constants;
17
18 my $conf = gimme_conf();
19
20 my $cb = CPANPLUS::Backend->new( $conf );
21 isa_ok($cb, "CPANPLUS::Internals" );
22
23 my $mod = $cb->module_tree( TEST_CONF_MODULE );
24 isa_ok( $mod,  'CPANPLUS::Module' );
25
26 ### fail host tests ###
27 {   my $host = {};
28     my $rv   = $cb->_add_fail_host( host => $host );
29     
30     ok( $rv,                    "Failed host added " );
31     ok(!$cb->_host_ok( host => $host),   
32                                 "   Host registered as failed" );
33     ok( $cb->_host_ok( host => {} ),    
34                                 "   Fresh host unregistered" );
35 }
36
37 ### refetch, even if it's there already ###
38 {   my $where = $cb->_fetch( module => $mod, force => 1 );
39
40     ok( $where,                 "File downloaded to '$where'" );
41     ok( -s $where,              "   File exists" );                          
42     unlink $where;
43     ok(!-e $where,              "   File removed" );
44 }
45
46 ### try to fetch something that doesn't exist ###
47 {   ### set up a bogus host first ###
48     my $hosts   = $conf->get_conf('hosts');
49     my $fail    = { scheme  => 'file', 
50                     path    => "$0/$0" };
51     
52     unshift @$hosts, $fail;
53     $conf->set_conf( hosts => $hosts );
54     
55     ### the fallback host will get it ###
56     my $where = $cb->_fetch( module => $mod, force => 1, verbose => 0 );
57     ok($where,                  "File downloaded to '$where'" );
58     ok( -s $where,              "   File exists" );                          
59     
60     ### but the error should be recorded ###
61     like( CPANPLUS::Error->stack_as_string, qr/Fetching of .*? failed/s,
62                                 "   Error recorded appropriately" ); 
63
64     ### host marked as bad? ###
65     ok(!$cb->_host_ok( host => $fail ),   
66                                 "   Failed host logged properly" );    
67
68     ### restore the hosts ###
69     shift @$hosts; $conf->set_conf( hosts => $hosts );
70 }
71
72 ### try and fetch a URI
73 {   my $base    = basename($0);
74
75     ### do an ON_UNIX test, cygwin will fail tests otherwise (#14553)
76     ### create a file URI. Make sure to split it by LOCAL rules
77     ### and JOIN by unix rules, so we get a proper file uri
78     ### otherwise, we might break win32. See bug #18702
79
80     my $cwd = cwd();
81     my $in_file;
82
83     if ($^O eq 'VMS') {
84         $in_file = File::Spec->catfile($cwd, $base);
85         ### Force UNIX syntax on VMS
86         $in_file = VMS::Filespec::unixify($in_file);
87     } else {
88         $in_file = File::Spec::Unix->catfile(
89                           File::Spec::Unix->catdir(
90                               File::Spec->spitdir( $cwd ),
91                           ),
92                           $base
93                       )
94     }
95
96     my $target  = CREATE_FILE_URI->($in_file);
97                   
98     my $fake    = $cb->parse_module( module => $target );
99     
100     ok( IS_FAKE_MODOBJ->(mod => $fake), 
101                                 "Fake module created from $0" );
102     is( $fake->status->_fetch_from, $target,
103                                 "   Fetch from set ok" );                                 
104                                 
105     my $where = $fake->fetch;
106     ok( $where,                 "   $target fetched ok" );
107     ok( -s $where,              "   $where exists" );
108     like( $where, '/'. UNKNOWN_DL_LOCATION .'/',
109                                 "   Saved to proper location" );
110     like( $where, qr/$base$/,   "   Saved with proper name" );                                
111 }
112
113
114 # Local variables:
115 # c-indentation-style: bsd
116 # c-basic-offset: 4
117 # indent-tabs-mode: nil
118 # End:
119 # vim: expandtab shiftwidth=4: