1 BEGIN { chdir 't' if -d 't' };
6 use Test::More 'no_plan';
9 use File::Basename qw[basename];
12 use_ok('File::Fetch');
14 ### optionally set debugging ###
15 $File::Fetch::DEBUG = $File::Fetch::DEBUG = 1 if $ARGV[0];
16 $IPC::Cmd::DEBUG = $IPC::Cmd::DEBUG = 1 if $ARGV[0];
18 unless( $ENV{PERL_CORE} ) {
21 ####################### NOTE ##############################
23 Some of these tests assume you are connected to the
24 internet. If you are not, or if certain protocols or hosts
25 are blocked and/or firewalled, these tests could fail due
26 to no fault of the module itself.
28 ###########################################################
32 sleep 3 unless $File::Fetch::DEBUG;
35 ### show us the tools IPC::Cmd will use to run binary programs
36 if( $File::Fetch::DEBUG ) {
37 ### stupid 'used only once' warnings ;(
38 diag( "IPC::Run enabled: " .
39 $IPC::Cmd::USE_IPC_RUN || $IPC::Cmd::USE_IPC_RUN );
40 diag( "IPC::Run available: " . IPC::Cmd->can_use_ipc_run );
41 diag( "IPC::Run vesion: $IPC::Run::VERSION" );
42 diag( "IPC::Open3 enabled: " .
43 $IPC::Cmd::USE_IPC_OPEN3 || $IPC::Cmd::USE_IPC_OPEN3 );
44 diag( "IPC::Open3 available: " . IPC::Cmd->can_use_ipc_open3 );
45 diag( "IPC::Open3 vesion: $IPC::Open3::VERSION" );
49 ### these go on all platforms
51 { uri => 'ftp://cpan.org/pub/mirror/index.txt',
54 path => '/pub/mirror/',
57 { uri => 'rsync://cpan.pair.com/CPAN/MIRRORING.FROM',
59 host => 'cpan.pair.com',
61 file => 'MIRRORING.FROM',
63 { uri => 'http://localhost/tmp/index.txt',
65 host => 'localhost', # host is empty only on 'file://'
70 ### only test host part, the rest is OS dependant
71 { uri => 'file://localhost/tmp/index.txt',
72 host => '', # host should be empty on 'file://'
76 ### these only if we're not on win32/vms
78 { uri => 'file:///usr/local/tmp/foo.txt',
81 path => '/usr/local/tmp/',
84 { uri => 'file://hostname/tmp/foo.txt',
90 ) if not &File::Fetch::ON_WIN and not &File::Fetch::ON_VMS;
92 ### these only on win32
94 { uri => 'file:////hostname/share/tmp/foo.txt',
101 { uri => 'file:///D:/tmp/foo.txt',
108 { uri => 'file:///D|/tmp/foo.txt',
115 ) if &File::Fetch::ON_WIN;
119 { like( $File::Fetch::USER_AGENT, qr/$File::Fetch::VERSION/,
120 "User agent contains version" );
121 like( $File::Fetch::FROM_EMAIL, qr/@/,
122 q[Email contains '@'] );
125 ### parse uri tests ###
126 for my $entry (@map ) {
127 my $uri = $entry->{'uri'};
129 my $href = File::Fetch->_parse_uri( $uri );
130 ok( $href, "Able to parse uri '$uri'" );
132 for my $key ( sort keys %$entry ) {
133 is( $href->{$key}, $entry->{$key},
134 " '$key' ok ($entry->{$key}) for $uri");
138 ### File::Fetch->new tests ###
139 for my $entry (@map) {
140 my $ff = File::Fetch->new( uri => $entry->{uri} );
142 ok( $ff, "Object for uri '$entry->{uri}'" );
143 isa_ok( $ff, "File::Fetch", " Object" );
145 for my $acc ( keys %$entry ) {
146 is( $ff->$acc(), $entry->{$acc},
147 " Accessor '$acc' ok ($entry->{$acc})" );
151 ### fetch() tests ###
153 ### file:// tests ###
155 my $prefix = &File::Fetch::ON_UNIX ? 'file://' : 'file:///';
156 my $uri = $prefix . cwd() .'/'. basename($0);
158 for (qw[lwp lftp file]) {
159 _fetch_uri( file => $uri, $_ );
164 { my $uri = 'ftp://ftp.funet.fi/pub/CPAN/index.html';
165 for (qw[lwp netftp wget curl lftp ncftp]) {
167 ### STUPID STUPID warnings ###
168 next if $_ eq 'ncftp' and $File::Fetch::FTP_PASSIVE
169 and $File::Fetch::FTP_PASSIVE;
171 _fetch_uri( ftp => $uri, $_ );
175 ### http:// tests ###
176 { for my $uri ( 'http://www.cpan.org/index.html',
177 'http://www.cpan.org/index.html?q=1',
178 'http://www.cpan.org/index.html?q=1&y=2',
180 for (qw[lwp wget curl lftp lynx]) {
181 _fetch_uri( http => $uri, $_ );
186 ### rsync:// tests ###
187 { my $uri = 'rsync://cpan.pair.com/CPAN/MIRRORING.FROM';
190 _fetch_uri( rsync => $uri, $_ );
197 my $method = shift or return;
200 skip "'$method' fetching tests disabled under perl core", 4
203 ### stupid warnings ###
204 $File::Fetch::METHODS =
205 $File::Fetch::METHODS = { $type => [$method] };
208 my $ff = File::Fetch->new( uri => $uri );
210 ok( $ff, "FF object for $uri (fetch with $method)" );
212 for my $to ( 'tmp', do { \my $o } ) { SKIP: {
215 my $how = ref $to ? 'slurp' : 'file';
216 my $skip = ref $to ? 4 : 3;
218 ok( 1, " Fetching '$uri' in $how mode" );
220 my $file = $ff->fetch( to => $to );
222 skip "You do not have '$method' installed/available", $skip
223 if $File::Fetch::METHOD_FAIL->{$method} &&
224 $File::Fetch::METHOD_FAIL->{$method};
226 ### if the file wasn't fetched, it may be a network/firewall issue
227 skip "Fetch failed; no network connectivity for '$type'?", $skip
230 ok( $file, " File ($file) fetched with $method ($uri)" );
232 ### check we got some contents if we were meant to slurp
234 ok( $$to, " Contents slurped" );
237 ok( $file && -s $file,
239 is( $file && basename($file), $ff->output_file,
240 " File has expected name" );