File Coverage

blib/lib/Test/TempDir/Handle.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


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