From 615ce6536bdbb8b99418cf6562abca887803cea8 Mon Sep 17 00:00:00 2001 From: lpessoa Date: Mon, 3 Jan 2022 22:52:51 +0000 Subject: [PATCH] adding tests for legacy and new default sort for containers --- pkg/commands/docker.go | 2 +- pkg/commands/sort_container_test.go | 155 ++++++++++++++++++++++++++++ 2 files changed, 156 insertions(+), 1 deletion(-) create mode 100644 pkg/commands/sort_container_test.go diff --git a/pkg/commands/docker.go b/pkg/commands/docker.go index 58374445..98ac4825 100644 --- a/pkg/commands/docker.go +++ b/pkg/commands/docker.go @@ -285,7 +285,7 @@ func (c *DockerCommand) sortedContainers(containers []*Container) []*Container { "exited": 2, "created": 3, } - return states[containers[i].Container.State] > states[containers[j].Container.State] + return states[containers[i].Container.State] < states[containers[j].Container.State] }) } return containers diff --git a/pkg/commands/sort_container_test.go b/pkg/commands/sort_container_test.go new file mode 100644 index 00000000..d92fcafd --- /dev/null +++ b/pkg/commands/sort_container_test.go @@ -0,0 +1,155 @@ +package commands + +import ( + "github.com/docker/docker/api/types" + "github.com/jesseduffield/lazydocker/pkg/config" + "github.com/stretchr/testify/assert" + "testing" +) + +func sampleContainers(userConfig *config.AppConfig) []*Container { + return []*Container{ + { + ID: "1", + Container: types.Container{ + State: "exited", + }, + Config: userConfig, + }, + { + ID: "2", + Container: types.Container{ + State: "running", + }, + Config: userConfig, + }, + { + ID: "3", + Container: types.Container{ + State: "running", + }, + Config: userConfig, + }, + { + ID: "4", + Container: types.Container{ + State: "created", + }, + Config: userConfig, + }, + } +} + +func expectedPerStatusContainers(appConfig *config.AppConfig) []*Container { + return []*Container{ + { + ID: "2", + Container: types.Container{ + State: "running", + }, + Config: appConfig, + }, + { + ID: "3", + Container: types.Container{ + State: "running", + }, + Config: appConfig, + }, + { + ID: "1", + Container: types.Container{ + State: "exited", + }, + Config: appConfig, + }, + { + ID: "4", + Container: types.Container{ + State: "created", + }, + Config: appConfig, + }, + } +} + +func expectedLegacySortedContainers(appConfig *config.AppConfig) []*Container { + return []*Container{ + { + ID: "1", + Container: types.Container{ + State: "exited", + }, + Config: appConfig, + }, + { + ID: "2", + Container: types.Container{ + State: "running", + }, + Config: appConfig, + }, + { + ID: "3", + Container: types.Container{ + State: "running", + }, + Config: appConfig, + }, + { + ID: "4", + Container: types.Container{ + State: "created", + }, + Config: appConfig, + }, + } +} + +func TestSortContainers(t *testing.T) { + appConfig := NewDummyAppConfig() + appConfig.UserConfig = &config.UserConfig{ + Gui: config.GuiConfig{ + SortContainersByName: 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++ { + assert.Equal(t, ct[i].Container.State, sorted[i].Container.State) + assert.Equal(t, ct[i].Container.ID, sorted[i].Container.ID) + } +} + +func TestLegacySortedContainers(t *testing.T) { + appConfig := NewDummyAppConfig() + appConfig.UserConfig = &config.UserConfig{ + Gui: config.GuiConfig{ + SortContainersByName: true, + }, + } + command := &DockerCommand{ + Config: appConfig, + } + + containers := sampleContainers(appConfig) + + sorted := expectedLegacySortedContainers(appConfig) + + ct := command.sortedContainers(containers) + + for i := 0; i < len(ct); i++ { + assert.Equal(t, sorted[i].Container.State, ct[i].Container.State,) + assert.Equal(t, sorted[i].Container.ID, ct[i].Container.ID) + } +}