Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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.


In reply to Re^3: Can Test::MockObject mock a file? by davido
in thread Can Test::MockObject mock a file? by Lady_Aleena

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (3)
As of 2023-03-28 06:16 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (66 votes). Check out past polls.