James E Keenan wrote:
Let's say that I'm writing a test suite for a Perl module which creates files and then, optionally, moves those files to predetermined directories. To test this module's functionality, I would have to see what happens when the user running the tests does not have write permissions on the destination directory (e.g., test whether an appropriate warning was issued). But to do *that*, I would have to change permissions on a directory to forbid myself to write to it.

This code is intended to achieve that goal but doesn't DWIM:

my ($file, $workdir, $destdir);
{
    $workdir = File::Temp::tempdir();
    chdir $workdir or die "Cannot change to $workdir";
    $file = system("touch alpha");
    $destdir = "$workdir/other";
    chmod 0330, ($destdir)
        or die "Cannot change permissions on directory";
    # dies on preceding line
    rename $file, $destdir/$file or die "Cannot move $file";
}

Is there any other way to do this? Or am I mistaken in even attempting to try it? Thanks.

How portable does this need to be? My inclination is not to mess with file permissions in a test suite if you can avoid it. I'd probably just mock/override "rename" to report failure in the module under test:

  BEGIN {
    *Module::Under::Test::rename = sub { 0 };
  }
  use Module::Under::Test;

For system interaction tests, I prefer to fake failures rather than try to manufacture them.

Regards,
David

Reply via email to