File: TempDir.pm

package info (click to toggle)
libtest-tempdir-perl 0.11-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 324 kB
  • sloc: perl: 422; makefile: 2
file content (197 lines) | stat: -rw-r--r-- 4,049 bytes parent folder | download | duplicates (2)
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
use strict;
use warnings;
package Test::TempDir; # git description: v0.10-11-g629f4d3
# ABSTRACT: (DEPRECATED) Temporary files support for testing

our $VERSION = '0.11';

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

=encoding UTF-8

=head1 NAME

Test::TempDir - (DEPRECATED) Temporary files support for testing

=head1 VERSION

version 0.11

=head1 DEPRECATION NOTICE

There have been numerous issues found with this module, particularly with its
use of locks (unreliable, may result in your entire C<$TMPDIR> being deleted)
and MSWin32 compatibility. As well, it uses Moose, which is nowadays considered
to be heavier than necessary.

L<Test::TempDir::Tiny> was written as a replacement. Please use it instead!

=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<File::Temp> are:

=over 4

=item *

=for stopwords creatable

If C<t/tmp> is available (writable, creatable, etc) it's preferred over
C<$ENV{TMPDIR}> etc. Otherwise a temporary directory will be used.

This is C<temp_root>

=item *

Lock files are used on C<t/tmp>, to prevent race conditions when running under a
parallel test harness.

=item *

The C<temp_root> is cleaned at the end of a test run, but not if tests failed.

=item *

C<temp_root> is emptied at the beginning of a test run unconditionally.

=item *

The default policy is not to clean the individual C<tempfiles> and C<tempdirs>
within C<temp_root>, in order to aid in debugging of failed tests.

=back

=head1 EXPORTS

=head2 C<temp_root>

The root of the temporary stuff.

=head2 C<tempfile>

=head2 C<tempdir>

Wrappers for the L<File::Temp> functions of the same name.

=for stopwords overridable

The default options are changed to use C<temp_root> for C<DIR> and disable
C<CLEANUP>, but these are overridable.

=head2 C<scratch>

Loads L<Directory::Scratch> and instantiates a new one, with the same default
options as C<tempfile> and C<tempdir>.

=head1 SEE ALSO

=over 4

=item *

L<File::Temp>,

=item *

L<Directory::Scratch>

=item *

L<Path::Class>

=back

=head1 SUPPORT

Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=Test-TempDir>
(or L<bug-Test-TempDir@rt.cpan.org|mailto:bug-Test-TempDir@rt.cpan.org>).

There is also a mailing list available for users of this distribution, at
L<http://lists.perl.org/list/perl-qa.html>.

There is also an irc channel available for users of this distribution, at
L<C<#perl> on C<irc.perl.org>|irc://irc.perl.org/#perl-qa>.

=head1 AUTHOR

יובל קוג'מן (Yuval Kogman) <nothingmuch@woobling.org>

=head1 CONTRIBUTORS

=for stopwords Karen Etheridge Florian Ragwitz

=over 4

=item *

Karen Etheridge <ether@cpan.org>

=item *

Florian Ragwitz <rafl@debian.org>

=back

=head1 COPYRIGHT AND LICENCE

This software is copyright (c) 2006 by יובל קוג'מן (Yuval Kogman).

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut