Skip to content

Commit 8b0040c

Browse files
committed
TF_SCHEMA_PANIC_ON_ERROR is now on by default
opt out with Go parsable boolean (False, false, FALSE, 0)
1 parent c3c5d6c commit 8b0040c

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed

helper/schema/resource_data_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2232,7 +2232,7 @@ func TestResourceDataSet(t *testing.T) {
22322232
}
22332233

22342234
oldEnv := os.Getenv(PanicOnErr)
2235-
os.Setenv(PanicOnErr, "")
2235+
os.Setenv(PanicOnErr, "false")
22362236
defer os.Setenv(PanicOnErr, oldEnv)
22372237

22382238
for i, tc := range cases {

helper/schema/schema.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ package schema
1414
import (
1515
"context"
1616
"fmt"
17+
"log"
1718
"os"
1819
"reflect"
1920
"regexp"
@@ -411,10 +412,18 @@ type InternalMap = schemaMap
411412
type schemaMap map[string]*Schema
412413

413414
func (m schemaMap) panicOnError() bool {
414-
if os.Getenv(PanicOnErr) != "" {
415+
if env := os.Getenv(PanicOnErr); env == "" {
416+
// default to true
417+
return true
418+
} else if b, err := strconv.ParseBool(env); err == nil {
419+
// allow opt out
420+
return b
421+
} else {
422+
// default to true for anything set, this is backwards compatible
423+
// with the previous implementation
424+
log.Printf("[WARN] %s=%s not parsable: %s, defaulting to true", PanicOnErr, env, err)
415425
return true
416426
}
417-
return false
418427
}
419428

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

helper/schema/schema_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6457,3 +6457,47 @@ func TestValidateAtLeastOneOfAttributes(t *testing.T) {
64576457
})
64586458
}
64596459
}
6460+
6461+
func Test_panicOnErrDefaultTrue(t *testing.T) {
6462+
oldEnv := os.Getenv(PanicOnErr)
6463+
6464+
os.Setenv(PanicOnErr, "")
6465+
if !schemaMap(nil).panicOnError() {
6466+
t.Fatalf("Empty %s should default to true", PanicOnErr)
6467+
}
6468+
6469+
os.Setenv(PanicOnErr, oldEnv)
6470+
}
6471+
6472+
func Test_panicOnErrParsableTrue(t *testing.T) {
6473+
oldEnv := os.Getenv(PanicOnErr)
6474+
6475+
os.Setenv(PanicOnErr, "true")
6476+
if !schemaMap(nil).panicOnError() {
6477+
t.Fatalf("Parsable truthy %s should return true", PanicOnErr)
6478+
}
6479+
6480+
os.Setenv(PanicOnErr, oldEnv)
6481+
}
6482+
6483+
func Test_panicOnErrParsableFalse(t *testing.T) {
6484+
oldEnv := os.Getenv(PanicOnErr)
6485+
6486+
os.Setenv(PanicOnErr, "false")
6487+
if schemaMap(nil).panicOnError() {
6488+
t.Fatalf("Parsable falsy %s should return false", PanicOnErr)
6489+
}
6490+
6491+
os.Setenv(PanicOnErr, oldEnv)
6492+
}
6493+
6494+
func Test_panicOnErrUnparsableDefaultTrue(t *testing.T) {
6495+
oldEnv := os.Getenv(PanicOnErr)
6496+
6497+
os.Setenv(PanicOnErr, "FOO")
6498+
if !schemaMap(nil).panicOnError() {
6499+
t.Fatalf("Any set value for %s should return true", PanicOnErr)
6500+
}
6501+
6502+
os.Setenv(PanicOnErr, oldEnv)
6503+
}

0 commit comments

Comments
 (0)