1 BEGIN { chdir 't' if -d 't' };
6 use Test::More 'no_plan';
9 use File::Basename qw[basename];
10 use File::Path qw[rmtree];
13 use_ok('File::Fetch');
15 ### optionally set debugging ###
16 $File::Fetch::DEBUG = $File::Fetch::DEBUG = 1 if $ARGV[0];
17 $IPC::Cmd::DEBUG = $IPC::Cmd::DEBUG = 1 if $ARGV[0];
19 $File::Fetch::FORCEIPV4=1;
21 unless( $ENV{PERL_CORE} ) {
24 ####################### NOTE ##############################
26 Some of these tests assume you are connected to the
27 internet. If you are not, or if certain protocols or hosts
28 are blocked and/or firewalled, these tests could fail due
29 to no fault of the module itself.
31 ###########################################################
35 sleep 3 unless $File::Fetch::DEBUG;
38 ### show us the tools IPC::Cmd will use to run binary programs
39 if( $File::Fetch::DEBUG ) {
40 ### stupid 'used only once' warnings ;(
41 diag( "IPC::Run enabled: " .
42 $IPC::Cmd::USE_IPC_RUN || $IPC::Cmd::USE_IPC_RUN );
43 diag( "IPC::Run available: " . IPC::Cmd->can_use_ipc_run );
44 diag( "IPC::Run vesion: $IPC::Run::VERSION" );
45 diag( "IPC::Open3 enabled: " .
46 $IPC::Cmd::USE_IPC_OPEN3 || $IPC::Cmd::USE_IPC_OPEN3 );
47 diag( "IPC::Open3 available: " . IPC::Cmd->can_use_ipc_open3 );
48 diag( "IPC::Open3 vesion: $IPC::Open3::VERSION" );
52 my %heuristics = map { $_ => 1 } qw(http ftp rsync file git);
54 ### these go on all platforms
56 { uri => 'ftp://cpan.org/pub/mirror/index.txt',
59 path => '/pub/mirror/',
62 { uri => 'rsync://cpan.pair.com/CPAN/MIRRORING.FROM',
64 host => 'cpan.pair.com',
66 file => 'MIRRORING.FROM',
68 { uri => 'git://github.com/Perl-Toolchain-Gang/file-fetch.git',
71 path => '/Perl-Toolchain-Gang/',
72 file => 'file-fetch.git',
74 { uri => 'http://localhost/tmp/index.txt',
76 host => 'localhost', # host is empty only on 'file://'
81 ### only test host part, the rest is OS dependant
82 { uri => 'file://localhost/tmp/index.txt',
83 host => '', # host should be empty on 'file://'
87 ### these only if we're not on win32/vms
89 { uri => 'file:///usr/local/tmp/foo.txt',
92 path => '/usr/local/tmp/',
95 { uri => 'file://hostname/tmp/foo.txt',
101 ) if not &File::Fetch::ON_WIN and not &File::Fetch::ON_VMS;
103 ### these only on win32
105 { uri => 'file:////hostname/share/tmp/foo.txt',
112 { uri => 'file:///D:/tmp/foo.txt',
119 { uri => 'file:///D|/tmp/foo.txt',
126 ) if &File::Fetch::ON_WIN;
132 like( $File::Fetch::USER_AGENT, qr/$File::Fetch::VERSION/,
133 "User agent contains version" );
134 like( $File::Fetch::FROM_EMAIL, qr/@/,
135 q[Email contains '@'] );
138 ### parse uri tests ###
139 for my $entry (@map ) {
140 my $uri = $entry->{'uri'};
142 my $href = File::Fetch->_parse_uri( $uri );
143 ok( $href, "Able to parse uri '$uri'" );
145 for my $key ( sort keys %$entry ) {
146 is( $href->{$key}, $entry->{$key},
147 " '$key' ok ($entry->{$key}) for $uri");
151 ### File::Fetch->new tests ###
152 for my $entry (@map) {
153 my $ff = File::Fetch->new( uri => $entry->{uri} );
155 ok( $ff, "Object for uri '$entry->{uri}'" );
156 isa_ok( $ff, "File::Fetch", " Object" );
158 for my $acc ( keys %$entry ) {
159 is( $ff->$acc(), $entry->{$acc},
160 " Accessor '$acc' ok ($entry->{$acc})" );
164 ### fetch() tests ###
166 ### file:// tests ###
168 my $prefix = &File::Fetch::ON_UNIX ? 'file://' : 'file:///';
169 my $uri = $prefix . cwd() .'/'. basename($0);
171 for (qw[lwp lftp file]) {
172 _fetch_uri( file => $uri, $_ );
178 require IO::Socket::INET;
179 my $sock = IO::Socket::INET->new( PeerAddr => 'mirror.bytemark.co.uk', PeerPort => 21, Timeout => 20 )
180 or $heuristics{ftp} = 0;
184 { my $uri = 'ftp://mirror.bytemark.co.uk/CPAN/index.html';
185 for (qw[wget curl lftp fetch ncftp]) {
187 ### STUPID STUPID warnings ###
188 next if $_ eq 'ncftp' and $File::Fetch::FTP_PASSIVE
189 and $File::Fetch::FTP_PASSIVE;
191 _fetch_uri( ftp => $uri, $_ );
197 require IO::Socket::INET;
198 my $sock = IO::Socket::INET->new( PeerAddr => 'httpbin.org', PeerPort => 80, Timeout => 20 )
199 or $heuristics{http} = 0;
202 ### http:// tests ###
203 { for my $uri ( 'http://httpbin.org/html',
204 'http://httpbin.org/response-headers?q=1',
205 'http://httpbin.org/response-headers?q=1&y=2',
206 #'http://www.cpan.org/index.html?q=1&y=2',
207 #'http://user:passwd@httpbin.org/basic-auth/user/passwd',
209 for (qw[lwp httptiny wget curl lftp fetch lynx httplite iosock]) {
210 _fetch_uri( http => $uri, $_ );
217 require IO::Socket::INET;
218 my $sock = IO::Socket::INET->new( PeerAddr => 'cpan.pair.com', PeerPort => 873, Timeout => 20 )
219 or $heuristics{rsync} = 0;
222 ### rsync:// tests ###
223 { my $uri = 'rsync://cpan.pair.com/CPAN/MIRRORING.FROM';
226 _fetch_uri( rsync => $uri, $_ );
232 require IO::Socket::INET;
233 my $sock = IO::Socket::INET->new( PeerAddr => 'github.com', PeerPort => 9418, Timeout => 20 )
234 or $heuristics{git} = 0;
238 { my $uri = 'git://github.com/Perl-Toolchain-Gang/file-fetch.git';
241 local $ENV{GIT_CONFIG_NOSYSTEM} = 1;
242 local $ENV{XDG_CONFIG_HOME};
244 _fetch_uri( git => $uri, $_ );
251 my $method = shift or return;
254 skip "'$method' fetching tests disabled under perl core", 4
257 skip "'$type' fetching tests disabled due to heuristic failure", 4
258 unless $heuristics{ $type };
260 ### stupid warnings ###
261 $File::Fetch::METHODS =
262 $File::Fetch::METHODS = { $type => [$method] };
265 my $ff = File::Fetch->new( uri => $uri );
267 ok( $ff, "FF object for $uri (fetch with $method)" );
269 for my $to ( 'tmp', do { \my $o } ) { SKIP: {
272 my $how = ref $to && $type ne 'git' ? 'slurp' : 'file';
273 my $skip = ref $to ? 4 : 3;
275 ok( 1, " Fetching '$uri' in $how mode" );
277 my $file = $ff->fetch( to => $to );
279 skip "You do not have '$method' installed/available", $skip
280 if $File::Fetch::METHOD_FAIL->{$method} &&
281 $File::Fetch::METHOD_FAIL->{$method};
283 ### if the file wasn't fetched, it may be a network/firewall issue
284 skip "Fetch failed; no network connectivity for '$type'?", $skip
287 ok( $file, " File ($file) fetched with $method ($uri)" );
289 ### check we got some contents if we were meant to slurp
290 if( ref $to && $type ne 'git' ) {
291 ok( $$to, " Contents slurped" );
294 ok( $file && -s $file,
296 is( $file && basename($file), $ff->output_file,
297 " File has expected name" );