blib/lib/Dist/Zilla/App/Command/xtest.pm untested

filter: Statementstmt 0.0% 0 / 68 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 125 CC 12 SCAR 44.5
CC
12
Cov
0%
execute
156.0
opt_spec
2.0
abstract
2.0
CRAP 86.0
1 use strict;
2 use warnings;
3  
4 package Dist::Zilla::App::Command::xtest;
5 # ABSTRACT: run xt tests for your dist
6  
7 our $VERSION = '0.029';
8  
9 use Dist::Zilla::App -command;
10 use Path::Tiny;
11  
12 #pod =head1 SYNOPSIS
13 #pod
14 #pod Run xt tests for your distribution:
15 #pod
16 #pod dzil xtest [ --no-author] [ --no-release ] [ --no-automated ] [ --all ]
17 #pod
18 #pod This runs with AUTHOR_TESTING and RELEASE_TESTING environment variables turned
19 #pod on, so it's like doing this:
20 #pod
21 #pod export AUTHOR_TESTING=1
22 #pod export RELEASE_TESTING=1
23 #pod dzil build
24 #pod rsync -avp My-Project-Version/ .build/
25 #pod cd .build;
26 #pod perl Makefile.PL
27 #pod make
28 #pod prove -l -r xt
29 #pod
30 #pod Except for the fact it's built directly in a subdir of .build (like
31 #pod F<.build/ASDF123>).
32 #pod
33 #pod A build that fails tests will be left behind for analysis, and F<dzil> will
34 #pod exit a non-zero value. If the tests are successful, the build directory will
35 #pod be removed and F<dzil> will exit with status 0.
36 #pod
37 #pod You can also use 'xt' as an alias for 'xtest':
38 #pod
39 #pod dzil xt
40 #pod
41 #pod If you provide one or more filenames on the command line, only
42 #pod those tests will be run (however deeply they are nested).
43 #pod
44 #pod dzil xtest pod-spell.t
45 #pod
46 #pod Arguments are turned into regexp patterns, so you can
47 #pod do any sort of partial match you want:
48 #pod
49 #pod dzil xtest author/ # just the author tests
50 #pod dzil xtest spell # a test with 'spell' in the path
51 #pod
52 #pod There is no need to add anything to F<dist.ini> -- installation of this module
53 #pod is sufficient to make the command available.
54 #pod
55 #pod =cut
56  
57 sub opt_spec {
58 [
59 'author!' => 'enables the AUTHOR_TESTING env variable (default behavior)',
60 { default => 1 }
61 ],
62 [
63 'release!' => 'enables the RELEASE_TESTING env variable (default behavior)',
64 { default => 1 }
65 ],
66 [ 'automated' => 'enables the AUTOMATED_TESTING env variable', { default => 0 } ],
67 [
68 'all' =>
69 'enables the RELEASE_TESTING, AUTOMATED_TESTING and AUTHOR_TESTING env variables',
70 { default => 0 }
71 ],
72 [ 'jobs|j=i' => 'number of parallel test jobs to run', { default => 1 } ];
73 }
74  
75 #pod =head1 OPTIONS
76 #pod
77 #pod =head2 --no-author
78 #pod
79 #pod This will run the test suite without setting AUTHOR_TESTING
80 #pod
81 #pod =head2 --no-release
82 #pod
83 #pod This will run the test suite without setting RELEASE_TESTING
84 #pod
85 #pod =head2 --automated
86 #pod
87 #pod This will run the test suite with AUTOMATED_TESTING=1
88 #pod
89 #pod =head2 --all
90 #pod
91 #pod Equivalent to --release --automated --author
92 #pod
93 #pod =cut
94  
95 sub abstract { 'run xt tests for your dist' }
96  
97 sub command_names {
98 my ($self) = @_;
99 return ( $self->SUPER::command_names, 'xt' );
100 }
101  
102 sub execute {
103 my ( $self, $opt, $arg ) = @_;
104  
105 require App::Prove;
106 require File::pushd;
107  
108 local $ENV{AUTHOR_TESTING} = 1 if $opt->author or $opt->all;
109 local $ENV{RELEASE_TESTING} = 1 if $opt->release or $opt->all;
110 local $ENV{AUTOMATED_TESTING} = 1 if $opt->automated or $opt->all;
111  
112 my ( $target, $latest ) = $self->zilla->ensure_built_in_tmpdir;
113  
114 my $error;
115 {
116 my $wd = File::pushd::pushd($target);
117  
118 my @builders = @{ $self->zilla->plugins_with( -BuildRunner ) };
119 die "no BuildRunner plugins specified" unless @builders;
120 $_->build for @builders;
121  
122 my @v = $self->zilla->logger->get_debug ? ('-v') : ();
123  
124 my $app = App::Prove->new;
125 if ( ref $arg eq 'ARRAY' && @$arg ) {
126 require Path::Iterator::Rule;
127 my $pcr = Path::Iterator::Rule->new->file->and(
128 sub {
129 my $path = $_;
130 return grep { $path =~ /$_/ } @$arg;
131 }
132 );
133 my @t = map { "$_" } $pcr->all('xt');
134 if (@t) {
135 $app->process_args( '-j', $opt->jobs, @v, qw/-r -b/, @t );
136 $error = "Failed xt tests" unless $app->run;
137 }
138 else {
139 $self->log("no xt files found matching: @$arg");
140 }
141 }
142 else {
143 $app->process_args( '-j', $opt->jobs, @v, qw/-r -b xt/ );
144 $error = "Failed xt tests" unless $app->run;
145 }
146 }
147  
148 if ($error) {
149 $self->log($error);
150 $self->log("left failed dist in place at $target");
151 exit 1;
152 }
153 else {
154 $self->log("all's well; removing $target");
155 path($target)->remove_tree( { safe => 0 } );
156 $latest->remove if $latest;
157 }
158  
159 }
160  
161 1;
162  
163 __END__