Skip to content

Commit

Permalink
Remove TF_SCHEMA_PANIC_ON_ERR
Browse files Browse the repository at this point in the history
Schema errors will panic if TF_ACC is set, it is no longer on by default
but cannot be opted out of when running acceptance tests.
  • Loading branch information
appilon committed May 27, 2020
1 parent 048e70e commit af281a4
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 57 deletions.
5 changes: 0 additions & 5 deletions helper/resource/testing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ import (
)

func init() {
// TODO: Remove when we remove the guard on id checks
if err := os.Setenv("TF_ACC_IDONLY", "1"); err != nil {
panic(err)
}

if err := os.Setenv(testEnvVar, "1"); err != nil {
panic(err)
}
Expand Down
5 changes: 0 additions & 5 deletions helper/schema/resource_data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package schema
import (
"fmt"
"math"
"os"
"reflect"
"testing"
"time"
Expand Down Expand Up @@ -2231,10 +2230,6 @@ func TestResourceDataSet(t *testing.T) {
},
}

oldEnv := os.Getenv(PanicOnErr)
os.Setenv(PanicOnErr, "false")
defer os.Setenv(PanicOnErr, oldEnv)

for i, tc := range cases {
d, err := schemaMap(tc.Schema).Data(tc.State, tc.Diff)
if err != nil {
Expand Down
16 changes: 1 addition & 15 deletions helper/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

// Name of ENV variable which (if not empty) prefers panic over error
const PanicOnErr = "TF_SCHEMA_PANIC_ON_ERROR"

// Schema is used to describe the structure of a value.
//
// Read the documentation of the struct elements for important details.
Expand Down Expand Up @@ -471,18 +468,7 @@ type InternalMap = schemaMap
type schemaMap map[string]*Schema

func (m schemaMap) panicOnError() bool {
if env := os.Getenv(PanicOnErr); env == "" {
// default to true
return true
} else if b, err := strconv.ParseBool(env); err == nil {
// allow opt out
return b
} else {
// default to true for anything set, this is backwards compatible
// with the previous implementation
log.Printf("[WARN] %s=%s not parsable: %s, defaulting to true", PanicOnErr, env, err)
return true
}
return os.Getenv("TF_ACC") != ""
}

// Data returns a ResourceData for the given schema, state, and diff.
Expand Down
42 changes: 10 additions & 32 deletions helper/schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8209,48 +8209,26 @@ func TestValidateAtLeastOneOfAttributes(t *testing.T) {
}
}

func Test_panicOnErrDefaultTrue(t *testing.T) {
oldEnv := os.Getenv(PanicOnErr)
func TestPanicOnErrorDefaultsFalse(t *testing.T) {
oldEnv := os.Getenv("TF_ACC")

os.Setenv(PanicOnErr, "")
if !schemaMap(nil).panicOnError() {
t.Fatalf("Empty %s should default to true", PanicOnErr)
}

os.Setenv(PanicOnErr, oldEnv)
}

func Test_panicOnErrParsableTrue(t *testing.T) {
oldEnv := os.Getenv(PanicOnErr)

os.Setenv(PanicOnErr, "true")
if !schemaMap(nil).panicOnError() {
t.Fatalf("Parsable truthy %s should return true", PanicOnErr)
}

os.Setenv(PanicOnErr, oldEnv)
}

func Test_panicOnErrParsableFalse(t *testing.T) {
oldEnv := os.Getenv(PanicOnErr)

os.Setenv(PanicOnErr, "false")
os.Setenv("TF_ACC", "")
if schemaMap(nil).panicOnError() {
t.Fatalf("Parsable falsy %s should return false", PanicOnErr)
t.Fatalf("panicOnError should be false when TF_ACC is empty")
}

os.Setenv(PanicOnErr, oldEnv)
os.Setenv("TF_ACC", oldEnv)
}

func Test_panicOnErrUnparsableDefaultTrue(t *testing.T) {
oldEnv := os.Getenv(PanicOnErr)
func TestPanicOnErrorTF_ACCSet(t *testing.T) {
oldEnv := os.Getenv("TF_ACC")

os.Setenv(PanicOnErr, "FOO")
os.Setenv("TF_ACC", "1")
if !schemaMap(nil).panicOnError() {
t.Fatalf("Any set value for %s should return true", PanicOnErr)
t.Fatalf("panicOnError should be true when TF_ACC is not empty")
}

os.Setenv(PanicOnErr, oldEnv)
os.Setenv("TF_ACC", oldEnv)
}

func TestValidateRequiredWithAttributes(t *testing.T) {
Expand Down

0 comments on commit af281a4

Please sign in to comment.