Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^3: Can Test::MockObject mock a file?

by davido (Cardinal)
on Sep 20, 2020 at 04:38 UTC ( #11121957=note: print w/replies, xml ) Need Help??


in reply to Re^2: Can Test::MockObject mock a file?
in thread Can Test::MockObject mock a file?

Your test script can use File::Temp to create a file for you that you write to, and that you then can read back in your test. Done correctly, the file will be removed on end of scope. I usually prefer creating a tempdir with a template such as testname-XXXXX. That way I can create any files I want within that directory, and it will get torn down on close of scope; I don't have to bother with temp files for each thing that needs a file, so long as I put everything ephemeral into the tempdir.

A staunch unit-test advocate would say that unit tests shouldn't touch the filesystem unless they're specifically testing low level filesystem operations. It may be possible for your tests to open filehandles to in-memory files instead of touching the filesystem. The documentation for open discusses how to do this, I think. If it doesn't, then perlopentut would be the other place to look. But in short:

my $in_string = "Hello world.\n"; my $out_string = ''; open my $infh, '<', \$in_string; open my $outfh, '>', \$out_string; while(<$infh>) { print $outfh "$.: $_"; } print $out_string; # prints 1: Hello world.\n

So here I gave Perl a variable containing "Hello world.\n" and told it to treat it as a file, allowing $infh to stream through the pseudo-file. I also gave Perl an empty string for output, and printed the input file to the output file, with a line number prepended. Finally, I printed the contents of the output string, and it contained the contents of the input string, but with a line number prepended.

This technique is great for tests, because it allows you to test in isolation of the filesystem. Later, you can independently test that when you create files (in a tempdir) they have the appropriate names, permissions, and owner.

My preference is to test as much as possible in isolation of the filesystem, but then in more functional tests, also verify things like the characteristics of created files, and the ability to read the files we need to be able to read. That way I can do really fast testing of things like what contents are dumped to or read from files, but can also verify that I'm working effectively within the environment.


Dave

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11121957]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (1)
As of 2021-10-17 23:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (72 votes). Check out past polls.

    Notices?