Footprintless/Localhost.pm

filter: Statementstmt 98.4% 64 / 65 Branchbran 68.1% 15 / 22 Conditioncond 90.9% 10 / 11 MC/DCmcdc 83.3% 5 / 6 Subroutinesub 100% 13 / 13 Podpod 100% 8 / 8 totaltotal 92.0% 115 / 125 CC 18 SCAR 13.1
CC
18
Cov
91%
load_etc_hosts
6.0
_init
6.0
is_alias
4.1
CRAP 3.7
« Footprintless/CommandOptionsFactory.pm ↑ Summary Footprintless/App/Action.pm »
1 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Footprintless::Localhost;
5 12 $Footprintless::Localhost::VERSION = '1.29';
6 # ABSTRACT: A localhost alias resolver
7 # PODNAME: Footprintless::Localhost
8  
9 12 ▶use Log::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 12 my $logger = Log::Any->get_logger();
12 12 my $loopback_ipv4_regex = qr/127(?:.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}/;
13  
14 36 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
15 36 return bless( {}, shift )->_init(@_);
16 }
17  
18 171 ▶sub _add_alias {
Subroutine _add_alias: called CC=2 SCAR=7.0
19 171 my ( $self, $alias ) = @_;
20  
21 171 ▶ if ($alias) {
Branch: 1 branch
BranchTrueFalseTotal
if ($alias) 171 0 171
22 171 $logger->debugf( 'adding alias [%s]', $alias );
23 171 $self->{aliases}{ lc($alias) } = 1;
24 }
25  
26 171 return $self;
27 }
28  
29 36 ▶sub _init {
Subroutine _init: called CC=6 SCAR=17.9
30 36 my ( $self, %options ) = @_;
31  
32 # what gets returned if is_alias( <falsey> );
33 36 ▶ $self->{empty} = exists( $options{empty} ) ? $options{empty} : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $options{'empty'} ? : 0 36 36
34 36 ▶ $self->{etc_hosts_file} = $options{etc_hosts_file} || '/etc/hosts';
Condition100%2/2
$options{'etc_hosts_file'} || '/etc/hosts'
l!l
135
MC/DC100%1/1
$options{'etc_hosts_file'} || '/etc/hosts'
$options{'etc_hosts_file'}
Truth table100%2/2
A
A= $options{'etc_hosts_file'}
Aresult
11
00
35  
36 $self->_add_alias('localhost')->_add_alias('127.0.0.1')
37 36 ▶ unless ( $options{none} );
Branch: 1 branch
BranchTrueFalseTotal
unless $options{"none"} 34 2 36
38  
39 36 ▶ if ( $options{aliases} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"aliases"}) 1 35 36
40 1 $self->_add_alias($_) foreach @{ $options{aliases} };
41 }
42  
43 36 return $self;
44 }
45  
46 48 ▶sub is_alias {
Subroutine is_alias: called CC=4 SCAR=14.0
Pod documented
47 48 my ( $self, $hostname ) = @_;
48  
49 48 ▶ if ($hostname) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hostname) { } 48 0 48
50 return ( $self->{loaded}{'127 subnet'} && $hostname =~ /^\s*$loopback_ipv4_regex\s*$/ )
51 48 ▶ || $self->{aliases}{ lc($hostname) };
Condition100%6/6
$self->{'loaded'}{'127 subnet'} && $hostname =~ /^\s*$loopback_ipv4_regex\s*$/ || $self->{'aliases'}{lc $hostname}
l!l&&r!l&&!r
13710
$self->{"loaded"}{"127 subnet"} and $hostname =~ /^\s*$loopback_ipv4_regex\s*$/
!ll&&!rl&&r
6411
MC/DC100%3/3
$self->{'loaded'}{'127 subnet'} && $hostname =~ /^\s*$loopback_ipv4_regex\s*$/ || $self->{'aliases'}{lc $hostname}
$self->{"loaded"}{"127 subnet"} $hostname =~ /^\s*$loopback_ipv4_regex\s*$/ $self->{'aliases'}{lc $hostname}
Truth table100%5/5
A and B || C
A= $self->{"loaded"}{"127 subnet"}B= $hostname =~ /^\s*$loopback_ipv4_regex\s*$/C= $self->{'aliases'}{lc $hostname}
ABCresult
11X1
0X11
1011
0X00
1000
52 }
53 else {
54 0 return $self->{empty};
55 }
56 }
57  
58 10 ▶sub is_loaded {
Subroutine is_loaded: called CC=2 SCAR=7.1
Pod documented
59 10 my ( $self, $extra ) = @_;
60 10 ▶ return $self->{loaded} && $self->{loaded}{$extra};
Condition66%2/3
$self->{'loaded'} && $self->{'loaded'}{$extra}
!ll&&!rl&&r
037
MC/DC50%1/2
$self->{'loaded'} && $self->{'loaded'}{$extra}
$self->{'loaded'} $self->{'loaded'}{$extra}
Truth table66%2/3
A && B
A= $self->{'loaded'}B= $self->{'loaded'}{$extra}
ABresult
0X0
100
111
61 }
62  
63 32 ▶sub load_127_subnet {
Subroutine load_127_subnet: called CC=1 SCAR=0.0
Pod documented
64 32 my ($self) = @_;
65 32 $self->{loaded}{'127 subnet'} = 1;
66 32 return $self;
67 }
68  
69 32 ▶sub load_all {
Subroutine load_all: called CC=1 SCAR=0.0
Pod documented
70 32 return shift->load_etc_hosts()->load_hostfqdn()->load_hostname()->load_127_subnet();
71 }
72  
73 33 ▶sub load_etc_hosts {
Subroutine load_etc_hosts: called CC=6 SCAR=18.0
Pod documented
74 33 my ( $self, $etc_hosts_file ) = @_;
75 33 ▶ $etc_hosts_file = $self->{etc_hosts_file} unless ($etc_hosts_file);
Branch: 1 branch
BranchTrueFalseTotal
unless $etc_hosts_file 33 0 33
76  
77 33 ▶ if ( !$self->{loaded}{'/etc/hosts'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"loaded"}{"/etc/hosts"}) 33 0 33
78  
79 # attempt to load aliases from hosts file
80 33 $logger->debug( 'loading /etc/hosts from: %s', $etc_hosts_file );
81 33 eval {
82 33 open( my $handle, '<', $etc_hosts_file );
83 33 ▶ while ( my $line = <$handle> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $handle)) 228 33 261
84 228 ▶ if ( $line =~ /^$loopback_ipv4_regex\s+(\S.*?)\s*$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^$loopback_ipv4_regex\s+(\S.*?)\s*$/) 34 194 228
85 34 $self->_add_alias($_) foreach map { lc($_) } split( /\s+/, $1 );
86 }
87 }
88 33 close($handle);
89 };
90 33 $self->{loaded}{'/etc/hosts'} = 1;
91 }
92  
93 33 return $self;
94 }
95  
96 33 ▶sub load_hostfqdn {
Subroutine load_hostfqdn: called CC=2 SCAR=7.0
Pod documented
97 33 my ($self) = @_;
98  
99 33 ▶ if ( !$self->{loaded}{hostfqdn} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"loaded"}{"hostfqdn"}) 33 0 33
100  
101 # if fqdn is present, add it too...
102 33 $logger->debug('loading hostfqdn');
103 33 require Net::Domain;
104 33 $self->_add_alias( Net::Domain::hostfqdn() );
105 33 $self->{loaded}{hostfqdn} = 1;
106 }
107  
108 33 return $self;
109 }
110  
111 33 ▶sub load_hostname {
Subroutine load_hostname: called CC=2 SCAR=7.0
Pod documented
112 33 my ($self) = @_;
113  
114 33 ▶ if ( !$self->{loaded}{hostname} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"loaded"}{"hostname"}) 33 0 33
115  
116 # load alias from hostname
117 33 $logger->debug('loading hostname');
118 33 eval {
119 # hostname() croaks on fail, so just ignore
120 33 require Sys::Hostname;
121 33 $self->_add_alias( Sys::Hostname::hostname() );
122 };
123 33 $self->{loaded}{hostname} = 1;
124 }
125  
126 33 return $self;
127 }
128  
129 12 1;
130  
131 __END__
« Footprintless/CommandOptionsFactory.pm ↑ Summary Footprintless/App/Action.pm »