/usr/share/perl5/Test/TempDir/Factory.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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 | package Test::TempDir::Factory;
BEGIN {
$Test::TempDir::Factory::AUTHORITY = 'cpan:NUFFIN';
}
{
$Test::TempDir::Factory::VERSION = '0.08';
}
use Moose;
use Carp qw(croak carp);
use File::Spec;
use File::Temp;
use Path::Class;
use MooseX::Types::Path::Class qw(Dir);
use Test::TempDir::Handle;
use namespace::autoclean;
has lock => (
isa => "Bool",
is => "rw",
default => 1,
);
has lock_opts => (
isa => "HashRef",
is => "rw",
default => sub { { lock_type => "NONBLOCKING" } },
);
has lock_attempts => (
isa => "Int",
is => "rw",
default => 2,
);
has dir_name => (
isa => Dir,
is => "rw",
coerce => 1,
default => sub { dir($ENV{TEST_TEMPDIR} || $ENV{TEST_TMPDIR} || "tmp") },
);
has cleanup_policy => (
isa => "Str",
is => "rw",
default => sub { $ENV{TEST_TEMPDIR_CLEANUP} || "success" },
);
has t_dir => (
isa => Dir,
is => "rw",
coerce => 1,
default => sub { dir("t") },
);
has options => (
isa => "HashRef",
is => "rw",
default => sub { {} },
);
has use_subdir => (
isa => "Bool",
is => "rw",
default => sub { $ENV{TEST_TEMPDIR_USE_SUBDIR} ? 1 : 0 },
);
has subdir_template => (
isa => "Str",
is => "rw",
default => File::Temp::TEMPXXX,
);
has handle_class => (
isa => "ClassName",
is => "rw",
default => "Test::TempDir::Handle",
handles => { new_handle => "new" },
);
has verbose => (
isa => "Bool",
is => "rw",
default => 0,
);
sub create {
my ( $self, @args ) = @_;
my ( $path, $lock ) = $self->create_and_lock( $self->base_path(@args), @args );
my $h = $self->new_handle(
dir => $path,
( defined($lock) ? ( lock => $lock ) : () ),
cleanup_policy => $self->cleanup_policy,
@args,
);
$h->empty;
return $h;
}
sub create_and_lock {
my ( $self, $preferred, @args ) = @_;
if ( $self->use_subdir ) {
$preferred = $self->make_subdir($preferred);
} else {
$preferred->mkpath unless -d $preferred;
}
unless ( $self->lock ) {
return $preferred;
} else {
croak "When locking is enabled you must call create_and_lock in list context" unless wantarray;
if ( my $lock = $self->try_lock($preferred) ) {
return ( $preferred, $lock );
}
return $self->create_and_lock_fallback(@args);
}
}
sub create_and_lock_fallback {
my ( $self, @args ) = @_;
my $base = $self->fallback_base_path;
for ( 1 .. $self->lock_attempts ) {
my $dir = $self->make_subdir($base);
if ( $self->lock ) {
if ( my $lock = $self->try_lock($dir) ) {
return ( $dir, $lock );
}
rmdir $dir;
} else {
return $dir;
}
}
croak "Unable to create locked tempdir";
}
sub try_lock {
my ( $self, $path ) = @_;
return 1 if !$self->lock;
require File::NFSLock;
File::NFSLock->new({
file => $path->stringify . ".lock", # FIXME $path->file ? make sure it's not zapped by empty
%{ $self->lock_opts },
});
}
sub make_subdir {
my ( $self, $dir ) = @_;
$dir->mkpath unless -d $dir;
dir( File::Temp::tempdir( $self->subdir_template, DIR => $dir->stringify ) );
}
sub base_path {
my ( $self, @args ) = @_;
my $dir = $self->dir_name;
return $dir if -d $dir and -w $dir;
my $t = $self->t_dir;
if ( -d $t and -w $t ) {
$dir = $t->subdir($dir);
return $dir if -d $dir && -w $dir or not -e $dir;
}
$self->blurt("$t is not writable, using fallback");
return $self->fallback_base_path(@args);
}
sub blurt {
my ( $self, @blah ) = @_;
if ( $self->can("logger") and my $logger = $self->logger ) {
$logger->warn(@blah);
} else {
return unless $self->verbose;
carp(@blah);
}
}
sub fallback_base_path {
return dir(File::Spec->tmpdir);
}
__PACKAGE__
__END__
=pod
=head1 NAME
Test::TempDir::Factory - A factory for creating L<Test::TempDir::Handle>
objects.
=head1 SYNOPSIS
my $f = Test::TempDir::Factory->new;
my $d = $f->create;
$d->empty;
# ...
$d->cleanup
=head1 DESCRIPTION
This class creates L<Test::TempDir::Handle> objects with the right C<dir>
parameter, taking care of obtaining locks, creating directories, and handling
fallback logic.
=head1 ATTRIBUTES
=over 4
=item C<lock>
Whether or not to enable locking.
Defaults to true.
=item C<lock_opts>
A hash reference to pass to L<File::NFSLock>.
Defaults to C<NONBLOCKING>
=item C<lock_attempts>
How many times to try to create and lock a directory.
Defaults to 2.
=item C<dir_name>
The directory under C<t_dir> to use.
Defaults to C<tmp>
=item C<t_dir>
Defaults to C<t>
=item C<use_subdir>
Whether to always use a temporary subdirectory under the temporary root.
This means that with a C<success> cleanup policy all failures are retained.
When disabled, C<t/tmp> will be used directly as C<temp_root>.
Defaults to true.
=item C<subdir_template>
The template to pass to C<tempdir>. Defaults to C<File::Temp::TEMPXXX>.
=item C<handle_class>
Defaults to L<Test::TempDir::Handle>.
=item C<verbose>
Whether or not to C<carp> diagnostics when falling back.
If you subclass this factory and add a C<logger> method a la L<MooseX::Logger>
then this parameter is ignored and all messages will be C<warn>ed on the
logger.
=back
=head1 METHODS
=over 4
=item C<create>
Create a L<Test::TempDir::Handle> object with a proper C<dir> attribute.
=back
=cut
|