File Coverage

blib/lib/Dist/Zilla/Tester.pm
Criterion Covered Total %
statement 93 111 83.7
branch 2 8 25.0
condition n/a
subroutine 31 36 86.1
pod 1 12 8.3
total 127 167 76.0


line stmt bran cond sub pod time code
1             # ABSTRACT: a testing-enabling stand-in for Dist::Zilla
2              
3             use Moose;
4 49     49   105136 extends 'Dist::Zilla::Dist::Builder';
  49         17930477  
  49         385  
5              
6             use Dist::Zilla::Pragmas;
7 49     49   368206  
  49         125  
  49         463  
8             use autodie;
9 49     49   24406 use Dist::Zilla::Chrome::Test;
  49         530210  
  49         322  
10 49     49   365012 use File::pushd ();
  49         239  
  49         2601  
11 49     49   28480 use File::Spec;
  49         885396  
  49         1518  
12 49     49   401 use File::Temp;
  49         130  
  49         948  
13 49     49   265 use Dist::Zilla::Path;
  49         110  
  49         4395  
14 49     49   21979  
  49         176  
  49         278  
15             use Sub::Exporter::Util ();
16 49     49   17463 use Sub::Exporter -setup => {
  49         112  
  49         4995  
17             exports => [
18             Builder => sub { $_[0]->can('builder') },
19 1         270 Minter => sub { $_[0]->can('minter') },
20 1         25 ],
21 49         503  
22             groups => [ default => [ qw(Builder Minter) ] ],
23             };
24 49     49   306  
  49         98  
25             use namespace::autoclean -except => 'import';
26 49     49   25486  
  49         111  
  49         282  
27             my ($self, @arg) = @_;
28              
29 1     1 1 5 # The only thing using a local time zone should be NextRelease. Normally it
30             # defaults to "local," but since some users won't have an automatically
31             # determinable time zone, we'll switch to not-local times for testing.
32             # -- rjbs, 2015-11-26
33             local $Dist::Zilla::Plugin::NextRelease::DEFAULT_TIME_ZONE = 'GMT';
34              
35 1         2 return $self->builder->from_config(@arg);
36             }
37 1         6  
38              
39              
40 165     165 0 2432 {
41             package
42 1     1 0 15 Dist::Zilla::Tester::_Role;
43              
44             use Moose::Role;
45              
46             has tempdir_root => (
47             is => 'rw', isa => 'Str|Undef',
48 49     49   11570 writer => '_set_tempdir_root',
  49         137  
  49         581  
49             );
50             has tempdir_obj => (
51             is => 'ro', isa => 'File::Temp::Dir',
52             clearer => '_clear_tempdir_obj',
53             writer => '_set_tempdir_obj',
54             );
55              
56             around DEMOLISH => sub {
57             my $orig = shift;
58             my $self = shift;
59              
60       10 0   # File::Temp deletes the directory when it goes out of scope
61             $self->_clear_tempdir_obj;
62              
63             rmdir $self->tempdir_root if $self->tempdir_root;
64             return $self->$orig(@_);
65             };
66              
67             has tempdir => (
68             is => 'ro',
69             writer => '_set_tempdir',
70             init_arg => undef,
71             );
72              
73             my ($self) = @_;
74             $self->chrome->logger->clear_events;
75             }
76              
77             my ($self) = @_;
78             $self->chrome->logger->events;
79 0     0 0 0 }
80 0         0  
81             my ($self) = @_;
82             [ map {; $_->{message} } @{ $self->chrome->logger->events } ];
83             }
84 0     0 0 0  
85 0         0 my ($self, $filename) = @_;
86              
87             Dist::Zilla::Path::path(
88             $self->tempdir->child($filename)
89 27     27 0 50581 )->slurp_utf8;
90 27         151 }
  189         842  
  27         1090  
