line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Test::Format; |
2
|
|
|
|
|
|
|
$Test::Format::VERSION = '1.0.0'; |
3
|
|
|
|
|
|
|
# ABSTRACT: test files if they match format |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
|
6
|
4
|
|
|
4
|
|
330622
|
use strict; |
|
4
|
|
|
|
|
41
|
|
|
4
|
|
|
|
|
134
|
|
7
|
4
|
|
|
4
|
|
22
|
use warnings FATAL => 'all'; |
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
173
|
|
8
|
4
|
|
|
4
|
|
21
|
use utf8; |
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
29
|
|
9
|
4
|
|
|
4
|
|
126
|
use open qw(:std :utf8); |
|
4
|
|
|
|
|
9
|
|
|
4
|
|
|
|
|
27
|
|
10
|
|
|
|
|
|
|
|
11
|
4
|
|
|
4
|
|
677
|
use Test::More; |
|
4
|
|
|
|
|
8
|
|
|
4
|
|
|
|
|
23
|
|
12
|
4
|
|
|
4
|
|
3962
|
use JSON::PP; |
|
4
|
|
|
|
|
62866
|
|
|
4
|
|
|
|
|
392
|
|
13
|
4
|
|
|
4
|
|
35
|
use Exporter; |
|
4
|
|
|
|
|
8
|
|
|
4
|
|
|
|
|
3537
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
16
|
|
|
|
|
|
|
our @EXPORT_OK = qw( |
17
|
|
|
|
|
|
|
test_format |
18
|
|
|
|
|
|
|
); |
19
|
|
|
|
|
|
|
our @EXPORT = @EXPORT_OK; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub test_format { |
23
|
13
|
|
|
13
|
1
|
7877
|
my (@opts) = @_; |
24
|
|
|
|
|
|
|
|
25
|
13
|
100
|
|
|
|
53
|
die 'Must specify opts' if scalar(@opts) == 0; |
26
|
12
|
100
|
|
|
|
42
|
die 'There must be key-value pairs' if scalar(@opts) % 2; |
27
|
|
|
|
|
|
|
|
28
|
11
|
|
|
|
|
33
|
my %opts = @opts; |
29
|
|
|
|
|
|
|
|
30
|
11
|
|
|
|
|
75
|
my $files = delete $opts{files}; |
31
|
11
|
|
|
|
|
23
|
my $format = delete $opts{format}; |
32
|
11
|
|
|
|
|
22
|
my $format_sub = delete $opts{format_sub}; |
33
|
|
|
|
|
|
|
|
34
|
11
|
|
|
|
|
47
|
my @unknown_opts = keys %opts; |
35
|
11
|
100
|
|
|
|
36
|
die 'Unknown opts: ' . join(', ', @unknown_opts) if @unknown_opts; |
36
|
|
|
|
|
|
|
|
37
|
10
|
50
|
|
|
|
27
|
die "Must specify 'files'" if not defined $files; |
38
|
10
|
50
|
|
|
|
32
|
die "'files' must be an array" if ref $files ne 'ARRAY'; |
39
|
10
|
100
|
|
|
|
23
|
die "'files' can't be an empty array" if scalar(@{$files}) == 0; |
|
10
|
|
|
|
|
36
|
|
40
|
|
|
|
|
|
|
|
41
|
9
|
100
|
100
|
|
|
58
|
die "Must specify 'format' or 'format_sub'" if !defined($format) && !defined($format_sub); |
42
|
7
|
100
|
100
|
|
|
38
|
die "Can't specify both 'format' and 'format_sub'" if defined($format) && defined($format_sub); |
43
|
|
|
|
|
|
|
|
44
|
6
|
100
|
100
|
|
|
29
|
die "Unknown value for 'format' opt: '$format'" if defined($format) && $format ne 'pretty_json'; |
45
|
5
|
100
|
100
|
|
|
27
|
die "'format_sub' must be sub" if defined($format_sub) && ref($format_sub) ne 'CODE'; |
46
|
|
|
|
|
|
|
|
47
|
4
|
100
|
66
|
|
|
18
|
my $sub = defined($format) && $format eq 'pretty_json' ? \&_pretty_json : $format_sub; |
48
|
|
|
|
|
|
|
|
49
|
4
|
|
|
|
|
7
|
foreach my $file (@{$files}) { |
|
4
|
|
|
|
|
10
|
|
50
|
4
|
|
|
|
|
123
|
foreach my $file_name (glob $file) { |
51
|
4
|
50
|
|
|
|
52
|
if (-e $file_name) { |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
# $content is chars, not bytes |
54
|
4
|
|
|
|
|
28
|
my $content = _read_file($file_name); |
55
|
|
|
|
|
|
|
|
56
|
4
|
|
|
|
|
15
|
my $expected_content = $sub->($content); |
57
|
|
|
|
|
|
|
|
58
|
4
|
100
|
|
|
|
179
|
if ($ENV{SELF_UPDATE}) { |
59
|
2
|
50
|
|
|
|
5
|
if ($content eq $expected_content) { |
60
|
0
|
|
|
|
|
0
|
pass("File $file_name is in expected format"), |
61
|
|
|
|
|
|
|
} else { |
62
|
2
|
|
|
|
|
7
|
_write_file($file_name, $expected_content); |
63
|
2
|
|
|
|
|
16
|
pass("Writing fixed file $file_name"); |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
} else { |
66
|
2
|
|
|
|
|
13
|
is($content, $expected_content, "File $file_name is in expected format"), |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
} else { |
69
|
0
|
|
|
|
|
0
|
fail("File $file_name does not exist"); |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
4
|
|
|
|
|
1654
|
return 1; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
sub _pretty_json { |
78
|
8
|
|
|
8
|
|
3213
|
my ($content) = @_; |
79
|
|
|
|
|
|
|
|
80
|
8
|
|
|
|
|
36
|
my $json_coder = JSON::PP |
81
|
|
|
|
|
|
|
->new |
82
|
|
|
|
|
|
|
->pretty |
83
|
|
|
|
|
|
|
->canonical |
84
|
|
|
|
|
|
|
->indent_length(4) |
85
|
|
|
|
|
|
|
; |
86
|
|
|
|
|
|
|
|
87
|
8
|
|
|
|
|
914
|
my $data = JSON::PP->new->decode($content); |
88
|
6
|
|
|
|
|
1964
|
my $pretty_json = $json_coder->encode($data); |
89
|
|
|
|
|
|
|
|
90
|
6
|
|
|
|
|
916
|
return $pretty_json; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
sub _read_file { |
94
|
6
|
|
|
6
|
|
21
|
my ($file_name) = @_; |
95
|
|
|
|
|
|
|
|
96
|
6
|
|
|
|
|
14
|
my $content = ''; |
97
|
|
|
|
|
|
|
|
98
|
6
|
50
|
|
|
|
511
|
open FH, '<', $file_name or die "Can't open < $file_name for reading: $!"; |
99
|
|
|
|
|
|
|
|
100
|
6
|
|
|
|
|
131
|
while () { |
101
|
29
|
|
|
|
|
109
|
$content .= $_; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
6
|
|
|
|
|
30
|
return $content; |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
sub _write_file { |
108
|
4
|
|
|
4
|
|
1691
|
my ($file_name, $content) = @_; |
109
|
|
|
|
|
|
|
|
110
|
4
|
50
|
|
|
|
298
|
open FH, '>', $file_name or die "Can't open $file_name for writing: $!"; |
111
|
|
|
|
|
|
|
|
112
|
4
|
|
|
|
|
46
|
print FH $content; |
113
|
|
|
|
|
|
|
|
114
|
4
|
|
|
|
|
15
|
return 1; |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
1; |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
__END__ |