Skip to content

Commit

Permalink
Only run on-line tests when wanted
Browse files Browse the repository at this point in the history
  • Loading branch information
nigelhorne committed May 12, 2017
1 parent 70969f7 commit de23a34
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 27 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ nytprof.out
*.o
*.bs
/_eumm/
t/online.enabled
.*.swp
3 changes: 3 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
Revision history for Geo-Coder-XYZ

0.02
Only run on-line tests when wanted

0.01 Thu May 11 18:31:28 EDT 2017
First version

57 changes: 57 additions & 0 deletions Makefile.PL
Original file line number Diff line number Diff line change
@@ -1,6 +1,29 @@
use strict;
use warnings;
use ExtUtils::MakeMaker;
use Getopt::Long; # Technique inspired by IO::Lambda
use IO::Socket::INET;

my $online_tests;

if($ENV{RELEASE_TESTING}) {
$online_tests = are_online();
} elsif($ENV{AUTOMATED_TESTING} || $ENV{NO_NETWORK_TESTING}) {
$online_tests = 0;
} else {
Getopt::Long::GetOptions('online-tests!' => \$online_tests);

if(!defined($online_tests)) {
$online_tests = are_online();
}
}

if($online_tests) {
open(my $enabled, '>', 't/online.enabled') || die "Can't touch t/online.enabled $!";
close($enabled) || die "Can't touch t/online.enabled $!";
} else {
unlink('t/online.enabled');
}

WriteMakefile(
NAME => 'Geo::Coder::XYZ',
Expand Down Expand Up @@ -42,3 +65,37 @@ WriteMakefile(
},
MIN_PERL_VERSION => '5.6.2' # Probably would work, but never tested on earlier versions than this
);

sub are_online
{
my $s = IO::Socket::INET->new(
PeerAddr => 'geocode.xyz:443',
Timeout => 10
);
if($s) {
print <<EOF;
You appear to be directly connected to the Internet. I have some tests
that try to query geocode.xyz.
EOF
close($s);

# Timeout inspired by Mail::IMAPClient
my $rc;
eval {
local $SIG{ALRM} = sub { die "alarm\n" };
alarm(60);
$rc = prompt('Do you want to enable these tests?', 'y') =~ /^y/i ? 1 : 0;
alarm(0);
};
if($@) {
print "\n";
return 1; # The default is 'y'
}
return $rc;
} else {
print "On-line tests disabled because I couldn't detect an Internet connexion\n";
return 0;
}
}
3 changes: 2 additions & 1 deletion ignore.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ pm_to_blib*
.lwpcookies
cover_db
pod2htm*.tmp
Geo-Coder-CA-*
Geo-Coder-XYZ-*
.git
*.swp
.appveyor.yml
.travis.yml
t/online.enabled
6 changes: 3 additions & 3 deletions lib/Geo/Coder/XYZ.pm
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use URI;

=head1 NAME
Geo::Coder::XYZ - Provides a geocoding functionality using http:://geocoder.xyz
Geo::Coder::XYZ - Provides a geocoding functionality using https://geocode.xyz
=head1 VERSION
Expand All @@ -31,7 +31,7 @@ our $VERSION = '0.01';
=head1 DESCRIPTION
Geo::Coder::XYZ provides an interface to geocoder.xyz, a free geocode database covering many countries.
Geo::Coder::XYZ provides an interface to geocode.xyz, a free geocode database covering many countries.
=head1 METHODS
Expand Down Expand Up @@ -158,7 +158,7 @@ Based on L<Geo::Coder::Coder::Googleplaces>.
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
Lots of thanks to the folks at geocoder.xyz.
Lots of thanks to the folks at geocode.xyz.
=head1 SEE ALSO
Expand Down
29 changes: 18 additions & 11 deletions t/ca.t
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,26 @@
use warnings;
use strict;
use Test::Number::Delta within => 1e-2;
use Test::Most tests => 6;
use Geo::Coder::XYZ;
use Test::Most tests => 7;

BEGIN {
use_ok('Geo::Coder::XYZ');
}

CA: {
my $geocoder = new_ok('Geo::Coder::XYZ');
my $location = $geocoder->geocode('9235 Main St, Richibucto, New Brunswick, Canada');
delta_ok($location->{latt}, 46.67);
delta_ok($location->{longt}, -64.87);
SKIP: {
skip 'Test requires Internet access', 6 unless(-e 't/online.enabled');

my $geocoder = new_ok('Geo::Coder::XYZ');
my $location = $geocoder->geocode('9235 Main St, Richibucto, New Brunswick, Canada');
delta_ok($location->{latt}, 46.67);
delta_ok($location->{longt}, -64.87);

$location = $geocoder->geocode(location => '9235 Main St, Richibucto, New Brunswick, Canada');
delta_ok($location->{latt}, 46.67);
delta_ok($location->{longt}, -64.87);
$location = $geocoder->geocode(location => '9235 Main St, Richibucto, New Brunswick, Canada');
delta_ok($location->{latt}, 46.67);
delta_ok($location->{longt}, -64.87);

my $address = $geocoder->reverse_geocode(latlng => '46.67,-64.87');
like($address->{'city'}, qr/Richibucto/i, 'test reverse');
my $address = $geocoder->reverse_geocode(latlng => '46.67,-64.87');
like($address->{'city'}, qr/Richibucto/i, 'test reverse');
}
}
31 changes: 19 additions & 12 deletions t/uk.t
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,27 @@
use warnings;
use strict;
use Test::Number::Delta within => 1e-2;
use Test::Most tests => 6;
use Geo::Coder::XYZ;
use Test::Most tests => 7;

XYZ: {
my $geocoder = new_ok('Geo::Coder::XYZ');
BEGIN {
use_ok('Geo::Coder::XYZ');
}

UK: {
SKIP: {
skip 'Test requires Internet access', 6 unless(-e 't/online.enabled');

my $geocoder = new_ok('Geo::Coder::XYZ');

my $location = $geocoder->geocode('10 Downing St., London, UK');
delta_ok($location->{latt}, 51.50);
delta_ok($location->{longt}, -0.13);
my $location = $geocoder->geocode('10 Downing St., London, UK');
delta_ok($location->{latt}, 51.50);
delta_ok($location->{longt}, -0.13);

$location = $geocoder->geocode(location => '10 Downing St., London, UK');
delta_ok($location->{latt}, 51.50);
delta_ok($location->{longt}, -0.13);
$location = $geocoder->geocode(location => '10 Downing St., London, UK');
delta_ok($location->{latt}, 51.50);
delta_ok($location->{longt}, -0.13);

my $address = $geocoder->reverse_geocode(latlng => '51.50,-0.13');
like($address->{'city'}, qr/^London$/i, 'test reverse');
my $address = $geocoder->reverse_geocode(latlng => '51.50,-0.13');
like($address->{'city'}, qr/^London$/i, 'test reverse');
}
}

0 comments on commit de23a34

Please sign in to comment.