terraform: Diff.Empty

This commit is contained in:
Mitchell Hashimoto 2014-06-19 14:57:36 -07:00
parent 23c8350ba4
commit 1449d8a510
2 changed files with 41 additions and 0 deletions

View File

@ -69,6 +69,21 @@ func (d *Diff) init() {
})
}
// Empty returns true if the diff has no changes.
func (d *Diff) Empty() bool {
if len(d.Resources) == 0 {
return true
}
for _, rd := range d.Resources {
if len(rd.Attributes) > 0 {
return false
}
}
return true
}
// String outputs the diff in a long but command-line friendly output
// format that users can read to quickly inspect a diff.
func (d *Diff) String() string {

View File

@ -7,6 +7,32 @@ import (
"testing"
)
func TestDiff_Empty(t *testing.T) {
diff := new(Diff)
if !diff.Empty() {
t.Fatal("should be empty")
}
diff.Resources = map[string]*ResourceDiff{
"nodeA": &ResourceDiff{},
}
if !diff.Empty() {
t.Fatal("should be empty")
}
diff.Resources["nodeA"].Attributes = map[string]*ResourceAttrDiff{
"foo": &ResourceAttrDiff{
Old: "foo",
New: "bar",
},
}
if diff.Empty() {
t.Fatal("should not be empty")
}
}
func TestDiff_String(t *testing.T) {
diff := &Diff{
Resources: map[string]*ResourceDiff{