From 330364f6687d70f4123935c2d3626dc07eaad124 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 22 Feb 2015 10:35:26 -0800 Subject: [PATCH] terraform: State.IsEmpty --- terraform/state.go | 11 ++++++++++- terraform/state_test.go | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/terraform/state.go b/terraform/state.go index 52d4c8ca0..3ef3f3afe 100644 --- a/terraform/state.go +++ b/terraform/state.go @@ -125,6 +125,15 @@ func (s *State) ModuleOrphans(path []string, c *config.Config) [][]string { return orphans } +// Empty returns true if the state is empty. +func (s *State) Empty() bool { + if s == nil { + return true + } + + return len(s.Modules) == 0 +} + // IsRemote returns true if State represents a state that exists and is // remote. func (s *State) IsRemote() bool { @@ -282,7 +291,7 @@ func (r *RemoteState) deepcopy() *RemoteState { } func (r *RemoteState) Empty() bool { - return r.Type == "" && len(r.Config) == 0 + return r == nil || r.Type == "" } func (r *RemoteState) Equals(other *RemoteState) bool { diff --git a/terraform/state_test.go b/terraform/state_test.go index aa3fe6e0b..fc969e973 100644 --- a/terraform/state_test.go +++ b/terraform/state_test.go @@ -312,6 +312,42 @@ func TestInstanceStateEqual(t *testing.T) { } } +func TestStateEmpty(t *testing.T) { + cases := []struct { + In *State + Result bool + }{ + { + nil, + true, + }, + { + &State{}, + true, + }, + { + &State{ + Remote: &RemoteState{Type: "foo"}, + }, + true, + }, + { + &State{ + Modules: []*ModuleState{ + &ModuleState{}, + }, + }, + false, + }, + } + + for i, tc := range cases { + if tc.In.Empty() != tc.Result { + t.Fatalf("bad %d %#v:\n\n%#v", i, tc.Result, tc.In) + } + } +} + func TestStateIsRemote(t *testing.T) { cases := []struct { In *State