File Coverage

blib/lib/Test/TempDir/Handle.pm
Criterion Covered Total %
statement 36 37 97.3
branch 5 8 62.5
condition n/a
subroutine 14 14 100.0
pod 3 9 33.3
total 58 68 85.2


line stmt bran cond sub pod time code
1             package Test::TempDir::Handle;
2             # ABSTRACT: A handle for managing a temporary directory root
3              
4             our $VERSION = '0.11';
5              
6 5     5   186301 use Moose;
  5         950034  
  5         37  
7 5     5   37849 use MooseX::Types::Path::Class qw(Dir);
  5         262217  
  5         41  
8 5     5   6209 use Moose::Util::TypeConstraints;
  5         13  
  5         43  
9              
10 5     5   11413 use namespace::autoclean 0.08;
  5         142  
  5         39  
11              
12             has dir => (
13             isa => Dir,
14             is => "ro",
15             handles => [qw(file subdir rmtree)],
16             );
17              
18             has cleanup_policy => (
19             isa => enum([ qw(success always never) ]),
20             is => "rw",
21             default => "success",
22             );
23              
24             has test_builder => (
25             isa => "Test::Builder",
26             is => "rw",
27             lazy_build => 1,
28             handles => { test_summary => "summary" },
29             );
30              
31             sub _build_test_builder {
32 4     4   28 require Test::Builder;
33 4         23 Test::Builder->new;
34             }
35              
36             sub failing_tests {
37 4     4 0 8 my $self = shift;
38 4         24 grep { !$_ } $self->test_summary;
  62         832  
39             }
40              
41             sub empty {
42 5     5 1 1376 my $self = shift;
43 5 50       146 return unless -d $self->dir;
44 5         236 $self->rmtree({ keep_root => 1 });
45             }
46              
47             sub delete {
48 6     6 1 11 my $self = shift;
49 6 100       185 return unless -d $self->dir;
50 3         144 $self->rmtree({ keep_root => 0 });
51 3         1324 $self->dir->parent->remove; # rmdir, safe, and we don't care about errors
52             }
53              
54       7 0   sub release_lock {
55             # no more File::NFSLock
56             }
57              
58             sub DEMOLISH {
59 5     5 0 3128 my $self = shift;
60 5         40 $self->cleanup;
61             }
62              
63             sub cleanup {
64 7     7 1 27 my ( $self, @args ) = @_;
65              
66 7         85 $self->release_lock;
67              
68 7         233 my $policy = "cleanup_policy_" . $self->cleanup_policy;
69              
70 7 50       40 $self->can($policy) or die "Unknown cleanup policy " . $self->cleanup_policy;
71              
72 7         40 $self->$policy(@args);
73             }
74              
75       1 0   sub cleanup_policy_never {}
76              
77             sub cleanup_policy_always {
78 6     6 0 20 my ( $self, @args ) = @_;
79              
80 6         23 $self->delete;
81             }
82              
83             sub cleanup_policy_success {
84 4     4 0 14 my ( $self, @args ) = @_;
85              
86 4 50       14 if ( $self->failing_tests ) {
87 0         0 $self->test_builder->diag("Leaving temporary directory '" . $self->dir . "' due to test fails");
88             } else {
89 4         17 $self->cleanup_policy_always(@args);
90             }
91             }
92              
93             __PACKAGE__
94              
95             __END__
96              
97             =pod
98              
99             =encoding UTF-8
100              
101             =head1 NAME
102              
103             Test::TempDir::Handle - A handle for managing a temporary directory root
104              
105             =head1 VERSION
106              
107             version 0.11
108              
109             =head1 SYNOPSIS
110              
111             use Test::TempDir::Handle;
112              
113             my $h = Test::TempDir::Handle->new( dir => dir("t/tmp") );
114              
115             $h->empty;
116              
117             # ...
118              
119             $h->cleanup; # will delete on success by default
120              
121             =head1 DESCRIPTION
122              
123             This class manages a temporary directory.
124              
125             =head1 ATTRIBUTES
126              
127             =head2 C<dir>
128              
129             The L<Path::Class::Dir> that is being managed.
130              
131             =head2 C<lock>
132              
133             No longer used.
134              
135             =head2 C<cleanup_policy>
136              
137             One of C<success>, C<always> or C<never>.
138              
139             C<success> means that C<cleanup> deletes only if C<test_builder> says the tests
140             have passed.
141              
142             =head2 C<test_builder>
143              
144             The L<Test::Builder> singleton.
145              
146             =head1 METHODS
147              
148             =head2 C<empty>
149              
150             Cleans out the directory but doesn't delete it.
151              
152             =head2 C<delete>
153              
154             Cleans out the directory and removes it.
155              
156             =head2 C<cleanup>
157              
158             Calls C<delete> if the C<cleanup_policy> dictates to do so.
159              
160             This is normally called automatically at destruction.
161              
162             =head1 SUPPORT
163              
164             Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=Test-TempDir>
165             (or L<bug-Test-TempDir@rt.cpan.org|mailto:bug-Test-TempDir@rt.cpan.org>).
166              
167             There is also a mailing list available for users of this distribution, at
168             L<http://lists.perl.org/list/perl-qa.html>.
169              
170             There is also an irc channel available for users of this distribution, at
171             L<C<#perl> on C<irc.perl.org>|irc://irc.perl.org/#perl-qa>.
172              
173             =head1 AUTHOR
174              
175             יובל קוג'מן (Yuval Kogman) <nothingmuch@woobling.org>
176              
177             =head1 COPYRIGHT AND LICENCE
178              
179             This software is copyright (c) 2006 by יובל קוג'מן (Yuval Kogman).
180              
181             This is free software; you can redistribute it and/or modify it under
182             the same terms as the Perl 5 programming language system itself.
183              
184             =cut