Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove TF_SCHEMA_PANIC_ON_ERR #462

Merged
merged 2 commits into from
May 27, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not referenced anywhere else

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", "")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh god this is just asking for a race condition problem 😭

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, these unit tests aren't super high value so we could ditch the tests if you find this too "anti pattern"

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