blib/lib/App/Stow/Check.pm

filter: Statementstmt 63.1% 36 / 57 Branchbran 8.3% 1 / 12 Conditioncond 22.2% 2 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 90.0% 9 / 10 Podpod 100% 2 / 2 totaltotal 52.6% 50 / 95 CC 10 SCAR 27.7
CC
10
Cov
50%
_check
42.0
run
8.5
BEGIN
1.0
CRAP 15.9
↑ Summary
1 package App::Stow::Check;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 ▶use Cwd qw(realpath);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use File::Spec::Functions qw(abs2rel splitdir);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use File::Which qw(which);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Getopt::Std;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 4 Readonly::Scalar our $DEFAULT_STOW_DIR => '/usr/local/stow';
13  
14 4 our $VERSION = 0.03;
15  
16 # Constructor.
17 2 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
18 2 my ($class, @params) = @_;
19  
20 # Create object.
21 2 my $self = bless {}, $class;
22  
23 # Object.
24 2 return $self;
25 }
26  
27 # Run.
28 1 ▶sub run {
Subroutine run: called CC=5 SCAR=21.4
Pod documented
29 1 my $self = shift;
30  
31 # Process arguments.
32 1 $self->{'_opts'} = {
33 'd' => $DEFAULT_STOW_DIR,
34 'h' => 0,
35 };
36 1 ▶ if (! getopts('d:h', $self->{'_opts'}) || $self->{'_opts'}->{'h'} || @ARGV < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (not &getopts("d:h", $self->{"_opts"}) or $self->{"_opts"}{"h"} or @ARGV < 1) 1 0 1
Condition33%2/6
not &getopts("d:h", $self->{"_opts"}) or $self->{"_opts"}{"h"} or @ARGV < 1
l!l&&r!l&&!r
100
not &getopts("d:h", $self->{"_opts"}) or $self->{"_opts"}{"h"}
l!l&&r!l&&!r
010
MC/DC0%0/3
not &getopts("d:h", $self->{"_opts"}) or $self->{"_opts"}{"h"} or @ARGV < 1
not &getopts("d:h", $self->{"_opts"}) $self->{"_opts"}{"h"} @ARGV < 1
Truth table25%1/4
not(A) or B or C
A= not &getopts("d:h", $self->{"_opts"})B= $self->{"_opts"}{"h"}C= @ARGV < 1
ABCresult
1XX1
01X1
0011
0000
37 1 print STDERR "Usage: $0 [-d stow_dir] [-h] [--version] command\n";
38 1 print STDERR "\t-d stow_dir\tStow directory (default value is '$DEFAULT_STOW_DIR').\n";
39 1 print STDERR "\t-h\t\tPrint help.\n";
40 1 print STDERR "\t--version\tPrint version.\n";
41 1 print STDERR "\tcommand\t\tCommand for which is stow dist looking.\n";
42 1 return 1;
43 }
44 0 $self->{'_command'} = $ARGV[0];
45  
46 0 my ($exit_code, $message) = $self->_check;
47 0 ▶ if ($exit_code == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exit_code == 0) { } 0 0 0
48 0 print $message."\n";
49 } else {
50 0 print STDERR $message."\n";
51 }
52  
53 0 return $exit_code;
54 }
55  
56 0 ▶sub _check {
Subroutine _check: not called CC=6 SCAR=37.4
57 0 my $self = shift;
58  
59 0 my $command = $self->{'_command'};
60  
61 0 my $command_path = which($command);
62 0 ▶ if (! defined $command_path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $command_path) 0 0 0
63 0 return (1, "Command '$command' not found.");
64 }
65  
66 0 my $stow_dir = $self->{'_opts'}->{'d'};
67 0 ▶ if (! -d $stow_dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $stow_dir) 0 0 0
68 0 return (1, "Stow directory '$stow_dir' doesn't exist.");
69 }
70 0 my $rel_path = abs2rel(realpath($command_path), $stow_dir);
71 0 my @rel_path = splitdir($rel_path);
72 0 ▶ if ($rel_path[0] eq '..') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rel_path[0] eq "..") 0 0 0
73 0 return (1, "Command '$command' doesn't use stow.");
74 }
75 0 ▶ if ($rel_path[1] ne 'bin' && $rel_path[1] ne 'sbin') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rel_path[1] ne "bin" and $rel_path[1] ne "sbin") 0 0 0
Condition0%0/3
$rel_path[1] ne "bin" and $rel_path[1] ne "sbin"
!ll&&!rl&&r
000
MC/DC0%0/2
$rel_path[1] ne "bin" and $rel_path[1] ne "sbin"
$rel_path[1] ne "bin" $rel_path[1] ne "sbin"
Truth table0%0/3
A and B
A= $rel_path[1] ne "bin"B= $rel_path[1] ne "sbin"
ABresult
0X0
100
111
76 0 return (1, "Command '$command' don't use 'bin/sbin' path.");
77 }
78  
79 0 return (0, $rel_path[0]);
80 }
81  
82 4 1;
83  
84  
85 __END__
↑ Summary