blib/lib/App/Ack/ConfigFinder.pm

filter: Statementstmt 96.5% 56 / 58 Branchbran 87.5% 21 / 24 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 10 / 10 Podpod 100% 2 / 2 totaltotal 94.9% 94 / 99 CC 15 SCAR 15.1
CC
15
Cov
94%
find_config_files
7.1
_remove_redundancies
6.0
_check_for_ackrc
4.0
CRAP 4.5
« blib/lib/App/Ack/ConfigLoader.pm ↑ Summary blib/lib/App/Ack/Filter/Collection.pm »
1 package App::Ack::ConfigFinder;
2  
3 =head1 NAME
4  
5 App::Ack::ConfigFinder
6  
7 =head1 DESCRIPTION
8  
9 A module that contains the logic for locating the various configuration
10 files.
11  
12 =head1 LOCATING CONFIG FILES
13  
14 First, ack looks for a global ackrc.
15  
16 =over
17  
18 =item On Windows, this is `ackrc` in either COMMON_APPDATA or APPDATA.
19 If `ackrc` is present in both directories, ack uses both files in that
20 order.
21  
22 =item On a non-Windows OS, this is `/etc/ackrc`.
23  
24 =back
25  
26 Then, ack looks for a user-specific ackrc if the HOME environment
27 variable is set. This is either F<$HOME/.ackrc> or F<$HOME/_ackrc>.
28  
29 Then, ack looks for a project-specific ackrc file. ack searches
30 up the directory hierarchy for the first `.ackrc` or `_ackrc` file.
31 If this is one of the ackrc files found in the previous steps, it is
32 not loaded again.
33  
34 It is a fatal error if a directory contains both F<.ackrc> and F<_ackrc>.
35  
36 After ack loads the options from the found ackrc files, ack looks
37 at the C<ACKRC_OPTIONS> environment variable.
38  
39 Finally, ack takes settings from the command line.
40  
41 =cut
42  
43 583 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 583 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 583 ▶use App::Ack ();
Subroutine BEGIN: called CC=1 SCAR=0.0
47 583 ▶use Cwd 3.00 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
48 583 ▶use File::Spec 3.00 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 583 ▶use if ($^O eq 'MSWin32'), 'Win32';
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 =head1 METHODS
53  
54 =head2 new
55  
56 Creates a new config finder.
57  
58 =cut
59  
60 17 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
61 17 my ( $class ) = @_;
62  
63 17 return bless {}, $class;
64 }
65  
66  
67 38 ▶sub _remove_redundancies {
Subroutine _remove_redundancies: called CC=6 SCAR=17.9
68 38 my @configs = @_;
69  
70 38 my %seen;
71 my @uniq;
72 38 foreach my $config (@configs) {
73 79 my $path = $config->{path};
74 79 ▶ my $key = -e $path ? Cwd::realpath( $path ) : $path;
Branch: 1 branch
BranchTrueFalseTotal
-e $path ? : 41 38 79
75 79 ▶ if ( not $App::Ack::is_windows ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($App::Ack::is_windows) 79 0 79
76 # On Unix, uniquify on inode.
77 79 my ($dev, $inode) = (stat $key)[0, 1];
78 79 ▶ $key = "$dev:$inode" if defined $dev;
Branch: 1 branch
BranchTrueFalseTotal
if defined $dev 41 38 79
79 }
80 79 ▶ push( @uniq, $config ) unless $seen{$key}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $seen{$key}++ 78 1 79
81 }
82 38 return @uniq;
83 }
84  
85  
86 125 ▶sub _check_for_ackrc {
Subroutine _check_for_ackrc: called CC=4 SCAR=13.9
87 125 ▶ return unless defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 23 102 125
88  
89 204 my @files = grep { -f }
90 102 map { File::Spec->catfile(@_, $_) }
91 qw(.ackrc _ackrc);
92  
93 102 ▶ App::Ack::die( File::Spec->catdir(@_) . ' contains both .ackrc and _ackrc. Please remove one of those files.' )
Branch: 1 branch
BranchTrueFalseTotal
if @files > 1 2 100 102
94 if @files > 1;
95  
96 100 ▶ return wantarray ? @files : $files[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 15 85 100
97 } # end _check_for_ackrc
98  
99  
100 =head2 $finder->find_config_files
101  
102 Locates config files, and returns a list of them.
103  
104 =cut
105  
106 40 ▶sub find_config_files {
Subroutine find_config_files: called CC=7 SCAR=19.6
Pod documented
107 40 my @config_files;
108  
109 40 ▶ if ( $App::Ack::is_windows ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($App::Ack::is_windows) { } 0 40 40
110 0 push @config_files, map { +{ path => File::Spec->catfile($_, 'ackrc') } } (
111 Win32::GetFolderPath(Win32::CSIDL_COMMON_APPDATA()),
112 Win32::GetFolderPath(Win32::CSIDL_APPDATA()),
113 );
114 }
115 else {
116 40 push @config_files, { path => '/etc/ackrc' };
117 }
118  
119  
120 40 ▶ if ( $ENV{'ACKRC'} && -f $ENV{'ACKRC'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{'ACKRC'} and -f $ENV{'ACKRC'}) { } 2 38 40
Condition100%3/3
$ENV{"ACKRC"} and -f $ENV{"ACKRC"}
!ll&&!rl&&r
3712
MC/DC100%2/2
$ENV{"ACKRC"} and -f $ENV{"ACKRC"}
$ENV{"ACKRC"} -f $ENV{"ACKRC"}
Truth table100%3/3
A and B
A= $ENV{"ACKRC"}B= -f $ENV{"ACKRC"}
ABresult
0X0
100
111
121 2 push @config_files, { path => $ENV{'ACKRC'} };
122 }
123 else {
124 38 push @config_files, map { +{ path => $_ } } _check_for_ackrc($ENV{'HOME'});
125 }
126  
127 40 my $cwd = Cwd::getcwd();
128 40 ▶ return () unless defined $cwd;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cwd 0 40 40
129  
130 # XXX This should go through some untainted cwd-fetching function, and not get untainted brute-force like this.
131 40 $cwd =~ /(.+)/;
132 40 $cwd = $1;
133 40 my @dirs = File::Spec->splitdir( $cwd );
134 40 ▶ while ( @dirs ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@dirs) 87 13 100
135 87 my $ackrc = _check_for_ackrc(@dirs);
136 85 ▶ if ( defined $ackrc ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ackrc) 25 60 85
137 25 push @config_files, { project => 1, path => $ackrc };
138 25 last;
139 }
140 60 pop @dirs;
141 }
142  
143 # We only test for existence here, so if the file is deleted out from under us, this will fail later.
144 38 return _remove_redundancies( @config_files );
145 }
146  
147 583 1;
« blib/lib/App/Ack/ConfigLoader.pm ↑ Summary blib/lib/App/Ack/Filter/Collection.pm »