Skip to content

Commit

Permalink
Fix a bug for state list with no resources
Browse files Browse the repository at this point in the history
  • Loading branch information
minamijoyo committed Jul 27, 2020
1 parent d3a7caf commit dea9fac
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
10 changes: 9 additions & 1 deletion tfexec/terraform_state_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,13 @@ func (c *terraformCLI) StateList(ctx context.Context, state *State, addresses []
return nil, err
}

return strings.Split(strings.TrimRight(stdout, "\n"), "\n"), nil
// we want to split stdout by '\n', but strings.Split returns []string{""} if stdout is empty.
// we should remove empty strings from the list so that its length to be 0.
resources := strings.FieldsFunc(
strings.TrimRight(stdout, "\n"),
func(c rune) bool {
return c == '\n'
},
)
return resources, nil
}
13 changes: 13 additions & 0 deletions tfexec/terraform_state_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,19 @@ aws_security_group.foo
want: nil,
ok: false,
},
{
desc: "no resources",
mockCommands: []*mockCommand{
{
args: []string{"terraform", "state", "list"},
stdout: "",
exitCode: 0,
},
},
state: nil,
want: []string{},
ok: true,
},
}

for _, tc := range cases {
Expand Down

0 comments on commit dea9fac

Please sign in to comment.