From d6cba1e802960a310cd1045ec809c7170083e757 Mon Sep 17 00:00:00 2001 From: Paul Cochrane Date: Mon, 5 Aug 2019 13:41:43 +0200 Subject: [PATCH] Warn user if interface cannot be detected 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. --- Changes | 1 + dist.ini | 1 + lib/Sys/HostIP.pm | 12 +++++++--- t/03-iface-undetected.t | 49 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 t/03-iface-undetected.t diff --git a/Changes b/Changes index 3293959..2371a47 100644 --- a/Changes +++ b/Changes @@ -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 diff --git a/dist.ini b/dist.ini index 5cb87f4..7cc3dea 100644 --- a/dist.ini +++ b/dist.ini @@ -52,6 +52,7 @@ File::Basename = 0 [Prereqs / TestRequires ] Test::More = 0 +Capture::Tiny = 0 Data::Dumper = 0 File::Spec = 0 Test::Pod = 0 diff --git a/lib/Sys/HostIP.pm b/lib/Sys/HostIP.pm index a0d95e0..f5a98b2 100644 --- a/lib/Sys/HostIP.pm +++ b/lib/Sys/HostIP.pm @@ -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; @@ -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 ''; } } @@ -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 { diff --git a/t/03-iface-undetected.t b/t/03-iface-undetected.t new file mode 100644 index 0000000..d56b218 --- /dev/null +++ b/t/03-iface-undetected.t @@ -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" ); +}