Is there a method or module that allows me to simulate a fake filesystem for use with Perl unit testing?
I use a variety of Test::*
and Test::Mock*
modules in my unit testing, but occasionally I'd like to be able to fake a filesystem. A simple example (and a minimum requirement) would be faking the result from the -r
or -f
file test operators, so that when I call objects or modules that perform file tests, I can more easily control the result.
I know that there are all manner of ways of getting around this problem, such as creating temporary files or wrapping file tests in their own functions which can then be mocked, but sometimes it would be nice if I could just get Perl to lie to me... So solutions that suggest rewriting the code being tested are not required ;-)
Update:
After having just found and read this SO question, rewriting the code so that it is more testable is looking like the more sensible option. Still, fingers crossed...
These are worth a look: Filesys::Virtual and Filesys::Virtual::Async. I’ve used the first. Mixed with Path::Class and File::Temp it is easy to do all kinds of self-cleaning things tersely and elegantly. Test::Virtual::Filesystem also looks interesting.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With