fix occasional op/time.t failure
[p5sagit/p5-mst-13.2.git] / t / op / chdir.t
index f9c64a5..5b5ca3f 100644 (file)
@@ -9,9 +9,10 @@ BEGIN {
 
 use Config;
 require "test.pl";
-plan(tests => 31);
+plan(tests => 41);
 
-my $IsVMS = $^O eq 'VMS';
+my $IsVMS   = $^O eq 'VMS';
+my $IsMacOS = $^O eq 'MacOS';
 
 # Might be a little early in the testing process to start using these,
 # but I can't think of a way to write this test without them.
@@ -20,7 +21,11 @@ use File::Spec::Functions qw(:DEFAULT splitdir rel2abs splitpath);
 # Can't use Cwd::abs_path() because it has different ideas about
 # path separators than File::Spec.
 sub abs_path {
-    $IsVMS ? uc(rel2abs(curdir)) : rel2abs(curdir);
+    my $d = rel2abs(curdir);
+
+    $d = uc($d) if $IsVMS;
+    $d = lc($d) if $^O =~ /^uwin/;
+    $d;
 }
 
 my $Cwd = abs_path;
@@ -37,6 +42,45 @@ SKIP: {
 
 $Cwd = abs_path;
 
+SKIP: {
+    skip("no fchdir", 9) unless ($Config{d_fchdir} || "") eq "define";
+    ok(opendir(my $dh, "."), "opendir .");
+    ok(open(my $fh, "<", "op"), "open op");
+    ok(chdir($fh), "fchdir op");
+    ok(-f "chdir.t", "verify that we are in op");
+    if (($Config{d_dirfd} || "") eq "define") {
+       ok(chdir($dh), "fchdir back");
+    }
+    else {
+       eval { chdir($dh); };
+       like($@, qr/^The dirfd function is unimplemented at/, "dirfd is unimplemented");
+       chdir "..";
+    }
+
+    # same with bareword file handles
+    no warnings 'once';
+    *DH = $dh;
+    *FH = $fh;
+    ok(chdir FH, "fchdir op bareword");
+    ok(-f "chdir.t", "verify that we are in op");
+    if (($Config{d_dirfd} || "") eq "define") {
+       ok(chdir DH, "fchdir back bareword");
+    }
+    else {
+       eval { chdir(DH); };
+       like($@, qr/^The dirfd function is unimplemented at/, "dirfd is unimplemented");
+       chdir "..";
+    }
+    ok(-d "op", "verify that we are back");
+}
+
+SKIP: {
+    skip("has fchdir", 1) if ($Config{d_fchdir} || "") eq "define";
+    opendir(my $dh, "op");
+    eval { chdir($dh); };
+    like($@, qr/^The fchdir function is unimplemented at/, "fchdir is unimplemented");
+}
+
 # The environment variables chdir() pays attention to.
 my @magic_envs = qw(HOME LOGDIR SYS$LOGIN);
 
@@ -44,7 +88,7 @@ sub check_env {
     my($key) = @_;
 
     # Make sure $ENV{'SYS$LOGIN'} is only honored on VMS.
-    if( $key eq 'SYS$LOGIN' && !$IsVMS ) {
+    if( $key eq 'SYS$LOGIN' && !$IsVMS && !$IsMacOS ) {
         ok( !chdir(),         "chdir() on $^O ignores only \$ENV{$key} set" );
         is( abs_path, $Cwd,   '  abs_path() did not change' );
         pass( "  no need to test SYS\$LOGIN on $^O" ) for 1..7;
@@ -92,8 +136,10 @@ sub clean_env {
         next if $IsVMS && $env eq 'SYS$LOGIN';
         next if $IsVMS && $env eq 'HOME' && !$Config{'d_setenv'};
 
-        # On VMS, %ENV is many layered.
-        delete $ENV{$env} while exists $ENV{$env};
+        unless ($IsMacOS) { # ENV on MacOS is "special" :-)
+            # On VMS, %ENV is many layered.
+            delete $ENV{$env} while exists $ENV{$env};
+        }
     }
 
     # The following means we won't really be testing for non-existence,
@@ -107,6 +153,10 @@ END {
 
     # Restore the environment for VMS (and doesn't hurt for anyone else)
     @ENV{@magic_envs} = @Saved_Env{@magic_envs};
+
+    # On VMS this must be deleted or process table is wrong on exit
+    # when this script is run interactively.
+    delete $ENV{'SYS$LOGIN'} if $IsVMS;
 }
 
 
@@ -122,7 +172,7 @@ foreach my $key (@magic_envs) {
 
 {
     clean_env;
-    if ($IsVMS && !$Config{'d_setenv'}) {
+    if (($IsVMS || $IsMacOS) && !$Config{'d_setenv'}) {
         pass("Can't reset HOME, so chdir() test meaningless");
     } else {
         ok( !chdir(),                   'chdir() w/o any ENV set' );