From 95cee378cbff56be92b3b8f86d3cf583040b323e Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Sun, 23 Oct 2022 14:17:34 -0700 Subject: [PATCH] restore sorting tests --- pkg/commands/sort_container_test.go | 165 ---------------------------- pkg/gui/containers_panel.go | 38 ++++--- pkg/gui/sort_container_test.go | 148 +++++++++++++++++++++++++ 3 files changed, 169 insertions(+), 182 deletions(-) delete mode 100644 pkg/commands/sort_container_test.go create mode 100644 pkg/gui/sort_container_test.go diff --git a/pkg/commands/sort_container_test.go b/pkg/commands/sort_container_test.go deleted file mode 100644 index a903d6ce..00000000 --- a/pkg/commands/sort_container_test.go +++ /dev/null @@ -1,165 +0,0 @@ -package commands - -// func sampleContainers(userConfig *config.AppConfig) []*Container { -// return []*Container{ -// { -// ID: "1", -// Name: "1", -// Container: types.Container{ -// State: "exited", -// }, -// Config: userConfig, -// }, -// { -// ID: "2", -// Name: "2", -// Container: types.Container{ -// State: "running", -// }, -// Config: userConfig, -// }, -// { -// ID: "3", -// Name: "3", -// Container: types.Container{ -// State: "running", -// }, -// Config: userConfig, -// }, -// { -// ID: "4", -// Name: "4", -// Container: types.Container{ -// State: "created", -// }, -// Config: userConfig, -// }, -// } -// } - -// func expectedPerStatusContainers(appConfig *config.AppConfig) []*Container { -// return []*Container{ -// { -// ID: "2", -// Name: "2", -// Container: types.Container{ -// State: "running", -// }, -// Config: appConfig, -// }, -// { -// ID: "3", -// Name: "3", -// Container: types.Container{ -// State: "running", -// }, -// Config: appConfig, -// }, -// { -// ID: "1", -// Name: "1", -// Container: types.Container{ -// State: "exited", -// }, -// Config: appConfig, -// }, -// { -// ID: "4", -// Name: "4", -// Container: types.Container{ -// State: "created", -// }, -// Config: appConfig, -// }, -// } -// } - -// func expectedLegacySortedContainers(appConfig *config.AppConfig) []*Container { -// return []*Container{ -// { -// ID: "1", -// Name: "1", -// Container: types.Container{ -// State: "exited", -// }, -// Config: appConfig, -// }, -// { -// ID: "2", -// Name: "2", -// Container: types.Container{ -// State: "running", -// }, -// Config: appConfig, -// }, -// { -// ID: "3", -// Name: "3", -// Container: types.Container{ -// State: "running", -// }, -// Config: appConfig, -// }, -// { -// ID: "4", -// Name: "4", -// Container: types.Container{ -// State: "created", -// }, -// Config: appConfig, -// }, -// } -// } - -// func assertEqualContainers(t *testing.T, left *Container, right *Container) { -// t.Helper() -// assert.Equal(t, left.Container.State, right.Container.State) -// assert.Equal(t, left.Container.ID, right.Container.ID) -// assert.Equal(t, left.Name, right.Name) -// } - -// func TestSortContainers(t *testing.T) { -// appConfig := NewDummyAppConfig() -// appConfig.UserConfig = &config.UserConfig{ -// Gui: config.GuiConfig{ -// LegacySortContainers: false, -// }, -// } -// command := &DockerCommand{ -// Config: appConfig, -// } - -// containers := sampleContainers(appConfig) - -// sorted := expectedPerStatusContainers(appConfig) - -// ct := command.sortedContainers(containers) - -// assert.Equal(t, len(ct), len(sorted)) - -// for i := 0; i < len(ct); i++ { -// assertEqualContainers(t, sorted[i], ct[i]) -// } -// } - -// func TestLegacySortedContainers(t *testing.T) { -// appConfig := NewDummyAppConfig() -// appConfig.UserConfig = &config.UserConfig{ -// Gui: config.GuiConfig{ -// LegacySortContainers: true, -// }, -// } -// command := &DockerCommand{ -// Config: appConfig, -// } - -// containers := sampleContainers(appConfig) - -// sorted := expectedLegacySortedContainers(appConfig) - -// ct := command.sortedContainers(containers) - -// for i := 0; i < len(ct); i++ { -// assertEqualContainers(t, sorted[i], ct[i]) -// } -// } diff --git a/pkg/gui/containers_panel.go b/pkg/gui/containers_panel.go index a55e6b7c..0744d547 100644 --- a/pkg/gui/containers_panel.go +++ b/pkg/gui/containers_panel.go @@ -16,12 +16,6 @@ import ( ) func (gui *Gui) getContainersPanel() *SideListPanel[*commands.Container] { - states := map[string]int{ - "running": 1, - "exited": 2, - "created": 3, - } - // Standalone containers are containers which are either one-off containers, or whose service is not part of this docker-compose context. isStandaloneContainer := func(container *commands.Container) bool { if container.OneOff || container.ServiceName == "" { @@ -77,17 +71,7 @@ func (gui *Gui) getContainersPanel() *SideListPanel[*commands.Container] { // sortedContainers returns containers sorted by state if c.SortContainersByState is true (follows 1- running, 2- exited, 3- created) // and sorted by name if c.SortContainersByState is false sort: func(a *commands.Container, b *commands.Container) bool { - if gui.Config.UserConfig.Gui.LegacySortContainers { - return a.Name < b.Name - } - - stateLeft := states[a.Container.State] - stateRight := states[b.Container.State] - if stateLeft == stateRight { - return a.Name < b.Name - } - - return states[a.Container.State] < states[b.Container.State] + return sortContainers(a, b, gui.Config.UserConfig.Gui.LegacySortContainers) }, filter: func(container *commands.Container) bool { // Note that this is O(N*M) time complexity where N is the number of services @@ -118,6 +102,26 @@ func (gui *Gui) getContainersPanel() *SideListPanel[*commands.Container] { } } +var containerStates = map[string]int{ + "running": 1, + "exited": 2, + "created": 3, +} + +func sortContainers(a *commands.Container, b *commands.Container, legacySort bool) bool { + if legacySort { + return a.Name < b.Name + } + + stateLeft := containerStates[a.Container.State] + stateRight := containerStates[b.Container.State] + if stateLeft == stateRight { + return a.Name < b.Name + } + + return containerStates[a.Container.State] < containerStates[b.Container.State] +} + func (gui *Gui) renderContainerEnv(container *commands.Container) error { if !container.DetailsLoaded() { return gui.T.NewTask(func(stop chan struct{}) { diff --git a/pkg/gui/sort_container_test.go b/pkg/gui/sort_container_test.go new file mode 100644 index 00000000..ed194888 --- /dev/null +++ b/pkg/gui/sort_container_test.go @@ -0,0 +1,148 @@ +package gui + +import ( + "sort" + "testing" + + "github.com/docker/docker/api/types" + "github.com/jesseduffield/lazydocker/pkg/commands" + "github.com/stretchr/testify/assert" +) + +func sampleContainers() []*commands.Container { + return []*commands.Container{ + { + ID: "1", + Name: "1", + Container: types.Container{ + State: "exited", + }, + }, + { + ID: "2", + Name: "2", + Container: types.Container{ + State: "running", + }, + }, + { + ID: "3", + Name: "3", + Container: types.Container{ + State: "running", + }, + }, + { + ID: "4", + Name: "4", + Container: types.Container{ + State: "created", + }, + }, + } +} + +func expectedPerStatusContainers() []*commands.Container { + return []*commands.Container{ + { + ID: "2", + Name: "2", + Container: types.Container{ + State: "running", + }, + }, + { + ID: "3", + Name: "3", + Container: types.Container{ + State: "running", + }, + }, + { + ID: "1", + Name: "1", + Container: types.Container{ + State: "exited", + }, + }, + { + ID: "4", + Name: "4", + Container: types.Container{ + State: "created", + }, + }, + } +} + +func expectedLegacySortedContainers() []*commands.Container { + return []*commands.Container{ + { + ID: "1", + Name: "1", + Container: types.Container{ + State: "exited", + }, + }, + { + ID: "2", + Name: "2", + Container: types.Container{ + State: "running", + }, + }, + { + ID: "3", + Name: "3", + Container: types.Container{ + State: "running", + }, + }, + { + ID: "4", + Name: "4", + Container: types.Container{ + State: "created", + }, + }, + } +} + +func assertEqualContainers(t *testing.T, left *commands.Container, right *commands.Container) { + t.Helper() + assert.Equal(t, left.Container.State, right.Container.State) + assert.Equal(t, left.Container.ID, right.Container.ID) + assert.Equal(t, left.Name, right.Name) +} + +func TestSortContainers(t *testing.T) { + actual := sampleContainers() + + expected := expectedPerStatusContainers() + + sort.Slice(actual, func(i, j int) bool { + return sortContainers(actual[i], actual[j], false) + }) + + assert.Equal(t, len(actual), len(expected)) + + for i := 0; i < len(actual); i++ { + assertEqualContainers(t, expected[i], actual[i]) + } +} + +func TestLegacySortedContainers(t *testing.T) { + actual := sampleContainers() + + expected := expectedLegacySortedContainers() + + sort.Slice(actual, func(i, j int) bool { + return sortContainers(actual[i], actual[j], true) + }) + + assert.Equal(t, len(actual), len(expected)) + + for i := 0; i < len(actual); i++ { + assertEqualContainers(t, expected[i], actual[i]) + } +}