Skip to content

Commit

Permalink
Adding more testing to varfile
Browse files Browse the repository at this point in the history
  • Loading branch information
angrycub committed Aug 30, 2023
1 parent 5f35f8e commit 33a3dd4
Showing 1 changed file with 44 additions and 18 deletions.
62 changes: 44 additions & 18 deletions internal/pkg/varfile/varfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ func TestVarfile_DecodeResult_Merge(t *testing.T) {
"p1": []*Override{{Name: "o1"}, {Name: "o2"}},
},
}

t.Run("errors when redefined", func(t *testing.T) {
dr := DecodeResult{
Overrides: Overrides{
Expand All @@ -146,43 +145,70 @@ func TestVarfile_DecodeResult_Merge(t *testing.T) {
must.True(t, dr.Diags.HasErrors())
must.ErrorContains(t, dr.Diags, "variable o1 can not be redefined")
})
}

func TestVarFile_Merge_Good(t *testing.T) {
d1 := DecodeResult{
Overrides: Overrides{
"p1": []*Override{{Name: "o1"}, {Name: "o2"}},
},
}

t.Run("succeeds for", func(t *testing.T) {
t.Run("okay for variables of same name in different pack", func(t *testing.T) {
d2 := DecodeResult{
dr := DecodeResult{
Overrides: Overrides{
"p2": []*Override{{Name: "o1"}, {Name: "o2"}},
},
}
d2.Merge(d1)
must.False(t, d2.Diags.HasErrors())
must.Len(t, 2, d2.Overrides["p1"])
must.Len(t, 2, d2.Overrides["p2"])
})

t.Run("okay for repeated pointers to same override", func(t *testing.T) {
dr := d1
dr.Merge(d1)
must.False(t, dr.Diags.HasErrors())
must.Len(t, 2, dr.Overrides["p1"])
must.Len(t, 2, dr.Overrides["p2"])
})

t.Run("okay for nil overrides", func(t *testing.T) {
t.Run("okay for repeated pointers to same override", func(t *testing.T) {
dr := DecodeResult{
Overrides: Overrides{},
Overrides: Overrides{
"p2": []*Override{{Name: "o1"}, {Name: "o2"}},
},
}
dr.Merge(d1)
dr2 := dr
dr2.Merge(dr)
must.False(t, dr.Diags.HasErrors())
must.Len(t, 2, dr.Overrides["p1"])
must.Len(t, 2, dr.Overrides["p2"])
})

t.Run("okay for nil override pointer", func(t *testing.T) {
t.Run("for nil overrides", func(t *testing.T) {
dr := DecodeResult{
Overrides: Overrides{"p1": nil},
Overrides: Overrides{
"p2": []*Override{{Name: "o1"}, {Name: "o2"}},
},
}
d1.Merge(dr)
d2 := DecodeResult{
Overrides: Overrides{},
}
dr.Merge(d2)
must.False(t, dr.Diags.HasErrors())
must.Len(t, 2, dr.Overrides["p1"])
must.Len(t, 2, dr.Overrides["p2"])
must.MapNotContainsKey(t, d2.Overrides, "p1")
})

//TODO: Investigatethis broken test.
// t.Run("for nil override pointer", func(t *testing.T) {

// ov := &Override{
// Name: "datacenter",
// Path: "simple_raw_exec_1",
// Type: cty.List(cty.String),
// Value: cty.ListValEmpty(cty.String),
// }

// dr := DecodeResult{Overrides: Overrides{"p1": []*Override{ov, ov}}}

// dr.Merge(dr)
// must.False(t, dr.Diags.HasErrors())
// must.Len(t, 1, dr.Overrides["p1"])
// })
})
}

0 comments on commit 33a3dd4

Please sign in to comment.