Skip to content

Commit

Permalink
Refactor install dir func
Browse files Browse the repository at this point in the history
  • Loading branch information
paultyng committed Aug 16, 2020
1 parent 0819f64 commit de20f17
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 21 deletions.
3 changes: 2 additions & 1 deletion tfexec/internal/testutil/tfcache.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package testutil

import (
"context"
"os"
"path/filepath"
"sync"
Expand Down Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion tfexec/terraform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions tfexec/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
27 changes: 15 additions & 12 deletions tfinstall/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion tfinstall/exact_path_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tfinstall

import (
"context"
"fmt"
"os/exec"
"strings"
Expand All @@ -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)
}
Expand Down
9 changes: 7 additions & 2 deletions tfinstall/exact_version_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tfinstall

import (
"context"
"io/ioutil"
"os"
"os/exec"
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down
4 changes: 3 additions & 1 deletion tfinstall/latest_version_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tfinstall

import (
"context"
"io/ioutil"
"os"
"os/exec"
Expand All @@ -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")
Expand All @@ -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)
}
Expand Down
5 changes: 4 additions & 1 deletion tfinstall/tfinstall_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tfinstall

import (
"context"
"io/ioutil"
"os"
"os/exec"
Expand All @@ -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)
}
Expand Down

0 comments on commit de20f17

Please sign in to comment.