| 1 | package Dist::Conformance::Check 0.011; | ||||||||||||
| 2 | |||||||||||||
| 3 | # ABSTRACT: The interface of a check, and what the checks share | ||||||||||||
| 4 | |||||||||||||
| 5 | 15 | ▶ | use 5.040; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 15 | ▶ | use warnings FATAL => 'all'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 15 | ▶ | use re '/aasx'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 15 | ▶ | use Mojo::Base -base, -signatures; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | |||||||||||||
| 10 | 15 | ▶ | use List::Util(); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | 15 | ▶ | use Module::Runtime(); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | |||||||||||||
| 13 | 15 | ▶ | use Dist::Conformance::Schema(); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | |||||||||||||
| 15 | |||||||||||||
| 16 | 15 | has 'name'; | |||||||||||
| 17 | 0 | ▶ | has config => sub { {} }; | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||
| 18 | 15 | has 'source'; | |||||||||||
| 19 | 0 | ▶ | has variables => sub { {} }; | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||
| 20 | |||||||||||||
| 21 | 15 | my $MODULE_NAME_RX = $Module::Runtime::module_name_rx; | |||||||||||
| 22 | 15 | my $MODULE_RX = qr/\A$MODULE_NAME_RX\z/; | |||||||||||
| 23 | 15 | our $MODULE = { type => 'string', pattern => "$MODULE_RX" }; | |||||||||||
| 24 | |||||||||||||
| 25 | # The schema of the keys that this check takes. Every key in it is documented | ||||||||||||
| 26 | # in the POD of the check. The base class takes none of its own; L</schema> | ||||||||||||
| 27 | # adds L</THE KEYS THAT EVERY CHECK TAKES>. | ||||||||||||
| 28 | # @returns HashRef with C<properties>, and C<required> if any key is required | ||||||||||||
| 29 | # @example my $schema = Dist::Conformance::Check::ClaudeMd->config_schema; | ||||||||||||
| 30 | 0 | ▶ | sub config_schema ($) { return { properties => {} } } | ||||||||||
|
Subroutine
config_schema: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 31 | |||||||||||||
| 32 | # The whole schema of the configuration: L</config_schema>, the keys that every | ||||||||||||
| 33 | # check takes, and no others. | ||||||||||||
| 34 | # @returns HashRef an object schema that allows no other keys | ||||||||||||
| 35 | # @example my $schema = Dist::Conformance::Check::ClaudeMd->schema; | ||||||||||||
| 36 | 0 | ▶ | sub schema ($class) { | ||||||||||
|
Subroutine
schema: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 37 | 0 | my $own = $class->config_schema; | |||||||||||
| 38 | return { | ||||||||||||
| 39 | type => 'object', | ||||||||||||
| 40 | additionalProperties => 0, | ||||||||||||
| 41 | required => $own->{required} // [], | ||||||||||||
| 42 | properties => { | ||||||||||||
| 43 | module => $MODULE, | ||||||||||||
| 44 | level => { type => 'string', enum => [qw{fail info}], default => 'fail' }, | ||||||||||||
| 45 | 0 | ▶ | %{ $own->{properties} }, | ||||||||||
|
Condition0%0/2
$$own{"required"} // []
MC/DC0%0/1
$$own{"required"} // []
$$own{"required"}
Truth table0%0/2
A
A= $$own{"required"}
| |||||||||||||
| 46 | }, | ||||||||||||
| 47 | }; | ||||||||||||
| 48 | } | ||||||||||||
| 49 | |||||||||||||
| 50 | # A new check, whose C<config> is validated against L</schema> first. The other | ||||||||||||
| 51 | # arguments, such as C<source> and C<variables>, are the attributes of the new | ||||||||||||
| 52 | # check. | ||||||||||||
| 53 | # @param name Str the name of the check in the flavor | ||||||||||||
| 54 | # @param config HashRef optional: the configuration, default empty | ||||||||||||
| 55 | # @param where Str optional: names the configuration in an error, such as | ||||||||||||
| 56 | # C<the check ClaudeMd of the flavor troglodyne>; default C<the check NAME> | ||||||||||||
| 57 | # @returns InstanceOf["Dist::Conformance::Check"] the check, of the class that it is called on | ||||||||||||
| 58 | # @throws when the schema refuses the configuration, with the message of | ||||||||||||
| 59 | # L<Dist::Conformance::Schema/"validate($data, $schema, $where)"> | ||||||||||||
| 60 | # @example my $check = Dist::Conformance::Check::ClaudeMd->from_config( | ||||||||||||
| 61 | # name => 'ClaudeMd', | ||||||||||||
| 62 | # config => { file => 'CLAUDE.md', mentions => [] }, | ||||||||||||
| 63 | # source => $source, | ||||||||||||
| 64 | # ); | ||||||||||||
| 65 | 0 | ▶ | sub from_config ( $class, %arguments ) { | ||||||||||
|
Subroutine
from_config: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||
| 66 | 0 | ▶ | my $where = delete $arguments{where} // "the check $arguments{name}"; | ||||||||||
|
Condition0%0/2
delete $arguments{"where"} // qq[the check $arguments{"name"}]
MC/DC0%0/1
delete $arguments{"where"} // qq[the check $arguments{"name"}]
delete $arguments{"where"}
Truth table0%0/2
A
A= delete $arguments{"where"}
| |||||||||||||
| 67 | 0 | ▶ | my $config = Dist::Conformance::Schema::validate( $arguments{config} // {}, $class->schema, $where ); | ||||||||||
|
Condition0%0/2
$arguments{"config"} // {}
MC/DC0%0/1
$arguments{"config"} // {}
$arguments{"config"}
Truth table0%0/2
A
A= $arguments{"config"}
| |||||||||||||
| 68 | 0 | return $class->new( %arguments, config => $config ); | |||||||||||
| 69 | } | ||||||||||||
| 70 | |||||||||||||
| 71 | # Checks the L<Dist::Conformance::Snapshot> that it is given. A subclass must | ||||||||||||
| 72 | # implement it. | ||||||||||||
| 73 | # @returns C<($status, $detail)>, as a list: the status is C<pass>, C<fail> or | ||||||||||||
| 74 | # C<info>, and the detail is one line for a person | ||||||||||||
| 75 | # @throws when the subclass does not implement it | ||||||||||||
| 76 | # @example my ( $status, $detail ) = $check->check($snapshot); | ||||||||||||
| 77 | 0 | ▶ | sub check ( $self, $ ) { die( ref($self) . " does not implement check\n" ) } | ||||||||||
|
Subroutine
check: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 78 | |||||||||||||
| 79 | # Changes the checkout of C<< $update->snapshot >> through the | ||||||||||||
| 80 | # L<Dist::Conformance::Update>. | ||||||||||||
| 81 | # | ||||||||||||
| 82 | # The default is for a check that cannot change anything: when L</check> fails, | ||||||||||||
| 83 | # it records the detail and that there is no automatic fix. | ||||||||||||
| 84 | # @param update InstanceOf["Dist::Conformance::Update"] what records and writes the changes | ||||||||||||
| 85 | # @returns what the subclass chooses, which no caller reads: L<Dist::Conformance/"update(dry_run)"> | ||||||||||||
| 86 | # ignores it. The default returns nothing. | ||||||||||||
| 87 | # @example $check->update($update); | ||||||||||||
| 88 | 0 | ▶ | sub update ( $self, $update ) { | ||||||||||
|
Subroutine
update: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 89 | 0 | my ( $status, $detail ) = $self->check( $update->snapshot ); | |||||||||||
| 90 | 0 | ▶ | $update->by_hand( "$detail: " . $self->name . ' has no automatic fix' ) if $status eq 'fail'; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 91 | 0 | return; | |||||||||||
| 92 | } | ||||||||||||
| 93 | |||||||||||||
| 94 | # The configuration that would make this check accept the repository in the | ||||||||||||
| 95 | # L<Dist::Conformance::Snapshot> that it is given as it is now, as the keys of | ||||||||||||
| 96 | # this check for F<.conformance.yml>. | ||||||||||||
| 97 | # C<conformance adopt> prints it, so that a person can record the choices of a | ||||||||||||
| 98 | # repository before an update replaces them. The default is nothing: a check | ||||||||||||
| 99 | # whose configuration has no room for the choices of a repository has nothing to | ||||||||||||
| 100 | # adopt. | ||||||||||||
| 101 | # @returns HashRef the keys, or nothing | ||||||||||||
| 102 | # @example my $keys = $check->adopt($snapshot); | ||||||||||||
| 103 | 0 | ▶ | sub adopt ( $, $ ) { return } | ||||||||||
|
Subroutine
adopt: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 104 | |||||||||||||
| 105 | # The template at C<$path> in the source with the variables filled in, and then | ||||||||||||
| 106 | # the sorted names of the placeholders that no variable fills. | ||||||||||||
| 107 | # @param path Str the path of the template in the source | ||||||||||||
| 108 | # @returns the text, and then the names of the placeholders that are left, as a list | ||||||||||||
| 109 | # @throws when the source has no such file, with the message of | ||||||||||||
| 110 | # L<Dist::Conformance::Source/"file(relative)"> | ||||||||||||
| 111 | # @example my ( $text, @unfilled ) = $check->template('templates/CLAUDE.md'); | ||||||||||||
| 112 | 0 | ▶ | sub template ( $self, $path ) { | ||||||||||
|
Subroutine
template: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 113 | 0 | my $text = $self->source->file($path); | |||||||||||
| 114 | 0 | my $variables = $self->variables; | |||||||||||
| 115 | 0 | ▶ | $text =~ s/\{\{ ([[:upper:]][[:upper:][:digit:]_]*) \}\}/exists $variables->{$1} ? $variables->{$1} : "{{$1}}"/ge; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 116 | 0 | my @unfilled = List::Util::uniq( sort $text =~ m/\{\{ ([[:upper:]][[:upper:][:digit:]_]*) \}\}/g ); | |||||||||||
| 117 | 0 | return ( $text, @unfilled ); | |||||||||||
| 118 | } | ||||||||||||
| 119 | |||||||||||||
| 120 | # Writes the template C<$template> to C<$path>, with C<$mode>, through | ||||||||||||
| 121 | # C<$update>. When a placeholder is left, writes nothing and records what is | ||||||||||||
| 122 | # needed. | ||||||||||||
| 123 | # @param update InstanceOf["Dist::Conformance::Update"] what writes the file | ||||||||||||
| 124 | # @param path Str the path to write in the checkout | ||||||||||||
| 125 | # @param template Str the path of the template in the source | ||||||||||||
| 126 | # @param mode Int optional: the permissions, default 0644 | ||||||||||||
| 127 | # @returns Bool whether it wrote | ||||||||||||
| 128 | # @throws when the source has no such template, with the message of L<template|/"template(path)"> | ||||||||||||
| 129 | # @example $check->write_template( $update, '.perltidyrc', 'templates/perltidyrc' ); | ||||||||||||
| 130 | 0 | ▶ | sub write_template ( $self, $update, $path, $template, $mode = oct 644 ) { | ||||||||||
|
Subroutine
write_template: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||
| 131 | 0 | my ( $text, @unfilled ) = $self->template($template); | |||||||||||
| 132 | 0 | ▶ | if (@unfilled) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 133 | 0 | $update->by_hand( "Write $path from $template by hand, or set the variables " . join( ', ', @unfilled ) . ' in .conformance.yml: nothing sets them' ); | |||||||||||
| 134 | 0 | return 0; | |||||||||||
| 135 | } | ||||||||||||
| 136 | 0 | return $update->write( $path, $text, $mode ); | |||||||||||
| 137 | } | ||||||||||||
| 138 | |||||||||||||
| 139 | |||||||||||||
| 140 | 0 | ▶ | sub missing ( $, $snapshot, @paths ) { | ||||||||||
|
Subroutine
missing: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 141 | 0 | my @missing = grep { local ($_) = $_; !$snapshot->contains($_) } @paths; | |||||||||||
| 142 | 0 | return @missing; | |||||||||||
| 143 | } | ||||||||||||
| 144 | |||||||||||||
| 145 | __END__ | ||||||||||||