use reusable I/O utils, more solid error handling for DiskUsage probe
Robert 'phaylon' Sedlacek [Thu, 10 May 2012 18:48:36 +0000 (18:48 +0000)]
lib/System/Introspector/DiskUsage.pm
t/diskusage.t

index ef7e6d9..467f976 100644 (file)
@@ -1,25 +1,31 @@
 package System::Introspector::DiskUsage;
 use Moo;
 
+use System::Introspector::Util qw(
+    lines_from_command
+    transform_exceptions
+);
+
 sub gather {
     my ($self) = @_;
-    my @lines = `df -aP`;
-    shift @lines; # header
-    chomp @lines;
-    my @rows;
-    for my $line (@lines) {
-        my %row;
-        @row{qw(
-            filesystem
-            blocks_1024
-            used
-            available
-            capacity
-            mount_point
-        )} = split m{\s+}, $line;
-        push @rows, \%row;
-    }
-    return \@rows;
+    return transform_exceptions {
+        my @lines = lines_from_command ['df', '-aP'];
+        shift @lines; # header
+        my @rows;
+        for my $line (@lines) {
+            my %row;
+            @row{qw(
+                filesystem
+                blocks_1024
+                used
+                available
+                capacity
+                mount_point
+            )} = split m{\s+}, $line;
+            push @rows, \%row;
+        }
+        return { disk_usage => \@rows };
+    };
 }
 
 1;
index 6499aa3..aa8b27f 100644 (file)
@@ -4,7 +4,11 @@ use Test::More;
 use System::Introspector::DiskUsage;
 
 my $probe = System::Introspector::DiskUsage->new;
-my $data  = $probe->gather;
+
+my $result = $probe->gather;
+ok $result, 'received data';
+my $data = $result->{disk_usage};
+ok $data, 'received disk usage data';
 
 ok scalar(@$data), 'received data';
 my @fields = qw( filesystem blocks_1024 used available capacity mount_point );