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 {
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;
use strict;
use autodie;
-use Test::More tests => 17;
+use Test::More tests => 18;
use Data::Dump qw/dump/;
use_ok 'file';
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' );
like( $content, qr/foobar.*exit/s, 'foobar before exit' );
ok( unlink($file2), 'unlink' );
-
+ok( rmdir("$file.dir"), 'rmdir' );
ok( unlink($file), 'unlink' );