Csistck/Test/Template.pm

filter: Statementstmt 55.3% 36 / 65 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 72.2% 13 / 18 Podpod 14.2% 1 / 7 totaltotal 45.0% 50 / 111 SCAR 31.7
CC
9
Cov
43%
template_file
30.0
file_check
20.0
file_repair
6.0
CRAP 23.7
« Csistck.pm ↑ Summary Csistck/Term.pm »
1 package Csistck::Test::Template;
2  
3 17 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 17 ▶use base 'Csistck::Test::FileBase';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 17 ▶use Csistck::Oper qw/debug/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 17 ▶use Csistck::Util qw/hash_file hash_string/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 17 our @EXPORT_OK = qw/template/;
12  
13 17 ▶use Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 17 ▶use File::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 17 ▶use Sys::Hostname::Long qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 17 ▶use FindBin;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 17 ▶use Text::Diff ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 2 ▶sub template { Csistck::Test::Template->new(@_); };
Subroutine template: called CC=1 SCAR=0.0
Pod documented
20  
21 8 ▶sub desc { sprintf("Template check for destination %s", shift->dest); }
Subroutine desc: called CC=1 SCAR=0.0
Pod undocumented
22  
23 sub file_check {
24 0 ▶ my $self = shift;
Subroutine file_check: not called CC=4 SCAR=30.0
Pod undocumented
25 0 my $tplout;
26  
27 0 ▶ $self->template_file(\$tplout)
Branch: 1 branch
BranchTrueFalseTotal
$self->template_file(\$tplout) or die "Template file not processed: template=<${\$self->src;}>" 0 0 0
28 0 or die("Template file not processed: template=<${\$self->src}>");
29
30 0 my $hashsrc = hash_string($tplout);
31 0 my $hashdst = hash_file($self->dest);
32
33 0 ▶ return (defined $hashsrc and defined $hashdst and ($hashsrc eq $hashdst));
Condition0%0/6
defined $hashsrc && defined $hashdst && $hashsrc eq $hashdst
!ll&&!rl&&r
000
defined $hashsrc and defined $hashdst
!ll&&!rl&&r
000
MC/DC0%0/3
defined $hashsrc && defined $hashdst && $hashsrc eq $hashdst
defined $hashsrc defined $hashdst $hashsrc eq $hashdst
Truth table0%0/4
A and B && C
A= defined $hashsrcB= defined $hashdstC= $hashsrc eq $hashdst
ABCresult
0XX0
10X0
1100
1111
34 }
35  
36 sub file_repair {
37 0 ▶ my $self = shift;
Subroutine file_repair: not called CC=2 SCAR=17.9
Pod undocumented
38 0 debug(sprintf("Output template: template=<%s> dest=<%s>",
39 $self->src, $self->dest));
40 # TODO tmp file for template
41 0 ▶ open(my $h, '>', $self->dest)
Branch: 1 branch
BranchTrueFalseTotal
open my $h, ">", $self->dest or die "Permission denied writing template" 0 0 0
42 or die("Permission denied writing template");
43 0 $self->template_file($h);
44 0 close($h);
45 0 return 1;
46 }
47  
48 sub file_diff {
49 0 ▶ my $self = shift;
Subroutine file_diff: not called CC=1 SCAR=6.9
Pod undocumented
50 0 my $temp_h;
51 # TODO prune args in common function
52 0 $self->template_file(\$temp_h);
53 0 say(Text::Diff::diff($self->dest, \$temp_h));
54 }
55  
56 # Processing absoulte template name and outputs to reference
57 # variable $out. Die on error or unreadable template file
58 sub template_file {
59 0 ▶ my ($self, $out) = @_;
Subroutine template_file: not called CC=5 SCAR=34.0
Pod undocumented
60
61 0 ▶ die("Invalid template name")
Branch: 1 branch
BranchTrueFalseTotal
unless $self->src =~ m[^[A-Za-z0-9\-\_][A-Za-z0-9/\-\_\.]+$] 0 0 0
62 unless($self->src =~ /^[A-Za-z0-9\-\_][A-Za-z0-9\/\-\_\.]+$/);
63
64 # Build object, finish checks
65 0 my $t = Template->new();
66 0 my $file = get_absolute_template($self->src);
67 0 $self->{hostname} = Sys::Hostname::Long::hostname_long();
68 0 ▶ die("Template not found")
Branch: 1 branch
BranchTrueFalseTotal
unless -e $file 0 0 0
69 if(! -e $file);
70 0 ▶ die("Permission denied reading template")
Branch: 1 branch
BranchTrueFalseTotal
unless -r $file 0 0 0
71 if(! -r $file);
72  
73 # Create Template object, no config for now
74 0 open(my $h, $file);
75 0 ▶ $t->process($h, $self, $out) or die $t->error();
Branch: 1 branch
BranchTrueFalseTotal
$t->process($h, $self, $out) or die $t->error 0 0 0
76 0 close($h);
77 }
78  
79 # Get absoulte path from relative path
80 # TODO error checking
81 sub get_absolute_template {
82 0 ▶ my $template = shift;
Subroutine get_absolute_template: not called CC=1 SCAR=6.9
Pod undocumented
83 0 return join "/", $FindBin::Bin, $template;
84 }
85  
86 17 1;
87 __END__
« Csistck.pm ↑ Summary Csistck/Term.pm »