blib/lib/Text/Nunjucks/Loader/FileSystem.pm

filter: Statementstmt 65.6% 65 / 99 Branchbran 50.0% 18 / 36 Conditioncond 40.0% 4 / 10 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 81.2% 13 / 16 Podpod 33.3% 2 / 6 totaltotal 60.1% 104 / 173 CC 23 SCAR 22.5
CC
23
Cov
60%
BUILD
16.8
BUILD
16.8
get_source
8.1
CRAP 9.5
« blib/lib/Text/Nunjucks/Compiler/Compiler.pm ↑ Summary blib/lib/Text/Nunjucks/Nodes/CallExtension.pm »
1 package Text::Nunjucks::Loader::FileSystem;
2 # ABSTRACT: 1:1 port of Nunjucks src/node-loaders.js FileSystemLoader -- templates from search paths on disk
3  
4 15 our $VERSION = '0.001';
5  
6 15 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 15 ▶use builtin qw( is_bool true false );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 15 ▶use Text::Nunjucks::Lib;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 15 ▶use Text::Nunjucks::Lib::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 15 ▶use Text::Nunjucks::Runtime;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 15 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 15 ▶no warnings 'experimental::builtin';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 15 extends 'Text::Nunjucks::Loader';
16  
17 # src/node-loaders.js defines FileSystemLoader and NodeResolveLoader and
18 # re-exports PrecompiledLoader. Only FileSystemLoader is ported here;
19 # PrecompiledLoader is Text::Nunjucks::Loader::Precompiled, NodeResolveLoader
20 # (require.resolve over node_modules) is out of scope (ADR 0018).
21 #
22 # The node built-ins it calls -- path.normalize, path.resolve, fs.existsSync,
23 # fs.readFileSync(p, 'utf-8'), console.log -- are Lib's node_* functions, node
24 # v24.19.0's behaviour, posix (ADR 0018). The opts it reads are any JS value
25 # (upstream still warns about a boolean), so they are read as JS reads them,
26 # through Runtime's member_lookup and truthiness.
27 #
28 # constructor(searchPaths, opts) sets this.pathsToNames, this.noCache and
29 # this.searchPaths: the attributes below, snake_cased for Perl. They are rw
30 # because the constructor body assigns them, as upstream does, once Moo built
31 # the object: BUILDARGS keeps the positional arguments, BUILD is the body
32 # (super() is Moo's).
33  
34 15 has search_paths => ( is => 'rw' );
35 15 has no_cache => ( is => 'rw' );
36 15 has paths_to_names => ( is => 'rw' );
37 15 has _init_args => ( is => 'ro' );
38  
39 # Collaborators named once, reached through the invocant (house rule).
40 1047 ▶sub _lib { 'Text::Nunjucks::Lib' }
Subroutine _lib: called CC=1 SCAR=0.0
41 111 ▶sub _runtime { 'Text::Nunjucks::Runtime' }
Subroutine _runtime: called CC=1 SCAR=0.0
42 2 ▶sub _error_class { 'Text::Nunjucks::Lib::Error' }
Subroutine _error_class: called CC=1 SCAR=0.0
43  
44 # new FileSystemLoader(searchPaths, opts) -- positional.
45 111 ▶sub BUILDARGS {
Subroutine BUILDARGS: called CC=1 SCAR=1.2
Pod undocumented
46 0 ▶ my ( $class, @args ) = @_;
Subroutine BUILDARGS: not called CC=1 SCAR=1.2
Pod undocumented
47 0 return { _init_args => [@args] };
48 }
49  
50 # constructor(searchPaths, opts)
51 111 ▶sub BUILD {
Subroutine BUILD: called CC=8 SCAR=28.2
Pod undocumented
52 0 ▶ my ($self) = @_;
Subroutine BUILD: not called CC=8 SCAR=28.2
Pod undocumented
53 0 ▶ my ( $search_paths, $opts ) = @{ $self->_init_args // [] };
Condition25%1/4
$self->_init_args // []
l!l
1110
$self->_init_args // []
l!l
00
MC/DC0%0/1
$self->_init_args // []
$self->_init_args
MC/DC0%0/1
$self->_init_args // []
$self->_init_args
Truth table50%1/2
A
A= $self->_init_args
Aresult
11
00
Truth table0%0/2
A
A= $self->_init_args
Aresult
11
00
54 0 delete $self->{_init_args};
55 0 my $rt = $self->_runtime;
56 0 ▶ if ( is_bool($opts) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::is_bool($opts)) 3 108 111
if (builtin::is_bool($opts)) 0 0 0
57 0 $self->_lib->node_console_log(
58 '[nunjucks] Warning: you passed a boolean as the second '
59 . 'argument to FileSystemLoader, but it now takes an options '
60 . 'object. See http://mozilla.github.io/nunjucks/api.html#filesystemloader'
61 );
62 }
63  
64 0 ▶ $opts = $rt->_truthy($opts) ? $opts : {};
Branch: 2 branches
BranchTrueFalseTotal
$rt->_truthy($opts) ? : 21 90 111
$rt->_truthy($opts) ? : 0 0 0
65 0 $self->paths_to_names( {} );
66 0 ▶ $self->no_cache( $rt->_truthy( $rt->member_lookup( $opts, 'noCache' ) ) ? true : false );
Branch: 2 branches
BranchTrueFalseTotal
$rt->_truthy($rt->member_lookup($opts, 'noCache')) ? : 5 106 111
$rt->_truthy($rt->member_lookup($opts, 'noCache')) ? : 0 0 0
67  
68 0 ▶ if ( $rt->_truthy($search_paths) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rt->_truthy($search_paths)) { } 107 4 111
if ($rt->_truthy($search_paths)) { } 0 0 0
69 0 ▶ $search_paths = $self->_lib->is_array($search_paths) ? $search_paths : [$search_paths];
Branch: 2 branches
BranchTrueFalseTotal
$self->_lib->is_array($search_paths) ? : 22 85 107
$self->_lib->is_array($search_paths) ? : 0 0 0
70 # For windows, convert to forward slashes
71 0 $self->search_paths( [ map { $self->_lib->node_path_normalize($_) } @$search_paths ] );
72 }
73 else {
74 0 $self->search_paths( ['.'] );
75 }
76  
77 0 ▶ if ( $rt->_truthy( $rt->member_lookup( $opts, 'watch' ) ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rt->_truthy($rt->member_lookup($opts, "watch"))) 2 99 101
if ($rt->_truthy($rt->member_lookup($opts, "watch"))) 0 0 0
78 # Watch all the templates in the paths and fire an event when
79 # they change -- through chokidar, which has no Perl image: the port is
80 # upstream without it installed, so its require() fails and upstream's
81 # catch throws (ADR 0018).
82 0 die $self->_error_class->new('watch requires chokidar to be installed');
83 }
84 }
85  
86 163 ▶sub get_source {
Subroutine get_source: called CC=5 SCAR=20.9
Pod documented
87 0 ▶ my ( $self, $name ) = @_;
Subroutine get_source: not called CC=5 SCAR=20.9
Pod documented
88 0 my $fullpath = undef;
89 0 my $paths = $self->search_paths;
90  
91 0 ▶ for ( my $i = 0; $i < @$paths; $i++ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($i < @$paths) 166 36 202
if ($i < @$paths) 0 0 0
92 0 my $base_path = $self->_lib->node_path_resolve( $paths->[$i] );
93 0 my $p = $self->_lib->node_path_resolve( $paths->[$i], $name );
94  
95 # Only allow the current directory and anything
96 # underneath it to be searched
97 #
98 # A string-prefix test, as upstream's p.indexOf(basePath) === 0: a sibling
99 # whose name extends the search path's passes it ('views' admits
100 # '../views2/x.njk'). Ported as is (ADR 0004).
101 0 ▶ if ( index( $p, $base_path ) == 0 && $self->_lib->node_fs_exists_sync($p) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (index($p, $base_path) == 0 and $self->_lib->node_fs_exists_sync($p)) 127 39 166
if (index($p, $base_path) == 0 and $self->_lib->node_fs_exists_sync($p)) 0 0 0
Condition50%3/6
index($p, $base_path) == 0 and $self->_lib->node_fs_exists_sync($p)
!ll&&!rl&&r
336127
index($p, $base_path) == 0 and $self->_lib->node_fs_exists_sync($p)
!ll&&!rl&&r
000
MC/DC100%2/2
index($p, $base_path) == 0 and $self->_lib->node_fs_exists_sync($p)
index($p, $base_path) == 0 $self->_lib->node_fs_exists_sync($p)
MC/DC0%0/2
index($p, $base_path) == 0 and $self->_lib->node_fs_exists_sync($p)
index($p, $base_path) == 0 $self->_lib->node_fs_exists_sync($p)
Truth table100%3/3
A and B
A= index($p, $base_path) == 0B= $self->_lib->node_fs_exists_sync($p)
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= index($p, $base_path) == 0B= $self->_lib->node_fs_exists_sync($p)
ABresult
0X0
100
111
102 0 $fullpath = $p;
103 0 last;
104 }
105 }
106  
107 0 ▶ if ( !$fullpath ) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($fullpath) 36 127 163
unless ($fullpath) 0 0 0
108 0 return undef;
109 }
110  
111 0 $self->paths_to_names->{$fullpath} = $name;
112  
113 0 my $source = {
114 src => $self->_lib->node_fs_read_file_sync_utf8($fullpath),
115 path => $fullpath,
116 noCache => $self->no_cache,
117 };
118 0 $self->emit( 'load', $name, $source );
119 0 return $source;
120 }
121  
122 15 1;
123  
124 __END__
« blib/lib/Text/Nunjucks/Compiler/Compiler.pm ↑ Summary blib/lib/Text/Nunjucks/Nodes/CallExtension.pm »