in reply to Unit testing OS rich code

`touch $to_base.FLG`;
Are you really running the external touch command via backticks without checking for errors? Eeww. IMHO, Perl internal functions should generally be preferred to running external commands; they tend to be faster, more portable, more secure, and more robust with better error diagnostics. To mimic the touch command in Perl is not difficult. If the file already exists, you could simply use:
my $now = time(); utime($now, $now, $file);
Or you might employ the CPAN File::Touch module.

As for testing this sort of stuff, I don't usually mock at all, just have the test setup create a new empty scratch directory, populate it with known content, test against that, and have the test teardown remove the scratch directory. Oh, and do it all in Perl, not running external commands. :)

Replies are listed 'Best First'.
Re^2: Unit testing OS rich code
by Voronich (Hermit) on Oct 13, 2011 at 13:18 UTC
    I really don't much care about the timestamp. I'm using touch as the quickest easiest way to create an empty file.
    Me
      the quickest easiest way to create an empty file.

      Quickest? Definitely not. Easiest? Your call :)

      cmpthese 1, { a=>q[ qx[ touch junka.$_ ] for 1 .. 1000 ], b=>q[ do{ open my $f, ">junkb.$_" } for 1 .. 1000 ], };; s/iter a b a 8.83 -- -95% b 0.421 1997% --

      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

        Certainly quickest for the programmer. This has to create 5 sentinel files every 3 months.

        Love that profiling snippet. Gonna have to pull that for meeting arguments ;)

        Me

      I'm using touch as the quickest easiest way to create an empty file
      Are you seriously arguing that using:
      `touch $file`
      to create an empty file should be preferred to, for example:
      { open(my $fh, '>', $file) or die "error creating empty '$file': $!"; }
      on the grounds that it is "quicker and easier" to write?

      If you are that stretched for time, how on earth do you expect to find time to write your proposed test mock framework?

        Good grief. I'm in fact not "seriously arguing" anything at all, least of all what someone 'should' be doing.
        Me