mirror of
https://github.com/jesseduffield/lazydocker.git
synced 2026-07-21 23:01:03 +00:00
adding tests for legacy and new default sort for containers
This commit is contained in:
parent
db288a1727
commit
615ce6536b
2 changed files with 156 additions and 1 deletions
|
|
@ -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
|
||||
|
|
|
|||
155
pkg/commands/sort_container_test.go
Normal file
155
pkg/commands/sort_container_test.go
Normal file
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue