Test::T...ir::Tiny(3)
π Test::TempDir::Tiny β like, the whole manual, bestie β¨
Okay so you're looking at the Test::T...ir::Tiny(3) page (the truncated name is just the man page being dramatic about column width, it's Test::TempDir::Tiny, we been knew π). Version 0.018, and it's about to make your test suite SO much less annoying. Let's go π¦
β¨ NAME β¨
Test::TempDir::Tiny β temporary directories that stick around when tests fail πβ¨
πΈ VERSION πΈ
version 0.018
π SYNOPSIS π
# t/foo.t
use Test::More;
use Test::TempDir::Tiny;
# default tempdirs
$dir = tempdir(); # ./tmp/t_foo_t/default_1/
$dir = tempdir(); # ./tmp/t_foo_t/default_2/
# labeled tempdirs
$dir = tempdir("label"); # ./tmp/t_foo_t/label_1/
$dir = tempdir("label"); # ./tmp/t_foo_t/label_2/
# labels with spaces and non-word characters
$dir = tempdir("bar baz") # ./tmp/t_foo_t/bar_baz_1/
$dir = tempdir("!!!bang") # ./tmp/t_foo_t/_bang_1/
# run code in a temporary directory
in_tempdir "label becomes name" => sub {
my $cwd = shift;
# do stuff in a tempdir
};
π DESCRIPTION π
So this module teams up with Test::More to make temporary directories that stay put if your tests fail. Iconic behavior, honestly π
It's loosely based on Test::TempDir β but with less complexity, greater portability, and zero non-core dependencies. (It does recommend Capture::Tiny for testing, fyi π«Ά)
The tempdir and in_tempdir functions are exported by default. No qw() gymnastics needed, periodt.
If your current directory is writable, the root for all these directories will be ./tmp. If it's not writable, a File::Temp directory gets created wherever temporary directories are stored for your system. Either way you're covered β¨
Every *.t file gets its own little subdirectory under that root, named after the test filename β but with slashes and periods swapped out for underscores. So t/foo.t gets its very own test-file-specific subdirectory ./tmp/t_foo_t/. Directories made by tempdir go inside that. Which means when tests fail you can actually find your files later instead of crying. Life-changing π
The test-file-specific name is consistent from run-to-run. And if an old directory already exists? It gets removed. Fresh slate, bestie π§
When the test file exits and all tests passed, that test-file-specific directory gets recursively yeeted.
If a test failed and the root is ./tmp, the test-file-specific directory sticks around so you can go inspect it π (But if the root is a File::Temp directory, it's always discarded β no exceptions).
And if nothing is left in ./tmp (meaning no other test file failed), then ./tmp gets cleaned up too β unless it's a symlink. Respect the symlink π«‘
This module also tries to avoid race conditions from parallel testing. In extreme cases, the test-file-specific subdirectory might get created as a regular File::Temp directory instead of in ./tmp. If that happens, you'll get a warning. Not a crash, just a heads up π
π₯ FUNCTIONS π₯
β tempdir
$dir = tempdir(); # .../default_1/
$dir = tempdir("label"); # .../label_1/
Creates a directory underneath a test-file-specific temporary directory and returns the absolute path to it, in platform-native form (i.e. with backslashes on Windows πͺ).
The function takes a single argument as a label for the directory, or defaults to "default". An incremental counter value gets appended so you can reuse the same label in a loop and still get distinct temporary directories:
# t/foo.t
for ( 1 .. 3 ) {
tempdir("in loop");
}
# creates:
# ./tmp/t_foo_t/in_loop_1
# ./tmp/t_foo_t/in_loop_2
# ./tmp/t_foo_t/in_loop_3
If the label has any characters besides alphanumerics, underscore and dash, they get collapsed and replaced with a single underscore:
$dir = tempdir("a space"); # .../a_space_1/
$dir = tempdir("a!bang"); # .../a_bang_1/
The test-file-specific directory and everything inside it gets cleaned up in an END block if the current test file passes its tests π
π¦ in_tempdir
in_tempdir "label becomes name" => sub {
my $cwd = shift;
# this happens in tempdir
};
Give it a label and a code reference, and it creates a temporary directory based on that label (following the same rules as tempdir), changes into that directory, runs your code, then changes back to the original directory. Chef's kiss π©βπ³
The temporary directory path gets passed as an argument to your code reference.
When the code finishes β even if it dies β in_tempdir will change back to the original directory if it can, or to the root if it can't, and it will rethrow any fatal errors. It's giving responsible main character energy π
π ENVIRONMENT π
πΈ PERL_TEST_TEMPDIR_TINY_NOCLEANUP
When this environment variable is true, directories will not be cleaned up β even if tests pass. You want the mess? You get the mess, bestie π
π SEE ALSO π
File::TempPath::Tiny
π SUPPORT π
π Bugs / Feature Requests
Please report any bugs or feature requests through the issue tracker at <https://github.com/dagolden/Test-TempDir-Tiny/issues>. You'll be notified automatically of any progress on your issue β no refreshing required π¬
π§ Source Code
This is open source software. The code repository is available for public review and contribution under the terms of the license.
<https://github.com/dagolden/Test-TempDir-Tiny>
git clone https://github.com/dagolden/Test-TempDir-Tiny.git
β AUTHOR β
David Golden <dagolden@cpan.org>
π CONTRIBUTORS π
- Christian Walde <walde.christian@googlemail.com>
- Kent Fredric <kentfredric@gmail.com>
- Shawn Laffan <shawnlaffan@gmail.com>
- Sven Kirmess <sven.kirmess@kzone.ch>
π COPYRIGHT AND LICENSE π
This software is Copyright (c) 2014 by David Golden.
This is free software, licensed under:
The Apache License, Version 2.0, January 2004
and that's the whole page, no cap π
β this was Test::TempDir::Tiny version 0.018, rendered from the perl v5.42.0 toolchain, dated 2018-09-20. go forth and never debug a vanished tempdir again, ily π«Άβ¨
π¦ where this page comes from
- opensuse-leap 16.0: perl-Test-TempDir-Tiny 0.018-bp160.1.11 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms