Approaching from the other side, I have a CPAN module with tests, some of which create files and directories on the target machine. In order to make the tests rerunnable, I have a test t/00_clear.t, as follows:
I had a specific objective in this module, to make it portable. One of the target platforms was VMS, and I was disappointed to find this test failing when the test suite was rerun (and subsequent tests owing to data lying around).# This is a special 'test' to clear out the residue from any # previous tests that were run, prior to running new tests. # Note: this needs to be portable, so we can't use `rm -rf test`. ######################### use Test::More tests => 1; use File::Find; find( { bydepth => 1, wanted => sub { (-d $_) ? rmdir($_) : unlink($_); }, }, 'test'); rmdir 'test'; ok(!(-d 'test'),"Test directory removed");
Further digging showed that $! contained "directory not empty", and I realised that the problem was that VMS has multiply versioned files, but File::Find will only visit each filename once, so the above code leaves any back versions behind, and fails to remove the containing directories.
I posted to the vmsperl list, and received an immediate response:
1 while unlink 'foo';Thinking about this incantation, I incorporated it into my module test:
(this is the canonical method)
and this worked a treat!# This is a special 'test' to clear out the residue from any # previous tests that were run, prior to running new tests. # Note: this needs to be portable, so we can't use `rm -rf test`. ######################### use Test::More tests => 1; use File::Find; find( { bydepth => 1, wanted => sub { if (-d $_) { rmdir $_ ; } else { 1 while unlink $_; } }, }, 'test'); rmdir 'test'; ok(!(-d 'test'),"Test directory removed");
I recommend this idiom for everyone writing tests for CPAN modules whenever there is a possibility that a file can become multiversioned on some platforms. I think that portability is a noble aim in itself.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: 1 while unlink 'foo'
by ysth (Canon) on Jan 28, 2004 at 16:43 UTC | |
|
Re: 1 while unlink 'foo'
by chromatic (Archbishop) on Jan 28, 2004 at 19:27 UTC | |
|
Re: 1 while unlink 'foo'
by adrianh (Chancellor) on Feb 03, 2004 at 17:01 UTC |