terraform: State.IsEmpty

This commit is contained in:
Mitchell Hashimoto 2015-02-22 10:35:26 -08:00
parent 6cd5c894e8
commit 330364f668
2 changed files with 46 additions and 1 deletions

View File

@ -125,6 +125,15 @@ func (s *State) ModuleOrphans(path []string, c *config.Config) [][]string {
return orphans 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 // IsRemote returns true if State represents a state that exists and is
// remote. // remote.
func (s *State) IsRemote() bool { func (s *State) IsRemote() bool {
@ -282,7 +291,7 @@ func (r *RemoteState) deepcopy() *RemoteState {
} }
func (r *RemoteState) Empty() bool { func (r *RemoteState) Empty() bool {
return r.Type == "" && len(r.Config) == 0 return r == nil || r.Type == ""
} }
func (r *RemoteState) Equals(other *RemoteState) bool { func (r *RemoteState) Equals(other *RemoteState) bool {

View File

@ -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) { func TestStateIsRemote(t *testing.T) {
cases := []struct { cases := []struct {
In *State In *State