Tidy/Config.pm

filter: Statementstmt 92.8% 78 / 84 Branchbran 80.9% 34 / 42 Conditioncond 78.5% 11 / 14 MC/DCmcdc 57.1% 4 / 7 Subroutinesub 89.4% 17 / 19 Podpod 0.0% 0 / 11 totaltotal 81.3% 144 / 177 CC 30 SCAR 23.2
CC
30
Cov
88%
new
21.1
adjacency
4.7
addheader
3.0
CRAP 10.2
« Tidy.pm ↑ Summary Tidy/Node.pm »
1 # ABSTRACT: yamltidy config module
2 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use v5.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use experimental qw/ signatures /;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 package YAML::Tidy::Config;
7  
8 8 our $VERSION = '0.007'; # VERSION
9  
10 8 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 use YAML::PP::Common qw/
12 YAML_PLAIN_SCALAR_STYLE YAML_SINGLE_QUOTED_SCALAR_STYLE
13 YAML_DOUBLE_QUOTED_SCALAR_STYLE YAML_LITERAL_SCALAR_STYLE
14 YAML_FOLDED_SCALAR_STYLE
15 YAML_FLOW_SEQUENCE_STYLE YAML_FLOW_MAPPING_STYLE
16 8 ▶/;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 8 my %stylemap = (
18 plain => YAML_PLAIN_SCALAR_STYLE,
19 single => YAML_SINGLE_QUOTED_SCALAR_STYLE,
20 double => YAML_DOUBLE_QUOTED_SCALAR_STYLE,
21 # literal => YAML_LITERAL_SCALAR_STYLE,
22 # folded => YAML_FOLDED_SCALAR_STYLE,
23 );
24  
25 ▶sub new($class, %args) {
Subroutine new: called CC=20 SCAR=30.5
Pod undocumented
26 24 my $yaml;
27 24 my $overridespaces = delete $args{indentspaces};
28 24 ▶ if (defined $args{configdata}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{'configdata'}) { } 0 24 24
29 0 $yaml = $args{configdata};
30 }
31 else {
32 24 my $file = $args{configfile};
33 24 ▶ unless (defined $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $file) 3 21 24
34 3 my ($home) = $class->_homedir;
35 3 my $cwd = $class->_cwd;
36 3 my @candidates = (
37 "$cwd/.yamltidy",
38 "$home/.config/yamltidy/config.yaml",
39 "$home/.yamltidy",
40 );
41 3 for my $c (@candidates) {
42 9 ▶ if (-f $c) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $c) 0 9 9
43 0 $file = $c;
44 0 last;
45 }
46 }
47 }
48 24 ▶ if (defined $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file) 21 3 24
49 21 ▶ open my $fh, '<', $file or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or die $! 0 21 21
50 21 $yaml = do { local $/; <$fh> };
51 21 close $fh;
52 }
53 }
54 24 ▶ unless (defined $yaml) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $yaml) 3 21 24
55 3 $yaml = $class->standardcfg('default');
56 }
57 24 my $cfg;
58 24 my $yp = YAML::PP->new(
59 schema => [qw/ + Merge /],
60 cyclic_refs => 'fatal',
61 );
62 24 $cfg = $yp->load_string($yaml);
63 24 my $v = delete $cfg->{v};
64 24 ▶ my $indent = delete $cfg->{indentation} || {};
Condition50%1/2
delete $cfg->{'indentation'} || {}
l!l
240
MC/DC0%0/1
delete $cfg->{'indentation'} || {}
delete $cfg->{'indentation'}
Truth table50%1/2
A
A= delete $cfg->{'indentation'}
Aresult
11
00
65 24 ▶ $indent->{spaces} = $overridespaces if defined $overridespaces;
Branch: 1 branch
BranchTrueFalseTotal
if defined $overridespaces 0 24 24
66 24 ▶ $indent->{spaces} //= 2; # TODO support keeping original indent
Condition50%1/2
$indent->{"spaces"} //= 2
l!l
240
MC/DC0%0/1
$indent->{"spaces"} //= 2
$indent->{"spaces"}
Truth table50%1/2
A
A= $indent->{"spaces"}
Aresult
11
00
67 24 ▶ $indent->{'block-sequence-in-mapping'} //= 0;
Condition100%2/2
$indent->{"block-sequence-in-mapping"} //= 0
l!l
717
MC/DC100%1/1
$indent->{"block-sequence-in-mapping"} //= 0
$indent->{"block-sequence-in-mapping"}
Truth table100%2/2
A
A= $indent->{"block-sequence-in-mapping"}
Aresult
11
00
68 24 ▶ my $trimtrailing = $cfg->{'trailing-spaces'} || '';
Condition50%1/2
$cfg->{'trailing-spaces'} || ''
l!l
240
MC/DC0%0/1
$cfg->{'trailing-spaces'} || ''
$cfg->{'trailing-spaces'}
Truth table50%1/2
A
A= $cfg->{'trailing-spaces'}
Aresult
11
00
69 24 ▶ if ($trimtrailing eq 'fix') {
Branch: 1 branch
BranchTrueFalseTotal
if ($trimtrailing eq 'fix') { } 23 1 24
70 23 $trimtrailing = 1;
71 }
72 else {
73 1 $trimtrailing = 0;
74 }
75 24 my $scalarstyle = { default => YAML_PLAIN_SCALAR_STYLE };
76 24 ▶ if (my $scalar = delete $cfg->{'scalar-style'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $scalar = delete $cfg->{"scalar-style"}) 24 0 24
77 24 my $default = $scalar->{default};
78 24 ▶ $scalarstyle->{default} = $default ? $stylemap{ $default } : undef;
Branch: 1 branch
BranchTrueFalseTotal
$default ? : 6 18 24
79 }
80  
81 24 delete @args{qw/ configfile configdata /};
82 24 ▶ if (my @unknown = keys %args) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@unknown) = keys %args) 1 23 24
83 1 die "Unknown configuration parameters: @unknown";
84 }
85 my $self = bless {
86 version => $v,
87 indentation => $indent,
88 trimtrailing => $trimtrailing,
89 header => delete $cfg->{header} // 'keep',
90 footer => delete $cfg->{footer} // 'keep',
91 23 ▶ adjacency => delete $cfg->{adjacency} // 'keep',
Condition100%6/6
delete $cfg->{"header"} // "keep"
l!l
1013
delete $cfg->{"footer"} // "keep"
l!l
815
delete $cfg->{"adjacency"} // "keep"
l!l
221
MC/DC100%1/1
delete $cfg->{"header"} // "keep"
delete $cfg->{"header"}
MC/DC100%1/1
delete $cfg->{"footer"} // "keep"
delete $cfg->{"footer"}
MC/DC100%1/1
delete $cfg->{"adjacency"} // "keep"
delete $cfg->{"adjacency"}
Truth table100%2/2
A
A= delete $cfg->{"header"}
Aresult
11
00
Truth table100%2/2
A
A= delete $cfg->{"footer"}
Aresult
11
00
Truth table100%2/2
A
A= delete $cfg->{"adjacency"}
Aresult
11
00
92 scalar_style => $scalarstyle,
93 }, $class;
94 23 return $self;
95 }
96  
97 sub _cwd {
98 0 ▶ return Cwd::cwd();
Subroutine _cwd: not called CC=1 SCAR=6.9
99 }
100  
101 ▶sub _homedir($class) {
Subroutine _homedir: not called CC=1 SCAR=6.9
102 0 return <~>;
103 }
104  
105 ▶sub indent($self) {
Subroutine indent: called CC=1 SCAR=0.0
Pod undocumented
106 25445 return $self->{indentation}->{spaces};
107 }
108 ▶sub indent_seq_in_map($self) {
Subroutine indent_seq_in_map: called CC=1 SCAR=0.0
Pod undocumented
109 370 return $self->{indentation}->{'block-sequence-in-mapping'};
110 }
111  
112 ▶sub trimtrailing($self) {
Subroutine trimtrailing: called CC=1 SCAR=0.0
Pod undocumented
113 24473 return $self->{trimtrailing};
114 }
115  
116 ▶sub addheader($self) {
Subroutine addheader: called CC=3 SCAR=11.0
Pod undocumented
117 2043 my $header = $self->{header};
118 2043 ▶ return 0 if $header eq 'keep';
Branch: 1 branch
BranchTrueFalseTotal
if $header eq "keep" 1365 678 2043
119 678 ▶ return $header ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$header ? : 339 339 678
120 }
121  
122 ▶sub addfooter($self) {
Subroutine addfooter: called CC=3 SCAR=11.0
Pod undocumented
123 3200 my $footer = $self->{footer};
124 3200 ▶ return 0 if $footer eq 'keep';
Branch: 1 branch
BranchTrueFalseTotal
if $footer eq "keep" 2138 1062 3200
125 1062 ▶ return $footer ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$footer ? : 531 531 1062
126 }
127  
128 ▶sub adjacency($self) {
Subroutine adjacency: called CC=3 SCAR=15.4
Pod undocumented
129 749 my $adjacency = $self->{adjacency};
130 749 ▶ return undef if $adjacency eq 'keep';
Branch: 1 branch
BranchTrueFalseTotal
if $adjacency eq "keep" 749 0 749
131 0 ▶ return $adjacency ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$adjacency ? : 0 0 0
132 }
133  
134 ▶sub removeheader($self) {
Subroutine removeheader: called CC=3 SCAR=11.0
Pod undocumented
135 955 my $header = $self->{header};
136 955 ▶ return 0 if $header eq 'keep';
Branch: 1 branch
BranchTrueFalseTotal
if $header eq "keep" 637 318 955
137 318 ▶ return $header ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$header ? : 159 159 318
138 }
139  
140 ▶sub removefooter($self) {
Subroutine removefooter: called CC=3 SCAR=11.0
Pod undocumented
141 147 my $footer = $self->{footer};
142 147 ▶ return 0 if $footer eq 'keep';
Branch: 1 branch
BranchTrueFalseTotal
if $footer eq "keep" 93 54 147
143 54 ▶ return $footer ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$footer ? : 27 27 54
144 }
145  
146 ▶sub default_scalar_style($self) {
Subroutine default_scalar_style: called CC=1 SCAR=0.0
Pod undocumented
147 10419 return $self->{scalar_style}->{default};
148 }
149  
150 sub standardcfg {
151 3 ▶ my $yaml = <<'EOM';
Subroutine standardcfg: called CC=1 SCAR=0.0
Pod undocumented
152 ---
153 v: v0.1
154 indentation:
155 spaces: 2
156 block-sequence-in-mapping: 0
157 trailing-spaces: fix
158 header: true
159 scalar-style:
160 default: plain
161 adjacency: 0
162 EOM
163 }
164  
165 8 1;
« Tidy.pm ↑ Summary Tidy/Node.pm »