Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make state.ToList stable #412

Merged
merged 1 commit into from
Apr 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion internal/pkg/statemanager/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ var _ = Describe("Statemanager", func() {
})

It("Should get the state list", func() {
// Adding order: A,C,B
// List order should be: A,B,C
key := statemanager.StateKey("a_githubactions")
stateA := statemanager.State{
Name: "a",
Expand All @@ -54,6 +56,16 @@ var _ = Describe("Statemanager", func() {
err = smgr.AddState(key, stateA)
Expect(err).NotTo(HaveOccurred())

key = statemanager.StateKey("c_githubactions")
stateC := statemanager.State{
Name: "c",
Plugin: "githubactions",
Options: map[string]interface{}{"c": "value"},
Resource: map[string]interface{}{"c": "value"},
}
err = smgr.AddState(key, stateC)
Expect(err).NotTo(HaveOccurred())

key = statemanager.StateKey("b_githubactions")
stateB := statemanager.State{
Name: "b",
Expand All @@ -65,7 +77,7 @@ var _ = Describe("Statemanager", func() {
Expect(err).NotTo(HaveOccurred())

stateList := smgr.GetStatesMap().ToList()
Expect(stateList).To(Equal([]statemanager.State{stateA, stateB}))
Expect(stateList).To(Equal([]statemanager.State{stateA, stateB, stateC}))
})

AfterEach(func() {
Expand Down
8 changes: 8 additions & 0 deletions internal/pkg/statemanager/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package statemanager
import (
"bytes"
"fmt"
"sort"

"gopkg.in/yaml.v3"

Expand Down Expand Up @@ -45,6 +46,13 @@ func (s StatesMap) ToList() []State {
res = append(res, value.(State))
return true
})

sort.Slice(res, func(i, j int) bool {
keyi := fmt.Sprintf("%s.%s", res[i].Name, res[i].Plugin)
keyj := fmt.Sprintf("%s.%s", res[j].Name, res[j].Plugin)
return keyi < keyj
})

return res
}

Expand Down