Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

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

by Lady_Aleena (Priest)
on Sep 20, 2020 at 15:45 UTC ( [id://11121962]=note: print w/replies, xml ) Need Help??


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

I went back and gave File::Temp a try. I did not like the idea of putting files for these tests on a user's filesystem. Here is what I wrote, with the suggestions to make my test code less repetitive and to test whether a newline or lack thereof at the end of the file would matter. I hope cheating and using the @wanted array to make the temp test files is not a bad thing.

Note: I cargo culted the use of File::Temp from the one place in my code where I used it before. I am still a little fuzzy on how it works, but it does appear to work and the tests still passed.

#!perl use strict; use warnings; use v5.10.0; use Test::More tests => "9"; use File::Temp qw(tempfile); BEGIN { use_ok( 'Fancy::Open', qw(fancy_open) ) or die "Fancy::Open is not available\n"; } diag( "Testing Fancy::Open $Fancy::Open::VERSION, Perl $], $^X" ); my @wanted = qw(red orange yellow spring green teal cyan azure blue vi +olet magenta pink white black gray); # Creating file that ends with a newline my ($n_fh, $n_file) = tempfile(); $n_fh->print(join("\n", @wanted)."\n"); $n_fh->close(); # Testing with file that ends with a newline is_deeply( [ Fancy::Open::fancy_open($n_file) ], [ @wanted ], "testing a plain array with file that ends with a newline" ); is_deeply( [ Fancy::Open::fancy_open($n_file, { 'before' => 'solid ' }) ], [ map "solid $_", @wanted ], "testing an array with before option with file that ends with a newl +ine" ); is_deeply( [ Fancy::Open::fancy_open($n_file, { 'after' => ' bead' }) ], [ map "$_ bead", @wanted ], "testing an array with after option with file that ends with a newli +ne" ); is_deeply( [ Fancy::Open::fancy_open($n_file, { 'before' => 'solid ', 'after' = +> ' bead' }) ], [ map "solid $_ bead", @wanted ], "testing an array with before and after options with file that ends +with a newline" ); # Creating file that does not end with a newline my ($no_n_fh, $no_n_file) = tempfile(); $no_n_fh->print(join("\n", @wanted)); $no_n_fh->close(); # Testing with file that does not end with a newline is_deeply( [ Fancy::Open::fancy_open($no_n_file) ], [ @wanted ], "testing a plain array with file that does not end with a newline" ); is_deeply( [ Fancy::Open::fancy_open($no_n_file, { 'before' => 'solid ' }) ], [ map "solid $_", @wanted ], "testing an array with before option with file that does not end wit +h a newline" ); is_deeply( [ Fancy::Open::fancy_open($no_n_file, { 'after' => ' bead' }) ], [ map "$_ bead", @wanted ], "testing an array with after option with file that does not end with + a newline" ); is_deeply( [ Fancy::Open::fancy_open($no_n_file, { 'before' => 'solid ', 'after +' => ' bead' }) ], [ map "solid $_ bead", @wanted ], "testing an array with before and after options with file that does +not end with a newline" ); done_testing();

I have not figured out how to test the encoding option for this module.

My OS is Debian 10 (Buster); my perl versions are 5.28.1 local and 5.16.3 or 5.30.0 on web host depending on the shebang.

No matter how hysterical I get, my problems are not time sensitive. So, relax, have a cookie, and a very nice day!
Lady Aleena

Replies are listed 'Best First'.
Re^3: Can Test::MockObject mock a file?
by chromatic (Archbishop) on Sep 20, 2020 at 17:01 UTC
    I did not like the idea of putting files for these tests on a user's filesystem.

    Think of it this way; it's difficult to anticipate all of the ways user filesystems might differ from your expectations. One might be encrypted. Another might be case-insensitive. A third might be on a slow network mount.

    By testing as much of that system as possible—by not mocking things—your tests are richer and better represent the conditions you want to validate.

    Using File::Temp avoids at least two difficult problems. First, it reduces your need to clean up after yourself by handling it for you. Hopefully that eases your mind about writing to user filesystems. Second, it makes your tests less serial; you can run them in parallel because there's very little chance multiple tests or multiple runs will collide because they all depend on the state of one specific file with a hard-coded name and path.

    If you wrap your temporary file generation in a function, it could be even cleaner. The third time you write to a temporary file, I'd do that; you'll reduce duplication and further reduce coupling on the generated name of the temporary file.

      I am sorry I am having a hard time figuring out why I would want to put the file generation into a function. Each "file" generated is different, and they do not take up that many lines in the test. Not much is being duplicated as far as I can see.

      My OS is Debian 10 (Buster); my perl versions are 5.28.1 local and 5.16.3 or 5.30.0 on web host depending on the shebang.

      No matter how hysterical I get, my problems are not time sensitive. So, relax, have a cookie, and a very nice day!
      Lady Aleena
        Not much is being duplicated as far as I can see.

        Not a huge amount, no, but when you start putting encoding there, you may find that modifying all the places you generate these files is more work than modifying it in one place.

        Each "file" generated is different

        Sure, but creating the filehandle, writing it out, closing it, checking for errors, all of that is the same.

        Like I said, I wouldn't worry about it until your test file gets much larger. It's just a matter of taste at this point.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (6)
As of 2024-04-23 21:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found