91              
92             my ($self, $filename) = @_;
93              
94 108     108 0 136091 Dist::Zilla::Path::path(
95             $self->tempdir->child($filename)
96 108         3943 )->slurp_raw;
97             }
98              
99              
100             no Moose::Role;
101             }
102 0     0 0 0  
103             {
104 0         0  
105             use Moose;
106             extends 'Dist::Zilla::Dist::Builder';
107             with 'Dist::Zilla::Tester::_Role';
108              
109 145     145   4815 use File::Copy::Recursive 0.41 qw(dircopy);
110             use Dist::Zilla::Path;
111 49     49   302466  
  49         139  
  49         319  
112             our $Log_Events = [];
113             return @{ $Log_Events }
114             }
115              
116             around from_config => sub {
117 49     49   14852 my ($orig, $self, $arg, $tester_arg) = @_;
  49         128  
  49         351  
118              
119             confess "dist_root required for from_config" unless $arg->{dist_root};
120              
121 49     49   373481 my $source = $arg->{dist_root};
  49         316883  
  49         3724  
122 49     49   420  
  49         129  
  49         403  
123             my $tempdir_root = exists $tester_arg->{tempdir_root}
124             ? $tester_arg->{tempdir_root}
125             : 'tmp';
126 0     0 0 0  
  0         0  
127             mkdir $tempdir_root if defined $tempdir_root and not -d $tempdir_root;
128              
129             my $tempdir_obj = File::Temp->newdir(
130             CLEANUP => 1,
131             (defined $tempdir_root ? (DIR => $tempdir_root) : ()),
132             );
133              
134             my $tempdir = path( path($tempdir_obj)->absolute) ;
135              
136             my $root = $tempdir->child('source');
137             $root->mkpath;
138              
139             dircopy($source, $root);
140              
141             if ($tester_arg->{also_copy}) {
142             while (my ($src, $dest) = each %{ $tester_arg->{also_copy} }) {
143             dircopy($src, $tempdir->child($dest));
144             }
145             }
146              
147             if (my $files = $tester_arg->{add_files}) {
148             while (my ($name, $content) = each %$files) {
149             die "Unix path '$name' does not seem to be portable to the current OS"
150             if !unix_path_seems_portable($name);
151             my $fn = $tempdir->child($name);
152             $fn->parent->mkpath;
153             Dist::Zilla::Path::path($fn)->spew_utf8($content);
154             }
155             }
156              
157             local $arg->{dist_root} = "$root";
158             local $arg->{chrome} = Dist::Zilla::Chrome::Test->new;
159              
160             $Log_Events = $arg->{chrome}->logger->events;
161              
162             local @INC = @INC;
163              
164             my $had_dot;
165             if ($INC[-1] eq '.') {
166             $had_dot = 1;
167             pop @INC;
168             }
169              
170             @INC = map {; ref($_) ? $_ : File::Spec->rel2abs($_) } @INC;
171              
172             push @INC, '.' if $had_dot;
173              
174             # We do this so that . in @INC will find plugins like [=inc::YourFace]
175             # -- rjbs, 2016-04-24
176             my $wd = File::pushd::pushd($arg->{dist_root});
177              
178              
179             local $ENV{DZIL_GLOBAL_CONFIG_ROOT};
180             $ENV{DZIL_GLOBAL_CONFIG_ROOT} = $tester_arg->{global_config_root}
181             if defined $tester_arg->{global_config_root};
182              
183             my $zilla = $self->$orig($arg);
184              
185             $zilla->_set_tempdir_root($tempdir_root);
186             $zilla->_set_tempdir_obj($tempdir_obj);
187             $zilla->_set_tempdir($tempdir);
188              
189             return $zilla;
190             };
191              
192             around build_in => sub {
193             my ($orig, $self, $target) = @_;
194              
195             # Sometimes, we can't get a local time zone. When that happens, we're just
196             # going to pretend to be in UTC. We don't do this during actual runtime
197             # because the user can fix their own environment, but we'll let them do
198             # that after they get the software installed. -- rjbs, 2020-01-26
199             my $ok = eval { DateTime::TimeZone->new(name => 'local'); 1 };
200             local $ENV{TZ} = $ok ? $ENV{TZ} : 'UTC';
201              
202             # XXX: We *must eliminate* the need for this! It's only here because right
203             # now building a dist with (root <> cwd) doesn't work. -- rjbs, 2010-03-08
204             my $wd = File::pushd::pushd($self->root);
205              
206             $target ||= do {
207             my $target = path($self->tempdir)->child('build');
208             $target->mkpath;
209             $target;
210             };
211              
212             return $self->$orig($target);
213             };
214              
215             around ['test', 'release'] => sub {
216             my ($orig, $self) = @_;
217              
218             # XXX: We *must eliminate* the need for this! It's only here because right
219             # now building a dist with (root <> cwd) doesn't work. -- rjbs, 2010-03-08
220             my $wd = File::pushd::pushd($self->root);
221              
222             return $self->$orig;
223             };
224              
225             no Moose;
226              
227             return 1 if $^O eq "linux"; # this check only makes sense on non-unixes
228              
229             my ($unix_path) = @_;
230              
231             # split the $unix_path into 3 strings: $volume, $directories, $file; with:
232             my @native_parts = File::Spec->splitpath($unix_path); # current OS rules
233             my @unix_parts = File::Spec::Unix->splitpath($unix_path); # unix rules
234             return unless join(qq{\0}, @native_parts) eq join(qq{\0}, @unix_parts);
235              
236             # split the $directories string into a list of the sub-directories; with:
237             my @native_dirs = File::Spec->splitdir($native_parts[1]); # current OS rules
238 49     49   62056 my @unix_dirs = File::Spec::Unix->splitdir($unix_parts[1]); # unix rules
  49         130  
  49         383  
239             return unless join(qq{\0}, @native_dirs) eq join(qq{\0}, @unix_dirs);
240              
241 283 50   283 0 1734 return 1;
242             }
243 0         0  
244             {
245              
246 0         0 use Moose;
247 0         0 extends 'Dist::Zilla::Dist::Minter';
248 0 0       0 with 'Dist::Zilla::Tester::_Role';
249              
250             use File::Copy::Recursive 0.41 qw(dircopy);
251 0         0 use Dist::Zilla::Path;
252 0         0  
253 0 0       0 our $Log_Events = [];
254             return @{ $Log_Events }
255 0         0 }
256              
257             my ($self) = @_;
258              
259             my $name = $self->name;
260             my $dir = $self->tempdir->child('mint')->absolute;
261              
262 49     49   22071 $self->log_fatal("$dir already exists") if -e $dir;
  49         146  
  49         279  
