Skip to content

Commit

Permalink
Merge pull request #16 from hashicorp/tfexec-remove-discovery
Browse files Browse the repository at this point in the history
Remove discovery of terraform executable from tfexec
  • Loading branch information
kmoe authored Jul 9, 2020
2 parents 7b2fc9a + b7007ef commit f7bd40c
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 43 deletions.
27 changes: 0 additions & 27 deletions tfexec/main.go

This file was deleted.

6 changes: 2 additions & 4 deletions tfexec/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,8 @@ func NewTerraform(workingDir string, execPath string) (*Terraform, error) {
}

if execPath == "" {
execPath, err = FindTerraform()
if err != nil {
return nil, &ErrNoSuitableBinary{err: err}
}
err := fmt.Errorf("NewTerraform: please supply the path to a Terraform executable using execPath, e.g. using the tfinstall package.")
return nil, &ErrNoSuitableBinary{err: err}

}
tf := Terraform{
Expand Down
63 changes: 51 additions & 12 deletions tfexec/terraform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tfexec

import (
"context"
"errors"
"io"
"io/ioutil"
"os"
Expand All @@ -11,6 +12,7 @@ import (
"testing"

"github.com/davecgh/go-spew/spew"
"github.com/hashicorp/terraform-exec/tfinstall"
tfjson "github.com/hashicorp/terraform-json"
)

Expand All @@ -19,11 +21,48 @@ const testConfigFileName = "main.tf"
const testStateJsonFileName = "state.json"
const testTerraformStateFileName = "terraform.tfstate"

var tfPath string

func TestMain(m *testing.M) {
var err error
td, err := ioutil.TempDir("", "tfinstall")
if err != nil {
panic(err)
}
defer os.RemoveAll(td)

tfPath, err = tfinstall.Find(tfinstall.LookPath(), tfinstall.LatestVersion(td, true))
if err != nil {
panic(err)
}
exitCode := m.Run()
os.Exit(exitCode)

}

// test that a suitable error is returned if NewTerraform is called without a valid
// executable path
func TestNoTerraformBinary(t *testing.T) {
td := testTempDir(t)
defer os.RemoveAll(td)

_, err := NewTerraform(td, "")
if err == nil {
t.Fatal("expected NewTerraform to error, but it did not")
}

var e *ErrNoSuitableBinary

if !errors.As(err, &e) {
t.Fatal("expected error to be ErrNoSuitableBinary")
}
}

func TestCheckpointDisablePropagation(t *testing.T) {
td := testTempDir(t)
defer os.RemoveAll(td)

tf, err := NewTerraform(td, "")
tf, err := NewTerraform(td, tfPath)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -60,7 +99,7 @@ func TestInitCmd(t *testing.T) {
td := testTempDir(t)
defer os.RemoveAll(td)

tf, err := NewTerraform(td, "")
tf, err := NewTerraform(td, tfPath)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -92,7 +131,7 @@ func TestPlanCmd(t *testing.T) {
td := testTempDir(t)
defer os.RemoveAll(td)

tf, err := NewTerraform(td, "")
tf, err := NewTerraform(td, tfPath)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -124,7 +163,7 @@ func TestApplyCmd(t *testing.T) {
td := testTempDir(t)
defer os.RemoveAll(td)

tf, err := NewTerraform(td, "")
tf, err := NewTerraform(td, tfPath)
if err != nil {
t.Fatal(err)
}
Expand All @@ -144,7 +183,7 @@ func TestDestroyCmd(t *testing.T) {
td := testTempDir(t)
defer os.RemoveAll(td)

tf, err := NewTerraform(td, "")
tf, err := NewTerraform(td, tfPath)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -176,7 +215,7 @@ func TestImportCmd(t *testing.T) {
td := testTempDir(t)
defer os.RemoveAll(td)

tf, err := NewTerraform(td, "")
tf, err := NewTerraform(td, tfPath)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -217,7 +256,7 @@ func TestOutputCmd(t *testing.T) {
td := testTempDir(t)
defer os.RemoveAll(td)

tf, err := NewTerraform(td, "")
tf, err := NewTerraform(td, tfPath)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -250,7 +289,7 @@ func TestStateShowCmd(t *testing.T) {
td := testTempDir(t)
defer os.RemoveAll(td)

tf, err := NewTerraform(td, "")
tf, err := NewTerraform(td, tfPath)
if err != nil {
t.Fatal(err)
}
Expand All @@ -271,7 +310,7 @@ func TestProvidersSchemaCmd(t *testing.T) {
td := testTempDir(t)
defer os.RemoveAll(td)

tf, err := NewTerraform(td, "")
tf, err := NewTerraform(td, tfPath)
if err != nil {
t.Fatal(err)
}
Expand All @@ -292,7 +331,7 @@ func TestStateShow(t *testing.T) {
td := testTempDir(t)
defer os.RemoveAll(td)

tf, err := NewTerraform(td, "")
tf, err := NewTerraform(td, tfPath)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -346,7 +385,7 @@ func TestShow_errInitRequired(t *testing.T) {
td := testTempDir(t)
defer os.RemoveAll(td)

tf, err := NewTerraform(td, "")
tf, err := NewTerraform(td, tfPath)
if err != nil {
t.Fatal(err)
}
Expand All @@ -368,7 +407,7 @@ func TestApply(t *testing.T) {
td := testTempDir(t)
defer os.RemoveAll(td)

tf, err := NewTerraform(td, "")
tf, err := NewTerraform(td, tfPath)
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit f7bd40c

Please sign in to comment.