Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mock filesystem for Perl unit testing

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...

like image 496
Mike Avatar asked Jul 13 '11 12:07

Mike


1 Answers

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.

like image 179
Ashley Avatar answered Nov 15 '22 22:11

Ashley