From: Robert 'phaylon' Sedlacek Date: Tue, 19 Jun 2012 20:31:51 +0000 (+0000) Subject: simplified hosts to be standard file data structure, also preserves comments X-Git-Tag: v0.001_001~36 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=5a9311b3b3213061347169c8fc9022a0c02c0fc3;hp=c7c9858838ae3bc4b457fff62f38cf8e35f81951;p=scpubgit%2FSystem-Introspector.git simplified hosts to be standard file data structure, also preserves comments --- diff --git a/lib/System/Introspector/Probe/Hosts.pm b/lib/System/Introspector/Probe/Hosts.pm index d1a0d8b..bc2ab76 100644 --- a/lib/System/Introspector/Probe/Hosts.pm +++ b/lib/System/Introspector/Probe/Hosts.pm @@ -2,7 +2,7 @@ package System::Introspector::Probe::Hosts; use Moo; use System::Introspector::Util qw( - handle_from_file + output_from_file transform_exceptions ); @@ -13,24 +13,17 @@ has hosts_file => ( sub gather { my ($self) = @_; - return transform_exceptions { - my $fh = $self->_open_hosts_file; - my @hosts; - while (defined( my $line = <$fh> )) { - chomp $line; - next if $line =~ m{^\s*$} - or $line =~ m{^\s*#}; - push @hosts, [split m{\s+}, $line]; - } - return { hosts => \@hosts }; + my $file = $self->hosts_file; + return { + hosts_file => transform_exceptions { + return { + file_name => $file, + body => scalar output_from_file $file, + }; + }, }; } -sub _open_hosts_file { - my ($self) = @_; - return handle_from_file $self->hosts_file; -} - 1; __END__ diff --git a/t/data/hosts b/t/data/hosts index 1711790..db6ba17 100644 --- a/t/data/hosts +++ b/t/data/hosts @@ -1,4 +1,4 @@ 1.2.3.4 foo bar -# another one +# some comment 2.3.4.5 bar diff --git a/t/hosts.t b/t/hosts.t index 65822bb..a7c7a43 100644 --- a/t/hosts.t +++ b/t/hosts.t @@ -10,11 +10,16 @@ my $probe = System::Introspector::Probe::Hosts->new( my $result = $probe->gather; ok $result, 'received data'; -my $data = $result->{hosts}; +my $data = $result->{hosts_file}; ok $data, 'received hosts data'; -is_deeply $data, - [['1.2.3.4', qw( foo bar )], ['2.3.4.5', 'bar']], - 'parsing worked'; +is $result->{__error__}, undef, 'no errors'; +ok $data->{file_name}, 'received file name'; + +my $body = $data->{body}; +ok $body, 'received a body'; +like $body, qr{1.2.3.4\s+foo\s+bar}, 'first host'; +like $body, qr{2.3.4.5\s+bar}, 'second host'; +like $body, qr{some comment}, 'comment preserved'; done_testing;