OpenSSL::Test: Fix directory calculations in __cwd()
authorRichard Levitte <levitte@openssl.org>
Mon, 20 Jun 2016 12:03:12 +0000 (14:03 +0200)
committerRichard Levitte <levitte@openssl.org>
Tue, 21 Jun 2016 12:52:34 +0000 (14:52 +0200)
We recalculate the location of the directories we keep track of.
However, we did so after having moved to the new directory already, so
the data we did the calculations from were possibly not quite correct.

This change moves the calculations to happen before moving to the new
directory.

This issue is sporadic, and possibly dependent on the platform.

Reviewed-by: Matt Caswell <matt@openssl.org>
test/testlib/OpenSSL/Test.pm

index 2cfb22a6533a9a143a1b9f6f878c76b46cc6778b..6a10afd653d7831bf66e2cb89c1468da94881617 100644 (file)
@@ -821,12 +821,10 @@ sub __cwd {
        mkpath($dir);
     }
 
-    # Should we just bail out here as well?  I'm unsure.
-    return undef unless chdir($dir);
-
-    if ($opts{cleanup}) {
-       rmtree(".", { safe => 0, keep_root => 1 });
-    }
+    # We are recalculating the directories we keep track of, but need to save
+    # away the result for after having moved into the new directory.
+    my %tmp_directories = ();
+    my %tmp_ENV = ();
 
     # For each of these directory variables, figure out where they are relative
     # to the directory we want to move to if they aren't absolute (if they are,
@@ -835,7 +833,7 @@ sub __cwd {
     foreach (@dirtags) {
        if (!file_name_is_absolute($directories{$_})) {
            my $newpath = abs2rel(rel2abs($directories{$_}), rel2abs($dir));
-           $directories{$_} = $newpath;
+           $tmp_directories{$_} = $newpath;
        }
     }
 
@@ -845,10 +843,22 @@ sub __cwd {
     foreach (@direnv) {
        if (!file_name_is_absolute($ENV{$_})) {
            my $newpath = abs2rel(rel2abs($ENV{$_}), rel2abs($dir));
-           $ENV{$_} = $newpath;
+           $tmp_ENV{$_} = $newpath;
        }
     }
 
+    # Should we just bail out here as well?  I'm unsure.
+    return undef unless chdir($dir);
+
+    if ($opts{cleanup}) {
+       rmtree(".", { safe => 0, keep_root => 1 });
+    }
+
+    %directories = %tmp_directories;
+    foreach (keys %tmp_ENV) {
+        $ENV{$_} = $tmp_ENV{$_};
+    }
+
     if ($debug) {
        print STDERR "DEBUG: __cwd(), directories and files:\n";
        print STDERR "  \$directories{BLDTEST} = \"$directories{BLDTEST}\"\n";