1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
|
use 5.008001;
use strict;
use warnings;
use Test::More 0.96;
use File::Temp qw(tmpnam tempdir);
use File::Spec;
use Cwd;
use lib 't/lib';
use TestUtils qw/exception has_symlinks/;
use Path::Tiny;
# Tests adapted from Path::Class t/basic.t
my $file = path( scalar tmpnam() );
ok $file, "Got a filename via tmpnam()";
{
my $fh = $file->openw;
ok $fh, "Opened $file for writing";
ok print( $fh "Foo\n" ), "Printed to $file";
}
ok -e $file, "$file should exist";
ok $file->is_file, "it's a file!";
if ( -e "/dev/null" ) {
ok( path("/dev/null")->is_file, "/dev/null is_file, too" );
}
my ( $volume, $dirname, $basename ) =
map { s{\\}{/}; $_ } File::Spec->splitpath($file);
is( $file->volume, $volume, "volume correct" );
is( $file->volume, $volume, "volume cached " ); # for coverage
is( $file->dirname, $dirname, "dirname correct" );
is( $file->basename, $basename, "basename correct" );
{
my $fh = $file->openr;
is scalar <$fh>, "Foo\n", "Read contents of $file correctly";
}
note "stat";
{
my $stat = $file->stat;
ok $stat;
cmp_ok $stat->mtime, '>', time() - 20; # Modified within last 20 seconds
$stat = $file->parent->stat;
ok $stat;
}
note "stat/lstat with no file";
{
my $file = "i/do/not/exist";
ok exception { path($file)->stat };
ok exception { path($file)->lstat };
}
1 while unlink $file;
ok not -e $file;
my $dir = path( tempdir( TMPDIR => 1, CLEANUP => 1 ) );
ok $dir;
ok -d $dir;
ok $dir->is_dir, "It's a directory!";
$file = $dir->child('foo.x');
$file->touch;
ok -e $file;
my $epoch = time - 10;
utime $epoch, $epoch, $file;
$file->touch;
ok( $file->stat->mtime > $epoch, "touch sets utime as current time" );
$file->touch($epoch);
ok( $file->stat->mtime == $epoch, "touch sets utime as 10 secs before" );
{
my @files = $dir->children;
is scalar @files, 1 or diag explain \@files;
ok scalar grep { /foo\.x/ } @files;
}
ok $dir->remove_tree, "Removed $dir";
ok !-e $dir, "$dir no longer exists";
ok !$dir->remove_tree, "Removing non-existent dir returns false";
my $tmpdir = Path::Tiny->tempdir;
{
$dir = path( $tmpdir, 'foo', 'bar' );
$dir->parent->remove_tree if -e $dir->parent;
ok $dir->mkdir, "Created $dir";
ok -d $dir, "$dir is a directory";
$dir = $dir->parent;
ok $dir->remove_tree( { safe => 1 } ); # check that we pass through args
ok !-e $dir;
}
{
$dir = path( $tmpdir, 'foo' );
ok $dir->mkdir;
ok $dir->child('dir')->mkdir;
ok -d $dir->child('dir');
ok $dir->child('file.x')->touch;
ok $dir->child('0')->touch;
ok $dir->child('foo/bar/baz.txt')->touchpath;
subtest 'iterator' => sub {
my @contents;
my $iter = $dir->iterator;
while ( my $file = $iter->() ) {
push @contents, $file;
}
is scalar @contents, 4
or diag explain \@contents;
is( $iter->(), undef, "exhausted iterator is undef" );
my $joined = join ' ', sort map $_->basename, grep { -f $_ } @contents;
is $joined, '0 file.x'
or diag explain \@contents;
my ($subdir) = grep { $_ eq $dir->child('dir') } @contents;
ok $subdir;
is -d $subdir, 1;
my ($file) = grep { $_ eq $dir->child('file.x') } @contents;
ok $file;
is -d $file, '';
};
subtest 'visit' => sub {
my @contents;
$dir->visit( sub { push @contents, $_[0] } );
is scalar @contents, 4
or diag explain \@contents;
my $joined = join ' ', sort map $_->basename, grep { -f $_ } @contents;
is $joined, '0 file.x'
or diag explain \@contents;
my ($subdir) = grep { $_ eq $dir->child('dir') } @contents;
ok $subdir;
is -d $subdir, 1;
my ($file) = grep { $_ eq $dir->child('file.x') } @contents;
ok $file;
is -d $file, '';
};
ok $dir->remove_tree;
ok !-e $dir;
# Try again with directory called '0', in curdir
my $orig = Path::Tiny->cwd;
ok $dir->mkdir;
ok chdir($dir);
my $dir2 = path(".");
ok $dir2->child('0')->mkdir;
ok -d $dir2->child('0');
subtest 'iterator' => sub {
my @contents;
my $iter = $dir2->iterator;
while ( my $file = $iter->() ) {
push @contents, $file;
}
ok grep { $_ eq '0' } @contents;
};
subtest 'visit' => sub {
my @contents;
$dir2->visit( sub { push @contents, $_[0] } );
ok grep { $_ eq '0' } @contents;
};
ok chdir($orig);
ok $dir->remove_tree;
ok !-e $dir;
}
{
my $file = path( $tmpdir, 'slurp' );
ok $file;
my $fh = $file->openw or die "Can't create $file: $!";
print $fh "Line1\nLine2\n";
close $fh;
ok -e $file;
my $content = $file->slurp;
is $content, "Line1\nLine2\n";
my @content = $file->lines;
is_deeply \@content, [ "Line1\n", "Line2\n" ];
@content = $file->lines( { chomp => 1 } );
is_deeply \@content, [ "Line1", "Line2" ];
ok( $file->remove, "removing file" );
ok !-e $file, "file is gone";
ok !$file->remove, "removing file again returns false";
my $subdir = $tmpdir->child('subdir');
ok $subdir->mkdir;
ok exception { $subdir->remove }, "calling 'remove' on a directory throws";
ok rmdir $subdir;
my $orig = Path::Tiny->cwd;
ok chdir $tmpdir;
my $zero_file = path '0';
ok $zero_file->openw;
ok $zero_file->remove, "removing file called '0'";
ok chdir $orig;
}
{
my $file = path( $tmpdir, 'slurp' );
ok $file;
my $fh = $file->openw(':raw') or die "Can't create $file: $!";
print $fh "Line1\r\nLine2\r\n\302\261\r\n";
close $fh;
ok -e $file;
my $content = $file->slurp( { binmode => ':raw' } );
is $content, "Line1\r\nLine2\r\n\302\261\r\n", "slurp raw";
my $line3 = "\302\261\n";
utf8::decode($line3);
$content = $file->slurp( { binmode => ':crlf:utf8' } );
is $content, "Line1\nLine2\n" . $line3, "slurp+crlf+utf8";
my @content = $file->lines( { binmode => ':crlf:utf8' } );
is_deeply \@content, [ "Line1\n", "Line2\n", $line3 ], "lines+crlf+utf8";
chop($line3);
@content = $file->lines( { chomp => 1, binmode => ':crlf:utf8' } );
is_deeply \@content, [ "Line1", "Line2", $line3 ], "lines+chomp+crlf+utf8";
$file->remove;
ok not -e $file;
}
{
my $file = path( $tmpdir, 'spew' );
$file->remove() if $file->exists;
$file->spew( { binmode => ':raw' }, "Line1\r\n" );
$file->append( { binmode => ':raw' }, "Line2" );
my $content = $file->slurp( { binmode => ':raw' } );
is( $content, "Line1\r\nLine2" );
}
{
# Make sure we can make an absolute/relative roundtrip
my $cwd = path(".");
is $cwd, $cwd->absolute->relative,
"from $cwd to " . $cwd->absolute . " to " . $cwd->absolute->relative;
}
{
# realpath should resolve ..
my $lib = path("t/../lib");
my $real = $lib->realpath;
unlike $real, qr/\.\./, "updir gone from realpath";
my $abs_lib = $lib->absolute;
my $abs_t = path("t")->absolute;
my $case = $abs_t->child("../lib");
is( $case->realpath, $lib->realpath, "realpath on absolute" );
# non-existent directory in realpath should throw error
eval { path("lkajdfak/djslakdj")->realpath };
like(
$@,
qr/Error resolving realpath/,
"caught error from realpath on non-existent dir"
);
# but non-existent basename in realpath should throw error
eval { path("./djslakdj")->realpath };
is( $@, '', "no error from realpath on non-existent last component" );
}
subtest "move()" => sub {
subtest "dest is a file (and does not exist)" => sub {
my $file = $tmpdir->child("mv-foo.txt");
$file->spew("Hello World\n");
my $moveto = $tmpdir->child("mv-bar.txt");
ok !-e $moveto, "destination does not exist before";
my $result = $file->move("$moveto");
is "$result" => "$moveto", "returned the right file";
is $moveto->slurp, "Hello World\n", "target exists and matches orig";
ok !$file->exists, "orig no longer exists";
};
subtest "dest is a dir" => sub {
my $file = $tmpdir->child("mv-dir.txt");
$file->spew("Hello World\n");
my $anothertmpdir = Path::Tiny->tempdir;
my $result = $file->move($anothertmpdir);
is "$result" => "$anothertmpdir/mv-dir.txt", "returned the right file";
is $result->slurp, "Hello World\n", "target exists and matches orig";
ok !$file->exists, "orig no longer exists";
};
subtest "dest file already exists" => sub {
my $file = $tmpdir->child("mv-non.txt");
$file->spew("Hello World\n");
my $anothertmpdir = Path::Tiny->tempdir;
my $dest = $anothertmpdir->child( "move-it.there");
$dest->spew( "Original Content\n" );
ok -f $dest, "destination file exists";
my $result;
{
local ( $!, $@ );
$result = $file->move("$dest");
is $@, undef, q[$@ - no errors leaked on success];
is $!, "", q[$! - no errors leaked on success];
}
is "$result" => $dest, "returned the right file";
is $result->slurp, "Hello World\n", "target exists and matches orig";
ok !$file->exists, "orig no longer exists";
};
subtest "dest parent dir does not exist" => sub {
my $file = $tmpdir->child("mv-noparent.txt");
$file->spew("Hello World\n");
my $anothertmpdir = Path::Tiny->tempdir;
my $result = eval { $file->move("$anothertmpdir/rutroh/yo.txt") };
ok !$result, "does not return true";
like "$@", qr/move/, "throws error";
ok $file->exists, "orig still exists";
}
};
subtest "copy()" => sub {
my $file = $tmpdir->child("foo.txt");
$file->spew("Hello World\n");
my $copy;
subtest "dest is a file" => sub {
$copy = $tmpdir->child("bar.txt");
my $result = $file->copy($copy);
is "$result" => "$copy", "returned the right file";
is( $copy->slurp, "Hello World\n", "file copied" );
};
subtest "dest is a dir" => sub {
# new tempdir not to clobber the original foo.txt
my $tmpdir = Path::Tiny->tempdir;
my $result = $file->copy($tmpdir);
is "$result" => "$tmpdir/foo.txt", "returned the right file";
is $result->slurp, "Hello World\n", "file copied";
};
subtest "try some different chmods" => sub {
ok( $copy->chmod(0000), "chmod(0000)" );
ok( $copy->chmod("0400"), "chmod('0400')" );
SKIP: {
skip "No exception if run as root", 1 if $> == 0;
skip "No exception writing to read-only file", 1
unless
exception { open my $fh, ">", "$copy" or die }; # probe if actually read-only
my $error = exception { $file->copy($copy) };
ok( $error, "copy throws error if permission denied" );
like( $error, qr/\Q$file/, "error messages includes the source file name" );
like( $error, qr/\Q$copy/, "error messages includes the destination file name" );
}
ok( $copy->chmod("u+w"), "chmod('u+w')" );
};
};
{
$tmpdir->child( "subdir", "touched.txt" )->touchpath->spew("Hello World\n");
is(
$tmpdir->child( "subdir", "touched.txt" )->slurp,
"Hello World\n",
"touch can chain"
);
}
{
# Only run if a 255 length filename is allowed. Test checks that the temp
# file created doesn't exceed 255 characters.
my $file = $tmpdir->child("A" x 255);
if ( eval { $file->touch; 1 } ) {;
ok( path($file)->spew("Hello"), "spew to long filename" );
}
}
SKIP: {
my $newtmp = Path::Tiny->tempdir;
my $file = $newtmp->child("foo.txt");
my $link = $newtmp->child("bar.txt");
$file->spew("Hello World\n");
skip "symlink unavailable", 1 unless has_symlinks();
eval { symlink $file => $link };
ok( $link->lstat->size, "lstat" );
is( $link->realpath, $file->realpath, "realpath resolves symlinks" );
ok $link->remove, 'remove symbolic link';
ok $file->remove;
$file = $newtmp->child("foo.txt");
$link = $newtmp->child("bar.txt");
$file->spew("Hello World\n");
ok symlink $file => $link;
ok $file->remove;
ok $link->remove, 'remove broken symbolic link';
my $dir = $newtmp->child('foo');
$link = $newtmp->child("bar");
ok $dir->mkdir;
ok -d $dir;
$file = $dir->child("baz.txt");
$file->spew("Hello World\n");
ok symlink $dir => $link;
ok $link->remove_tree, 'remove_tree symbolic link';
ok $dir->remove_tree;
$dir = $newtmp->child('foo');
$link = $newtmp->child("bar");
ok $dir->mkdir;
ok -d $dir;
$file = $dir->child("baz.txt");
$file->spew("Hello World\n");
ok symlink $dir => $link;
ok $dir->remove_tree;
ok $link->remove_tree, 'remove_tree broken symbolic link';
$file = $newtmp->child("foo.txt");
$link = $newtmp->child("bar.txt");
my $link2 = $newtmp->child("baz.txt");
$file->spew("Hello World\n");
ok symlink $file => $link;
ok symlink $link => $link2;
$link2->spew("Hello Perl\n");
ok -l $link2, 'path is still symbolic link after spewing';
is readlink($link2), $link, 'symbolic link is available after spewing';
is readlink($link), $file, 'symbolic link is available after spewing';
is $file->slurp, "Hello Perl\n",
'spewing follows the link and replace the destination instead';
}
{
my $newtmp = Path::Tiny->tempdir;
my $to_delete = $newtmp->child('to-delete')->mkdir;
my $error = exception { $newtmp->remove_tree('to-delete'); };
like(
$error,
qr/method argument was given, but was not a hash reference/,
"passing a weird argument to ->remove_tree throws",
);
ok -d $newtmp, "we did not remove path after bad call to remove_tree";
}
# We don't have subsume so comment these out. Keep in case we
# implement it later
##{
## my $t = path( 't');
## my $foo_bar = $t->child('foo','bar');
## $foo_bar->remove; # Make sure it doesn't exist
##
## ok $t->subsumes($foo_bar), "t subsumes t/foo/bar";
## ok !$t->contains($foo_bar), "t doesn't contain t/foo/bar";
##
## $foo_bar->mkdir;
## ok $t->subsumes($foo_bar), "t still subsumes t/foo/bar";
## ok $t->contains($foo_bar), "t now contains t/foo/bar";
##
## $t->child('foo')->remove;
##}
done_testing;
|