Upgrade to PathTools 3.05
[p5sagit/p5-mst-13.2.git] / ext / Cwd / t / cwd.t
index c2142e9..280394b 100644 (file)
@@ -1,17 +1,34 @@
-#!./perl
+#!./perl -w
 
-use Cwd;
 BEGIN {
-    chdir 't' if -d 't';
+    if ($ENV{PERL_CORE}) {
+        chdir 't';
+        @INC = '../lib';
+    }
 }
+use Cwd;
+chdir 't';
 
-use Config;
 use strict;
-use warnings;
+use Config;
 use File::Spec;
 use File::Path;
 
-use Test::More tests => 20;
+use lib File::Spec->catdir('t', 'lib');
+use Test::More;
+require VMS::Filespec if $^O eq 'VMS';
+
+my $tests = 28;
+# _perl_abs_path() currently only works when the directory separator
+# is '/', so don't test it when it won't work.
+my $EXTRA_ABSPATH_TESTS = ($Config{prefix} =~ m/\//) && $^O ne 'cygwin';
+$tests += 4 if $EXTRA_ABSPATH_TESTS;
+plan tests => $tests;
+
+SKIP: {
+  skip "no need to check for blib/ in the core", 1 if $ENV{PERL_CORE};
+  like $INC{'Cwd.pm'}, qr{blib}i, "Cwd should be loaded from blib/ during testing";
+}
 
 my $IsVMS = $^O eq 'VMS';
 my $IsMacOS = $^O eq 'MacOS';
@@ -125,11 +142,20 @@ rmtree($test_dirs[0], 0, 0);
   like($ENV{PWD}, $check);
 }
 
+{
+  # Make sure abs_path() doesn't trample $ENV{PWD}
+  my $start_pwd = $ENV{PWD};
+  mkpath([$Test_Dir], 0, 0777);
+  Cwd::abs_path($Test_Dir);
+  is $ENV{PWD}, $start_pwd;
+  rmtree($test_dirs[0], 0, 0);
+}
+
 SKIP: {
-    skip "no symlinks on this platform", 2 unless $Config{d_symlink};
+    skip "no symlinks on this platform", 2+$EXTRA_ABSPATH_TESTS unless $Config{d_symlink};
 
     mkpath([$Test_Dir], 0, 0777);
-    symlink $Test_Dir => "linktest";
+    symlink $Test_Dir, "linktest";
 
     my $abs_path      =  Cwd::abs_path("linktest");
     my $fast_abs_path =  Cwd::fast_abs_path("linktest");
@@ -137,23 +163,73 @@ SKIP: {
 
     like($abs_path,      qr|$want$|);
     like($fast_abs_path, qr|$want$|);
+    like(Cwd::_perl_abs_path("linktest"), qr|$want$|) if $EXTRA_ABSPATH_TESTS;
 
     rmtree($test_dirs[0], 0, 0);
     unlink "linktest";
 }
 
+if ($ENV{PERL_CORE}) {
+    chdir '../ext/Cwd/t';
+    unshift @INC, '../../../lib';
+}
+
+# Make sure we can run abs_path() on files, not just directories
+my $path = 'cwd.t';
+path_ends_with(Cwd::abs_path($path), 'cwd.t', 'abs_path() can be invoked on a file');
+path_ends_with(Cwd::fast_abs_path($path), 'cwd.t', 'fast_abs_path() can be invoked on a file');
+path_ends_with(Cwd::_perl_abs_path($path), 'cwd.t', '_perl_abs_path() can be invoked on a file')
+  if $EXTRA_ABSPATH_TESTS;
+
+$path = File::Spec->catfile(File::Spec->updir, 't', $path);
+path_ends_with(Cwd::abs_path($path), 'cwd.t', 'abs_path() can be invoked on a file');
+path_ends_with(Cwd::fast_abs_path($path), 'cwd.t', 'fast_abs_path() can be invoked on a file');
+path_ends_with(Cwd::_perl_abs_path($path), 'cwd.t', '_perl_abs_path() can be invoked on a file')
+  if $EXTRA_ABSPATH_TESTS;
+
+
+  
+SKIP: {
+  my $file;
+  {
+    my $root = File::Spec->rootdir;
+    local *FH;
+    opendir FH, $root or skip("Can't opendir($root): $!", 2+$EXTRA_ABSPATH_TESTS);
+    ($file) = grep {-f $_ and not -l $_} map File::Spec->catfile($root, $_), readdir FH;
+    closedir FH;
+  }
+  skip "No plain file in root directory to test with", 2+$EXTRA_ABSPATH_TESTS unless $file;
+  
+  $file = VMS::Filespec::rmsexpand($file) if $^O eq 'VMS';
+  is Cwd::abs_path($file), $file, 'abs_path() works on files in the root directory';
+  is Cwd::fast_abs_path($file), $file, 'fast_abs_path() works on files in the root directory';
+  is Cwd::_perl_abs_path($file), $file, '_perl_abs_path() works on files in the root directory'
+    if $EXTRA_ABSPATH_TESTS;
+}
+
+
 #############################################
-# These two routines give us sort of a poor-man's cross-platform
-# directory comparison routine.
+# These routines give us sort of a poor-man's cross-platform
+# directory or path comparison capability.
 
-sub bracketed_form {
+sub bracketed_form_dir {
   return join '', map "[$_]", 
     grep length, File::Spec->splitdir(File::Spec->canonpath( shift() ));
 }
 
 sub dir_ends_with {
   my ($dir, $expect) = (shift, shift);
-  my $bracketed_expect = quotemeta bracketed_form($expect);
-  like( bracketed_form($dir), qr|$bracketed_expect$|i, (@_ ? shift : ()) );
+  my $bracketed_expect = quotemeta bracketed_form_dir($expect);
+  like( bracketed_form_dir($dir), qr|$bracketed_expect$|i, (@_ ? shift : ()) );
 }
 
+sub bracketed_form_path {
+  return join '', map "[$_]", 
+    grep length, File::Spec->splitpath(File::Spec->canonpath( shift() ));
+}
+
+sub path_ends_with {
+  my ($dir, $expect) = (shift, shift);
+  my $bracketed_expect = quotemeta bracketed_form_path($expect);
+  like( bracketed_form_path($dir), qr|$bracketed_expect$|i, (@_ ? shift : ()) );
+}