Skip to content

Commit

Permalink
Warn user if interface cannot be detected
Browse files Browse the repository at this point in the history
We also tell the user how this situation can be fixed (by asking them to
send us the output if `ifconfig` or `ipconfig`).  This is much more
helpful than "uninitialized value" warnings.

If the interface can't be detected, we return the empty string from the
`ip()` sub so that *something* turns up, but not something that's going
to be misleading.
  • Loading branch information
paultcochrane committed Aug 17, 2019
1 parent 887f011 commit d6cba1e
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 3 deletions.
1 change: 1 addition & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Revision history for Perl extension Sys::HostIP.
{{$NEXT}}

* Fix test failures on systems with a Russian locale.
* Warn users if the interface can't be detected.

2.110 2019-02-23 17:40:31+02:00 Asia/Jerusalem

Expand Down
1 change: 1 addition & 0 deletions dist.ini
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ File::Basename = 0

[Prereqs / TestRequires ]
Test::More = 0
Capture::Tiny = 0
Data::Dumper = 0
File::Spec = 0
Test::Pod = 0
Expand Down
12 changes: 9 additions & 3 deletions lib/Sys/HostIP.pm
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ sub ip {

if ($IS_WIN) {
my @if_keys = sort keys %{$if_info};
return ( $if_info->{ $if_keys[0] } );
return scalar @if_keys != 0 ? ( $if_info->{ $if_keys[0] } ) : '';
} else {
my $lo_found;

Expand All @@ -73,7 +73,7 @@ sub ip {
# we get here if loopback is the only active device
$lo_found and return '127.0.0.1';

return;
return '';
}
}

Expand Down Expand Up @@ -127,9 +127,15 @@ sub _get_ifconfig_binary {
sub _get_interface_info {
my $self = shift;

return $IS_WIN
my $interface_info = $IS_WIN
? $self->_get_win32_interface_info()
: $self->_get_unix_interface_info();

my $warning_msg = "Unable to detect interface information!\n"
. "Please open an issue on https://github.com/xsawyerx/sys-hostip/issues with your 'ipconfig' or 'ifconfig' output";
warn $warning_msg if values %$interface_info == 0;

return $interface_info;
}

sub _clean_ifconfig_env {
Expand Down
49 changes: 49 additions & 0 deletions t/03-iface-undetected.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!perl

use strict;
use warnings;

use lib '.';
use Test::More 'tests' => 4;
use Sys::HostIP qw/ip/;
use Capture::Tiny qw/capture/;

# check unavailable interface on Windows
{
# Mock Windows
local $Sys::HostIP::IS_WIN = 1;
{
## no critic qw(TestingAndDebugging::ProhibitNoWarnings)
no warnings qw/redefine once/;
*Sys::HostIP::_get_win32_interface_info = sub {
return {};
};
}
my ($stdout, $stderr, @result) = capture { ip() };

like(
$stderr,
qr/Unable to detect interface information!/,
"Inform user if interface info not detectable"
);
is( $result[0], '', "Empty ip info returned" );
}

# check unavailable interface on *nix
{
{
## no critic qw(TestingAndDebugging::ProhibitNoWarnings)
no warnings qw/redefine once/;
*Sys::HostIP::_get_unix_interface_info = sub {
return {};
};
}
my ($stdout, $stderr, @result) = capture { ip() };

like(
$stderr,
qr/Unable to detect interface information!/,
"Inform user if interface info not detectable"
);
is( $result[0], '', "Empty ip info returned" );
}

0 comments on commit d6cba1e

Please sign in to comment.