263              
264             return $dir;
265             }
266 49     49   327632  
  49         817  
  49         3997  
267 49     49   319 my ($self, $dir, $arg) = @_;
  49         124  
  49         257  
268              
269             my $config_base = path($dir)->child('config');
270              
271 0     0 0 0 my $stash_registry = {};
  0         0  
272              
273             require Dist::Zilla::MVP::Assembler::GlobalConfig;
274             require Dist::Zilla::MVP::Section;
275 1     1   4 my $assembler = Dist::Zilla::MVP::Assembler::GlobalConfig->new({
276             chrome => $arg->{chrome},
277 1         22 stash_registry => $stash_registry,
278 1         31 section_class => 'Dist::Zilla::MVP::Section', # make this DZMA default
279             });
280 1 50       171  
281             require Dist::Zilla::MVP::Reader::Finder;
282 1         64 my $reader = Dist::Zilla::MVP::Reader::Finder->new;
283              
284             my $seq = $reader->read_config($config_base, { assembler => $assembler });
285              
286 1     1   6 return $stash_registry;
287             }
288 1         5  
289             around _new_from_profile => sub {
290 1         85 my ($orig, $self, $profile_data, $arg, $tester_arg) = @_;
291              
292 1         600 # Sometimes, we can't get a local time zone. When that happens, we're just
293 1         580 # going to pretend to be in UTC. We don't do this during actual runtime
294             # because the user can fix their own environment, but we'll let them do
295             # that after they get the software installed. -- rjbs, 2020-01-26
296 1         48 my $ok = eval { DateTime::TimeZone->new(name => 'local'); 1 };
297             local $ENV{TZ} = $ok ? $ENV{TZ} : 'UTC';
298              
299             my $tempdir_root = exists $tester_arg->{tempdir_root}
300 1         522 ? $tester_arg->{tempdir_root}
301 1         44 : 'tmp';
302              
303 1         10 mkdir $tempdir_root if defined $tempdir_root and not -d $tempdir_root;
304              
305 1         631 my $tempdir_obj = File::Temp->newdir(
306             CLEANUP => 1,
307             (defined $tempdir_root ? (DIR => $tempdir_root) : ()),
308             );
309             my $tempdir = path($tempdir_obj)->absolute;
310              
311             local $arg->{chrome} = Dist::Zilla::Chrome::Test->new;
312             $Log_Events = $arg->{chrome}->logger->events;
313              
314             local @INC = map {; ref($_) ? $_ : File::Spec->rel2abs($_) } @INC;
315              
316             my $global_config_root = path($tester_arg->{global_config_root})->absolute;
317              
318             local $ENV{DZIL_GLOBAL_CONFIG_ROOT} = $global_config_root;
319              
320             my $global_stashes = $self->_setup_global_config(
321             $global_config_root,
322             { chrome => $arg->{chrome} },
323             );
324              
325             local $arg->{_global_stashes} = $global_stashes;
326              
327             my $zilla = $self->$orig($profile_data, $arg);
328              
329             $zilla->_set_tempdir_root($tempdir_root);
330             $zilla->_set_tempdir_obj($tempdir_obj);
331             $zilla->_set_tempdir($tempdir);
332              
333             return $zilla;
334             };
335              
336             no Moose; # XXX: namespace::autoclean caused problems -- rjbs, 2011-08-19
337             __PACKAGE__->meta->make_immutable;
338             1;
339              
340              
341             =pod
342              
343             =encoding UTF-8
344              
345             =head1 NAME
346              
347             Dist::Zilla::Tester - a testing-enabling stand-in for Dist::Zilla
348              
349             =head1 VERSION
350              
351             version 6.028
352              
353             =head1 PERL VERSION
354              
355             This module should work on any version of perl still receiving updates from
356 49     49   53777 the Perl 5 Porters. This means it should work on any version of perl released
  49         133  
  49         246  
357             in the last two to three years. (That is, if the most recently released
358             version is v5.40, then this module should work on both v5.40 and v5.38.)
359              
360             Although it may work on older versions of perl, no guarantee is made that the
361             minimum required version will not be increased. The version may be increased
362             for any reason, and there is no promise that patches will be accepted to lower
363             the minimum required perl.
364              
365             =head1 AUTHOR
366              
367             Ricardo SIGNES 😏 <cpan@semiotic.systems>
368              
369             =head1 COPYRIGHT AND LICENSE
370              
371             This software is copyright (c) 2022 by Ricardo SIGNES.
372              
373             This is free software; you can redistribute it and/or modify it under
374             the same terms as the Perl 5 programming language system itself.
375              
376             =cut