File Coverage

lib/App/Perlbrew/Path.pm
Criterion Covered Total %
statement 64 64 100.0
branch 8 12 66.6
condition 3 6 50.0
subroutine 23 23 100.0
pod 0 13 0.0
total 98 118 83.0


line stmt bran cond sub pod time code
1 73     73   885001 use strict;
  73         166  
  73         3944  
2 73     73   397 use warnings;
  73         146  
  73         4601  
3              
4             package App::Perlbrew::Path;
5              
6 73     73   472 use File::Basename ();
  73         152  
  73         1405  
7 73     73   350 use File::Glob ();
  73         131  
  73         1331  
8 73     73   327 use File::Path ();
  73         145  
  73         2822  
9              
10             use overload (
11 73         777 '""' => \& stringify,
12             fallback => 1,
13 73     73   423 );
  73         196  
14              
15             sub _joinpath {
16 4580     4580   8370 for my $entry (@_) {
17 73     73   8348 no warnings 'uninitialized';
  73         170  
  73         61690  
18 8676 100       18688 die 'Received an undefined entry as a parameter (all parameters are: '. join(', ', @_). ')' unless (defined($entry));
19             }
20 4579         32411 return join "/", @_;
21             }
22              
23             sub _child {
24 3214     3214   6923 my ($self, $package, @path) = @_;
25              
26 3214         13270 $package->new($self->{path}, @path);
27             }
28              
29             sub _children {
30 659     659   1222 my ($self, $package) = @_;
31              
32 659         1467 map { $package->new($_) } File::Glob::bsd_glob($self->child("*"));
  524         1397  
33             }
34              
35             sub new {
36 4580     4580 0 10975658 my ($class, @path) = @_;
37              
38 4580         8790 bless { path => _joinpath (@path) }, $class;
39             }
40              
41             sub exists {
42 63     63 0 166 my ($self) = @_;
43              
44 63         242 -e $self->stringify;
45             }
46              
47             sub basename {
48 549     549 0 27492 my ($self, $suffix) = @_;
49              
50 549         4059 return scalar File::Basename::fileparse($self, ($suffix) x!! defined $suffix);
51             }
52              
53             sub child {
54 2437     2437 0 9619 my ($self, @path) = @_;
55              
56 2437         4808 return $self->_child(__PACKAGE__, @path);
57             }
58              
59             sub children {
60 480     480 0 795 my ($self) = @_;
61              
62 480         987 return $self->_children(__PACKAGE__);
63             }
64              
65             sub dirname {
66 8     8 0 39 my ($self) = @_;
67              
68 8         70 return App::Perlbrew::Path->new( File::Basename::dirname($self) );
69             }
70              
71             sub mkpath {
72 234     234 0 18200 my ($self) = @_;
73 234         851 File::Path::mkpath( [$self->stringify], 0, 0777 );
74 234         1335 return $self;
75             }
76              
77             sub readlink {
78 63     63 0 1273 my ($self) = @_;
79              
80 63         288 my $link = CORE::readlink( $self->stringify );
81 63 100       427 $link = __PACKAGE__->new($link) if defined $link;
82              
83 63         371 return $link;
84             }
85              
86             sub rmpath {
87 11     11 0 8373 my ($self) = @_;
88 11         43 File::Path::rmtree( [$self->stringify], 0, 0 );
89 11         77 return $self;
90             }
91              
92             sub stringify {
93 8236     8236 0 96433 my ($self) = @_;
94              
95 8236         40809674 return $self->{path};
96             }
97              
98             sub stringify_with_tilde {
99 34     34 0 8187 my ($self) = @_;
100 34         134 my $path = $self->stringify;
101 34         156 my $home = $ENV{HOME};
102 34 50       579 $path =~ s!\Q$home/\E!~/! if $home;
103 34         256 return $path;
104             }
105              
106             sub symlink {
107 3     3 0 8 my ($self, $destination, $force) = @_;
108 3 50       5 $destination = App::Perlbrew::Path->new($destination) unless ref $destination;
109              
110 3 50 33     8 CORE::unlink($destination) if $force && (-e $destination || -l $destination);
      66        
111              
112 3 50       8 $destination if CORE::symlink($self, $destination);
113             }
114              
115             sub unlink {
116 8     8 0 36 my ($self) = @_;
117 8         70 CORE::unlink($self);
118             }
119              
120             1;
121