Re: Changing permissions on temporary directories during testing
Michael G Schwern wrote: There's other issues with this code. For one, its not portable. You're using a system command (touch) and you're assuming Unix filepath syntax. Another is your errors do not include $! which is the reason the command failed (no such file or directory). Additionally, 0333 is writeable and executable. You want 0500, executable and readable (not writable) only by this user. Finally, as I mentioned in another post on this thread, stay out of the system temp directory. Stick to t/. This was meant to be a quick example, not rigorously thought-out code. (And looking back at it, the absence of rigor even at that level is glaringly apparent.) But I'll take your suggestions under advisement when I start to work on the real code. Thanks to all who responded. jimk
Re: Changing permissions on temporary directories during testing
On 5/21/06, James E Keenan <[EMAIL PROTECTED]> wrote: This code is intended to achieve that goal but doesn't DWIM: This is the right idea, but... 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"; You forgot to make $destdir. # dies on preceding line rename $file, $destdir/$file or die "Cannot move $file"; } There's other issues with this code. For one, its not portable. You're using a system command (touch) and you're assuming Unix filepath syntax. Another is your errors do not include $! which is the reason the command failed (no such file or directory). Additionally, 0333 is writeable and executable. You want 0500, executable and readable (not writable) only by this user. Finally, as I mentioned in another post on this thread, stay out of the system temp directory. Stick to t/. use strict; use File::Spec; use File::Temp qw(tempdir tempfile); $File::Temp::DEBUG = 1; # Or use Shell::Command sub touch { open my $fh, ">$_[0]" or die "Can't touch $_[0]: $!"; } # CLEANUP tells File::Temp to delete the directory after the program exits. my $testdir = tempdir( DIR => 't', CLEANUP => 1 ); chdir $testdir or die "Can't chdir into $testdir: $!"; touch "testfile" or die "Can't touch testfile: $!"; mkdir "destdir" or die "Can't mkdir destdir: $!"; chmod 0500, "destdir" or die "Can't chmod destdir: $!"; # It should die here. rename "testfile", File::Spec->catfile("destdir", "testfile") or die "Can't rename: $!";
Re: Changing permissions on temporary directories during testing
On 5/22/06, David Golden <[EMAIL PROTECTED]> wrote: 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. ... For system interaction tests, I prefer to fake failures rather than try to manufacture them. All things being equal, its better to test the real code than stub bits out to fake a failure. It brings your tests closer to reality. Sometimes this isn't possible, but with filesystem tests it usually is. The safest thing to do is work inside your t/ directory rather than the system temp directory which you may or may not have permission to use. It may not even exist. You can still use File::Temp for this, just use the DIR option. use File::Temp; my $tempdir = tempdir( DIR => "t" ); This will create a temporary directory inside t/ which you own and you're free to do whatever you want with.
Re: Changing permissions on temporary directories during testing
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
Re: Changing permissions on temporary directories during testing
See the test suite for File::Flat, I put a ton of work into making sure it worked, and a lot of code doing filesystem stuff. Adam K 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. Jim Keenan
Re: Changing permissions on temporary directories during testing
On May 21, 2006, at 7:29 PM, 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. Jim Keenan use strict; use warnings; use File::Temp; my ($file, $workdir, $destdir); { $workdir = File::Temp::tempdir(); chdir $workdir or die "Cannot change to $workdir"; $file = system("touch alpha"); $destdir = "$workdir/other"; if ( ! -d $destdir ) { die "Directory '$destdir' missing!"; } chmod 0330, $destdir or die "Cannot change permissions on directory"; # dies on preceding line rename $file, $destdir/$file or die "Cannot move $file"; } gives: # perl /tmp/perms.pl Directory '/tmp/xljbEqunk1/other' missing! at /tmp/perms.pl line 11.
Changing permissions on temporary directories during testing
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. Jim Keenan