#!/usr/bin/perl package Test::TempDir; use strict; use warnings; our $VERSION = "0.04"; use File::Temp (); use Test::TempDir::Factory; use Sub::Exporter -setup => { exports => [qw(temp_root tempdir tempfile scratch)], groups => { default => [qw(temp_root tempdir tempfile)], }, }; our ( $factory, $dir ); sub _factory { $factory ||= Test::TempDir::Factory->new } sub _dir { $dir ||= _factory->create } END { undef $dir; undef $factory }; sub temp_root () { _dir->dir } sub _temp_args { DIR => temp_root()->stringify, CLEANUP => 0 } sub _template_args { if ( @_ % 2 == 0 ) { return ( _temp_args, @_ ); } else { return ( $_[0], _temp_args, @_[1 .. $#_] ); } } sub tempdir { File::Temp::tempdir( _template_args(@_) ) } sub tempfile { File::Temp::tempfile( _template_args(@_) ) } sub scratch { require Directory::Scratch; Directory::Scratch->new( _temp_args, @_ ); } __PACKAGE__ __END__ =pod =head1 NAME Test::TempDir - Temporary files support for testing. =head1 SYNOPSIS use Test::TempDir; my $test_tempdir = temp_root(); my ( $fh, $file ) = tempfile(); my $directory_scratch_obj = scratch(); =head1 DESCRIPTION Test::TempDir provides temporary directory creation with testing in mind. The differences between using this and using L are: =over 4 =item * If C is available (writable, creatable, etc) it's preferred over C<$ENV{TMPDIR}> etc. Otherwise a temporary directory will be used. This is C =item * Lockfiles are used on C, to prevent race conditions when running under a parallel test harness. =item * The C is cleaned at the end of a test run, but not if tests failed. =item * C is emptied at the begining of a test run unconditionally. =item * The default policy is not to clean the individual C and C within C, in order to aid in debugging of failed tests. =back =head1 EXPORTS =over 4 =item temp_root The root of the temporary stuff. =item tempfile =item tempdir Wrappers for the L functions of the same name. The default options are changed to use C for C and disable C, but these are overridable. =item scrach Loads L and instantiates a new one, with the same default options as C and C. =back =head1 SEE ALSO L, L, L =head1 VERSION CONTROL This module is maintained using Darcs. You can get the latest version from L, and use C to commit changes. =head1 AUTHOR Yuval Kogman Enothingmuch@woobling.orgE =head1 COPYRIGHT Copyright (c) 2008 Yuval Kogman. All rights reserved This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =cut