From 537c1bedcf9c3285e5035474273ab9320113847c Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Wed, 25 Mar 2020 12:26:36 -0700 Subject: [PATCH] internal/providercache: LinkFromOtherCache removes target, not source This was incorrectly removing the _source_ entry prior to creating the symlink, therefore ending up with a dangling symlink and no source file. This wasn't obvious before because the test case for LinkFromOtherCache was also incorrectly named and therefore wasn't running. Fixing the name of that test made this problem apparent. The TestLinkFromOtherCache test case now ends up seeing the final resolved directory rather than the symlink target, because of upstream changes to the internal/getproviders filesystem scanning logic to handle symlinks properly. --- internal/providercache/dir_modify.go | 2 +- internal/providercache/dir_modify_test.go | 11 +++-------- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/internal/providercache/dir_modify.go b/internal/providercache/dir_modify.go index 1d79fd498..ddd1a5697 100644 --- a/internal/providercache/dir_modify.go +++ b/internal/providercache/dir_modify.go @@ -78,7 +78,7 @@ func (d *Dir) LinkFromOtherCache(entry *CachedProvider) error { d.metaCache = nil // Delete anything that's already present at this path first. - err = os.RemoveAll(currentPath) + err = os.RemoveAll(newPath) if err != nil && !os.IsNotExist(err) { return fmt.Errorf("failed to remove existing %s before linking it to %s: %s", currentPath, newPath, err) } diff --git a/internal/providercache/dir_modify_test.go b/internal/providercache/dir_modify_test.go index fcfcf72ca..efd1fc7f5 100644 --- a/internal/providercache/dir_modify_test.go +++ b/internal/providercache/dir_modify_test.go @@ -12,7 +12,7 @@ import ( "github.com/hashicorp/terraform/internal/getproviders" ) -func LinkFromOtherCache(t *testing.T) { +func TestLinkFromOtherCache(t *testing.T) { srcDirPath := "testdata/cachedir" tmpDirPath, err := ioutil.TempDir("", "terraform-test-providercache") if err != nil { @@ -82,13 +82,8 @@ func LinkFromOtherCache(t *testing.T) { // still packed and thus not considered to be a cache member. Version: versions.MustParseVersion("2.0.0"), - // These are still pointed into the testdata directory because - // we created a symlink in our tmpDir. (This part of the test - // is expected to fail if the temporary directory is on a - // filesystem that cannot support symlinks, in which case - // we should see the temporary directory paths here instead.) - PackageDir: "testdata/cachedir/registry.terraform.io/hashicorp/null/2.0.0/windows_amd64", - ExecutableFile: "testdata/cachedir/registry.terraform.io/hashicorp/null/2.0.0/windows_amd64/terraform-provider-null.exe", + PackageDir: tmpDirPath + "/registry.terraform.io/hashicorp/null/2.0.0/windows_amd64", + ExecutableFile: tmpDirPath + "/registry.terraform.io/hashicorp/null/2.0.0/windows_amd64/terraform-provider-null.exe", }, }, }