Test::Directory(3)
π Test::Directory(3) π
okay bestie, so this is the manual page for Test::Directory, a Perl extension for maintaining test directories. yes, that's the whole vibe: it's the Perl extension for maintaining test directories, and we're gonna go through it together. π«Ά
β¨ SYNOPSIS β¨
use Test::Directory
use My::Module
my $dir = Test::Directory->new($path);
$dir->touch($src_file);
My::Module::something( $dir->path($src_file), $dir->path($dst_file) );
$dir->has_ok($dst_file); #did my module create dst?
$dir->hasnt_ok($src_file); #is source still there?
like, that's literally the whole sales pitch in six lines. iconique. π
πΈ DESCRIPTION πΈ
So when you're testing code, a lot of the time you're checking that files get created and deleted like they're supposed to. And doing that by hand? lowkey a trap. It's so easy to forget a file, or to completely miss that some random unexpected file showed up and ruined everything. π
This module simplifies maintaining test directories by tracking their status as they're modified or tested through this API. Which makes it simple to test individual files, and to verify that there are no missing or unknown files. two for one, we love to see it. π
The whole idea: use this API to make a temporary directory and populate an initial set of files. Then, whenever something in the directory changes, use the test methods to verify the change happened like you expected. And at any time you can just verify that the contents of the directory are exactly as expected. vibe check: passed. β¨
Test::Directory implements an object-oriented interface for managing test directories. It tracks which files it knows about (by creating them or testing them via its API), and it can report if any files were missing or unexpectedly added. it's giving attentive bestie energy, honestly. π
There are two flavors of methods for interacting with the directory:
- Utility methods β they just return a value (i.e. the number of files/errors) with no output at all. quiet queen. π€«
- Test functions β these use
Test::Builderto produce the appropriate test results and diagnostics for the test harness.
And the directory is automatically cleaned up when the object goes out of scope β see the clean method below for the deets. π§
π CONSTRUCTOR π
new([$path, $options, ...])β Create a new instance pointing to the specified$path.$optionsis an optional hashref of options.
$path will be created (or the constructor will die). If $path is undefined, a unique path gets automatically generated; otherwise it is an error for $path to already exist. no squatters, periodt. π
π¦ UTILITY METHODS π¦
touch($file ...)β Create the specified$filesand track their state.create($file,%options)β Create the specified$fileand track its state. The%optionshash supports the following:time => $timestampβ Passed to"utime" in perlfuncto set the file's access and modification times.content => $dataβ Write$datato the file.
mkdir($directory)β Create the specified$directory; dies ifmkdirfails.name($file)β Returns the name of the$file, relative to the directory; including any separator normalization.$fileneed not exist. This method is used internally by most other methods to translate file paths.
For portability, this method implicitly splits the path on UNIX-style / separators, and rejoins it with the local directory separator. π§΅
Absent any separator substitution, the returned value would be equivalent to $file.
path($file)β Returns the path for the$file, including the directory name and any substitutions.$fileneed not exist.check_file($file)β Checks whether the specified$fileexists, and updates its state accordingly. Returns true if$fileexists, false otherwise.
This method is used internally by the corresponding test methods.
check_directory($directory)β Checks whether the specified$directoryexists, and updates its state accordingly. Returns true if$directoryexists, false otherwise.
This method is used internally by the corresponding test methods.
β οΈ heads up bestie: replacing a file with a directory, or the other way around, would require calling bothcheck_fileandcheck_directoryto update the state so it reflects both changes. don't skip one, i'm begging. π
remove_files($file...)β Remove the specified$files; return the number of files removed.remove_directories($directory...)β Remove the specified$directoriess; return the number of directories removed.cleanβ Remove all known files, then callrmdiron the directory; returns the status of thermdir. The presence of any unknown files will cause thermdirto fail, leaving the directory with these unknown files. so likeβ¦ clean up after yourself first, k? π
This method is called automatically when the object goes out of scope.
count_unknowncount_missingβ Returns a count of the unknown or missing files and directories.
π note: files and directories are interchangeable when counting missing files, but not when counting unknown files. tiny distinction, huge consequences. π
π TEST METHODS π
The test methods validate the state of the test directory, calling Test::Builder's ok and diag methods to generate output. they're the main characters of this whole page fr fr. β¨
has ($file, $test_name)hasnt($file, $test_name)β Verify the status of$file, and update its state. The test will pass if the state is expected. If$test_nameis undefined, a default will be generated. πhas_dir ($directory, $test_name);hasnt_dir($directory, $test_name);β Verify the status of$directory, and update its state. The test will pass if the state is expected. If$test_nameis undefined, a default will be generated.is_ok($test_name)β Pass if the test directory has no missing or extra files. the ultimate vibe check tbh. βclean_ok([$test_name])β Equivalent took(clean,$test_name)
π₯ EXAMPLES π₯
Calling an external program to move a file β okay so here's the little glow-up moment:
$dir->touch('my-file.txt');
system ('gzip', $dir->path('my-file.txt'));
$dir->has ('my-file.txt.gz', '.gz file is added');
$dir->hasnt('my-file.txt', '.txt file is removed');
$dir->is_ok; #verify no other changes to $dir
see how it checks both that the .gz showed up β¨ and that the .txt peaced out π, and then verifies nothing else in $dir changed? that's the whole flex. ate and left no crumbs. π
π SEE ALSO π
Test::Builder β go read that one too, iykyk. π
π AUTHOR π
Steve Sanbeg, <sanbeg@cpan.org<gt> β legend behavior, honestly. π
π COPYRIGHT AND LICENSE π
Copyright (C) 2013 by Steve Sanbeg
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.10.1 or, at your option, any later version of Perl 5 you may have available.
okay that's the whole page bestie, no cap, every last flag and file path intact π
β¨ and for the record, this copy is from perl v5.42.0, dated 2023-02-27 β mwah, stan, bye! β¨
π¦ where this page comes from
- opensuse-leap 16.0: perl-Test-Directory 0.052-bp160.1.12 Β· Artistic-1.0 OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms