| 1 | #!/usr/bin/env perl | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: JSON-Structure validator CLI for Perl | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | # PODNAME: pjstruct | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 12 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 12 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 12 | ▶ | use 5.020; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 12 | ▶ | use feature 'signatures'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 12 | ▶ | no warnings 'experimental::signatures'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 12 | ▶ | use Getopt::Long qw(:config gnu_getopt bundling); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 12 | ▶ | use Pod::Usage; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 12 | ▶ | use JSON::MaybeXS; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 12 | ▶ | use File::Basename; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 12 | ▶ | use JSON::Structure; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 12 | our $VERSION = '0.01'; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | # Exit codes | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 12 | ▶ | use constant { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 12 | EXIT_SUCCESS => 0, | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | EXIT_INVALID => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | EXIT_ERROR => 2, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 12 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | # Main entry point | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 12 | exit main(@ARGV); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 12 | ▶ | sub main (@args) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
main: called CC=9 SCAR=22.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 12 | local @ARGV = @args; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | # Global options | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 12 | my %opts = ( | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | format => 'text', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | quiet => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | verbose => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | help => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | version => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | # Parse global options first | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 12 | Getopt::Long::Configure('pass_through'); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | GetOptions( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 'h|help' => \$opts{help}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 'V|version' => \$opts{version}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 12 | ▶ | ) or return EXIT_ERROR; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 12 | ▶ | if ($opts{version}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 1 | say "pjstruct version $VERSION (JSON::Structure $JSON::Structure::VERSION)"; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 1 | return EXIT_SUCCESS; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | # Get command | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 11 | ▶ | my $command = shift @ARGV // ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
shift @ARGV // ""
MC/DC100%1/1
shift @ARGV // ""
shift @ARGV
Truth table100%2/2
A
A= shift @ARGV
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 11 | ▶ | if ($opts{help} || $command eq 'help' || $command eq '') { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$opts{"help"} or $command eq "help" or $command eq ""
$opts{"help"} or $command eq "help"
MC/DC66%2/3
$opts{"help"} or $command eq "help" or $command eq ""
$opts{"help"}
$command eq "help"
$command eq ""
Truth table75%3/4
A or B or C
A= $opts{"help"}B= $command eq "help"C= $command eq ""
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 2 | ▶ | return cmd_help($command eq 'help' ? shift @ARGV : undef); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | # Dispatch to command | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | my %commands = ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 'validate' => \&cmd_validate, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 'v' => \&cmd_validate, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 'check' => \&cmd_check, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 'c' => \&cmd_check, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 0 | ▶ | 'version' => sub { say "pjstruct version $VERSION"; return EXIT_SUCCESS; }, | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 9 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 9 | ▶ | if (my $handler = $commands{$command}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 9 | return $handler->(\%opts); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | warn "pjstruct: unknown command '$command'\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | warn "Run 'pjstruct help' for usage.\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | return EXIT_ERROR; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 2 | ▶ | sub cmd_help ($topic = undef) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
cmd_help: called CC=4 SCAR=14.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 2 | ▶ | if (!defined $topic) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 1 | print_usage(); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 1 | return EXIT_SUCCESS; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 1 | my %topics = ( | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 'validate' => \&help_validate, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 'v' => \&help_validate, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 'check' => \&help_check, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 'c' => \&help_check, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 1 | ▶ | if (my $handler = $topics{$topic}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 1 | $handler->(); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 1 | return EXIT_SUCCESS; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | warn "pjstruct: unknown help topic '$topic'\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | return EXIT_ERROR; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 1 | ▶ | sub print_usage { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
print_usage: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 1 | print <<"EOF"; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | pjstruct - JSON-Structure validator for Perl | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | Usage: pjstruct <command> [options] [files...] | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | Commands: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | validate, v Validate instance(s) against a schema | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | check, c Check schema(s) for validity | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | help Show help for a command | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | version Show version information | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | Options: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | -h, --help Show help | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | -V, --version Show version | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | Examples: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | pjstruct check schema.struct.json | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | pjstruct validate -s schema.struct.json data.json | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | pjstruct v -s schema.struct.json *.json | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | Run 'pjstruct help <command>' for more information on a command. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | EOF | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 1 | ▶ | sub help_validate { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
help_validate: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 1 | print <<"EOF"; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | pjstruct validate - Validate instance(s) against a schema | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | Usage: pjstruct validate [options] <file>... | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | Options: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | -s, --schema <file> Schema file (required) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | -f, --format <fmt> Output format: text, json, tap (default: text) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | -q, --quiet Suppress output, use exit code only | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | -v, --verbose Show detailed validation information | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | -h, --help Show this help | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | Arguments: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | <file>... JSON instance file(s) to validate | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | Use '-' to read from stdin | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | Exit codes: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 0 All instances are valid | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 1 One or more instances are invalid | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | 2 Error (file not found, parse error, etc.) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | Examples: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | pjstruct validate -s schema.struct.json data.json | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | pjstruct validate -s schema.struct.json *.json | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | pjstruct validate -s schema.struct.json data.json --format=json | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | cat data.json | pjstruct validate -s schema.struct.json - | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | EOF | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | 0 | ▶ | sub help_check { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
help_check: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | 0 | print <<"EOF"; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | pjstruct check - Check schema(s) for validity | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | Usage: pjstruct check [options] <file>... | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | Options: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | -f, --format <fmt> Output format: text, json, tap (default: text) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | -q, --quiet Suppress output, use exit code only | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | -v, --verbose Show detailed validation information | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | -h, --help Show this help | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | Arguments: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | <file>... Schema file(s) to check | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | Use '-' to read from stdin | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | Exit codes: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | 0 All schemas are valid | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | 1 One or more schemas are invalid | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | 2 Error (file not found, parse error, etc.) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | Examples: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | pjstruct check schema.struct.json | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | pjstruct check *.struct.json | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | pjstruct check schema.struct.json --format=json | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | EOF | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | 7 | ▶ | sub cmd_validate ($global_opts) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
cmd_validate: called CC=14 SCAR=28.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | 7 | my %opts = ( | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | schema => undef, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | format => 'text', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | quiet => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | verbose => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | help => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | 7 | Getopt::Long::Configure('no_pass_through'); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | GetOptions( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | 's|schema=s' => \$opts{schema}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | 'f|format=s' => \$opts{format}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | 'q|quiet' => \$opts{quiet}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | 'v|verbose' => \$opts{verbose}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 'h|help' => \$opts{help}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | 7 | ▶ | ) or return EXIT_ERROR; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | 7 | ▶ | if ($opts{help}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | 0 | help_validate(); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | 0 | return EXIT_SUCCESS; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | 7 | ▶ | unless ($opts{schema}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | 1 | warn "pjstruct validate: missing required option --schema\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | 1 | warn "Run 'pjstruct help validate' for usage.\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | 1 | return EXIT_ERROR; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | 6 | ▶ | unless (@ARGV) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | 0 | warn "pjstruct validate: no input files specified\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | 0 | warn "Run 'pjstruct help validate' for usage.\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | 0 | return EXIT_ERROR; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | # Validate format option | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | 6 | ▶ | unless ($opts{format} =~ /^(text|json|tap)$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | 0 | warn "pjstruct validate: invalid format '$opts{format}'\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | 0 | warn "Valid formats: text, json, tap\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | 0 | return EXIT_ERROR; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | # Load schema | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | 6 | my ($schema, $schema_error) = load_json_file($opts{schema}); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | 6 | ▶ | unless (defined $schema) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | 0 | output_error(\%opts, $opts{schema}, $schema_error, 'schema'); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | 0 | return EXIT_ERROR; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | # Check schema is valid first | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | 6 | my $schema_validator = JSON::Structure::SchemaValidator->new(); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | 6 | my $schema_result = $schema_validator->validate($schema); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | 6 | ▶ | if (!$schema_result->is_valid) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | 0 | my $first_error = $schema_result->errors->[0]; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | 0 | output_error(\%opts, $opts{schema}, "invalid schema: " . $first_error->message, 'schema'); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | 0 | return EXIT_ERROR; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | # Create instance validator | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | 6 | my $validator = JSON::Structure::InstanceValidator->new(schema => $schema); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | 6 | my @results; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | 6 | my $has_invalid = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | 6 | my $has_error = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | 6 | for my $file (@ARGV) { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | 6 | my ($instance, $load_error) = load_json_file($file); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | 6 | ▶ | if (!defined $instance) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | 1 | push @results, { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | file => $file, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | valid => JSON::MaybeXS::false, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | error => $load_error, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | errors => [], | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | 1 | $has_error = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | 1 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | 5 | my $result = $validator->validate($instance); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | 5 | my $valid = $result->is_valid; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | # Convert ValidationError objects to plain hashes for output | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | 5 | my @errors = map { error_to_hash($_) } @{$result->errors}; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | 5 | ▶ | push @results, { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | file => $file, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | valid => $valid ? JSON::MaybeXS::true : JSON::MaybeXS::false, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | errors => \@errors, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | 5 | ▶ | $has_invalid = 1 unless $valid; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | # Output results | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | 6 | output_results(\%opts, \@results, 'validate'); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | 6 | ▶ | return $has_error ? EXIT_ERROR : ($has_invalid ? EXIT_INVALID : EXIT_SUCCESS); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | 2 | ▶ | sub cmd_check ($global_opts) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
cmd_check: called CC=11 SCAR=27.4
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | 2 | my %opts = ( | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | format => 'text', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | quiet => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | verbose => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | help => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | 2 | Getopt::Long::Configure('no_pass_through'); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | GetOptions( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | 'f|format=s' => \$opts{format}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | 'q|quiet' => \$opts{quiet}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | 'v|verbose' => \$opts{verbose}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | 'h|help' => \$opts{help}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | 2 | ▶ | ) or return EXIT_ERROR; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | 2 | ▶ | if ($opts{help}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | 0 | help_check(); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | 0 | return EXIT_SUCCESS; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | 2 | ▶ | unless (@ARGV) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | 0 | warn "pjstruct check: no input files specified\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | 0 | warn "Run 'pjstruct help check' for usage.\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | 0 | return EXIT_ERROR; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | # Validate format option | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | 2 | ▶ | unless ($opts{format} =~ /^(text|json|tap)$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | 0 | warn "pjstruct check: invalid format '$opts{format}'\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | 0 | warn "Valid formats: text, json, tap\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | 0 | return EXIT_ERROR; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | 2 | my $validator = JSON::Structure::SchemaValidator->new(); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | 2 | my @results; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | 2 | my $has_invalid = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | 2 | my $has_error = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | 2 | for my $file (@ARGV) { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | 2 | my ($schema, $load_error) = load_json_file($file); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | 2 | ▶ | if (!defined $schema) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | 0 | push @results, { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | file => $file, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | valid => JSON::MaybeXS::false, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | error => $load_error, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | errors => [], | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | 0 | $has_error = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | 2 | my $result = $validator->validate($schema); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | 2 | my $valid = $result->is_valid; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | # Convert ValidationError objects to plain hashes for output | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | 2 | my @errors = map { error_to_hash($_) } @{$result->errors}; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | 2 | ▶ | push @results, { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | file => $file, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | valid => $valid ? JSON::MaybeXS::true : JSON::MaybeXS::false, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | errors => \@errors, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | 2 | ▶ | $has_invalid = 1 unless $valid; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | # Output results | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | 2 | output_results(\%opts, \@results, 'check'); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | 2 | ▶ | return $has_error ? EXIT_ERROR : ($has_invalid ? EXIT_INVALID : EXIT_SUCCESS); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | 14 | ▶ | sub load_json_file ($file) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
load_json_file: called CC=5 SCAR=17.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | 14 | my $content; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | 14 | ▶ | if ($file eq '-') { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | 0 | local $/; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | 0 | $content = <STDIN>; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | 14 | ▶ | unless (-f $file) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | 1 | return (undef, "file not found: $file"); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | 13 | ▶ | open my $fh, '<:encoding(UTF-8)', $file | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | or return (undef, "cannot open file: $!"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 376 | 13 | local $/; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 377 | 13 | $content = <$fh>; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 378 | 13 | close $fh; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 379 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 381 | 13 | my $json = JSON::MaybeXS->new->utf8(0)->allow_nonref; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 382 | 13 | my $data = eval { $json->decode($content) }; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 384 | 13 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 385 | 0 | my $error = $@; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 386 | 0 | $error =~ s/ at \S+ line \d+.*//s; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 387 | 0 | return (undef, "JSON parse error: $error"); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 388 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 390 | 13 | return ($data, undef); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 391 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 393 | # Convert a ValidationError object to a plain hash for output | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 394 | 5 | ▶ | sub error_to_hash ($error) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
error_to_hash: called CC=6 SCAR=20.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 395 | 5 | my $location = $error->location; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 396 | return { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 397 | 5 | ▶ | path => $error->path // '/', | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition42%3/7
$error->path // "/"
$error->message // "Unknown error"
$location and $location->is_known
MC/DC0%0/1
$error->path // "/"
$error->path
MC/DC0%0/1
$error->message // "Unknown error"
$error->message
MC/DC0%0/2
$location and $location->is_known
$location
$location->is_known
Truth table50%1/2
A
A= $error->path
Truth table50%1/2
A
A= $error->message
Truth table33%1/3
A and B
A= $locationB= $location->is_known
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 398 | message => $error->message // 'Unknown error', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 399 | code => $error->code, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 400 | ($location && $location->is_known ? ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 401 | line => $location->line, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 402 | column => $location->column, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 403 | ) : ()), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 404 | (defined $error->schema_path ? (schema_path => $error->schema_path) : ()), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 405 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 406 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 408 | 8 | ▶ | sub output_results ($opts, $results, $command) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
output_results: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 409 | 8 | ▶ | return if $opts->{quiet}; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 411 | 7 | my $format = $opts->{format}; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 413 | 7 | ▶ | if ($format eq 'json') { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 414 | 1 | output_json($results); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 415 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 416 | elsif ($format eq 'tap') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 417 | 1 | output_tap($results, $opts->{verbose}); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 418 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 419 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 420 | 5 | output_text($results, $opts->{verbose}); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 421 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 422 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 424 | 0 | ▶ | sub output_error ($opts, $file, $error, $type) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
output_error: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 425 | 0 | ▶ | return if $opts->{quiet}; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 427 | 0 | my $format = $opts->{format}; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 429 | 0 | ▶ | if ($format eq 'json') { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 430 | 0 | my $json = JSON::MaybeXS->new->utf8->pretty->canonical; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 431 | 0 | say $json->encode({ | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 432 | file => $file, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 433 | valid => JSON::MaybeXS::false, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 434 | error => $error, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 435 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 436 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 437 | elsif ($format eq 'tap') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 438 | 0 | say "1..1"; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 439 | 0 | say "not ok 1 - $file"; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 440 | 0 | say " # $error"; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 441 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 442 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 443 | 0 | say STDERR "✗ $file: $error"; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 444 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 445 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 447 | 5 | ▶ | sub output_text ($results, $verbose) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
output_text: called CC=8 SCAR=21.4
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 448 | 5 | for my $result (@$results) { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 449 | 5 | my $file = $result->{file}; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 451 | 5 | ▶ | if ($result->{error}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 452 | 1 | say "✗ $file: $result->{error}"; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 453 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 454 | elsif ($result->{valid}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 455 | 2 | say "✓ $file: valid"; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 456 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 457 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 458 | 2 | say "✗ $file: invalid"; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 459 | 2 | for my $error (@{$result->{errors}}) { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 460 | 5 | ▶ | my $path = $error->{path} // '/'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$error->{"path"} // "/"
MC/DC0%0/1
$error->{"path"} // "/"
$error->{"path"}
Truth table50%1/2
A
A= $error->{"path"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 461 | 5 | my $msg = $error->{message}; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 462 | 5 | my $loc = ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 463 | 5 | ▶ | if ($verbose && $error->{line}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$verbose and $error->{"line"}
MC/DC0%0/2
$verbose and $error->{"line"}
$verbose
$error->{"line"}
Truth table33%1/3
A and B
A= $verboseB= $error->{"line"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 464 | 0 | $loc = " (line $error->{line}, col $error->{column})"; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 465 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 466 | 5 | say " - $path: $msg$loc"; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 467 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 468 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 469 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 470 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 471 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 472 | 1 | ▶ | sub output_json ($results) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
output_json: called CC=2 SCAR=7.6
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 473 | 1 | my $json = JSON::MaybeXS->new->utf8->pretty->canonical; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 475 | # Single result: output object, multiple: output array | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 476 | 1 | ▶ | if (@$results == 1) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 477 | 1 | print $json->encode($results->[0]); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 478 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 479 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 480 | 0 | print $json->encode($results); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 481 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 482 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 483 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 484 | 1 | ▶ | sub output_tap ($results, $verbose) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
output_tap: called CC=6 SCAR=24.7
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 485 | 1 | my $count = scalar @$results; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 486 | 1 | say "1..$count"; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 487 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 488 | 1 | my $n = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 489 | 1 | for my $result (@$results) { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 490 | 1 | $n++; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 491 | 1 | my $file = $result->{file}; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 492 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 493 | 1 | ▶ | if ($result->{error}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 494 | 0 | say "not ok $n - $file"; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 495 | 0 | say " # Error: $result->{error}"; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 496 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 497 | elsif ($result->{valid}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 498 | 1 | say "ok $n - $file"; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 499 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 500 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 501 | 0 | say "not ok $n - $file"; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 502 | 0 | for my $error (@{$result->{errors}}) { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 503 | 0 | ▶ | my $path = $error->{path} // '/'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$error->{"path"} // "/"
MC/DC0%0/1
$error->{"path"} // "/"
$error->{"path"}
Truth table0%0/2
A
A= $error->{"path"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 504 | 0 | my $msg = $error->{message}; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 505 | 0 | say " # $path: $msg"; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 506 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 507 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 508 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 509 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 510 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 511 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||