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