Skip to content

Commit f027b94

Browse files
committed
tests simplification
1 parent 1b2eedd commit f027b94

File tree

8 files changed

+16
-192
lines changed

8 files changed

+16
-192
lines changed

internal/remotestate/backend/gcs/config.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ import (
66

77
"github.com/gruntwork-io/terragrunt/internal/errors"
88
"github.com/gruntwork-io/terragrunt/internal/remotestate/backend"
9+
"github.com/gruntwork-io/terragrunt/internal/util"
910
"github.com/gruntwork-io/terragrunt/pkg/log"
11+
"github.com/mitchellh/mapstructure"
1012
"golang.org/x/exp/slices"
1113

1214
"maps"
13-
14-
"github.com/gruntwork-io/terragrunt/internal/util"
1515
)
1616

1717
type Config map[string]any
@@ -33,7 +33,7 @@ func (cfg Config) FilterOutTerragruntKeys() Config {
3333
func (cfg Config) IsEqual(targetCfg Config, logger log.Logger) bool {
3434
// If other keys in config are bools, DeepEqual also will consider the maps to be different.
3535
// Note: strconv.ParseBool is intentionally lenient here (accepts "1"/"0"/"t"/"f") for backward
36-
// compatibility with existing configs. DecodeWithStringBoolHook uses strict "true"/"false" only.
36+
// compatibility with existing configs. WeakDecode also accepts these via strconv.ParseBool.
3737
for key, value := range targetCfg {
3838
if util.KindOf(targetCfg[key]) == reflect.String && util.KindOf(cfg[key]) == reflect.Bool {
3939
if convertedValue, err := strconv.ParseBool(value.(string)); err == nil {
@@ -57,11 +57,11 @@ func (cfg Config) ParseExtendedGCSConfig() (*ExtendedRemoteStateConfigGCS, error
5757
extendedConfig ExtendedRemoteStateConfigGCS
5858
)
5959

60-
if err := util.DecodeWithStringBoolHook(cfg, &gcsConfig); err != nil {
60+
if err := mapstructure.WeakDecode(cfg, &gcsConfig); err != nil {
6161
return nil, errors.New(err)
6262
}
6363

64-
if err := util.DecodeWithStringBoolHook(cfg, &extendedConfig); err != nil {
64+
if err := mapstructure.WeakDecode(cfg, &extendedConfig); err != nil {
6565
return nil, errors.New(err)
6666
}
6767

internal/remotestate/backend/s3/config.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import (
66

77
"github.com/gruntwork-io/terragrunt/internal/errors"
88
"github.com/gruntwork-io/terragrunt/internal/hclhelper"
9+
"github.com/gruntwork-io/terragrunt/internal/util"
910
"github.com/gruntwork-io/terragrunt/pkg/log"
11+
"github.com/mitchellh/mapstructure"
1012

1113
"maps"
12-
13-
"github.com/gruntwork-io/terragrunt/internal/util"
1414
)
1515

1616
const (
@@ -102,11 +102,11 @@ func (cfg Config) ParseExtendedS3Config() (*ExtendedRemoteStateConfigS3, error)
102102
extendedConfig ExtendedRemoteStateConfigS3
103103
)
104104

105-
if err := util.DecodeWithStringBoolHook(cfg, &s3Config); err != nil {
105+
if err := mapstructure.WeakDecode(cfg, &s3Config); err != nil {
106106
return nil, errors.New(err)
107107
}
108108

109-
if err := util.DecodeWithStringBoolHook(cfg, &extendedConfig); err != nil {
109+
if err := mapstructure.WeakDecode(cfg, &extendedConfig); err != nil {
110110
return nil, errors.New(err)
111111
}
112112

internal/remotestate/backend/s3/config_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func TestParseExtendedS3Config_StringBoolCoercion(t *testing.T) {
112112
}
113113
}
114114

115-
// TestParseExtendedS3Config_InvalidStringBool verifies that DecodeWithStringBoolHook rejects
115+
// TestParseExtendedS3Config_InvalidStringBool verifies that WeakDecode rejects
116116
// invalid string values for bool fields (e.g. "maybe" is not a valid bool).
117117
func TestParseExtendedS3Config_InvalidStringBool(t *testing.T) {
118118
t.Parallel()

internal/remotestate/remote_state_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func TestGetTFInitArgsNoBackendConfigs(t *testing.T) {
127127

128128
// TestGetTFInitArgs_StringBoolCoercion verifies that string boolean values
129129
// (from HCL ternary type unification) pass through correctly to terraform init
130-
// -backend-config args. Complements unit-level DecodeWithStringBoolHook coercion tests in
130+
// -backend-config args. Complements unit-level WeakDecode coercion tests in
131131
// s3/config_test.go and gcs/config_test.go. See #5475.
132132
func TestGetTFInitArgs_StringBoolCoercion(t *testing.T) {
133133
t.Parallel()

internal/util/mapstructure.go

Lines changed: 0 additions & 39 deletions
This file was deleted.

internal/util/mapstructure_test.go

Lines changed: 0 additions & 136 deletions
This file was deleted.

pkg/config/config.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import (
4040
"github.com/gruntwork-io/terragrunt/internal/util"
4141
"github.com/gruntwork-io/terragrunt/pkg/config/hclparse"
4242
"github.com/gruntwork-io/terragrunt/pkg/options"
43+
"github.com/mitchellh/mapstructure"
4344
)
4445

4546
const (
@@ -1679,8 +1680,8 @@ func convertToTerragruntConfig(ctx context.Context, pctx *ParsingContext, config
16791680
}
16801681

16811682
var config *remotestate.Config
1682-
// Decode with strict string->bool coercion for HCL ternary type unification. See #5475.
1683-
if err := util.DecodeWithStringBoolHook(remoteStateMap, &config); err != nil {
1683+
// WeakDecode: HCL ternary type unification sends string "true" for bool fields. See #5475.
1684+
if err := mapstructure.WeakDecode(remoteStateMap, &config); err != nil {
16841685
return nil, err
16851686
}
16861687

@@ -1791,8 +1792,8 @@ func convertToTerragruntConfig(ctx context.Context, pctx *ParsingContext, config
17911792

17921793
for name, block := range generateMap {
17931794
var generateBlock terragruntGenerateBlock
1794-
// Decode with strict string->bool coercion for HCL ternary type unification. See #5475.
1795-
if err := util.DecodeWithStringBoolHook(block, &generateBlock); err != nil {
1795+
// WeakDecode: HCL ternary type unification sends string "true" for bool fields. See #5475.
1796+
if err := mapstructure.WeakDecode(block, &generateBlock); err != nil {
17961797
return nil, err
17971798
}
17981799

pkg/config/config_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,6 @@ remote_state = {
132132
ctx, pctx := config.NewParsingContext(t.Context(), l, mockOptionsForTest(t))
133133
_, err := config.ParseConfigString(ctx, pctx, l, config.DefaultTerragruntConfigPath, cfg, nil)
134134
require.Error(t, err)
135-
assert.Contains(t, err.Error(), "invalid boolean string")
136135
}
137136

138137
func TestParseTerragruntConfigGenerateAttrStringBoolCoercion(t *testing.T) {
@@ -185,7 +184,6 @@ generate = {
185184
ctx, pctx := config.NewParsingContext(t.Context(), l, mockOptionsForTest(t))
186185
_, err := config.ParseConfigString(ctx, pctx, l, config.DefaultTerragruntConfigPath, cfg, nil)
187186
require.Error(t, err)
188-
assert.Contains(t, err.Error(), "invalid boolean string")
189187
}
190188

191189
func TestParseTerragruntJsonConfigRemoteStateMinimalConfig(t *testing.T) {

0 commit comments

Comments
 (0)