test and fix mkdir
authorDobrica Pavlinusic <dpavlin@rot13.org>
Wed, 26 Aug 2009 08:59:50 +0000 (08:59 +0000)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Wed, 26 Aug 2009 08:59:50 +0000 (08:59 +0000)
lib/PXElator/file.pm
lib/PXElator/t/file.t

index 242e0bf..49a2f62 100644 (file)
@@ -10,8 +10,11 @@ my $debug = 1;
 
 sub mkpath {
        my $file = shift;
-       my $dir = $1 if $file =~ s{^.+/[^/]+}{};
-       File::Path::mkpath $dir unless -e $dir;
+       my $dir = $1 if $file =~ s{(^.+)/[^/]+}{$1};
+       if ( $dir && ! -d $dir ) {
+               carp "# mkdir $dir";
+               File::Path::mkpath $dir;
+       }
 }
 
 sub append {
@@ -71,11 +74,11 @@ sub replace {
 
 sub copy_once {
        my ( $from, $to ) = @_;
-       $to ||= $from;
+       die "no destination" unless $to;
        return if -e $to;
        mkpath $to;
-       write_file $to, read_file $from;
-#      warn "# copy_once $to ", -s $to, " bytes";
+       carp "# copy_once $from => $to";
+       write_file $to, read_file($from);
 }
 
 1;
index 182df7a..7f812a3 100755 (executable)
@@ -4,7 +4,7 @@ use warnings;
 use strict;
 use autodie;
 
-use Test::More tests => 17;
+use Test::More tests => 18;
 use Data::Dump qw/dump/;
 
 use_ok 'file';
@@ -23,7 +23,7 @@ ok( ! file::change( $file, 'test' => 'foobar' ), 'change again' );
 
 ok( file::replace( $file, 'foobar' => 'replaced content' ), 'replace' );
 
-my $file2 = $file . '.2';
+my $file2 = $file . '.dir/test';
 
 ok( file::copy_once( '/etc/rc.local', $file2 ), 'copy_once' );
 cmp_ok( -s $file2, '==', -s '/etc/rc.local', 'size' );
@@ -32,5 +32,5 @@ ok( my $content = file::read_file($file2), 'read' );
 like( $content, qr/foobar.*exit/s, 'foobar before exit' );
 
 ok( unlink($file2), 'unlink' );
-
+ok( rmdir("$file.dir"), 'rmdir' );
 ok( unlink($file), 'unlink' );