File Coverage

blib/lib/Dist/Zilla/Plugin/TestMLIncluder.pm
Criterion Covered Total %
statement 14 44 31.8
branch 1 14 7.1
condition 0 3 0.0
subroutine 5 9 55.5
pod 0 4 0.0
total 20 74 27.0


line stmt bran cond sub pod time code
1             package Dist::Zilla::Plugin::TestMLIncluder;
2             our $VERSION = '0.18';
3              
4 1     1   1907 use Moose;
  1         472051  
  1         8  
5             with 'Dist::Zilla::Role::FileGatherer';
6             with 'Dist::Zilla::Role::FileMunger';
7             with 'Dist::Zilla::Role::PrereqSource';
8              
9 1     1   8800 use Dist::Zilla::File::InMemory;
  1         505018  
  1         52  
10 1     1   757 use IO::All;
  1         12435  
  1         11  
11              
12             # Check that author has TestML enabled:
13             my $testml_root;
14             BEGIN {
15 1     1   193 $testml_root = $ENV{TESTML_ROOT};
16              
17 1 50       539 if (not $ENV{PERL_ZILD_TEST_000_COMPILE_MODULES}) {
18 0 0       0 die <<'...' if not defined $testml_root;
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 0 0     0 -d $testml_root and -f "$testml_root/bin/testml"
29             or die "Invalid TESTML_ROOT '$testml_root'";
30              
31             # Load the local TestML::Compiler:
32 0         0 unshift @INC, "$testml_root/src/testml-compiler-perl5/lib";
33 0         0 require TestML::Compiler;
34             }
35             }
36              
37             # Pull the local Perl5 TestML modules into inc/lib/:
38             sub gather_files {
39 0     0 0   my ($self) = @_;
40              
41 0           for my $file (io("$testml_root/src/perl5/lib")->All_Files) {
42 0           my $path = $file->pathname;
43 0           $path =~ s{\Q$testml_root\E/src/perl5/}{};
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             sub munge_file {
74 0     0 0   my ($self, $file) = @_;
75              
76             # Change shebang lines for TestML .t files:
77 0 0         if ($file->name =~ m{^t/.*\.t$}) {
    0          
78 0           my $content = $file->content;
79 0 0         return unless $content =~ /\A#!.*testml.*/;
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 0         $name =~ s/\.t$// or die;
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             sub add {
138 0     0 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             sub register_prereqs {
149 0     0 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             __PACKAGE__->meta->make_immutable;
160              
161 1     1   9 no Moose;
  1         2  
  1         9  
162              
163             1;