File Coverage

blib/lib/Test/TempDir/Handle.pm
Criterion Covered Total %
statement 38 39 97.4
branch 5 8 62.5
condition n/a
subroutine 14 14 100.0
pod 3 9 33.3
total 60 70 85.7


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.10';
5              
6 5     5   91648 use Moose;
  5         872532  
  5         37  
7 5     5   33103 use MooseX::Types::Path::Class qw(Dir);
  5         812034  
  5         56  
8 5     5   5608 use Moose::Util::TypeConstraints;
  5         10  
  5         65  
9              
10 5     5   9662 use namespace::autoclean 0.08;
  5         174  
  5         45  
11              
12             has dir => (
13             isa => Dir,
14             is => "ro",
15             handles => [qw(file subdir rmtree)],
16             );
17              
18             has lock => (
19             isa => "File::NFSLock",
20             is => "ro",
21             predicate => "has_lock",
22             clearer => "clear_lock",
23             );
24              
25             has cleanup_policy => (
26             isa => enum([ qw(success always never) ]),
27             is => "rw",
28             default => "success",
29             );
30              
31             has test_builder => (
32             isa => "Test::Builder",
33             is => "rw",
34             lazy_build => 1,
35             handles => { test_summary => "summary" },
36             );
37              
38             sub _build_test_builder {
39 4     4   25 require Test::Builder;
40 4         20 Test::Builder->new;
41             }
42              
43             sub failing_tests {
44 4     4 0 8 my $self = shift;
45 4         66 grep { !$_ } $self->test_summary;
  77         254  
46             }
47              
48             sub empty {
49 7     7 1 2373 my $self = shift;
50 7 50       248 return unless -d $self->dir;
51 7         313 $self->rmtree({ keep_root => 1 });
52             }
53              
54             sub delete {
55 6     6 1 11 my $self = shift;
56 6 100       181 return unless -d $self->dir;
57 3         110 $self->rmtree({ keep_root => 0 });
58 3         1401 $self->dir->parent->remove; # rmdir, safe, and we don't care about errors
59             }
60              
61             sub release_lock {
62 7     7 0 13 my $self = shift;
63              
64 7         319 $self->clear_lock;
65              
66             # FIXME always unlock? or allow people to keep the locks around by enrefing them?
67              
68             #if ( $self->has_lock ) {
69             # $self->lock->unlock;
70             # $self->clear_lock;
71             #}
72             }
73              
74             sub DEMOLISH {
75 5     5 0 2917 my $self = shift;
76 5         22 $self->cleanup;
77             }
78              
79             sub cleanup {
80 7     7 1 22 my ( $self, @args ) = @_;
81              
82 7         24 $self->release_lock;
83              
84 7         585 my $policy = "cleanup_policy_" . $self->cleanup_policy;
85              
86 7 50       38 $self->can($policy) or die "Unknown cleanup policy " . $self->cleanup_policy;
87              
88 7         31 $self->$policy(@args);
89             }
90              
91       1 0   sub cleanup_policy_never {}
92              
93             sub cleanup_policy_always {
94 6     6 0 14 my ( $self, @args ) = @_;
95              
96 6         21 $self->delete;
97             }
98              
99             sub cleanup_policy_success {
100 4     4 0 10 my ( $self, @args ) = @_;
101              
102 4 50       17 if ( $self->failing_tests ) {
103 0         0 $self->test_builder->diag("Leaving temporary directory '" . $self->dir . "' due to test fails");
104             } else {
105 4         16 $self->cleanup_policy_always(@args);
106             }
107             }
108              
109             __PACKAGE__
110              
111             __END__
112              
113             =pod
114              
115             =encoding UTF-8
116              
117             =head1 NAME
118              
119             Test::TempDir::Handle - A handle for managing a temporary directory root
120              
121             =head1 VERSION
122              
123             version 0.10
124              
125             =head1 SYNOPSIS
126              
127             use Test::TempDir::Handle;
128              
129             my $h = Test::TempDir::Handle->new( dir => dir("t/tmp") );
130              
131             $h->empty;
132              
133             # ...
134              
135             $h->cleanup; # will delete on success by default
136              
137             =head1 DESCRIPTION
138              
139             This class manages a temporary directory.
140              
141             =head1 ATTRIBUTES
142              
143             =head2 C<dir>
144              
145             The L<Path::Class::Dir> that is being managed.
146              
147             =head2 C<lock>
148              
149             An optional lock object (L<File::NFSLock>). Just kept around for reference counting.
150              
151             =head2 C<cleanup_policy>
152              
153             One of C<success>, C<always> or C<never>.
154              
155             C<success> means that C<cleanup> deletes only if C<test_builder> says the tests
156             have passed.
157              
158             =head2 C<test_builder>
159              
160             The L<Test::Builder> singleton.
161              
162             =head1 METHODS
163              
164             =head2 C<empty>
165              
166             Cleans out the directory but doesn't delete it.
167              
168             =head2 C<delete>
169              
170             Cleans out the directory and removes it.
171              
172             =head2 C<cleanup>
173              
174             Calls C<delete> if the C<cleanup_policy> dictates to do so.
175              
176             This is normally called automatically at destruction.
177              
178             =head1 AUTHOR
179              
180             יובל קוג'מן (Yuval Kogman) <nothingmuch@woobling.org>
181              
182             =head1 COPYRIGHT AND LICENSE
183              
184             This software is copyright (c) 2006 by יובל קוג'מן (Yuval Kogman).
185              
186             This is free software; you can redistribute it and/or modify it under
187             the same terms as the Perl 5 programming language system itself.
188              
189             =cut