| 1 | package Test::DZil 6.040; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: tools for testing Dist::Zilla plugins | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 51 | ▶ | use Dist::Zilla::Pragmas; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 51 | ▶ | use Params::SomeUtil qw(_HASH0); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 51 | ▶ | use JSON::MaybeXS; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 51 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 51 | ▶ | use Test::Deep (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 51 | ▶ | use YAML::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 51 | ▶ | use Sub::Exporter -setup => { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | exports => [ | ||||||||||||||||||||||||||||
| 14 | is_filelist => | ||||||||||||||||||||||||||||
| 15 | is_yaml => | ||||||||||||||||||||||||||||
| 16 | is_json => | ||||||||||||||||||||||||||||
| 17 | dist_ini => \'_dist_ini', | ||||||||||||||||||||||||||||
| 18 | simple_ini => \'_simple_ini', | ||||||||||||||||||||||||||||
| 19 | Builder => | ||||||||||||||||||||||||||||
| 20 | Minter => | ||||||||||||||||||||||||||||
| 21 | ], | ||||||||||||||||||||||||||||
| 22 | groups => [ default => [ qw(-all) ] ], | ||||||||||||||||||||||||||||
| 23 | 51 | }; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 51 | ▶ | use namespace::autoclean -except => 'import'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | #pod =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 28 | #pod | ||||||||||||||||||||||||||||
| 29 | #pod Test::DZil provides routines for writing tests for Dist::Zilla plugins. | ||||||||||||||||||||||||||||
| 30 | #pod | ||||||||||||||||||||||||||||
| 31 | #pod =cut | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | #pod =func Builder | ||||||||||||||||||||||||||||
| 34 | #pod | ||||||||||||||||||||||||||||
| 35 | #pod =func Minter | ||||||||||||||||||||||||||||
| 36 | #pod | ||||||||||||||||||||||||||||
| 37 | #pod my $tzil = Builder->from_config(...); | ||||||||||||||||||||||||||||
| 38 | #pod | ||||||||||||||||||||||||||||
| 39 | #pod These return class names that subclass L<Dist::Zilla::Dist::Builder> or | ||||||||||||||||||||||||||||
| 40 | #pod L<Dist::Zilla::Dist::Minter>, respectively, with the L<Dist::Zilla::Tester> | ||||||||||||||||||||||||||||
| 41 | #pod behavior added. | ||||||||||||||||||||||||||||
| 42 | #pod | ||||||||||||||||||||||||||||
| 43 | #pod =cut | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 177 | ▶ | sub Builder { | ||||||||||||||||||||||||||
|
Subroutine
Builder: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 46 | 177 | require Dist::Zilla::Tester; | |||||||||||||||||||||||||||
| 47 | 177 | Dist::Zilla::Tester::builder(); | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 1 | ▶ | sub Minter { | ||||||||||||||||||||||||||
|
Subroutine
Minter: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 51 | 1 | require Dist::Zilla::Tester; | |||||||||||||||||||||||||||
| 52 | 1 | Dist::Zilla::Tester::minter(); | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | #pod =func is_filelist | ||||||||||||||||||||||||||||
| 56 | #pod | ||||||||||||||||||||||||||||
| 57 | #pod is_filelist( \@files_we_have, \@files_we_want, $desc ); | ||||||||||||||||||||||||||||
| 58 | #pod | ||||||||||||||||||||||||||||
| 59 | #pod This test assertion compares two arrayrefs of filenames, taking care of slash | ||||||||||||||||||||||||||||
| 60 | #pod normalization and sorting. C<@files_we_have> may also contain objects that | ||||||||||||||||||||||||||||
| 61 | #pod do L<Dist::Zilla::Role::File>. | ||||||||||||||||||||||||||||
| 62 | #pod | ||||||||||||||||||||||||||||
| 63 | #pod =cut | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 39 | ▶ | sub is_filelist { | ||||||||||||||||||||||||||
|
Subroutine
is_filelist: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 66 | 39 | my ($have, $want, $comment) = @_; | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 39 | my @want = @$want; | |||||||||||||||||||||||||||
| 69 | 39 | ▶ | my @have = map { my $str = (blessed $_ and | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($_) and $_->DOES("Dist::Zilla::Role::File")
MC/DC50%1/2
builtin::blessed($_) and $_->DOES("Dist::Zilla::Role::File")
builtin::blessed($_)
$_->DOES("Dist::Zilla::Role::File")
Truth table66%2/3
A and B
A= builtin::blessed($_)B= $_->DOES("Dist::Zilla::Role::File")
| |||||||||||||||||||||||||||||
| 70 | $_->DOES('Dist::Zilla::Role::File')) | ||||||||||||||||||||||||||||
| 71 | ? $_->name | ||||||||||||||||||||||||||||
| 72 | : $_; | ||||||||||||||||||||||||||||
| 73 | 233 | $str =~ s{\\}{/}g; $str } @$have; | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 39 | local $Test::Builder::Level = $Test::Builder::Level + 1; | |||||||||||||||||||||||||||
| 76 | 39 | Test::Deep::cmp_bag(\@have, \@want, $comment); | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | #pod =func is_yaml | ||||||||||||||||||||||||||||
| 80 | #pod | ||||||||||||||||||||||||||||
| 81 | #pod is_yaml( $yaml_string, $want_struct, $comment ); | ||||||||||||||||||||||||||||
| 82 | #pod | ||||||||||||||||||||||||||||
| 83 | #pod This test assertion deserializes the given YAML string and does a | ||||||||||||||||||||||||||||
| 84 | #pod C<L<cmp_deeply|Test::Deep/cmp_deeply>>. | ||||||||||||||||||||||||||||
| 85 | #pod | ||||||||||||||||||||||||||||
| 86 | #pod =cut | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 2 | ▶ | sub is_yaml { | ||||||||||||||||||||||||||
|
Subroutine
is_yaml: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 89 | 2 | my ($yaml, $want, $comment) = @_; | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 2 | ▶ | my $have = YAML::Tiny->read_string($yaml) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | or die "Cannot decode YAML"; | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 2 | local $Test::Builder::Level = $Test::Builder::Level + 1; | |||||||||||||||||||||||||||
| 95 | 2 | Test::Deep::cmp_deeply($have->[0], $want, $comment); | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | #pod =func is_json | ||||||||||||||||||||||||||||
| 99 | #pod | ||||||||||||||||||||||||||||
| 100 | #pod is_json( $json_string, $want_struct, $comment ); | ||||||||||||||||||||||||||||
| 101 | #pod | ||||||||||||||||||||||||||||
| 102 | #pod This test assertion deserializes the given JSON string and does a | ||||||||||||||||||||||||||||
| 103 | #pod C<L<cmp_deeply|Test::Deep/cmp_deeply>>. | ||||||||||||||||||||||||||||
| 104 | #pod | ||||||||||||||||||||||||||||
| 105 | #pod =cut | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 2 | ▶ | sub is_json { | ||||||||||||||||||||||||||
|
Subroutine
is_json: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 108 | 2 | my ($json, $want, $comment) = @_; | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 2 | ▶ | my $have = JSON::MaybeXS->new(ascii => 1)->decode($json) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | or die "Cannot decode JSON"; | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 2 | local $Test::Builder::Level = $Test::Builder::Level + 1; | |||||||||||||||||||||||||||
| 114 | 2 | Test::Deep::cmp_deeply($have, $want, $comment); | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 97 | ▶ | sub _build_ini_builder { | ||||||||||||||||||||||||||
|
Subroutine
_build_ini_builder: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 118 | 97 | my ($starting_core) = @_; | |||||||||||||||||||||||||||
| 119 | 97 | ▶ | $starting_core ||= {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$starting_core ||= {}
MC/DC100%1/1
$starting_core ||= {}
$starting_core
Truth table100%2/2
A
A= $starting_core
| |||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 172 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=15 SCAR=27.2
| |||||||||||||||||||||||||||||
| 122 | 172 | my (@arg) = @_; | |||||||||||||||||||||||||||
| 123 | 172 | ▶ | my $new_core = _HASH0($arg[0]) ? shift(@arg) : {}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 172 | my $core_config = { %$starting_core, %$new_core }; | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 172 | my $config = ''; | |||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 172 | for my $key (sort keys %$core_config) { | |||||||||||||||||||||||||||
| 130 | my @values = ref $core_config->{ $key } | ||||||||||||||||||||||||||||
| 131 | 0 | ? @{ $core_config->{ $key } } | |||||||||||||||||||||||||||
| 132 | 1047 | ▶ | : $core_config->{ $key }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 1047 | $config .= "$key = $_\n" for grep {defined} @values; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 172 | ▶ | $config .= "\n" if length $config; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 172 | for my $line (@arg) { | |||||||||||||||||||||||||||
| 140 | 489 | ▶ | my @plugin = ref $line ? @$line : ($line, {}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | 489 | my $moniker = shift @plugin; | |||||||||||||||||||||||||||
| 142 | 489 | ▶ | my $name = _HASH0($plugin[0]) ? undef : shift @plugin; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | 489 | ▶ | my $payload = shift(@plugin) || {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
shift @plugin || {}
MC/DC100%1/1
shift @plugin || {}
shift @plugin
Truth table100%2/2
A
A= shift @plugin
| |||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 489 | ▶ | Carp::confess("bogus plugin configuration: too many args") if @plugin; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 489 | $config .= '[' . $moniker; | |||||||||||||||||||||||||||
| 148 | 489 | ▶ | $config .= ' / ' . $name if defined $name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | 489 | $config .= "]\n"; | |||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 489 | for my $key (sort keys %$payload) { | |||||||||||||||||||||||||||
| 152 | my @values = ref $payload->{ $key } | ||||||||||||||||||||||||||||
| 153 | 17 | ? @{ $payload->{ $key } } | |||||||||||||||||||||||||||
| 154 | 322 | ▶ | : $payload->{ $key }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 322 | $config .= "$key = $_\n" for grep {defined} @values; | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 489 | $config .= "\n"; | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 172 | return $config; | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | 97 | } | |||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | #pod =func dist_ini | ||||||||||||||||||||||||||||
| 167 | #pod | ||||||||||||||||||||||||||||
| 168 | #pod my $ini_text = dist_ini(\%root_config, @plugins); | ||||||||||||||||||||||||||||
| 169 | #pod | ||||||||||||||||||||||||||||
| 170 | #pod This routine returns a string that could be used to populate a simple | ||||||||||||||||||||||||||||
| 171 | #pod F<dist.ini> file. The C<%root_config> gives data for the "root" section of the | ||||||||||||||||||||||||||||
| 172 | #pod configuration. To provide a line multiple times, provide an arrayref. For | ||||||||||||||||||||||||||||
| 173 | #pod example, the root section could read: | ||||||||||||||||||||||||||||
| 174 | #pod | ||||||||||||||||||||||||||||
| 175 | #pod { | ||||||||||||||||||||||||||||
| 176 | #pod name => 'Dist-Sample', | ||||||||||||||||||||||||||||
| 177 | #pod author => [ | ||||||||||||||||||||||||||||
| 178 | #pod 'J. Smith <jsmith@example.com>', | ||||||||||||||||||||||||||||
| 179 | #pod 'Q. Smith <qsmith@example.com>', | ||||||||||||||||||||||||||||
| 180 | #pod ], | ||||||||||||||||||||||||||||
| 181 | #pod } | ||||||||||||||||||||||||||||
| 182 | #pod | ||||||||||||||||||||||||||||
| 183 | #pod The root section is optional. | ||||||||||||||||||||||||||||
| 184 | #pod | ||||||||||||||||||||||||||||
| 185 | #pod Plugins can be given in a few ways: | ||||||||||||||||||||||||||||
| 186 | #pod | ||||||||||||||||||||||||||||
| 187 | #pod =begin :list | ||||||||||||||||||||||||||||
| 188 | #pod | ||||||||||||||||||||||||||||
| 189 | #pod = C<"PluginMoniker"> | ||||||||||||||||||||||||||||
| 190 | #pod | ||||||||||||||||||||||||||||
| 191 | #pod = C<[ "PluginMoniker" ]> | ||||||||||||||||||||||||||||
| 192 | #pod | ||||||||||||||||||||||||||||
| 193 | #pod These become C<[PluginMoniker]> | ||||||||||||||||||||||||||||
| 194 | #pod | ||||||||||||||||||||||||||||
| 195 | #pod = C<[ "PluginMoniker", "PluginName" ]> | ||||||||||||||||||||||||||||
| 196 | #pod | ||||||||||||||||||||||||||||
| 197 | #pod This becomes C<[PluginMoniker / PluginName]> | ||||||||||||||||||||||||||||
| 198 | #pod | ||||||||||||||||||||||||||||
| 199 | #pod = C<[ "PluginMoniker", { ... } ]> | ||||||||||||||||||||||||||||
| 200 | #pod | ||||||||||||||||||||||||||||
| 201 | #pod = C<[ "PluginMoniker", "PluginName", { ... } ]> | ||||||||||||||||||||||||||||
| 202 | #pod | ||||||||||||||||||||||||||||
| 203 | #pod These use the given hashref as the parameters inside the section, with the same | ||||||||||||||||||||||||||||
| 204 | #pod semantics as the root section. | ||||||||||||||||||||||||||||
| 205 | #pod | ||||||||||||||||||||||||||||
| 206 | #pod =end :list | ||||||||||||||||||||||||||||
| 207 | #pod | ||||||||||||||||||||||||||||
| 208 | #pod =cut | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | 47 | ▶ | sub _dist_ini { | ||||||||||||||||||||||||||
|
Subroutine
_dist_ini: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 211 | 47 | _build_ini_builder; | |||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | #pod =func simple_ini | ||||||||||||||||||||||||||||
| 215 | #pod | ||||||||||||||||||||||||||||
| 216 | #pod This behaves exactly like C<dist_ini>, but it merges any given root config into | ||||||||||||||||||||||||||||
| 217 | #pod a starter config, which means that you can often skip any explicit root config. | ||||||||||||||||||||||||||||
| 218 | #pod The starter config may change slightly over time, but is something like this: | ||||||||||||||||||||||||||||
| 219 | #pod | ||||||||||||||||||||||||||||
| 220 | #pod { | ||||||||||||||||||||||||||||
| 221 | #pod name => 'DZT-Sample', | ||||||||||||||||||||||||||||
| 222 | #pod abstract => 'Sample DZ Dist', | ||||||||||||||||||||||||||||
| 223 | #pod version => '0.001', | ||||||||||||||||||||||||||||
| 224 | #pod author => 'E. Xavier Ample <example@example.org>', | ||||||||||||||||||||||||||||
| 225 | #pod license => 'Perl_5', | ||||||||||||||||||||||||||||
| 226 | #pod copyright_holder => 'E. Xavier Ample', | ||||||||||||||||||||||||||||
| 227 | #pod } | ||||||||||||||||||||||||||||
| 228 | #pod | ||||||||||||||||||||||||||||
| 229 | #pod =cut | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | 50 | ▶ | sub _simple_ini { | ||||||||||||||||||||||||||
|
Subroutine
_simple_ini: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 232 | 50 | _build_ini_builder({ | |||||||||||||||||||||||||||
| 233 | name => 'DZT-Sample', | ||||||||||||||||||||||||||||
| 234 | abstract => 'Sample DZ Dist', | ||||||||||||||||||||||||||||
| 235 | version => '0.001', | ||||||||||||||||||||||||||||
| 236 | author => 'E. Xavier Ample <example@example.org>', | ||||||||||||||||||||||||||||
| 237 | license => 'Perl_5', | ||||||||||||||||||||||||||||
| 238 | copyright_holder => 'E. Xavier Ample', | ||||||||||||||||||||||||||||
| 239 | }); | ||||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | 51 | 1; | |||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | __END__ | ||||||||||||||||||||||||||||