script/pkwalify

filter: Statementstmt 42.3% 44 / 104 Branchbran 36.9% 17 / 46 Conditioncond 55.5% 10 / 18 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 66.6% 6 / 9 Podpod n/a totaltotal 40.7% 77 / 189 CC 24 SCAR 46.4
CC
24
Cov
42%
read_file
149.7
slurp_file
6.0
__ANON__
2.0
CRAP 103.6
↑ Summary lib/Kwalify.pm »
1 #!/usr/local/bin/perl -w
2 # -*- mode: cperl; coding: latin-2 -*-
3  
4 #
5 # Author: Slaven Rezic
6 #
7 # Copyright (C) 2006,2007,2008,2009,2015 Slaven Rezic. All rights reserved.
8 # This program is free software; you can redistribute it and/or
9 # modify it under the same terms as Perl itself.
10 #
11 # Mail: slaven@rezic.de
12 # WWW: http://www.rezic.de/eserte/
13 #
14  
15 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 $VERSION = '1.13';
18  
19 4 ▶use Kwalify;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 4 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 4 my $schema_file;
23 my $parse_mod;
24 4 my $silent;
25 4 my $show_version;
26 GetOptions("f=s" => \$schema_file,
27 "m|module=s" => \$parse_mod,
28 "s|silent" => \$silent,
29 "v|version" => \$show_version,
30 0 ▶ "h|help" => sub { print usage(); exit 0 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
31 )
32 4 ▶ or die usage();
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("f=s", \$schema_file, "m|module=s", \$parse_mod, "s|silent", \$silent, "v|version", \$show_version, "h|help", sub { print usage(); exit 0; } ) or die usage() 1 3 4
33
34 3 ▶if ($show_version) {
Branch: 1 branch
BranchTrueFalseTotal
if ($show_version) 0 3 3
35 0 version();
36 0 exit;
37 }
38  
39 3 ▶if (!defined $schema_file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $schema_file) 1 2 3
40 1 die usage("-f option is mandatory");
41 }
42  
43 2 my $data_file = shift @ARGV;
44 2 ▶if (!defined $data_file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $data_file) 1 1 2
45 1 die usage("datafile is mandatory");
46 }
47  
48 1 my(@schema) = read_file($schema_file);
49 0 ▶if (@schema != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@schema != 1) 0 0 0
50 0 print "<$schema_file> does not contain exactly one schema, cannot handle this.";
51 0 exit 1;
52 }
53 0 my $schema = $schema[0];
54 0 my(@data) = read_file($data_file);
55  
56 0 my $errors = 0;
57 0 my $document_index = 0;
58 0 for my $data (@data) {
59 0 my $document_label = $data_file . '#' . $document_index;
60 0 eval { Kwalify::validate($schema, $data) };
61 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
62 0 print "$document_label: INVALID\n$@\n";
63 0 $errors++;
64 } else {
65 0 ▶ if (!$silent) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($silent) 0 0 0
66 0 print "$document_label: valid.\n";
67 }
68 }
69 0 $document_index++;
70 }
71  
72 0 exit $errors;
73  
74 sub read_file {
75 1 ▶ my $file = shift;
Subroutine read_file: called CC=22 SCAR=50.1
76  
77 1 my @try_order;
78 1 ▶ if (defined $parse_mod) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $parse_mod) { } 0 1 1
elsif ($file =~ /\.json$/i) { } 0 1 1
79 0 @try_order = ($parse_mod);
80 } elsif ($file =~ m{\.json$}i) {
81 0 @try_order = ('JSON::XS', 'JSON', 'YAML::Syck', 'YAML', 'YAML::XS');
82 } else { # yaml or don't know
83 1 @try_order = ('YAML::Syck', 'YAML', 'YAML::XS', 'JSON::XS', 'JSON');
84 }
85  
86 1 my @errors;
87 1 for my $try (@try_order) {
88 5 ▶ if ($try eq 'YAML::Syck' && eval { require YAML::Syck; 1 }) {
Branch: 5 branches
BranchTrueFalseTotal
if ($try eq 'YAML::Syck' and eval { do { require YAML::Syck; 1 } }) { } 0 5 5
elsif ($try eq 'YAML::XS' and eval { do { require YAML::XS; 1 } }) { } 0 5 5
elsif ($try eq 'YAML' and eval { do { require YAML; 1 } }) { } 0 5 5
elsif ($try eq 'JSON::XS' and eval { do { require JSON::XS; 1 } }) { } 0 5 5
elsif ($try eq 'JSON' and eval { do { require JSON; 1 } }) { } 0 5 5
Condition66%2/3
$try eq "YAML::Syck" and eval { do { require YAML::Syck; 1 } }
!ll&&!rl&&r
410
MC/DC0%0/2
$try eq "YAML::Syck" and eval { do { require YAML::Syck; 1 } }
$try eq "YAML::Syck" eval { do { require YAML::Syck; 1 } }
Truth table66%2/3
A and B
A= $try eq "YAML::Syck"B= eval { do { require YAML::Syck; 1 } }
ABresult
0X0
100
111
89 0 my @data = eval { YAML::Syck::LoadFile($file) };
90 0 ▶ return @data if !$@;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 0 0 0
91 0 push @errors, $@;
92 1 ▶ } elsif ($try eq 'YAML::XS' && eval { require YAML::XS; 1 }) {
Condition66%2/3
$try eq "YAML::XS" and eval { do { require YAML::XS; 1 } }
!ll&&!rl&&r
410
MC/DC0%0/2
$try eq "YAML::XS" and eval { do { require YAML::XS; 1 } }
$try eq "YAML::XS" eval { do { require YAML::XS; 1 } }
Truth table66%2/3
A and B
A= $try eq "YAML::XS"B= eval { do { require YAML::XS; 1 } }
ABresult
0X0
100
111
93 0 my @data = eval { YAML::XS::LoadFile($file) };
94 0 ▶ return @data if !$@;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 0 0 0
95 0 push @errors, $@;
96 1 ▶ } elsif ($try eq 'YAML' && eval { require YAML; 1 }) {
Condition66%2/3
$try eq "YAML" and eval { do { require YAML; 1 } }
!ll&&!rl&&r
410
MC/DC0%0/2
$try eq "YAML" and eval { do { require YAML; 1 } }
$try eq "YAML" eval { do { require YAML; 1 } }
Truth table66%2/3
A and B
A= $try eq "YAML"B= eval { do { require YAML; 1 } }
ABresult
0X0
100
111
97 0 my @data = eval { YAML::LoadFile($file) };
98 0 ▶ return @data if !$@;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 0 0 0
99 0 push @errors, $@;
100 1 ▶ } elsif ($try eq 'JSON::XS' && eval { require JSON::XS; 1 }) {
Condition66%2/3
$try eq "JSON::XS" and eval { do { require JSON::XS; 1 } }
!ll&&!rl&&r
410
MC/DC0%0/2
$try eq "JSON::XS" and eval { do { require JSON::XS; 1 } }
$try eq "JSON::XS" eval { do { require JSON::XS; 1 } }
Truth table66%2/3
A and B
A= $try eq "JSON::XS"B= eval { do { require JSON::XS; 1 } }
ABresult
0X0
100
111
101 0 my @data = eval { JSON::XS::decode_json(slurp_file($file)) };
102 0 ▶ return @data if !$@;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 0 0 0
103 0 push @errors, $@;
104 1 ▶ } elsif ($try eq 'JSON' && eval { require JSON; 1 }) {
Condition66%2/3
$try eq "JSON" and eval { do { require JSON; 1 } }
!ll&&!rl&&r
410
MC/DC0%0/2
$try eq "JSON" and eval { do { require JSON; 1 } }
$try eq "JSON" eval { do { require JSON; 1 } }
Truth table66%2/3
A and B
A= $try eq "JSON"B= eval { do { require JSON; 1 } }
ABresult
0X0
100
111
105 0 my $data = eval {
106 0 my $json = slurp_file($file);
107 0 ▶ if (defined &JSON::from_json) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined &JSON::from_json) { } 0 0 0
108 0 JSON::from_json($json, {utf8 => 1});
109 } else { # old style
110 0 JSON::jsonToObj($json);
111 }
112 };
113 0 ▶ return ($data) if $data && !$@;
Branch: 1 branch
BranchTrueFalseTotal
if $data and not $@ 0 0 0
Condition0%0/3
$data and not $@
!ll&&!rl&&r
000
MC/DC0%0/2
$data and not $@
$data not $@
Truth table0%0/3
A and not(B)
A= $dataB= not $@
ABresult
0X0
100
111
114 0 push @errors, $@;
115 } else {
116 5 push @errors, "Unsupported module $try";
117 }
118 }
119 1 ▶ if (!@errors) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @errors) { } 0 1 1
120 0 die "Cannot parse <$file>. Try to install a YAML and/or JSON parsing module first.\n";
121 } else {
122 1 die "Cannot parse <$file>. Cumulated errors:\n" . join("\n", @errors) . "\n";
123 }
124 }
125  
126 sub slurp_file {
127 0 ▶ my $file = shift;
Subroutine slurp_file: not called CC=2 SCAR=17.9
128 0 ▶ open FH, "< $file"
Branch: 1 branch
BranchTrueFalseTotal
open FH, "< $file" or die "Can't open <$file>: $!" 0 0 0
129 or die "Can't open <$file>: $!";
130 0 local $/ = undef;
131 0 my $json = <FH>;
132 0 close FH;
133 0 $json;
134 }
135  
136 sub usage {
137 3 ▶ my($msg) = @_;
Subroutine usage: called CC=2 SCAR=6.9
138 3 ▶ if (defined $msg) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $msg) { } 2 1 3
139 2 $msg .= "\n";
140 } else {
141 1 $msg = "";
142 }
143 3 <<EOF;
144 ${msg}usage: $0 [-v] [-s] [-m parse-mod] -f schema.yml data.yml
145 $0 -f schema.json data.json
146 EOF
147 }
148  
149 sub version {
150 0 ▶ print <<EOF;
Subroutine version: not called CC=1 SCAR=6.9
151 pkwalify $VERSION
152 Kwalify.pm $Kwalify::VERSION
153 perl $]
154 EOF
155 }
156  
157 __END__
↑ Summary lib/Kwalify.pm »