Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

1 while unlink 'foo'

by rinceWind (Monsignor)
on Jan 28, 2004 at 10:54 UTC ( [id://324636]=perlmeditation: print w/replies, xml ) Need Help??

Those of you who have need to delve into the core perl tests will see this wierd looking idiom. The reason for this I will explain.

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:

# 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");
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).

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';

(this is the canonical method)
Thinking about this incantation, I incorporated it into my module test:
# 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");
and this worked a treat!

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.

--
I'm Not Just Another Perl Hacker

Replies are listed 'Best First'.
Re: 1 while unlink 'foo'
by ysth (Canon) on Jan 28, 2004 at 16:43 UTC
    A caution: never do this for a file that may be open (by the current process or by any other). Some platforms (cygwin on older windows, in particular) may internally schedule a deferred deletion of 'foo' and return true if unable to immediately delete it. This creates an endless loop.

    The 1 while unlink idiom is documented here.

Re: 1 while unlink 'foo'
by chromatic (Archbishop) on Jan 28, 2004 at 19:27 UTC

    Where possible, I use File::Path to create a directory of test files and then call rmtree() at the end of the test.

Re: 1 while unlink 'foo'
by adrianh (Chancellor) on Feb 03, 2004 at 17:01 UTC

    Excellent advice.

    Test::Harness's HARNESS_FILELEAK_IN_DIR can also be handy for tracking down accidental file creation in test suites.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://324636]
Approved by rnahi
Front-paged by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-19 20:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found