blib/lib/Dist/Zilla/Plugin/TestMLIncluder.pm

filter: Statementstmt 41.1% 21 / 51 Branchbran 7.1% 1 / 14 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 55.5% 5 / 9 Podpod 0.0% 0 / 4 totaltotal 32.5% 27 / 83 CC 10 SCAR 26.7
CC
10
Cov
32%
munge_file
30.0
BEGIN
17.8
gather_files
6.0
CRAP 14.4
↑ Summary
1 package Dist::Zilla::Plugin::TestMLIncluder;
2 1 our $VERSION = '0.19';
3  
4 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 with 'Dist::Zilla::Role::FileGatherer';
6 1 with 'Dist::Zilla::Role::FileMunger';
7 1 with 'Dist::Zilla::Role::PrereqSource';
8  
9 1 ▶use Dist::Zilla::File::InMemory;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use IO::All;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # Check that author has TestML enabled:
13 1 my $testml_root;
14 1 ▶BEGIN {
Subroutine BEGIN: called CC=5 SCAR=28.8
15 1 $testml_root = $ENV{TESTML_ROOT};
16  
17 1 ▶ if (not $ENV{PERL_ZILD_TEST_000_COMPILE_MODULES}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $ENV{"PERL_ZILD_TEST_000_COMPILE_MODULES"}) 0 1 1
18 0 ▶ die <<'...' if not defined $testml_root;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $testml_root 0 0 0
19 --------------------------------------------------------------------------------
20 TESTML_ROOT is not set in your environment.
21 This means TestML is not set up properly.
22  
23 For more information, see:
24 https://github.com/testml-lang/testml/wiki/publishing-cpan-modules-with-testml-tests
25 --------------------------------------------------------------------------------
26 ...
27  
28 0 ▶ -d $testml_root and -f "$testml_root/bin/testml"
Branch: 1 branch
BranchTrueFalseTotal
-d $testml_root and -f "$testml_root/bin/testml" or die "Invalid TESTML_ROOT '${testml_root}'" 0 0 0
Condition0%0/3
-d $testml_root and -f "$testml_root/bin/testml"
!ll&&!rl&&r
000
MC/DC0%0/2
-d $testml_root and -f "$testml_root/bin/testml"
-d $testml_root -f "$testml_root/bin/testml"
Truth table0%0/3
A and B
A= -d $testml_rootB= -f "$testml_root/bin/testml"
ABresult
0X0
100
111
29 or die "Invalid TESTML_ROOT '$testml_root'";
30  
31 # Load the local TestML::Compiler:
32 0 unshift @INC, "$testml_root/src/testml-compiler-perl/lib";
33 0 require TestML::Compiler;
34 }
35 }
36  
37 # Pull the local Perl TestML modules into inc/lib/:
38 0 ▶sub gather_files {
Subroutine gather_files: not called CC=2 SCAR=17.9
Pod undocumented
39 0 my ($self) = @_;
40  
41 0 for my $file (io("$testml_root/src/perl/lib")->All_Files) {
42 0 my $path = $file->pathname;
43 0 $path =~ s{\Q$testml_root\E/src/perl/}{};
44 0 $self->add("inc/$path", $file->all);
45 }
46  
47 # Also add the user-side-only TestML runner bin: 'testml-cpan':
48 0 my $testml_cpan = <<'...';
49 #!/usr/bin/perl
50  
51 use lib 't', 'inc/lib';
52  
53 use TestML::Run::TAP;
54  
55 my $testml_file = $ARGV[-1];
56 my $test_file = $testml_file;
57  
58 $test_file =~ s/(.*)\.t$/inc\/$1.tml.lingy/
59 or die "Error with '$testml_file'. testml-cpan only works with *.t files.";
60 -e $test_file
61 or die "TestML file '$testml_file' not compiled as '$test_file'";
62  
63 TestML::Run::TAP->run($test_file);
64 ...
65  
66 0 $self->add(
67 "inc/bin/testml-cpan",
68 $testml_cpan,
69 );
70 }
71  
72 # Modify TestML .t files and the Makefile.PL (on the user side):
73 0 ▶sub munge_file {
Subroutine munge_file: not called CC=5 SCAR=34.0
Pod undocumented
74 0 my ($self, $file) = @_;
75  
76 # Change shebang lines for TestML .t files:
77 0 ▶ if ($file->name =~ m{^t/.*\.t$}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($file->name =~ m[^t/.*\.t$]) { } 0 0 0
elsif ($file->name eq 'Makefile.PL') { } 0 0 0
78 0 my $content = $file->content;
79 0 ▶ return unless $content =~ /\A#!.*testml.*/;
Branch: 1 branch
BranchTrueFalseTotal
unless $content =~ /\A#!.*testml.*/ 0 0 0
80 0 $content =~ s{\A#!.*testml.*}{#!inc/bin/testml-cpan};
81 0 $file->content($content);
82  
83 # Then precompile the TestML .t files to Lingy/JSON:
84 0 my $compiler = TestML::Compiler->new;
85 0 my $lingy = $compiler->compile($content, $file->name);
86 0 my $name = $file->name;
87 0 ▶ $name =~ s/\.t$// or die;
Branch: 1 branch
BranchTrueFalseTotal
$name =~ s/\.t$// or die 0 0 0
88 0 $name = "inc/$name.tml.lingy";
89  
90 0 $self->add($name => $lingy);
91 }
92 # Add a footer to Makefile.PL to use the user's perl in testml-cpan:
93 elsif ($file->name eq 'Makefile.PL') {
94 0 my $content = $file->content;
95 0 $content .= <<'...';
96  
97 use Config;
98 use File::Find;
99  
100 my $file = 'inc/bin/testml-cpan';
101 open IN, '<', $file or die "Can't open '$file' for input";
102 my @bin = <IN>;
103 close IN;
104  
105 shift @bin;
106 unshift @bin, "#!$Config{perlpath}\n";
107 open OUT, '>', $file or die "Can't open '$file' for output";
108 print OUT @bin;
109 close OUT;
110  
111 chmod 0755, 'inc/bin/testml-cpan';
112  
113 if ($^O eq 'MSWin32') {
114 my $file = 'inc/bin/testml-cpan.cmd';
115 open OUT, '>', $file or die "Can't open '$file' for output";
116 print OUT 'if exist "%~dpn0" perl %0 %*', "\r\n";
117 close OUT;
118  
119 find sub {
120 return unless -f && /\.t$/;
121 my $file = $_;
122 open IN, '<', $file or die "Can't open '$file' for input";
123 return unless <IN> =~ /testml-cpan/;
124 my $text = do {local $/; <IN>};
125 close IN;
126 open OUT, '>', $file or die "Can't open '$file' for output";
127 print OUT '#!inc\\bin\\testml-cpan', "\r\n";
128 print OUT $text;
129 close OUT;
130 }, 't';
131 }
132 ...
133 0 $file->content($content);
134 }
135 }
136  
137 0 ▶sub add {
Subroutine add: not called CC=1 SCAR=6.9
Pod undocumented
138 0 my ($self, $name, $content) = @_;
139  
140 0 $self->add_file(
141 Dist::Zilla::File::InMemory->new(
142 name => $name,
143 content => $content,
144 )
145 );
146 }
147  
148 0 ▶sub register_prereqs {
Subroutine register_prereqs: not called CC=1 SCAR=6.9
Pod undocumented
149 0 my $self = shift;
150 0 $self->zilla->register_prereqs(
151 {
152 type => 'requires',
153 phase => 'test',
154 },
155 'JSON::PP' => 0,
156 );
157 }
158  
159 1 __PACKAGE__->meta->make_immutable;
160  
161 1 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
162  
163 1 1;
↑ Summary