| 1 | package App::Project::Doctor::Context; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | # Context is the filesystem helper that every check plugin receives. | ||||||||||||||||||||
| 4 | # It encapsulates the distribution root path and provides safe, validated | ||||||||||||||||||||
| 5 | # access to files underneath it. Checks must NEVER access the filesystem | ||||||||||||||||||||
| 6 | # directly -- they must always go through Context methods. | ||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 5 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 5 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 5 | ▶ | use autodie qw(:all); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | # croak dies with the caller's file/line so errors point at the plugin, not here. | ||||||||||||||||||||
| 13 | 5 | ▶ | use Carp qw(croak carp); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | # Readonly makes constants truly immutable; mutation throws at runtime. | ||||||||||||||||||||
| 15 | 5 | ▶ | use Readonly; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | # File::Spec builds cross-platform paths (handles Windows backslashes). | ||||||||||||||||||||
| 17 | 5 | ▶ | use File::Spec; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 18 | # File::Find is loaded without importing its 'find' function to avoid namespace pollution. | ||||||||||||||||||||
| 19 | 5 | ▶ | use File::Find (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 20 | # validate_strict enforces parameter schemas and throws immediately on failure. | ||||||||||||||||||||
| 21 | 5 | ▶ | use Params::Validate::Strict qw(validate_strict); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 22 | # Params::Get normalises @_ so both hash and hashref calling styles work. | ||||||||||||||||||||
| 23 | 5 | ▶ | use Params::Get; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | 5 | our $VERSION = '0.02'; | |||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | # --------------------------------------------------------------------------- | ||||||||||||||||||||
| 28 | # Constants | ||||||||||||||||||||
| 29 | # --------------------------------------------------------------------------- | ||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | # File extensions that identify Perl source files. | ||||||||||||||||||||
| 32 | # Used by perl_files() to filter results from _collect_files(). | ||||||||||||||||||||
| 33 | 5 | Readonly::Array my @PERL_EXTENSIONS => qw(.pm .pl .t .PL); | |||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | # Standard build-system files; the first one found is returned by builder_file(). | ||||||||||||||||||||
| 36 | 5 | Readonly::Array my @BUILDER_FILES => qw(Makefile.PL Build.PL dist.ini cpanfile); | |||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | # --------------------------------------------------------------------------- | ||||||||||||||||||||
| 39 | # Constructor | ||||||||||||||||||||
| 40 | # --------------------------------------------------------------------------- | ||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | 223 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 43 | 223 | my $class = shift; | |||||||||||||||||||
| 44 | # validate_strict normalises args, applies defaults, and throws on bad input. | ||||||||||||||||||||
| 45 | 223 | ▶ | my $args = validate_strict( | ||||||||||||||||||
|
Condition100%2/2
Params::Get::get_params(undef, \@_) || {}
MC/DC100%1/1
Params::Get::get_params(undef, \@_) || {}
Params::Get::get_params(undef, \@_)
Truth table100%2/2
A
A= Params::Get::get_params(undef, \@_)
| |||||||||||||||||||||
| 46 | args => Params::Get::get_params(undef, \@_) || {}, | ||||||||||||||||||||
| 47 | schema => { | ||||||||||||||||||||
| 48 | # root must be an existing directory; defaults to the current directory. | ||||||||||||||||||||
| 49 | root => { type => 'scalar', optional => 1, default => '.' }, | ||||||||||||||||||||
| 50 | # verbose is passed through to check plugins that want progress output. | ||||||||||||||||||||
| 51 | verbose => { type => 'scalar', optional => 1, default => 0 }, | ||||||||||||||||||||
| 52 | }, | ||||||||||||||||||||
| 53 | ); | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | # Confirm root is an actual directory before we store it. | ||||||||||||||||||||
| 56 | croak "root '$args->{root}' is not a directory" | ||||||||||||||||||||
| 57 | 223 | ▶ | unless -d $args->{root}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | # Convert root to an absolute path so all downstream operations are stable. | ||||||||||||||||||||
| 60 | return bless { | ||||||||||||||||||||
| 61 | root => File::Spec->rel2abs($args->{root}), | ||||||||||||||||||||
| 62 | verbose => $args->{verbose}, | ||||||||||||||||||||
| 63 | 217 | }, $class; | |||||||||||||||||||
| 64 | } | ||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | # --------------------------------------------------------------------------- | ||||||||||||||||||||
| 67 | # Accessors (read-only after construction) | ||||||||||||||||||||
| 68 | # --------------------------------------------------------------------------- | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | # The absolute path to the distribution root directory. | ||||||||||||||||||||
| 71 | 919 | ▶ | sub root { $_[0]->{root} } | ||||||||||||||||||
|
Subroutine
root: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 72 | # Whether verbose progress messages should be emitted. | ||||||||||||||||||||
| 73 | 4 | ▶ | sub verbose { $_[0]->{verbose} } | ||||||||||||||||||
|
Subroutine
verbose: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | # --------------------------------------------------------------------------- | ||||||||||||||||||||
| 76 | # Public methods | ||||||||||||||||||||
| 77 | # --------------------------------------------------------------------------- | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | =head2 has_file( $rel_path ) | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | Returns true when C<$rel_path> (relative to root) exists on disk. | ||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | =cut | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | 455 | ▶ | sub has_file { | ||||||||||||||||||
|
Subroutine
has_file: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 86 | 455 | my ($self, $rel_path) = @_; | |||||||||||||||||||
| 87 | 455 | ▶ | croak 'has_file requires a relative path' unless defined $rel_path; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 88 | # Route through abs_path so the path-traversal security check is inherited. | ||||||||||||||||||||
| 89 | 452 | return -e $self->abs_path($rel_path); | |||||||||||||||||||
| 90 | } | ||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | =head2 abs_path( $rel_path ) | ||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | Returns the absolute filesystem path for C<$rel_path>. | ||||||||||||||||||||
| 95 | Croaks if C<$rel_path> contains C<..> as a path component (path traversal). | ||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | =cut | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | 794 | ▶ | sub abs_path { | ||||||||||||||||||
|
Subroutine
abs_path: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 100 | 794 | my ($self, $rel_path) = @_; | |||||||||||||||||||
| 101 | 794 | ▶ | croak 'abs_path requires a relative path' unless defined $rel_path; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 102 | # Security: reject any path component that is exactly '..'. | ||||||||||||||||||||
| 103 | # This prevents a crafted check name or filename from escaping the root. | ||||||||||||||||||||
| 104 | croak "Path traversal detected in '$rel_path'" | ||||||||||||||||||||
| 105 | 791 | ▶ | if grep { $_ eq '..' } File::Spec->splitdir($rel_path); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 106 | 787 | return File::Spec->catfile($self->root, $rel_path); | |||||||||||||||||||
| 107 | } | ||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | =head2 slurp( $rel_path ) | ||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | Reads and returns the entire UTF-8 content of C<$rel_path>. | ||||||||||||||||||||
| 112 | Croaks if the file does not exist. | ||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | =cut | ||||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | 106 | ▶ | sub slurp { | ||||||||||||||||||
|
Subroutine
slurp: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 117 | 106 | my ($self, $rel_path) = @_; | |||||||||||||||||||
| 118 | # autodie wraps open() in an eval internally; 'local $@' protects the caller's $@. | ||||||||||||||||||||
| 119 | 106 | local $@; | |||||||||||||||||||
| 120 | 106 | ▶ | croak 'slurp requires a relative path' unless defined $rel_path; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 121 | 103 | my $abs = $self->abs_path($rel_path); | |||||||||||||||||||
| 122 | # Provide a clear error if the caller asks for a file that isn't there. | ||||||||||||||||||||
| 123 | 102 | ▶ | croak "File not found: $abs" unless -f $abs; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 124 | 99 | open my $fh, '<:encoding(UTF-8)', $abs; | |||||||||||||||||||
| 125 | # Undefine $/ to enable slurp mode (read the entire file in one operation). | ||||||||||||||||||||
| 126 | 99 | local $/; | |||||||||||||||||||
| 127 | 99 | my $content = <$fh>; | |||||||||||||||||||
| 128 | 99 | close $fh; | |||||||||||||||||||
| 129 | 99 | return $content; | |||||||||||||||||||
| 130 | } | ||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | =head2 perl_files( @dirs ) | ||||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | Returns an arrayref of paths (relative to root) for all Perl source files | ||||||||||||||||||||
| 135 | (.pm .pl .t .PL) found recursively under the given directories. | ||||||||||||||||||||
| 136 | Defaults to lib/, script/, bin/, t/. | ||||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | =cut | ||||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | 40 | ▶ | sub perl_files { | ||||||||||||||||||
|
Subroutine
perl_files: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 141 | 40 | my ($self, @dirs) = @_; | |||||||||||||||||||
| 142 | # Default to the standard Perl source directories when none are specified. | ||||||||||||||||||||
| 143 | 40 | ▶ | @dirs = qw(lib script bin t) unless @dirs; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 144 | 50 | ▶ | return $self->_collect_files(\@dirs, sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||
| 145 | 50 | my $file = shift; | |||||||||||||||||||
| 146 | # Extract the file extension and check it against our known Perl extensions. | ||||||||||||||||||||
| 147 | 50 | my ($ext) = $file =~ /(\.[^.]+)$/; | |||||||||||||||||||
| 148 | 50 | ▶ | return defined $ext && grep { $ext eq $_ } @PERL_EXTENSIONS; | ||||||||||||||||||
|
Condition66%2/3
defined $ext && grep({$ext eq $_;} @PERL_EXTENSIONS)
MC/DC50%1/2
defined $ext && grep({$ext eq $_;} @PERL_EXTENSIONS)
defined $ext
grep({$ext eq $_;} @PERL_EXTENSIONS)
Truth table66%2/3
A && B
A= defined $extB= grep({$ext eq $_;} @PERL_EXTENSIONS)
| |||||||||||||||||||||
| 149 | 40 | }); | |||||||||||||||||||
| 150 | } | ||||||||||||||||||||
| 151 | |||||||||||||||||||||
| 152 | =head2 lib_modules | ||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | Returns an arrayref of .pm paths (relative to root) found under lib/. | ||||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | =cut | ||||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | 53 | ▶ | sub lib_modules { | ||||||||||||||||||
|
Subroutine
lib_modules: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 159 | 53 | my $self = shift; | |||||||||||||||||||
| 160 | # Delegate to find_files with a suffix filter for .pm files. | ||||||||||||||||||||
| 161 | 53 | return $self->find_files('lib', '.pm'); | |||||||||||||||||||
| 162 | } | ||||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | =head2 test_files | ||||||||||||||||||||
| 165 | |||||||||||||||||||||
| 166 | Returns an arrayref of .t paths (relative to root) found under t/. | ||||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | =cut | ||||||||||||||||||||
| 169 | |||||||||||||||||||||
| 170 | 5 | ▶ | sub test_files { | ||||||||||||||||||
|
Subroutine
test_files: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 171 | 5 | my $self = shift; | |||||||||||||||||||
| 172 | # Delegate to find_files with a suffix filter for .t files. | ||||||||||||||||||||
| 173 | 5 | return $self->find_files('t', '.t'); | |||||||||||||||||||
| 174 | } | ||||||||||||||||||||
| 175 | |||||||||||||||||||||
| 176 | =head2 git_root | ||||||||||||||||||||
| 177 | |||||||||||||||||||||
| 178 | Returns the git repository root, or undef if not in a git repo. | ||||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | =cut | ||||||||||||||||||||
| 181 | |||||||||||||||||||||
| 182 | 1 | ▶ | sub git_root { | ||||||||||||||||||
|
Subroutine
git_root: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||
| 183 | 1 | my $self = shift; | |||||||||||||||||||
| 184 | 1 | my $root = $self->root; | |||||||||||||||||||
| 185 | # Ask git for the repository root; 2>/dev/null suppresses the error when | ||||||||||||||||||||
| 186 | # the directory is not inside any git repository. | ||||||||||||||||||||
| 187 | 1 | my $out = qx{git -C \Q$root\E rev-parse --show-toplevel 2>/dev/null}; | |||||||||||||||||||
| 188 | 1 | chomp $out; | |||||||||||||||||||
| 189 | # Return undef rather than an empty string when outside a git repo. | ||||||||||||||||||||
| 190 | 1 | ▶ | return (length $out) ? $out : undef; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 191 | } | ||||||||||||||||||||
| 192 | |||||||||||||||||||||
| 193 | =head2 builder_file | ||||||||||||||||||||
| 194 | |||||||||||||||||||||
| 195 | Returns the name (relative to root) of the first found builder file, or undef. | ||||||||||||||||||||
| 196 | |||||||||||||||||||||
| 197 | =cut | ||||||||||||||||||||
| 198 | |||||||||||||||||||||
| 199 | 15 | ▶ | sub builder_file { | ||||||||||||||||||
|
Subroutine
builder_file: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 200 | 15 | my $self = shift; | |||||||||||||||||||
| 201 | # Return the first builder file that actually exists in the distro root. | ||||||||||||||||||||
| 202 | 15 | for my $f (@BUILDER_FILES) { | |||||||||||||||||||
| 203 | 36 | ▶ | return $f if $self->has_file($f); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 204 | } | ||||||||||||||||||||
| 205 | # None of the known builder files were found. | ||||||||||||||||||||
| 206 | 3 | return undef; | |||||||||||||||||||
| 207 | } | ||||||||||||||||||||
| 208 | |||||||||||||||||||||
| 209 | =head2 find_files( $dir, $pattern ) | ||||||||||||||||||||
| 210 | |||||||||||||||||||||
| 211 | Returns an arrayref of all files under C<$dir> matching C<$pattern> | ||||||||||||||||||||
| 212 | (a string suffix or a compiled regexp). | ||||||||||||||||||||
| 213 | |||||||||||||||||||||
| 214 | =cut | ||||||||||||||||||||
| 215 | |||||||||||||||||||||
| 216 | 74 | ▶ | sub find_files { | ||||||||||||||||||
|
Subroutine
find_files: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 217 | 74 | my ($self, $dir, $pattern) = @_; | |||||||||||||||||||
| 218 | 74 | ▶ | croak 'find_files requires a directory' unless defined $dir; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 219 | 74 | ▶ | return $self->_collect_files([$dir], sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||
| 220 | 74 | my $rel = shift; | |||||||||||||||||||
| 221 | # No pattern means "match everything". | ||||||||||||||||||||
| 222 | 74 | ▶ | return 1 unless defined $pattern; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 223 | # Accept either a compiled regexp or a plain string suffix. | ||||||||||||||||||||
| 224 | 70 | ▶ | return ref $pattern eq 'Regexp' ? $rel =~ $pattern : $rel =~ /\Q$pattern\E$/; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 225 | 71 | }); | |||||||||||||||||||
| 226 | } | ||||||||||||||||||||
| 227 | |||||||||||||||||||||
| 228 | # --------------------------------------------------------------------------- | ||||||||||||||||||||
| 229 | # Private helpers | ||||||||||||||||||||
| 230 | # --------------------------------------------------------------------------- | ||||||||||||||||||||
| 231 | |||||||||||||||||||||
| 232 | # Purpose: Recursively walk a list of directories, filtering files with $accept. | ||||||||||||||||||||
| 233 | # Entry: $dirs is an arrayref of directory paths (relative to root). | ||||||||||||||||||||
| 234 | # $accept is a coderef ($rel_path) -> bool that filters results. | ||||||||||||||||||||
| 235 | # Exit: Arrayref of relative paths that passed the $accept filter. | ||||||||||||||||||||
| 236 | # Side effects: Reads the filesystem (no writes). | ||||||||||||||||||||
| 237 | 113 | ▶ | sub _collect_files { | ||||||||||||||||||
|
Subroutine
_collect_files: called CC=3 SCAR=11.0
| |||||||||||||||||||||
| 238 | 113 | my ($self, $dirs, $accept) = @_; | |||||||||||||||||||
| 239 | 113 | my @found; | |||||||||||||||||||
| 240 | 113 | for my $dir (@{$dirs}) { | |||||||||||||||||||
| 241 | 195 | my $abs_dir = $self->abs_path($dir); | |||||||||||||||||||
| 242 | # Skip directories that don't exist rather than croaking. | ||||||||||||||||||||
| 243 | 195 | ▶ | next unless -d $abs_dir; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 244 | File::Find::find({ | ||||||||||||||||||||
| 245 | no_chdir => 1, # Stay in one place; $_ is always absolute. | ||||||||||||||||||||
| 246 | 238 | ▶ | wanted => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||
| 247 | 238 | ▶ | return unless -f $_; # Skip directories and symlinks. | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 248 | 124 | my $rel = File::Spec->abs2rel($_, $self->root); | |||||||||||||||||||
| 249 | # Normalize to forward slashes on Windows where abs2rel uses backslashes. | ||||||||||||||||||||
| 250 | 124 | $rel =~ s{\\}{/}g; | |||||||||||||||||||
| 251 | 124 | ▶ | push @found, $rel if $accept->($rel); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 252 | }, | ||||||||||||||||||||
| 253 | 101 | }, $abs_dir); | |||||||||||||||||||
| 254 | } | ||||||||||||||||||||
| 255 | 113 | return \@found; | |||||||||||||||||||
| 256 | } | ||||||||||||||||||||
| 257 | |||||||||||||||||||||
| 258 | 5 | 1; | |||||||||||||||||||
| 259 | |||||||||||||||||||||
| 260 | __END__ | ||||||||||||||||||||