diff --git a/tfexec/internal/testutil/tfcache.go b/tfexec/internal/testutil/tfcache.go index 55b86127..65b0e6a7 100644 --- a/tfexec/internal/testutil/tfcache.go +++ b/tfexec/internal/testutil/tfcache.go @@ -1,6 +1,7 @@ package testutil import ( + "context" "os" "path/filepath" "sync" @@ -49,7 +50,7 @@ func (tf *TFCache) Version(t *testing.T, v string) string { t.Fatal(err) } - path, err = tfinstall.Find(tfinstall.ExactVersion(v, dir)) + path, err = tfinstall.Find(context.Background(), tfinstall.ExactVersion(v, dir)) if err != nil { t.Fatalf("error installing terraform version %q: %s", v, err) } diff --git a/tfexec/terraform_test.go b/tfexec/terraform_test.go index 4a668b86..a11d1e18 100644 --- a/tfexec/terraform_test.go +++ b/tfexec/terraform_test.go @@ -170,7 +170,7 @@ func tfVersion(t *testing.T, v string) string { if err != nil { t.Fatal(err) } - iv.path, iv.err = tfinstall.Find(tfinstall.ExactVersion(v, dir)) + iv.path, iv.err = tfinstall.Find(context.Background(), tfinstall.ExactVersion(v, dir)) installedVersions[v] = iv } diff --git a/tfexec/version_test.go b/tfexec/version_test.go index b0c7ef26..15405f02 100644 --- a/tfexec/version_test.go +++ b/tfexec/version_test.go @@ -173,11 +173,11 @@ func TestVersionInRange(t *testing.T) { } func TestCompatible(t *testing.T) { - tf01226, err := tfinstall.Find(tfinstall.ExactVersion("0.12.26", "")) + tf01226, err := tfinstall.Find(context.Background(), tfinstall.ExactVersion("0.12.26", "")) if err != nil { t.Fatal(err) } - tf013beta3, err := tfinstall.Find(tfinstall.ExactVersion("0.13.0-beta3", "")) + tf013beta3, err := tfinstall.Find(context.Background(), tfinstall.ExactVersion("0.13.0-beta3", "")) if err != nil { t.Fatal(err) } diff --git a/tfinstall/download.go b/tfinstall/download.go index a4b7ba8f..0420dd59 100644 --- a/tfinstall/download.go +++ b/tfinstall/download.go @@ -13,23 +13,26 @@ import ( "golang.org/x/crypto/openpgp" ) +func ensureInstallDir(installDir string) (string, error) { + if installDir == "" { + return ioutil.TempDir("", "tfexec") + } + + if _, err := os.Stat(installDir); err != nil { + return "", fmt.Errorf("could not access directory %s for installing Terraform: %w", installDir, err) + } + + return installDir, nil +} + func downloadWithVerification(ctx context.Context, tfVersion string, installDir string) (string, error) { osName := runtime.GOOS archName := runtime.GOARCH // setup: ensure we have a place to put our downloaded terraform binary - var tfDir string - var err error - if installDir == "" { - tfDir, err = ioutil.TempDir("", "tfexec") - if err != nil { - return "", fmt.Errorf("failed to create temp dir: %s", err) - } - } else { - if _, err := os.Stat(installDir); err != nil { - return "", fmt.Errorf("could not access directory %s for installing Terraform: %s", installDir, err) - } - tfDir = installDir + tfDir, err := ensureInstallDir(installDir) + if err != nil { + return "", err } // setup: getter client diff --git a/tfinstall/exact_path_test.go b/tfinstall/exact_path_test.go index c5910d07..d1f324b8 100644 --- a/tfinstall/exact_path_test.go +++ b/tfinstall/exact_path_test.go @@ -1,6 +1,7 @@ package tfinstall import ( + "context" "fmt" "os/exec" "strings" @@ -10,13 +11,15 @@ import ( // test that Find returns an appropriate error when given an exact path // which exists, but is not a terraform executable func TestExactPath(t *testing.T) { + ctx := context.Background() + // we just want the path to a local executable that definitely exists execPath, err := exec.LookPath("go") if err != nil { t.Fatal(err) } - _, err = Find(ExactPath(execPath)) + _, err = Find(ctx, ExactPath(execPath)) if err == nil { t.Fatalf("expected Find() to fail when given ExactPath(%s), but it did not", execPath) } diff --git a/tfinstall/exact_version_test.go b/tfinstall/exact_version_test.go index e83e1146..86a65164 100644 --- a/tfinstall/exact_version_test.go +++ b/tfinstall/exact_version_test.go @@ -1,6 +1,7 @@ package tfinstall import ( + "context" "io/ioutil" "os" "os/exec" @@ -10,13 +11,15 @@ import ( // downloads terraform 0.12.26 from the live releases site func TestFindExactVersion(t *testing.T) { + ctx := context.Background() + tmpDir, err := ioutil.TempDir("", "tfinstall-test") if err != nil { t.Fatal(err) } defer os.RemoveAll(tmpDir) - tfpath, err := Find(ExactVersion("0.12.26", tmpDir)) + tfpath, err := Find(ctx, ExactVersion("0.12.26", tmpDir)) if err != nil { t.Fatal(err) } @@ -38,13 +41,15 @@ func TestFindExactVersion(t *testing.T) { // downloads terraform 0.13.0-beta1 from the live releases site func TestFindExactVersionPrerelease(t *testing.T) { + ctx := context.Background() + tmpDir, err := ioutil.TempDir("", "tfinstall-test") if err != nil { t.Fatal(err) } defer os.RemoveAll(tmpDir) - tfpath, err := Find(ExactVersion("0.13.0-beta1", tmpDir)) + tfpath, err := Find(ctx, ExactVersion("0.13.0-beta1", tmpDir)) if err != nil { t.Fatal(err) } diff --git a/tfinstall/latest_version_test.go b/tfinstall/latest_version_test.go index d240228b..250b026b 100644 --- a/tfinstall/latest_version_test.go +++ b/tfinstall/latest_version_test.go @@ -1,6 +1,7 @@ package tfinstall import ( + "context" "io/ioutil" "os" "os/exec" @@ -13,6 +14,7 @@ import ( // latest version calculation itself is handled by checkpoint, so the test can be straightforward - // just test that we've managed to download a version of terraform later than 0.12.27 func TestLatestVersion(t *testing.T) { + ctx := context.Background() lowerBound := "0.12.27" tmpDir, err := ioutil.TempDir("", "tfinstall-test") @@ -21,7 +23,7 @@ func TestLatestVersion(t *testing.T) { } defer os.RemoveAll(tmpDir) - tfpath, err := Find(LatestVersion(tmpDir, false)) + tfpath, err := Find(ctx, LatestVersion(tmpDir, false)) if err != nil { t.Fatal(err) } diff --git a/tfinstall/tfinstall_test.go b/tfinstall/tfinstall_test.go index ca05dd95..b77ac1e1 100644 --- a/tfinstall/tfinstall_test.go +++ b/tfinstall/tfinstall_test.go @@ -1,6 +1,7 @@ package tfinstall import ( + "context" "io/ioutil" "os" "os/exec" @@ -11,13 +12,15 @@ import ( // test that Find falls back to the next working strategy when the file at // ExactPath does not exist func TestFindFallback(t *testing.T) { + ctx := context.Background() + tmpDir, err := ioutil.TempDir("", "tfinstall-test") if err != nil { t.Fatal(err) } defer os.RemoveAll(tmpDir) - tfpath, err := Find(ExactPath("/hopefully/completely/nonexistent/path"), ExactVersion("0.12.26", tmpDir)) + tfpath, err := Find(ctx, ExactPath("/hopefully/completely/nonexistent/path"), ExactVersion("0.12.26", tmpDir)) if err != nil { t.Fatal(err) }