Skip to content

Commit 922b8ba

Browse files
committed
tests update
1 parent d53d3a4 commit 922b8ba

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

internal/remotestate/backend/gcs/config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ func (cfg Config) ParseExtendedGCSConfig() (*ExtendedRemoteStateConfigGCS, error
5656
extendedConfig ExtendedRemoteStateConfigGCS
5757
)
5858

59+
// WeakDecode handles string->bool coercion ("true"/"false") needed when HCL ternary
60+
// type unification produces string values for bool fields (see #5475). Also accepts
61+
// strconv.ParseBool inputs ("1"/"0"/"t"/"f"). When adding new struct fields, verify
62+
// that WeakDecode coercion behavior is acceptable for the field type.
5963
if err := mapstructure.WeakDecode(cfg, &gcsConfig); err != nil {
6064
return nil, errors.New(err)
6165
}

internal/remotestate/backend/gcs/config_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,28 @@ func TestParseExtendedGCSConfig_StringBoolCoercion(t *testing.T) {
170170
assert.True(t, cfg.SkipBucketVersioning)
171171
},
172172
},
173+
{
174+
"empty-string-coerces-to-false",
175+
gcs.Config{
176+
"bucket": "my-bucket",
177+
"skip_bucket_versioning": "",
178+
},
179+
func(t *testing.T, cfg *gcs.ExtendedRemoteStateConfigGCS) {
180+
t.Helper()
181+
assert.False(t, cfg.SkipBucketVersioning)
182+
},
183+
},
184+
{
185+
"numeric-one-coerces-to-true",
186+
gcs.Config{
187+
"bucket": "my-bucket",
188+
"skip_bucket_versioning": "1",
189+
},
190+
func(t *testing.T, cfg *gcs.ExtendedRemoteStateConfigGCS) {
191+
t.Helper()
192+
assert.True(t, cfg.SkipBucketVersioning)
193+
},
194+
},
173195
}
174196

175197
for _, tc := range testCases {

internal/remotestate/backend/s3/config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ func (cfg Config) ParseExtendedS3Config() (*ExtendedRemoteStateConfigS3, error)
101101
extendedConfig ExtendedRemoteStateConfigS3
102102
)
103103

104+
// WeakDecode handles string->bool coercion ("true"/"false") needed when HCL ternary
105+
// type unification produces string values for bool fields (see #5475). Also accepts
106+
// strconv.ParseBool inputs ("1"/"0"/"t"/"f"). When adding new struct fields, verify
107+
// that WeakDecode coercion behavior is acceptable for the field type.
104108
if err := mapstructure.WeakDecode(cfg, &s3Config); err != nil {
105109
return nil, errors.New(err)
106110
}

internal/remotestate/backend/s3/config_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,32 @@ func TestParseExtendedS3Config_StringBoolCoercion(t *testing.T) {
9898
assert.True(t, cfg.RemoteStateConfigS3.UseLockfile)
9999
},
100100
},
101+
{
102+
"empty-string-coerces-to-false",
103+
s3backend.Config{
104+
"bucket": "my-bucket",
105+
"key": "my-key",
106+
"region": "us-east-1",
107+
"use_lockfile": "",
108+
},
109+
func(t *testing.T, cfg *s3backend.ExtendedRemoteStateConfigS3) {
110+
t.Helper()
111+
assert.False(t, cfg.RemoteStateConfigS3.UseLockfile)
112+
},
113+
},
114+
{
115+
"numeric-one-coerces-to-true",
116+
s3backend.Config{
117+
"bucket": "my-bucket",
118+
"key": "my-key",
119+
"region": "us-east-1",
120+
"use_lockfile": "1",
121+
},
122+
func(t *testing.T, cfg *s3backend.ExtendedRemoteStateConfigS3) {
123+
t.Helper()
124+
assert.True(t, cfg.RemoteStateConfigS3.UseLockfile)
125+
},
126+
},
101127
}
102128

103129
for _, tc := range testCases {

0 commit comments

Comments
 (0)