lib/App/Dapper/Check.pm

filter: Statementstmt 15.1% 24 / 158 Branchbran 0.0% 0 / 78 Conditioncond 0.0% 0 / 49 MC/DCmcdc 0.0% 0 / 29 Subroutinesub 50.0% 8 / 16 Podpod 100% 7 / 7 totaltotal 11.5% 39 / 337 CC 64 SCAR 50.2
CC
64
Cov
8%
target_path
380.0
links
156.0
warnings
132.0
CRAP 151.4
« lib/App/Dapper/Deploy.pm ↑ Summary lib/App/Dapper/Collections.pm »
1 package App::Dapper::Check;
2  
3 =head1 NAME
4  
5 App::Dapper::Check - Find the problems in a site without publishing it.
6  
7 =head1 DESCRIPTION
8  
9 C<dapper check> builds the site into a temporary directory, collecting every
10 problem instead of stopping at the first, then looks for links between pages
11 that lead nowhere. The real output directory is not touched.
12  
13 $ dapper check
14 $ dapper check --json
15  
16 It reports:
17  
18 =over 4
19  
20 =item * front matter that can't be read, or that doesn't match a C<_schema.yml>
21  
22 =item * layouts that don't exist, and errors in templates
23  
24 =item * two pages that would be written to the same file
25  
26 =item * links and images (C<href> and C<src>) inside the site that lead to no file
27  
28 =item * settings that need C<url> when it isn't set, and pages without a title
29  
30 =back
31  
32 With C<--json>, the report is printed as JSON, for scripts and AI assistants:
33  
34 {
35 "ok": false,
36 "pages": 12,
37 "errors": [ { "file": "...", "type": "...", "message": "..." } ],
38 "warnings": [ ... ]
39 }
40  
41 The command exits with status 1 if there are errors, and 0 otherwise.
42  
43 =cut
44  
45 32 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 32 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 32 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 32 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 32 ▶use File::Temp qw/ tempdir /;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 32 ▶use File::Spec::Functions qw/ catfile /;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 32 ▶use JSON ();
Subroutine BEGIN: called CC=1 SCAR=0.0
53  
54 32 ▶use App::Dapper::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
55  
56 =head2 run
57  
58 Takes a Dapper object and returns the report as a hash reference.
59  
60 =cut
61  
62 0 ▶sub run {
Subroutine run: not called CC=8 SCAR=42.8
Pod documented
63 0 my ($dapper) = @_;
64  
65 0 my $tmp = tempdir(CLEANUP => 1);
66  
67 # Build into the temporary directory, collecting problems
68 0 my %saved = map { $_ => $dapper->{$_} } qw/ collect quiet output /;
69 0 my $explicit_output = $dapper->{explicit}->{output};
70  
71 0 $dapper->{collect} = 1;
72 0 $dapper->{quiet} = 1;
73  
74 0 my ($pages, @problems) = (0);
75 0 my $ok = eval {
76 # Resolve the real directories first, then build into the temporary one
77 0 $dapper->read_project();
78 0 my $real_output = $dapper->{output};
79 0 $dapper->{output} = catfile($tmp, 'site');
80 0 $dapper->{explicit}->{output} = 1;
81 0 $dapper->build();
82 0 $dapper->{site}->{output} = $real_output;
83 0 1;
84 };
85  
86 0 ▶ @problems = @{ $dapper->{problems} || [] };
Condition0%0/2
$dapper->{'problems'} || []
l!l
00
MC/DC0%0/1
$dapper->{'problems'} || []
$dapper->{'problems'}
Truth table0%0/2
A
A= $dapper->{'problems'}
Aresult
11
00
87 0 ▶ if (not $ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 0 0
88 0 (my $message = $@) =~ s/^(?:error): //;
89 0 $message =~ s/\s+\z//;
90 0 push @problems, { severity => 'error', file => $dapper->{config}, message => $message };
91 }
92  
93 0 ▶ if ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) 0 0 0
94 0 $pages = scalar @{ $dapper->{site}->{pages} };
95 0 push @problems, links($dapper);
96 0 push @problems, warnings($dapper);
97 }
98  
99 0 $dapper->{$_} = $saved{$_} for keys %saved;
100 0 $dapper->{explicit}->{output} = $explicit_output;
101  
102 0 for my $problem (@problems) {
103 0 ▶ $problem->{type} = classify($problem) unless defined $problem->{type};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $problem->{"type"} 0 0 0
104 }
105  
106 0 my @errors = grep { $_->{severity} eq 'error' } @problems;
107 0 my @warnings = grep { $_->{severity} ne 'error' } @problems;
108  
109 return {
110 ok => @errors ? JSON::false : JSON::true,
111 pages => $pages,
112 0 errors => [ map { _clean($_) } @errors ],
113 0 ▶ warnings => [ map { _clean($_) } @warnings ],
Branch: 1 branch
BranchTrueFalseTotal
@errors ? : 0 0 0
114 };
115 }
116  
117 0 ▶sub _clean {
Subroutine _clean: not called CC=3 SCAR=24.8
118 0 my ($problem) = @_;
119  
120 # Messages often start with the file they are about; it is in "file" already
121 0 my $message = $problem->{message};
122 0 ▶ my $file = defined $problem->{file} ? $problem->{file} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $problem->{'file'} ? : 0 0 0
123 0 ▶ $message =~ s/^\Q$file\E:?\s+// if length $file;
Branch: 1 branch
BranchTrueFalseTotal
if length $file 0 0 0
124  
125 0 return { file => $problem->{file}, type => $problem->{type}, message => $message };
126 }
127  
128 =head2 classify
129  
130 Takes a problem and returns a short name for its kind: C<schema>,
131 C<front_matter>, C<layout>, C<template>, C<duplicate>, C<link>, C<setting> or
132 C<other>.
133  
134 =cut
135  
136 0 ▶sub classify {
Subroutine classify: not called CC=7 SCAR=40.3
Pod documented
137 0 my ($problem) = @_;
138  
139 0 my $message = $problem->{message};
140 0 ▶ return 'schema' if $message =~ /schema/;
Branch: 1 branch
BranchTrueFalseTotal
if $message =~ /schema/ 0 0 0
141 0 ▶ return 'front_matter' if $message =~ /front matter|could not understand the date|unknown time zone/;
Branch: 1 branch
BranchTrueFalseTotal
if $message =~ /front matter|could not understand the date|unknown time zone/ 0 0 0
142 0 ▶ return 'layout' if $message =~ /uses the layout|loop of layouts|names the layout/;
Branch: 1 branch
BranchTrueFalseTotal
if $message =~ /uses the layout|loop of layouts|names the layout/ 0 0 0
143 0 ▶ return 'template' if $message =~ /could not process/;
Branch: 1 branch
BranchTrueFalseTotal
if $message =~ /could not process/ 0 0 0
144 0 ▶ return 'duplicate' if $message =~ /both write|already writes/;
Branch: 1 branch
BranchTrueFalseTotal
if $message =~ /both write|already writes/ 0 0 0
145 0 ▶ return 'setting' if $message =~ /needs the site|needs a layout|needs Pagefind|must be settings/;
Branch: 1 branch
BranchTrueFalseTotal
if $message =~ /needs the site|needs a layout|needs Pagefind|must be settings/ 0 0 0
146 0 return 'other';
147 }
148  
149 =head2 links
150  
151 Takes a Dapper object that has just been built, and returns a problem for each
152 link or image in an HTML page that points to a file the site does not have.
153  
154 =cut
155  
156 0 ▶sub links {
Subroutine links: not called CC=12 SCAR=50.5
Pod documented
157 0 my ($dapper) = @_;
158  
159 0 my $site = $dapper->{site};
160 0 my $output = $dapper->{output};
161 0 ▶ my %source_of = %{ $dapper->{page_owner} || {} };
Condition0%0/2
$dapper->{'page_owner'} || {}
l!l
00
MC/DC0%0/1
$dapper->{'page_owner'} || {}
$dapper->{'page_owner'}
Truth table0%0/2
A
A= $dapper->{'page_owner'}
Aresult
11
00
162 0 ▶ my $base = defined $site->{url} ? $site->{url} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $site->{'url'} ? : 0 0 0
163 0 $base =~ s{/+$}{};
164  
165 0 my @problems;
166  
167 0 for my $relative (sort keys %{$dapper->{written}}) {
168 0 ▶ next unless $relative =~ /\.html?$/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $relative =~ /\.html?$/i 0 0 0
169 0 my $file = catfile($output, $relative);
170 0 my $html = App::Dapper::Utils::read_file($file);
171  
172 # Links inside comments (including old IE conditional comments) are never
173 # followed by a browser
174 0 $html =~ s/<!--.*?-->//gs;
175 0 my $page_dir = '/' . $relative;
176 0 $page_dir =~ s{[^/]*$}{};
177  
178 0 my %seen;
179 0 ▶ while ($html =~ /\b(?:href|src)\s*=\s*(["'])(.*?)\1/gis) {
Branch: 1 branch
BranchTrueFalseTotal
if ($html =~ /\b(?:href|src)\s*=\s*(["'])(.*?)\1/sig) 0 0 0
180 0 my $link = $2;
181 0 ▶ next if $seen{$link}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$link}++ 0 0 0
182  
183 0 my $path = target_path($link, $page_dir, $base);
184 0 ▶ next unless defined $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path 0 0 0
185 0 ▶ next if $site->{search} and $path =~ m{^/pagefind/};
Branch: 1 branch
BranchTrueFalseTotal
if $site->{"search"} and $path =~ m[^/pagefind/] 0 0 0
Condition0%0/3
$site->{"search"} and $path =~ m[^/pagefind/]
!ll&&!rl&&r
000
MC/DC0%0/2
$site->{"search"} and $path =~ m[^/pagefind/]
$site->{"search"} $path =~ m[^/pagefind/]
Truth table0%0/3
A and B
A= $site->{"search"}B= $path =~ m[^/pagefind/]
ABresult
0X0
100
111
186 0 ▶ next if exists_in($output, $path);
Branch: 1 branch
BranchTrueFalseTotal
if exists_in($output, $path) 0 0 0
187  
188 push @problems, {
189 severity => 'error',
190 type => 'link',
191 0 ▶ file => $source_of{$file} || $file,
Condition0%0/3
$source_of{$file} || $file
l!l&&r!l&&!r
000
MC/DC0%0/2
$source_of{$file} || $file
$source_of{$file} $file
Truth table0%0/3
A || B
A= $source_of{$file}B= $file
ABresult
1X1
011
000
192 message => "links to $link, which is not in the site",
193 };
194 }
195 }
196  
197 0 return @problems;
198 }
199  
200 =head2 target_path
201  
202 Takes a link, the directory of the page it is on (e.g. C</blog/post/>) and the
203 site's URL, and returns the path in the site it points to, or nothing for links
204 that lead outside the site (or to the same page).
205  
206 =cut
207  
208 0 ▶sub target_path {
Subroutine target_path: not called CC=19 SCAR=59.4
Pod documented
209 0 my ($link, $page_dir, $base) = @_;
210  
211 0 $link =~ s/^\s+|\s+$//g;
212 0 ▶ return if $link eq '' or $link =~ /^#/;
Branch: 1 branch
BranchTrueFalseTotal
if $link eq "" or $link =~ /^#/ 0 0 0
Condition0%0/3
$link eq "" or $link =~ /^#/
l!l&&r!l&&!r
000
MC/DC0%0/2
$link eq "" or $link =~ /^#/
$link eq "" $link =~ /^#/
Truth table0%0/3
A or B
A= $link eq ""B= $link =~ /^#/
ABresult
1X1
011
000
213  
214 0 ▶ if (length $base and index($link, $base) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $base and index($link, $base) == 0) 0 0 0
Condition0%0/3
length $base and index($link, $base) == 0
!ll&&!rl&&r
000
MC/DC0%0/2
length $base and index($link, $base) == 0
length $base index($link, $base) == 0
Truth table0%0/3
A and B
A= length $baseB= index($link, $base) == 0
ABresult
0X0
100
111
215 0 $link = substr($link, length $base);
216 0 ▶ $link = '/' unless length $link;
Branch: 1 branch
BranchTrueFalseTotal
unless length $link 0 0 0
217 }
218  
219 0 ▶ return if $link =~ m{^[a-z][a-z0-9+.-]*:}i or $link =~ m{^//};
Branch: 1 branch
BranchTrueFalseTotal
if $link =~ /^[a-z][a-z0-9+.-]*:/i or $link =~ m[^//] 0 0 0
Condition0%0/3
$link =~ /^[a-z][a-z0-9+.-]*:/i or $link =~ m[^//]
l!l&&r!l&&!r
000
MC/DC0%0/2
$link =~ /^[a-z][a-z0-9+.-]*:/i or $link =~ m[^//]
$link =~ /^[a-z][a-z0-9+.-]*:/i $link =~ m[^//]
Truth table0%0/3
A or B
A= $link =~ /^[a-z][a-z0-9+.-]*:/iB= $link =~ m[^//]
ABresult
1X1
011
000
220  
221 0 $link =~ s/[?#].*$//;
222 0 ▶ return if $link eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $link eq "" 0 0 0
223  
224 0 $link =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
225 0 $link =~ s/&amp;/&/g;
226  
227 0 ▶ my $path = $link =~ m{^/} ? $link : "$page_dir$link";
Branch: 1 branch
BranchTrueFalseTotal
$link =~ m[^/] ? : 0 0 0
228  
229 0 my @parts;
230 0 for my $part (split m{/}, $path, -1) {
231 0 ▶ next if $part eq '.' or ($part eq '' and @parts);
Branch: 1 branch
BranchTrueFalseTotal
if $part eq "." or $part eq "" and @parts 0 0 0
Condition0%0/6
$part eq "." or $part eq "" and @parts
l!l&&r!l&&!r
000
$part eq "" and @parts
!ll&&!rl&&r
000
MC/DC0%0/3
$part eq "." or $part eq "" and @parts
$part eq "." $part eq "" @parts
Truth table0%0/4
A or B and C
A= $part eq "."B= $part eq ""C= @parts
ABCresult
1XX1
0111
00X0
0100
232 0 ▶ if ($part eq '..') { pop @parts if @parts > 1; next }
Branch: 2 branches
BranchTrueFalseTotal
if ($part eq "..") 0 0 0
if @parts > 1 0 0 0
233 0 push @parts, $part;
234 }
235 0 my $resolved = join('/', @parts);
236 0 ▶ $resolved .= '/' if $path =~ m{/$} and $resolved !~ m{/$};
Branch: 1 branch
BranchTrueFalseTotal
if $path =~ m[/$] and not $resolved =~ m[/$] 0 0 0
Condition0%0/3
$path =~ m[/$] and not $resolved =~ m[/$]
!ll&&!rl&&r
000
MC/DC0%0/2
$path =~ m[/$] and not $resolved =~ m[/$]
$path =~ m[/$] not $resolved =~ m[/$]
Truth table0%0/3
A and not(B)
A= $path =~ m[/$]B= not $resolved =~ m[/$]
ABresult
0X0
100
111
237 0 ▶ $resolved = "/$resolved" unless $resolved =~ m{^/};
Branch: 1 branch
BranchTrueFalseTotal
unless $resolved =~ m[^/] 0 0 0
238  
239 0 return $resolved;
240 }
241  
242 =head2 exists_in
243  
244 Takes the output directory and a path in the site, and returns true if a file
245 is there (or, for a directory, an C<index.html> in it).
246  
247 =cut
248  
249 0 ▶sub exists_in {
Subroutine exists_in: not called CC=5 SCAR=34.0
Pod documented
250 0 my ($output, $path) = @_;
251  
252 0 my $file = catfile($output, $path);
253 0 ▶ return 1 if -f $file;
Branch: 1 branch
BranchTrueFalseTotal
if -f $file 0 0 0
254 0 ▶ return 1 if -d $file and (-f catfile($file, 'index.html') or -f catfile($file, 'index.htm'));
Branch: 1 branch
BranchTrueFalseTotal
if -d $file and -f catfile($file, "index.html") || -f catfile($file, "index.htm") 0 0 0
Condition0%0/6
-d $file and -f catfile($file, "index.html") || -f catfile($file, "index.htm")
!ll&&!rl&&r
000
-f catfile($file, "index.html") or -f catfile($file, "index.htm")
l!l&&r!l&&!r
000
MC/DC0%0/3
-d $file and -f catfile($file, "index.html") || -f catfile($file, "index.htm")
-d $file -f catfile($file, "index.html") -f catfile($file, "index.htm")
Truth table0%0/4
A and B or C
A= -d $fileB= -f catfile($file, "index.html")C= -f catfile($file, "index.htm")
ABCresult
0XX0
1000
11X1
1011
255 0 return 0;
256 }
257  
258 =head2 warnings
259  
260 Takes a Dapper object that has just been built, and returns warnings about the
261 site's settings and pages.
262  
263 =cut
264  
265 0 ▶sub warnings {
Subroutine warnings: not called CC=11 SCAR=48.8
Pod documented
266 0 my ($dapper) = @_;
267  
268 0 my $site = $dapper->{site};
269 0 my @problems;
270  
271 0 ▶ my $has_url = defined $site->{url} && length $site->{url};
Condition0%0/3
defined $site->{'url'} && length $site->{'url'}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $site->{'url'} && length $site->{'url'}
defined $site->{'url'} length $site->{'url'}
Truth table0%0/3
A && B
A= defined $site->{'url'}B= length $site->{'url'}
ABresult
0X0
100
111
272 0 ▶ if (not $has_url) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($has_url) 0 0 0
273 push @problems, {
274 severity => 'warning',
275 type => 'setting',
276 file => $dapper->{config},
277 0 message => 'url is not set, so there is no sitemap, and links in llms.txt and page.seo are not absolute',
278 };
279 }
280  
281 0 for my $page (@{$site->{pages}}) {
282 0 ▶ next if $page->{generated} or $page->{paginator} && $page->{paginator}->{page} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $page->{"generated"} or $page->{"paginator"} and $page->{"paginator"}{"page"} > 1 0 0 0
Condition0%0/6
$page->{"generated"} or $page->{"paginator"} and $page->{"paginator"}{"page"} > 1
l!l&&r!l&&!r
000
$page->{"paginator"} and $page->{"paginator"}{"page"} > 1
!ll&&!rl&&r
000
MC/DC0%0/3
$page->{"generated"} or $page->{"paginator"} and $page->{"paginator"}{"page"} > 1
$page->{"generated"} $page->{"paginator"} $page->{"paginator"}{"page"} > 1
Truth table0%0/4
A or B and C
A= $page->{"generated"}B= $page->{"paginator"}C= $page->{"paginator"}{"page"} > 1
ABCresult
1XX1
0111
00X0
0100
283 0 ▶ next unless defined $page->{filename} and $page->{filename} =~ /\.html?$/i;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $page->{"filename"} and $page->{"filename"} =~ /\.html?$/i 0 0 0
Condition0%0/3
defined $page->{"filename"} and $page->{"filename"} =~ /\.html?$/i
!ll&&!rl&&r
000
MC/DC0%0/2
defined $page->{"filename"} and $page->{"filename"} =~ /\.html?$/i
defined $page->{"filename"} $page->{"filename"} =~ /\.html?$/i
Truth table0%0/3
A and B
A= defined $page->{"filename"}B= $page->{"filename"} =~ /\.html?$/i
ABresult
0X0
100
111
284 0 ▶ next if defined $page->{title} and length $page->{title};
Branch: 1 branch
BranchTrueFalseTotal
if defined $page->{"title"} and length $page->{"title"} 0 0 0
Condition0%0/3
defined $page->{"title"} and length $page->{"title"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $page->{"title"} and length $page->{"title"}
defined $page->{"title"} length $page->{"title"}
Truth table0%0/3
A and B
A= defined $page->{"title"}B= length $page->{"title"}
ABresult
0X0
100
111
285 push @problems, {
286 severity => 'warning',
287 type => 'front_matter',
288 file => $page->{source},
289 0 message => 'has no title',
290 };
291 }
292  
293 0 return @problems;
294 }
295  
296 =head2 text
297  
298 Takes a report from L</run> and returns it as text for people.
299  
300 =cut
301  
302 0 ▶sub text {
Subroutine text: not called CC=6 SCAR=37.4
Pod documented
303 0 my ($report) = @_;
304  
305 0 my $out = '';
306 0 for my $problem (@{$report->{errors}}) {
307 0 $out .= "error: $problem->{file}: $problem->{message}\n";
308 }
309 0 for my $problem (@{$report->{warnings}}) {
310 0 $out .= "warning: $problem->{file}: $problem->{message}\n";
311 }
312  
313 0 my $errors = @{$report->{errors}};
314 0 my $warnings = @{$report->{warnings}};
315 $out .= sprintf("Checked %d page%s: %d error%s, %d warning%s.\n",
316 0 ▶ $report->{pages}, $report->{pages} == 1 ? '' : 's',
Branch: 3 branches
BranchTrueFalseTotal
$report->{'pages'} == 1 ? : 0 0 0
$errors == 1 ? : 0 0 0
$warnings == 1 ? : 0 0 0
317 $errors, $errors == 1 ? '' : 's',
318 $warnings, $warnings == 1 ? '' : 's');
319  
320 0 return $out;
321 }
322  
323 32 1;
« lib/App/Dapper/Deploy.pm ↑ Summary lib/App/Dapper/Collections.pm »