This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Grab the second half of r10084 for cwd.t, which wasn't in change 33042.
[perl5.git] / ext / Cwd / t / cwd.t
index 2c7d6c5..7d5f67f 100644 (file)
@@ -1,26 +1,35 @@
-#!./perl
+#!./perl -w
 
 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 lib File::Spec->catdir('t', 'lib');
 use Test::More;
+require VMS::Filespec if $^O eq 'VMS';
 
-my $tests = 24;
-my $EXTRA_ABSPATH_TESTS = $ENV{PERL_CORE} || $ENV{TEST_PERL_CWD_CODE};
-$tests += 3 if $EXTRA_ABSPATH_TESTS;
+my $tests = 30;
+# _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';
 
@@ -30,6 +39,13 @@ ok( !defined(&chdir),           'chdir() not exported by default' );
 ok( !defined(&abs_path),        '  nor abs_path()' );
 ok( !defined(&fast_abs_path),   '  nor fast_abs_path()');
 
+{
+  my @fields = qw(PATH IFS CDPATH ENV BASH_ENV);
+  my $before = grep exists $ENV{$_}, @fields;
+  cwd();
+  my $after = grep exists $ENV{$_}, @fields;
+  is($before, $after, "cwd() shouldn't create spurious entries in %ENV");
+}
 
 # XXX force Cwd to bootsrap its XSUBs since we have set @INC = "../lib"
 # XXX and subsequent chdir()s can make them impossible to find
@@ -109,19 +125,21 @@ foreach my $func (qw(cwd getcwd fastcwd fastgetcwd)) {
   dir_ends_with( $result, $Test_Dir, "$func()" );
 }
 
+{
+  # Some versions of File::Path (e.g. that shipped with perl 5.8.5)
+  # call getcwd() with an argument (perhaps by calling it as a
+  # method?), so make sure that doesn't die.
+  is getcwd(), getcwd('foo'), "Call getcwd() with an argument";
+}
+
 # Cwd::chdir should also update $ENV{PWD}
 dir_ends_with( $ENV{PWD}, $Test_Dir, 'Cwd::chdir() updates $ENV{PWD}' );
 my $updir = File::Spec->updir;
-Cwd::chdir $updir;
-print "#$ENV{PWD}\n";
-Cwd::chdir $updir;
-print "#$ENV{PWD}\n";
-Cwd::chdir $updir;
-print "#$ENV{PWD}\n";
-Cwd::chdir $updir;
-print "#$ENV{PWD}\n";
-Cwd::chdir $updir;
-print "#$ENV{PWD}\n";
+
+for (1..@test_dirs) {
+  Cwd::chdir $updir;
+  print "#$ENV{PWD}\n";
+}
 
 rmtree($test_dirs[0], 0, 0);
 
@@ -133,22 +151,32 @@ 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+$EXTRA_ABSPATH_TESTS unless $Config{d_symlink};
 
+    my $file = "linktest";
     mkpath([$Test_Dir], 0, 0777);
-    symlink $Test_Dir, "linktest";
+    symlink $Test_Dir, $file;
 
-    my $abs_path      =  Cwd::abs_path("linktest");
-    my $fast_abs_path =  Cwd::fast_abs_path("linktest");
-    my $want          =  File::Spec->catdir("t", $Test_Dir);
+    my $abs_path      =  Cwd::abs_path($file);
+    my $fast_abs_path =  Cwd::fast_abs_path($file);
+    my $want          =  quotemeta( File::Spec->rel2abs($Test_Dir) );
 
-    like($abs_path,      qr|$want$|);
-    like($fast_abs_path, qr|$want$|);
-    like(Cwd::_perl_abs_path("linktest"), qr|$want$|) if $EXTRA_ABSPATH_TESTS;
+    like($abs_path,      qr|$want$|i);
+    like($fast_abs_path, qr|$want$|i);
+    like(Cwd::_perl_abs_path($file), qr|$want$|i) if $EXTRA_ABSPATH_TESTS;
 
     rmtree($test_dirs[0], 0, 0);
-    unlink "linktest";
+    1 while unlink $file;
 }
 
 if ($ENV{PERL_CORE}) {
@@ -170,6 +198,26 @@ path_ends_with(Cwd::_perl_abs_path($path), 'cwd.t', '_perl_abs_path() can be inv
   if $EXTRA_ABSPATH_TESTS;
 
 
+  
+SKIP: {
+  my $file;
+  {
+    my $root = Cwd::abs_path(File::Spec->rootdir);     # Add drive letter?
+    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 routines give us sort of a poor-man's cross-platform
 # directory or path comparison capability.