Skip to content

Commit

Permalink
Handle maps in clone (#1164)
Browse files Browse the repository at this point in the history
  • Loading branch information
yorinasub17 authored Aug 4, 2022
1 parent 49466ad commit 3e7eac6
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
18 changes: 18 additions & 0 deletions modules/terraform/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,24 @@ func (options *Options) Clone() (*Options, error) {
if err := copier.Copy(newOptions, options); err != nil {
return nil, err
}
// copier does not deep copy maps, so we have to do it manually.
newOptions.EnvVars = make(map[string]string)
for key, val := range options.EnvVars {
newOptions.EnvVars[key] = val
}
newOptions.Vars = make(map[string]interface{})
for key, val := range options.Vars {
newOptions.Vars[key] = val
}
newOptions.BackendConfig = make(map[string]interface{})
for key, val := range options.BackendConfig {
newOptions.BackendConfig[key] = val
}
newOptions.RetryableTerraformErrors = make(map[string]string)
for key, val := range options.RetryableTerraformErrors {
newOptions.RetryableTerraformErrors[key] = val
}

return newOptions, nil
}

Expand Down
43 changes: 43 additions & 0 deletions modules/terraform/options_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package terraform

import (
"testing"

"github.com/gruntwork-io/terratest/modules/random"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestOptionsCloneDeepClonesEnvVars(t *testing.T) {
t.Parallel()

unique := random.UniqueId()
original := Options{
EnvVars: map[string]string{
"unique": unique,
"original": unique,
},
}
copied, err := original.Clone()
require.NoError(t, err)
copied.EnvVars["unique"] = "nullified"
assert.Equal(t, unique, original.EnvVars["unique"])
assert.Equal(t, unique, copied.EnvVars["original"])
}

func TestOptionsCloneDeepClonesVars(t *testing.T) {
t.Parallel()

unique := random.UniqueId()
original := Options{
Vars: map[string]interface{}{
"unique": unique,
"original": unique,
},
}
copied, err := original.Clone()
require.NoError(t, err)
copied.Vars["unique"] = "nullified"
assert.Equal(t, unique, original.Vars["unique"])
assert.Equal(t, unique, copied.Vars["original"])
}

0 comments on commit 3e7eac6

Please sign in to comment.