Skip to content

Commit

Permalink
Cleanup tests (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko authored Oct 5, 2020
1 parent 2f12ff0 commit 729720f
Show file tree
Hide file tree
Showing 17 changed files with 116 additions and 137 deletions.
2 changes: 0 additions & 2 deletions tfexec/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ package tfexec

import (
"context"
"os"
"testing"

"github.com/hashicorp/terraform-exec/tfexec/internal/testutil"
)

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

tf, err := NewTerraform(td, tfVersion(t, testutil.Latest012))
if err != nil {
Expand Down
2 changes: 0 additions & 2 deletions tfexec/destroy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ package tfexec

import (
"context"
"os"
"testing"

"github.com/hashicorp/terraform-exec/tfexec/internal/testutil"
)

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

tf, err := NewTerraform(td, tfVersion(t, testutil.Latest012))
if err != nil {
Expand Down
2 changes: 0 additions & 2 deletions tfexec/fmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ package tfexec
import (
"context"
"errors"
"os"
"testing"
)

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

tf, err := NewTerraform(td, tfVersion(t, "0.7.6"))
if err != nil {
Expand Down
2 changes: 0 additions & 2 deletions tfexec/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ package tfexec

import (
"context"
"os"
"testing"

"github.com/hashicorp/terraform-exec/tfexec/internal/testutil"
)

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

tf, err := NewTerraform(td, tfVersion(t, testutil.Latest012))
if err != nil {
Expand Down
2 changes: 0 additions & 2 deletions tfexec/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ package tfexec

import (
"context"
"os"
"testing"

"github.com/hashicorp/terraform-exec/tfexec/internal/testutil"
)

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

tf, err := NewTerraform(td, tfVersion(t, testutil.Latest012))
if err != nil {
Expand Down
24 changes: 24 additions & 0 deletions tfexec/internal/testutil/temp_dir.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// +build go1.14

package testutil

import (
"io/ioutil"
"os"
"testing"
)

// TODO: Remove once we drop support for Go <1.15
// in favour of native t.TempDir()
func TempDir(t *testing.T) string {
dir, err := ioutil.TempDir("", "tf")
if err != nil {
t.Fatalf("error creating temporary test directory: %s", err)
}

t.Cleanup(func() {
os.RemoveAll(dir)
})

return dir
}
44 changes: 0 additions & 44 deletions tfexec/internal/testutil/tfcache.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
package testutil

import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"sync"
"testing"

Expand Down Expand Up @@ -49,42 +44,3 @@ func (tf *TFCache) Version(t *testing.T, v string) string {
return f
})
}

func (tf *TFCache) find(t *testing.T, key string, finder func(dir string) tfinstall.ExecPathFinder) string {
t.Helper()

if tf.dir == "" {
// panic instead of t.fatal as this is going to affect all downstream tests reusing the cache entry
panic("installDir not yet configured")
}

tf.Lock()
defer tf.Unlock()

if path, ok := tf.execs[key]; ok {
return path
}

keyDir := key
keyDir = strings.ReplaceAll(keyDir, ":", "-")
keyDir = strings.ReplaceAll(keyDir, "/", "-")

dir := filepath.Join(tf.dir, keyDir)

t.Logf("caching exec %q in dir %q", key, dir)

err := os.MkdirAll(dir, 0777)
if err != nil {
// panic instead of t.fatal as this is going to affect all downstream tests reusing the cache entry
panic(fmt.Sprintf("unable to mkdir %q: %s", dir, err))
}

path, err := tfinstall.Find(context.Background(), finder(dir))
if err != nil {
// panic instead of t.fatal as this is going to affect all downstream tests reusing the cache entry
panic(fmt.Sprintf("error installing terraform %q: %s", key, err))
}
tf.execs[key] = path

return path
}
56 changes: 56 additions & 0 deletions tfexec/internal/testutil/tfcache_find.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// +build go1.14

package testutil

import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"testing"

"github.com/hashicorp/terraform-exec/tfinstall"
)

func (tf *TFCache) find(t *testing.T, key string, finder func(dir string) tfinstall.ExecPathFinder) string {
t.Helper()

if tf.dir == "" {
// panic instead of t.fatal as this is going to affect all downstream tests reusing the cache entry
panic("installDir not yet configured")
}

tf.Lock()
defer tf.Unlock()

if path, ok := tf.execs[key]; ok {
return path
}

keyDir := key
keyDir = strings.ReplaceAll(keyDir, ":", "-")
keyDir = strings.ReplaceAll(keyDir, "/", "-")

dir := filepath.Join(tf.dir, keyDir)

t.Logf("caching exec %q in dir %q", key, dir)

err := os.MkdirAll(dir, 0777)
if err != nil {
// panic instead of t.fatal as this is going to affect all downstream tests reusing the cache entry
panic(fmt.Sprintf("unable to mkdir %q: %s", dir, err))
}

ctx, cancelFunc := context.WithCancel(context.Background())
t.Cleanup(cancelFunc)

path, err := tfinstall.Find(ctx, finder(dir))
if err != nil {
// panic instead of t.fatal as this is going to affect all downstream tests reusing the cache entry
panic(fmt.Sprintf("error installing terraform %q: %s", key, err))
}
tf.execs[key] = path

return path
}
14 changes: 14 additions & 0 deletions tfexec/internal/testutil/tfcache_find_noop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// +build !go1.14

package testutil

import (
"runtime"
"testing"

"github.com/hashicorp/terraform-exec/tfinstall"
)

func (tf *TFCache) find(t *testing.T, key string, finder func(dir string) tfinstall.ExecPathFinder) string {
panic("not implemented for " + runtime.Version())
}
2 changes: 0 additions & 2 deletions tfexec/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ package tfexec

import (
"context"
"os"
"testing"

"github.com/hashicorp/terraform-exec/tfexec/internal/testutil"
)

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

tf, err := NewTerraform(td, tfVersion(t, testutil.Latest012))
if err != nil {
Expand Down
2 changes: 0 additions & 2 deletions tfexec/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ package tfexec

import (
"context"
"os"
"testing"

"github.com/hashicorp/terraform-exec/tfexec/internal/testutil"
)

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

tf, err := NewTerraform(td, tfVersion(t, testutil.Latest012))
if err != nil {
Expand Down
2 changes: 0 additions & 2 deletions tfexec/providers_schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ package tfexec

import (
"context"
"os"
"testing"

"github.com/hashicorp/terraform-exec/tfexec/internal/testutil"
)

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

tf, err := NewTerraform(td, tfVersion(t, testutil.Latest012))
if err != nil {
Expand Down
2 changes: 0 additions & 2 deletions tfexec/refresh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ package tfexec

import (
"context"
"os"
"testing"

"github.com/hashicorp/terraform-exec/tfexec/internal/testutil"
)

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

tf, err := NewTerraform(td, tfVersion(t, testutil.Latest013))
if err != nil {
Expand Down
5 changes: 0 additions & 5 deletions tfexec/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ package tfexec

import (
"context"
"os"
"testing"

"github.com/hashicorp/terraform-exec/tfexec/internal/testutil"
)

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

tf, err := NewTerraform(td, tfVersion(t, testutil.Latest012))
if err != nil {
Expand All @@ -32,7 +30,6 @@ func TestShowCmd(t *testing.T) {

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

tf, err := NewTerraform(td, tfVersion(t, testutil.Latest012))
if err != nil {
Expand All @@ -54,7 +51,6 @@ func TestShowStateFileCmd(t *testing.T) {

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

tf, err := NewTerraform(td, tfVersion(t, testutil.Latest012))
if err != nil {
Expand All @@ -76,7 +72,6 @@ func TestShowPlanFileCmd(t *testing.T) {

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

tf, err := NewTerraform(td, tfVersion(t, testutil.Latest012))
if err != nil {
Expand Down
27 changes: 0 additions & 27 deletions tfexec/terraform_go113_test.go

This file was deleted.

Loading

0 comments on commit 729720f

Please sign in to comment.