| 1 | package Dist::Conformance::Schema 0.008; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Parse YAML, JSON and INI, and validate the result against a schema | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 19 | ▶ | use 5.040; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 19 | ▶ | use warnings FATAL => 'all'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 19 | ▶ | use re '/aasx'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 19 | ▶ | use Clone (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 19 | ▶ | use Cpanel::JSON::XS(); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 19 | ▶ | use JSON::Validator::Schema::Troglodyne(); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 19 | ▶ | use YAML::XS(); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | # Each pattern is a qr// in a string, so that it keeps the modifiers of this file. | ||||||||||||||||||||||||||||
| 16 | 19 | my $NAME_RX = qr/\A[[:alnum:]][[:alnum:]._-]*\z/; | |||||||||||||||||||||||||||
| 17 | 19 | my $PATH_RX = qr{\A(?!/)(?!(?:[^\n]*/)?[.][.](?:/|\z))[^\n]+\z}; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 19 | our $NAME = { type => 'string', pattern => "$NAME_RX" }; | |||||||||||||||||||||||||||
| 20 | 19 | our $PATH = { type => 'string', pattern => "$PATH_RX" }; | |||||||||||||||||||||||||||
| 21 | 19 | our $STATUS = { type => 'string', enum => [qw{pass info fail}] }; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 0 | ▶ | sub parse_yaml ( $text, $where ) { | ||||||||||||||||||||||||||
|
Subroutine
parse_yaml: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 25 | 0 | my $data = eval { YAML::XS::Load($text) }; | |||||||||||||||||||||||||||
| 26 | 0 | ▶ | die "$where does not parse as YAML: $@" if !defined $data && $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $data and $@
MC/DC0%0/2
not defined $data and $@
not defined $data
$@
Truth table0%0/3
not(A) and B
A= not defined $dataB= $@
| |||||||||||||||||||||||||||||
| 27 | 0 | ▶ | die "$where does not hold a mapping at its top level\n" if ref $data ne 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | 0 | return $data; | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 0 | ▶ | sub parse_json ( $bytes, $where ) { | ||||||||||||||||||||||||||
|
Subroutine
parse_json: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 33 | 0 | my $data = eval { Cpanel::JSON::XS->new->utf8->decode($bytes) }; | |||||||||||||||||||||||||||
| 34 | 0 | ▶ | die( "$where does not parse as JSON: " . first_line($@), "\n" ) if !defined $data && $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $data and $@
MC/DC0%0/2
not defined $data and $@
not defined $data
$@
Truth table0%0/3
not(A) and B
A= not defined $dataB= $@
| |||||||||||||||||||||||||||||
| 35 | 0 | ▶ | die "$where does not hold an object at its top level\n" if ref $data ne 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 0 | return $data; | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 0 | ▶ | sub encode_json ($data) { | ||||||||||||||||||||||||||
|
Subroutine
encode_json: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 41 | 0 | return Cpanel::JSON::XS->new->utf8->pretty->space_before(0)->canonical->indent_length(2)->encode($data); | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 0 | ▶ | sub insert_yaml ( $text, $path, $data ) { | ||||||||||||||||||||||||||
|
Subroutine
insert_yaml: not called CC=11 SCAR=48.8
Pod
documented
| |||||||||||||||||||||||||||||
| 46 | 0 | ▶ | my $indent_of = sub ($line) { return length( $line =~ s/\S.*//sr ) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 47 | 0 | ▶ | my $fragment = sub ( $value, $indent ) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 48 | 0 | return map { ( q{ } x $indent ) . $_ } split /\n/, YAML::XS::Dump($value) =~ s/\A---\n//r; | |||||||||||||||||||||||||||
| 49 | 0 | }; | |||||||||||||||||||||||||||
| 50 | 0 | my @lines = split /\n/, $text, -1; | |||||||||||||||||||||||||||
| 51 | 0 | ▶ | pop @lines if @lines && $lines[-1] eq q{}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@lines and $lines[-1] eq ""
MC/DC0%0/2
@lines and $lines[-1] eq ""
@lines
$lines[-1] eq ""
Truth table0%0/3
A and B
A= @linesB= $lines[-1] eq ""
| |||||||||||||||||||||||||||||
| 52 | 0 | my ( $indent, $from, $to, @keys ) = ( 0, 0, scalar @lines, @$path ); | |||||||||||||||||||||||||||
| 53 | 0 | ▶ | while ( defined( my $key = shift @keys ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 0 | my ($at) = grep { $lines[$_] =~ m/\A[ ]{$indent}\Q$key\E:/ } $from .. $to - 1; | |||||||||||||||||||||||||||
| 55 | 0 | ▶ | if ( !defined $at ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 0 | my $nested = $data; | |||||||||||||||||||||||||||
| 57 | 0 | for my $inner ( reverse @keys ) { $nested = { $inner => $nested } } | |||||||||||||||||||||||||||
| 58 | 0 | splice @lines, $to, 0, $fragment->( { $key => $nested }, $indent ); | |||||||||||||||||||||||||||
| 59 | 0 | return join( "\n", @lines ) . "\n"; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | 0 | ▶ | die "$key has something after it on its line\n" if $lines[$at] !~ m/:\s*(?:\#.*)?\z/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 0 | my @block; | |||||||||||||||||||||||||||
| 63 | 0 | for my $index ( grep { $lines[$_] =~ m/\A\s*[^\s\#]/ } $at + 1 .. $to - 1 ) { | |||||||||||||||||||||||||||
| 64 | 0 | ▶ | last if $indent_of->( $lines[$index] ) <= $indent; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 0 | push @block, $index; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | 0 | ▶ | ( $from, $to ) = ( $at + 1, @block ? $block[-1] + 1 : $at + 1 ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 0 | ▶ | $indent = @block ? $indent_of->( $lines[ $block[0] ] ) : $indent + 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | 0 | splice @lines, $to, 0, $fragment->( $data, $indent ); | |||||||||||||||||||||||||||
| 71 | 0 | return join( "\n", @lines ) . "\n"; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 0 | ▶ | sub validate ( $data, $schema, $where ) { | ||||||||||||||||||||||||||
|
Subroutine
validate: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 76 | 0 | my $copy = Clone::clone($data); | |||||||||||||||||||||||||||
| 77 | 0 | my @errors = errors( $copy, $schema ); | |||||||||||||||||||||||||||
| 78 | 0 | ▶ | refuse( $where, @errors ) if @errors; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 0 | return $copy; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 0 | ▶ | sub refuse ( $where, @errors ) { | ||||||||||||||||||||||||||
|
Subroutine
refuse: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 84 | 0 | die join( "\n", "$where is not valid:", map { " $_" } @errors ), "\n"; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 0 | ▶ | sub errors ( $data, $schema ) { | ||||||||||||||||||||||||||
|
Subroutine
errors: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 89 | 0 | my $validator = JSON::Validator::Schema::Troglodyne->new; | |||||||||||||||||||||||||||
| 90 | 0 | $validator->coerce( { %{ $validator->coerce }, defaults => 1, booleans => 1 } ); | |||||||||||||||||||||||||||
| 91 | 0 | my @errors = map { "$_" } $validator->validate( $data, $schema ); | |||||||||||||||||||||||||||
| 92 | 0 | return @errors; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 0 | ▶ | sub matches ( $fragment, $value ) { | ||||||||||||||||||||||||||
|
Subroutine
matches: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | # (?^:) gives the pattern the default flags that JSON::Validator matches it with, not the /s of this file. | ||||||||||||||||||||||||||||
| 99 | 0 | ▶ | return defined $value && $value =~ m/(?^:$fragment->{pattern})/; | ||||||||||||||||||||||||||
|
Condition0%0/3
defined $value && $value =~ /(?^:$$fragment{'pattern'})/aasx
MC/DC0%0/2
defined $value && $value =~ /(?^:$$fragment{'pattern'})/aasx
defined $value
$value =~ /(?^:$$fragment{'pattern'})/aasx
Truth table0%0/3
A && B
A= defined $valueB= $value =~ /(?^:$$fragment{'pattern'})/aasx
| |||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 0 | ▶ | sub relative_path ( $path, $inside ) { | ||||||||||||||||||||||||||
|
Subroutine
relative_path: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 0 | ▶ | die( ( $path // 'undef' ) . " is not a relative path inside $inside\n" ) if !matches( $PATH, $path ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$path // "undef"
MC/DC0%0/1
$path // "undef"
$path
Truth table0%0/2
A
A= $path
| |||||||||||||||||||||||||||||
| 106 | 0 | return $path; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 0 | ▶ | sub parse_ini ( $text, $where ) { | ||||||||||||||||||||||||||
|
Subroutine
parse_ini: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 111 | 0 | my $data = eval { Dist::Conformance::Schema::INIReader->read_string($text) }; | |||||||||||||||||||||||||||
| 112 | 0 | ▶ | die( "$where does not parse as INI: " . first_line($@), "\n" ) if !defined $data; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 0 | return $data; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 0 | ▶ | sub first_line ($text) { | ||||||||||||||||||||||||||
|
Subroutine
first_line: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 118 | 0 | ▶ | my ($line) = split /\n/, $text // q{}; | ||||||||||||||||||||||||||
|
Condition0%0/2
$text // ""
MC/DC0%0/1
$text // ""
$text
Truth table0%0/2
A
A= $text
| |||||||||||||||||||||||||||||
| 119 | 0 | ▶ | $line //= q{}; | ||||||||||||||||||||||||||
|
Condition0%0/2
$line //= ""
MC/DC0%0/1
$line //= ""
$line
Truth table0%0/2
A
A= $line
| |||||||||||||||||||||||||||||
| 120 | 0 | $line =~ s/\ at\ \S+\ line\ \d+[.]?\z//; | |||||||||||||||||||||||||||
| 121 | 0 | return $line; | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | # Config::INI::Reader keeps the last value of a repeated key, so it collects each repeat into a list. | ||||||||||||||||||||||||||||
| 125 | 19 | package Dist::Conformance::Schema::INIReader 0.008 { | |||||||||||||||||||||||||||
| 126 | 19 | ▶ | use parent 'Config::INI::Reader'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 0 | ▶ | sub set_value ( $self, $name, $value ) { | ||||||||||||||||||||||||||
|
Subroutine
set_value: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 129 | 0 | ▶ | my $values = $self->{ __PACKAGE__ . '/values' }{ $self->current_section }{$name} //= []; | ||||||||||||||||||||||||||
|
Condition0%0/2
$$self{"Dist::Conformance::Schema::INIReader/values"}{$self->current_section}{$name} //= []
MC/DC0%0/1
$$self{"Dist::Conformance::Schema::INIReader/values"}{$self->current_section}{$name} //= []
$$self{"Dist::Conformance::Schema::INIReader/values"}{$self->current_section}{$name}
Truth table0%0/2
A
A= $$self{"Dist::Conformance::Schema::INIReader/values"}{$self->current_section}{$name}
| |||||||||||||||||||||||||||||
| 130 | 0 | push @$values, $value; | |||||||||||||||||||||||||||
| 131 | 0 | ▶ | return $self->SUPER::set_value( $name, @$values == 1 ? $value : [@$values] ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | __END__ | ||||||||||||||||||||||||||||