diff --git a/terraform/diff.go b/terraform/diff.go index 706197a2b..7f492e883 100644 --- a/terraform/diff.go +++ b/terraform/diff.go @@ -222,6 +222,12 @@ func (d *ResourceDiff) RequiresNew() bool { // just checking that the same attributes are changing, a destroy // isn't suddenly happening, etc. func (d *ResourceDiff) Same(d2 *ResourceDiff) bool { + if d == nil && d2 == nil { + return true + } else if d == nil || d2 == nil { + return false + } + if d.Destroy != d2.Destroy { return false } diff --git a/terraform/diff_test.go b/terraform/diff_test.go index 54205c88a..12fc9c65b 100644 --- a/terraform/diff_test.go +++ b/terraform/diff_test.go @@ -128,6 +128,86 @@ func TestResourceDiff_RequiresNew_nil(t *testing.T) { } } +func TestResourceDiffSame(t *testing.T) { + cases := []struct { + One, Two *ResourceDiff + Same bool + }{ + { + &ResourceDiff{}, + &ResourceDiff{}, + true, + }, + + { + nil, + nil, + true, + }, + + { + &ResourceDiff{Destroy: false}, + &ResourceDiff{Destroy: true}, + false, + }, + + { + &ResourceDiff{Destroy: true}, + &ResourceDiff{Destroy: true}, + true, + }, + + { + &ResourceDiff{ + Attributes: map[string]*ResourceAttrDiff{ + "foo": &ResourceAttrDiff{}, + }, + }, + &ResourceDiff{ + Attributes: map[string]*ResourceAttrDiff{ + "foo": &ResourceAttrDiff{}, + }, + }, + true, + }, + + { + &ResourceDiff{ + Attributes: map[string]*ResourceAttrDiff{ + "bar": &ResourceAttrDiff{}, + }, + }, + &ResourceDiff{ + Attributes: map[string]*ResourceAttrDiff{ + "foo": &ResourceAttrDiff{}, + }, + }, + false, + }, + + { + &ResourceDiff{ + Attributes: map[string]*ResourceAttrDiff{ + "foo": &ResourceAttrDiff{RequiresNew: true}, + }, + }, + &ResourceDiff{ + Attributes: map[string]*ResourceAttrDiff{ + "foo": &ResourceAttrDiff{RequiresNew: false}, + }, + }, + false, + }, + } + + for i, tc := range cases { + actual := tc.One.Same(tc.Two) + if actual != tc.Same { + t.Fatalf("Fail %d", i) + } + } +} + func TestReadWriteDiff(t *testing.T) { diff := &Diff{ Resources: map[string]*ResourceDiff{