Skip to content

Commit 1e52211

Browse files
committed
Remove TF_SCHEMA_PANIC_ON_ERR
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.
1 parent 048e70e commit 1e52211

File tree

4 files changed

+11
-57
lines changed

4 files changed

+11
-57
lines changed

helper/resource/testing_test.go

-5
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,6 @@ import (
1818
)
1919

2020
func init() {
21-
// TODO: Remove when we remove the guard on id checks
22-
if err := os.Setenv("TF_ACC_IDONLY", "1"); err != nil {
23-
panic(err)
24-
}
25-
2621
if err := os.Setenv(testEnvVar, "1"); err != nil {
2722
panic(err)
2823
}

helper/schema/resource_data_test.go

-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package schema
33
import (
44
"fmt"
55
"math"
6-
"os"
76
"reflect"
87
"testing"
98
"time"
@@ -2231,10 +2230,6 @@ func TestResourceDataSet(t *testing.T) {
22312230
},
22322231
}
22332232

2234-
oldEnv := os.Getenv(PanicOnErr)
2235-
os.Setenv(PanicOnErr, "false")
2236-
defer os.Setenv(PanicOnErr, oldEnv)
2237-
22382233
for i, tc := range cases {
22392234
d, err := schemaMap(tc.Schema).Data(tc.State, tc.Diff)
22402235
if err != nil {

helper/schema/schema.go

+1-15
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@ import (
3131
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
3232
)
3333

34-
// Name of ENV variable which (if not empty) prefers panic over error
35-
const PanicOnErr = "TF_SCHEMA_PANIC_ON_ERROR"
36-
3734
// Schema is used to describe the structure of a value.
3835
//
3936
// Read the documentation of the struct elements for important details.
@@ -471,18 +468,7 @@ type InternalMap = schemaMap
471468
type schemaMap map[string]*Schema
472469

473470
func (m schemaMap) panicOnError() bool {
474-
if env := os.Getenv(PanicOnErr); env == "" {
475-
// default to true
476-
return true
477-
} else if b, err := strconv.ParseBool(env); err == nil {
478-
// allow opt out
479-
return b
480-
} else {
481-
// default to true for anything set, this is backwards compatible
482-
// with the previous implementation
483-
log.Printf("[WARN] %s=%s not parsable: %s, defaulting to true", PanicOnErr, env, err)
484-
return true
485-
}
471+
return os.Getenv("TF_ACC") != ""
486472
}
487473

488474
// Data returns a ResourceData for the given schema, state, and diff.

helper/schema/schema_test.go

+10-32
Original file line numberDiff line numberDiff line change
@@ -8209,48 +8209,26 @@ func TestValidateAtLeastOneOfAttributes(t *testing.T) {
82098209
}
82108210
}
82118211

8212-
func Test_panicOnErrDefaultTrue(t *testing.T) {
8213-
oldEnv := os.Getenv(PanicOnErr)
8212+
func TestPanicOnErrorDefaultsFalse(t *testing.T) {
8213+
oldEnv := os.Getenv("TF_ACC")
82148214

8215-
os.Setenv(PanicOnErr, "")
8216-
if !schemaMap(nil).panicOnError() {
8217-
t.Fatalf("Empty %s should default to true", PanicOnErr)
8218-
}
8219-
8220-
os.Setenv(PanicOnErr, oldEnv)
8221-
}
8222-
8223-
func Test_panicOnErrParsableTrue(t *testing.T) {
8224-
oldEnv := os.Getenv(PanicOnErr)
8225-
8226-
os.Setenv(PanicOnErr, "true")
8227-
if !schemaMap(nil).panicOnError() {
8228-
t.Fatalf("Parsable truthy %s should return true", PanicOnErr)
8229-
}
8230-
8231-
os.Setenv(PanicOnErr, oldEnv)
8232-
}
8233-
8234-
func Test_panicOnErrParsableFalse(t *testing.T) {
8235-
oldEnv := os.Getenv(PanicOnErr)
8236-
8237-
os.Setenv(PanicOnErr, "false")
8215+
os.Setenv("TF_ACC", "")
82388216
if schemaMap(nil).panicOnError() {
8239-
t.Fatalf("Parsable falsy %s should return false", PanicOnErr)
8217+
t.Fatalf("panicOnError should be false when TF_ACC is empty")
82408218
}
82418219

8242-
os.Setenv(PanicOnErr, oldEnv)
8220+
os.Setenv("TF_ACC", oldEnv)
82438221
}
82448222

8245-
func Test_panicOnErrUnparsableDefaultTrue(t *testing.T) {
8246-
oldEnv := os.Getenv(PanicOnErr)
8223+
func TestPanicOnErrorTC_ACCSet(t *testing.T) {
8224+
oldEnv := os.Getenv("TF_ACC")
82478225

8248-
os.Setenv(PanicOnErr, "FOO")
8226+
os.Setenv("TF_ACC", "1")
82498227
if !schemaMap(nil).panicOnError() {
8250-
t.Fatalf("Any set value for %s should return true", PanicOnErr)
8228+
t.Fatalf("panicOnError should be true when TF_ACC is not empty")
82518229
}
82528230

8253-
os.Setenv(PanicOnErr, oldEnv)
8231+
os.Setenv("TF_ACC", oldEnv)
82548232
}
82558233

82568234
func TestValidateRequiredWithAttributes(t *testing.T) {

0 commit comments

Comments
 (0)