File Coverage

blib/lib/Test/TempDir.pm
Criterion Covered Total %
statement 26 28 92.8
branch 1 2 50.0
condition 3 6 50.0
subroutine 13 14 92.8
pod 4 4 100.0
total 47 54 87.0


line stmt bran cond sub pod time code
1 3     3   109331 use strict;
  3         7  
  3         77  
2 3     3   42 use warnings;
  3         6  
  3         151  
3             package Test::TempDir; # git description: v0.09-5-gd190d22
4             # ABSTRACT: (DEPRECATED) Temporary files support for testing
5              
6             our $VERSION = '0.10';
7              
8 3     3   1090 use File::Temp ();
  3         23897  
  3         60  
9              
10 3     3   983 use Test::TempDir::Factory;
  3         8  
  3         167  
11              
12 3         67 use Sub::Exporter -setup => {
13             exports => [qw(temp_root tempdir tempfile scratch)],
14             groups => {
15             default => [qw(temp_root tempdir tempfile)],
16             },
17 3     3   1300 };
  3         6  
18              
19             our ( $factory, $dir );
20              
21 2   33 2   50 sub _factory { $factory ||= Test::TempDir::Factory->new }
22 4   66 4   84 sub _dir { $dir ||= _factory->create }
23              
24 3     3   7026 END { undef $dir; undef $factory };
  3         414  
25              
26 4     4 1 27 sub temp_root () { _dir->dir }
27              
28 2     2   8 sub _temp_args { DIR => temp_root()->stringify, CLEANUP => 0 }
29             sub _template_args {
30 1 50   1   6 if ( @_ % 2 == 0 ) {
31 1         7 return ( _temp_args, @_ );
32             } else {
33 0         0 return ( $_[0], _temp_args, @_[1 .. $#_] );
34             }
35             }
36              
37 0     0 1 0 sub tempdir { File::Temp::tempdir( _template_args(@_) ) }
38              
39 1     1 1 411 sub tempfile { File::Temp::tempfile( _template_args(@_) ) }
40              
41             sub scratch {
42 1     1 1 14 require Directory::Scratch;
43 1         6 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.10
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 Karen Etheridge Florian Ragwitz
173              
174             =over 4
175              
176             =item *
177              
178             Karen Etheridge <ether@cpan.org>
179              
180             =item *
181              
182             Florian Ragwitz <rafl@debian.org>
183              
184             =back
185              
186             =cut