lib/Dancer2/Core/Role/HasLocation.pm

filter: Statementstmt 100% 49 / 49 Branchbran 88.8% 16 / 18 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 3 / 3 Subroutinesub 100% 9 / 9 Podpod n/a totaltotal 97.6% 83 / 85 CC 16 SCAR 16.8
CC
16
Cov
97%
_is_app_root
8.0
_build_location
7.0
_is_project_boundary
3.0
CRAP 5.3
« blib/lib/Dancer2/Core/Role/Serializer.pm ↑ Summary lib/Dancer2/Core/Request.pm »
1 package Dancer2::Core::Role::HasLocation;
2 # ABSTRACT: Role for application location "guessing"
3 185 $Dancer2::Core::Role::HasLocation::VERSION = '2.3.0';
4 185 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 185 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 185 ▶use Sub::Quote 'quote_sub';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 185 ▶use Path::Tiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 185 ▶use Dancer2::Core::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # the path to the caller script/app
11 # Note: to remove any ambiguity between the accessor for the
12 # 'caller' attribute and the core function caller(), explicitly
13 # specify we want the function 'CORE::caller' as the default for
14 # the attribute.
15 185 has caller => (
16 is => 'ro',
17 isa => Str,
18 default => quote_sub( q{
19 require Path::Tiny;
20 my ( $caller, $script ) = CORE::caller;
21 Path::Tiny::path($script)->relative->stringify;
22 } ),
23 );
24  
25 185 has location => (
26 is => 'ro',
27 isa => Str,
28 lazy => 1,
29 builder => '_build_location',
30 );
31  
32 185 has _location_path => (
33 is => 'ro',
34 lazy => 1,
35 builder => '_build_location_path',
36 init_arg => undef,
37 );
38  
39 295 ▶sub _build_location_path {
Subroutine _build_location_path: called CC=1 SCAR=0.0
40 295 my $self = shift;
41 295 return Path::Tiny::path( $self->location );
42 }
43  
44 # Config file basenames a Dancer2 app root may hold. Keep this in sync with
45 # Config::Any->extensions, which is what Dancer2::ConfigReader actually reads.
46 185 my @_config_files = map "config.$_",
47 qw< cnf conf ini json jsn pl perl xml yml yaml >;
48  
49 # Directories a Dancer2 app root may hold. These are precisely the things
50 # 'location' is used to resolve, so a directory holding one of them is a
51 # directory worth calling the app root.
52 185 my @_app_dirs = qw< environments views public >;
53  
54 # Files and directories marking the root of a checkout or a distribution.
55 185 my @_project_markers =
56 qw< .git .hg .svn Makefile.PL Build.PL dist.ini cpanfile >;
57  
58 # Is this directory a ceiling the walk must not cross? Whatever the app root
59 # is, it sits at or below its own project root, never in the home directory
60 # or the build directory above it. Only consulted for directories _is_app_root
61 # has already rejected, so a project root that is also an app root - which is
62 # the usual case, 'dancer2 gen' writes a cpanfile and a Makefile.PL - has been
63 # accepted long before this is reached.
64 250 ▶sub _is_project_boundary {
Subroutine _is_project_boundary: called CC=3 SCAR=11.0
65 250 my $dir = shift;
66 250 ▶ $dir->child($_)->exists and return 1 for @_project_markers;
Branch: 1 branch
BranchTrueFalseTotal
$dir->child($_)->exists and return 1 7 1722 1729
67 243 return 0;
68 }
69  
70 # Does this directory look like the root of a Dancer2 application?
71 #
72 # Historically the only test was "holds both lib/ and bin/". That is a poor
73 # proxy: it matches every Perl distribution root, most home directories, /usr
74 # and /usr/local. It went unnoticed for years because the upward walk in
75 # _build_location was effectively stalling, so it rarely got the chance to
76 # leave the script's own directory. Once the walk started working the weak
77 # test began selecting directories far above the real app. See GH #1781.
78 549 ▶sub _is_app_root {
Subroutine _is_app_root: called CC=8 SCAR=20.8
79 549 my $dir = shift;
80  
81 # explicit marker, placed by 'dancer2 gen' and by hand; always definitive
82 549 ▶ $dir->child('.dancer')->is_file
Branch: 1 branch
BranchTrueFalseTotal
$dir->child(".dancer")->is_file and return 1 6 543 549
83 and return 1;
84  
85 # actual Dancer2 artifacts
86 543 ▶ $dir->child($_)->is_dir and return 1 for @_app_dirs;
Branch: 1 branch
BranchTrueFalseTotal
$dir->child($_)->is_dir and return 1 257 1115 1372
87 286 ▶ $dir->child($_)->is_file and return 1 for @_config_files;
Branch: 1 branch
BranchTrueFalseTotal
$dir->child($_)->is_file and return 1 34 2792 2826
88  
89 # Legacy heuristic, kept for apps that carry no Dancer2 artifacts at all.
90 # Checked last so anything above only wins when it is found closer to the
91 # script. blib/ is skipped: it holds lib/ and bin/, but never views/ or
92 # public/, so it is never the app root.
93 252 ▶ return $dir !~ m![\\/]blib[\\/]?$!
Condition100%6/6
!($dir =~ m[[\\/]blib[\\/]?$]) && $dir->child('lib')->is_dir && $dir->child('bin')->is_dir
!ll&&!rl&&r
24462
not $dir =~ m[[\\/]blib[\\/]?$] and $dir->child("lib")->is_dir
!ll&&!rl&&r
12438
MC/DC100%3/3
!($dir =~ m[[\\/]blib[\\/]?$]) && $dir->child('lib')->is_dir && $dir->child('bin')->is_dir
not $dir =~ m[[\\/]blib[\\/]?$] $dir->child("lib")->is_dir $dir->child('bin')->is_dir
Truth table100%4/4
not(A) and B && C
A= not $dir =~ m[[\\/]blib[\\/]?$]B= $dir->child("lib")->is_dirC= $dir->child('bin')->is_dir
ABCresult
0XX0
10X0
1100
1111
94 && $dir->child('lib')->is_dir
95 && $dir->child('bin')->is_dir;
96 }
97  
98 # FIXME: i hate you most of all -- Sawyer X
99 306 ▶sub _build_location {
Subroutine _build_location: called CC=7 SCAR=19.5
100 306 my $self = shift;
101 306 my $script = $self->caller;
102  
103 # default to the dir that contains the script...
104 306 my $location = Path::Tiny::path($script)->parent;
105  
106 306 ▶ $location->is_dir
Branch: 1 branch
BranchTrueFalseTotal
$location->is_dir or Carp::croak("Caller $script is not an existing file") 306 0 306
107 or Carp::croak("Caller $script is not an existing file");
108  
109 # Walk up looking for the app root, closest match winning. Canonicalise
110 # first: the walk compares path strings, and a relative $subdir turns into
111 # a chain of '..' segments that no longer matches anything useful.
112 306 my $subdir = $location->realpath;
113 306 my $subdir_found = 0;
114  
115 #maximum of 10 iterations, to prevent infinite loop
116 306 for ( 1 .. 10 ) {
117 549 ▶ if ( _is_app_root($subdir) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_app_root($subdir)) 299 250 549
118 299 $subdir_found = 1;
119 299 last;
120 }
121  
122 # a checkout or distribution root that is not itself an app root:
123 # stop rather than escape into whatever happens to lie above it
124 250 ▶ last if _is_project_boundary($subdir);
Branch: 1 branch
BranchTrueFalseTotal
if _is_project_boundary($subdir) 7 243 250
125  
126 # stop at the volume root, where parent() is its own fixed point
127 243 my $parent = $subdir->parent;
128 243 ▶ last if $parent->stringify eq $subdir->stringify;
Branch: 1 branch
BranchTrueFalseTotal
if $parent->stringify eq $subdir->stringify 0 243 243
129  
130 243 $subdir = $parent;
131 }
132  
133 306 ▶ my $path = $subdir_found ? $subdir : $location;
Branch: 1 branch
BranchTrueFalseTotal
$subdir_found ? : 299 7 306
134  
135 # convert relative to absolute
136 306 return $path->realpath->stringify;
137 }
138  
139 185 1;
140  
141 __END__
« blib/lib/Dancer2/Core/Role/Serializer.pm ↑ Summary lib/Dancer2/Core/Request.pm »