File Coverage

blib/lib/Test/TempDir.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1 3     3   92033 use strict;
  3         8  
  3         103  
2 3     3   13 use warnings;
  3         4  
  3         137  
3             package Test::TempDir;
4             # git description: v0.08-13-g8fe7eae
5             $Test::TempDir::VERSION = '0.09';
6             # ABSTRACT: (DEPRECATED) Temporary files support for testing
7              
8 3     3   1643 use File::Temp ();
  3         44726  
  3         72  
9              
10 3     3   872 use Test::TempDir::Factory;
  0            
  0            
11              
12             use Sub::Exporter -setup => {
13             exports => [qw(temp_root tempdir tempfile scratch)],
14             groups => {
15             default => [qw(temp_root tempdir tempfile)],
16             },
17             };
18              
19             our ( $factory, $dir );
20              
21             sub _factory { $factory ||= Test::TempDir::Factory->new }
22             sub _dir { $dir ||= _factory->create }
23              
24             END { undef $dir; undef $factory };
25              
26             sub temp_root () { _dir->dir }
27              
28             sub _temp_args { DIR => temp_root()->stringify, CLEANUP => 0 }
29             sub _template_args {
30             if ( @_ % 2 == 0 ) {
31             return ( _temp_args, @_ );
32             } else {
33             return ( $_[0], _temp_args, @_[1 .. $#_] );
34             }
35             }
36              
37             sub tempdir { File::Temp::tempdir( _template_args(@_) ) }
38              
39             sub tempfile { File::Temp::tempfile( _template_args(@_) ) }
40              
41             sub scratch {
42             require Directory::Scratch;
43             Directory::Scratch->new( _temp_args, @_ );
44             }
45              
46              
47             __PACKAGE__
48              
49             __END__
50              
51             =pod
52              
53             =encoding UTF-8
54              
55             =head1 NAME
56              
57             Test::TempDir - (DEPRECATED) Temporary files support for testing
58              
59             =head1 VERSION
60              
61             version 0.09
62              
63             =head1 DEPRECATION NOTICE
64              
65             There have been numerous issues found with this module, particularly with its
66             use of locks (unreliable, may result in your entire C<$TMPDIR> being deleted)
67             and MSWin32 compatibility. As well, it uses Moose, which is nowadays considered
68             to be heavier than necessary.
69              
70             L<Test::TempDir::Tiny> was written as a replacement. Please use it instead!
71              
72             =head1 SYNOPSIS
73              
74             use Test::TempDir;
75              
76             my $test_tempdir = temp_root();
77              
78             my ( $fh, $file ) = tempfile();
79              
80             my $directory_scratch_obj = scratch();
81              
82             =head1 DESCRIPTION
83              
84             Test::TempDir provides temporary directory creation with testing in mind.
85              
86             The differences between using this and using L<File::Temp> are:
87              
88             =over 4
89              
90             =item *
91              
92             =for stopwords creatable
93              
94             If C<t/tmp> is available (writable, creatable, etc) it's preferred over
95             C<$ENV{TMPDIR}> etc. Otherwise a temporary directory will be used.
96              
97             This is C<temp_root>
98              
99             =item *
100              
101             Lock files are used on C<t/tmp>, to prevent race conditions when running under a
102             parallel test harness.
103              
104             =item *
105              
106             The C<temp_root> is cleaned at the end of a test run, but not if tests failed.
107              
108             =item *
109              
110             C<temp_root> is emptied at the beginning of a test run unconditionally.
111              
112             =item *
113              
114             The default policy is not to clean the individual C<tempfiles> and C<tempdirs>
115             within C<temp_root>, in order to aid in debugging of failed tests.
116              
117             =back
118              
119             =head1 EXPORTS
120              
121             =head2 C<temp_root>
122              
123             The root of the temporary stuff.
124              
125             =head2 C<tempfile>
126              
127             =head2 C<tempdir>
128              
129             Wrappers for the L<File::Temp> functions of the same name.
130              
131             =for stopwords overridable
132              
133             The default options are changed to use C<temp_root> for C<DIR> and disable
134             C<CLEANUP>, but these are overridable.
135              
136             =head2 C<scratch>
137              
138             Loads L<Directory::Scratch> and instantiates a new one, with the same default
139             options as C<tempfile> and C<tempdir>.
140              
141             =head1 SEE ALSO
142              
143             =over 4
144              
145             =item *
146              
147             L<File::Temp>,
148              
149             =item *
150              
151             L<Directory::Scratch>
152              
153             =item *
154              
155             L<Path::Class>
156              
157             =back
158              
159             =head1 AUTHOR
160              
161             יובל קוג'מן (Yuval Kogman) <nothingmuch@woobling.org>
162              
163             =head1 COPYRIGHT AND LICENSE
164              
165             This software is copyright (c) 2006 by יובל קוג'מן (Yuval Kogman).
166              
167             This is free software; you can redistribute it and/or modify it under
168             the same terms as the Perl 5 programming language system itself.
169              
170             =head1 CONTRIBUTORS
171              
172             =for stopwords Yuval Kogman Karen Etheridge Florian Ragwitz
173              
174             =over 4
175              
176             =item *
177              
178             Yuval Kogman <nothingmuch@woobling.org>
179              
180             =item *
181              
182             Karen Etheridge <ether@cpan.org>
183              
184             =item *
185              
186             Florian Ragwitz <rafl@debian.org>
187              
188             =back
189              
190             =cut