File Coverage

lib/LEOCHARRE/Checksetup.pm
Criterion Covered Total %
statement 43 81 53.0
branch 11 44 25.0
condition 1 10 10.0
subroutine 14 18 77.7
pod 7 10 70.0
total 76 163 46.6


line stmt bran cond sub pod time code
1             package LEOCHARRE::Checksetup;
2 2     2   90314 use strict;
  2         6  
  2         61  
3 2     2   11 use Exporter;
  2         4  
  2         90  
4 2     2   11 use Carp;
  2         8  
  2         184  
5 2     2   11 use Test::Simple '';
  2         4  
  2         11  
6 2     2   641 use vars qw(@ISA %EXPORT_TAGS @EXPORT_OK $VERSION);
  2         4  
  2         3501  
7             @ISA = qw/Exporter/;
8             @EXPORT_OK = qw(bad good ok_app ok_conf ok_mysql_local_server ok_os ok_perldeps ok_root say yn);
9             %EXPORT_TAGS = ( all => \@EXPORT_OK );
10             $VERSION = sprintf "%d.%02d", q$Revision: 1.3 $ =~ /(\d+)/g;
11              
12              
13              
14             sub bad;
15             sub good;
16             sub say;
17              
18              
19              
20              
21              
22              
23             sub yn {
24 0   0 0 1 0 my $question = shift; $question ||='Your answer? ';
  0         0  
25 0         0 my $val = undef;
26 0         0 until (defined $val){
27 0         0 print "$question (y/n): ";
28 0         0 $val = ;
29 0         0 chomp $val;
30 0 0       0 if ($val eq 'y'){ $val = 1; }
  0 0       0  
31 0         0 elsif ($val eq 'n'){ $val = 0;}
32 0         0 else { $val = undef; }
33             }
34 0         0 return $val;
35             }
36              
37              
38              
39              
40              
41             sub ok_app {
42 1     1 1 506 my $what = shift;
43 1 50       13 ok( _require_app($what), "Found '$what' installed.") or bad;
44             }
45              
46             sub _require_app {
47 1     1   8 my $app = shift;
48              
49 1         2448 require File::Which;
50 1 50       2557 File::Which::which($app) and return 1;
51            
52 0         0 print "Sorry, you're missing the application '$app'.\n";
53              
54 0 0       0 if ( File::Which::which('yum') ){
55            
56 0 0 0     0 yn("It seems that you have 'yum'. Would you like me try to install '$app' via yum?")
57             or say "In that case you should find '$app' and install it.\n"
58             and bad;
59              
60 0         0 ok_root();
61              
62 0         0 system('yum','-y','install',$app);
63              
64 0 0 0     0 File::Which::which($app) or say "Still can't find path to '$app'. Error.\n"
65             and bad;
66              
67 0         0 return 1;
68             }
69 0         0 bad;
70             }
71              
72             #use Smart::Comments '###';
73             sub _makefile_deps {
74 1     1   24 local $/;
75 1 50       99 open(FILE,'<','./Makefile.PL') or die($!);
76 1         29 my $txt = ;
77 1         14 close FILE;
78            
79 1 50       30 $txt=~/PREREQ_PM\s*=>\s*({[^\}]+})/s or return;
80              
81             ### $txt
82 1         286 my $href = eval "$1";
83              
84             ### $href
85 1         7 my @m = keys %$href;
86 1         10 return @m;
87              
88             }
89              
90              
91              
92 2     2 0 12605 sub say { print STDERR "@_" }
93 0     0 1 0 sub bad { say("\nCHECK FAILED.\n"); exit 1 }
  0         0  
94 0     0 1 0 sub good { say("\nCHECK PASSED.\n"); exit }
  0         0  
95              
96             # MODULE CHECKS
97             sub ok_perldeps {
98 5 50   5   11 sub _havemod { my $name = shift; ( eval "require $name;" ) ? 1 : 0 }
  5         307  
99              
100              
101 1     1 0 1606 my @mods = _makefile_deps();
102 1         9 my @missing;
103 1         4 MODULE: for my $mod ( @mods ){
104              
105 5 50       3746 if (_havemod($mod)){
106 5 50       114610 ok(1,"Have module '$mod'") and next MODULE;
107             }
108              
109              
110              
111             else {
112 0         0 push @missing, $mod;
113             }
114              
115             }
116              
117 1 0 50     4211 if (@missing and scalar @missing){
118 0         0 say("Missing modules:\n");
119 0         0 say("\t$_\n") for @missing;
120 0 0       0 yn("Want me to call cpan to install these?") or bad;
121              
122 0         0 system("cpan @missing");
123              
124 0         0 for my $mod (@missing){
125 0 0       0 ok( _havemod($mod),"Found module: $mod.") or bad;
126             }
127              
128             }
129            
130             } # END MODULE CHECKS
131              
132              
133             sub ok_os {
134              
135 1 50   1 1 1371 ok( $^O=~/linux/, 'using Linux') or bad;
136              
137             }
138              
139             sub ok_root {
140 2     2 1 32247 my $who = `whoami`;
141 2 50       157 ok( $who=~/\broot\b/,'installing as root') or bad();
142             }
143              
144              
145             sub ok_conf {
146 1     1 0 11204 my $abs = shift;
147 1 50       17 defined $abs or croak('missing abs conf arg');
148 1 50       36 ok( -e $abs,"have conf file '$abs'") or bad;
149             }
150              
151             sub ok_mysql_local_server {
152              
153 0     0 1   require File::Which;
154 0 0         ok( File::Which::which('mysql'),'found mysql cli') or bad;
155              
156              
157 0           my $mysqld = '/etc/init.d/mysqld';
158 0 0         if ( -e $mysqld ){
159 0           my $status = `$mysqld status`;
160 0 0         ok( $status=~/is running/,"$mysqld status, is running") or bad;
161             }
162             else {
163 0           ok( 1, "no '$mysqld'");
164             }
165              
166              
167             }
168              
169             1;
170              
171             __END__