mirror of
https://github.com/jesseduffield/lazydocker.git
synced 2026-07-25 08:31:03 +00:00
Merge f63ba854a4 into 7e7aadc207
This commit is contained in:
commit
383d127dd6
2 changed files with 94 additions and 1 deletions
87
pkg/gui/container_env_test.go
Normal file
87
pkg/gui/container_env_test.go
Normal file
|
|
@ -0,0 +1,87 @@
|
||||||
|
package gui
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/docker/docker/api/types/container"
|
||||||
|
"github.com/jesseduffield/lazydocker/pkg/commands"
|
||||||
|
"github.com/jesseduffield/lazydocker/pkg/i18n"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestContainerEnvAlphabeticalSorting(t *testing.T) {
|
||||||
|
// Create a mock Gui with minimal setup
|
||||||
|
log := logrus.NewEntry(logrus.New())
|
||||||
|
gui := &Gui{
|
||||||
|
Log: log,
|
||||||
|
Tr: i18n.NewTranslationSet(log, "en"),
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a container with unsorted environment variables
|
||||||
|
c := &commands.Container{}
|
||||||
|
c.Details = container.InspectResponse{
|
||||||
|
ContainerJSONBase: &container.ContainerJSONBase{},
|
||||||
|
Config: &container.Config{
|
||||||
|
Env: []string{
|
||||||
|
"ZEBRA=value1",
|
||||||
|
"ALPHA=value2",
|
||||||
|
"MIDDLE=value3",
|
||||||
|
"BETA=value4",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// Call containerEnv
|
||||||
|
result := gui.containerEnv(c)
|
||||||
|
|
||||||
|
// The result should contain environment variables in alphabetical order
|
||||||
|
// Check that ALPHA comes before BETA, BETA before MIDDLE, and MIDDLE before ZEBRA
|
||||||
|
alphaIndex := strings.Index(result, "ALPHA")
|
||||||
|
betaIndex := strings.Index(result, "BETA")
|
||||||
|
middleIndex := strings.Index(result, "MIDDLE")
|
||||||
|
zebraIndex := strings.Index(result, "ZEBRA")
|
||||||
|
|
||||||
|
assert.True(t, alphaIndex >= 0, "ALPHA should be present in result")
|
||||||
|
assert.True(t, betaIndex >= 0, "BETA should be present in result")
|
||||||
|
assert.True(t, middleIndex >= 0, "MIDDLE should be present in result")
|
||||||
|
assert.True(t, zebraIndex >= 0, "ZEBRA should be present in result")
|
||||||
|
|
||||||
|
assert.True(t, alphaIndex < betaIndex, "ALPHA should come before BETA")
|
||||||
|
assert.True(t, betaIndex < middleIndex, "BETA should come before MIDDLE")
|
||||||
|
assert.True(t, middleIndex < zebraIndex, "MIDDLE should come before ZEBRA")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestContainerEnvEmptyEnvironment(t *testing.T) {
|
||||||
|
log := logrus.NewEntry(logrus.New())
|
||||||
|
gui := &Gui{
|
||||||
|
Log: log,
|
||||||
|
Tr: i18n.NewTranslationSet(log, "en"),
|
||||||
|
}
|
||||||
|
|
||||||
|
c := &commands.Container{}
|
||||||
|
c.Details = container.InspectResponse{
|
||||||
|
ContainerJSONBase: &container.ContainerJSONBase{},
|
||||||
|
Config: &container.Config{
|
||||||
|
Env: []string{},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
result := gui.containerEnv(c)
|
||||||
|
assert.Contains(t, result, gui.Tr.NothingToDisplay)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestContainerEnvDetailsNotLoaded(t *testing.T) {
|
||||||
|
log := logrus.NewEntry(logrus.New())
|
||||||
|
gui := &Gui{
|
||||||
|
Log: log,
|
||||||
|
Tr: i18n.NewTranslationSet(log, "en"),
|
||||||
|
}
|
||||||
|
|
||||||
|
c := &commands.Container{}
|
||||||
|
// Details is not properly initialized, so DetailsLoaded() will return false
|
||||||
|
|
||||||
|
result := gui.containerEnv(c)
|
||||||
|
assert.Contains(t, result, gui.Tr.WaitingForContainerInfo)
|
||||||
|
}
|
||||||
|
|
@ -3,6 +3,7 @@ package gui
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
|
@ -155,7 +156,12 @@ func (gui *Gui) containerEnv(container *commands.Container) string {
|
||||||
return gui.Tr.NothingToDisplay
|
return gui.Tr.NothingToDisplay
|
||||||
}
|
}
|
||||||
|
|
||||||
envVarsList := lo.Map(container.Details.Config.Env, func(envVar string, _ int) []string {
|
// Create a sorted copy of the environment variables
|
||||||
|
sortedEnv := make([]string, len(container.Details.Config.Env))
|
||||||
|
copy(sortedEnv, container.Details.Config.Env)
|
||||||
|
sort.Strings(sortedEnv)
|
||||||
|
|
||||||
|
envVarsList := lo.Map(sortedEnv, func(envVar string, _ int) []string {
|
||||||
splitEnv := strings.SplitN(envVar, "=", 2)
|
splitEnv := strings.SplitN(envVar, "=", 2)
|
||||||
key := splitEnv[0]
|
key := splitEnv[0]
|
||||||
value := ""
|
value := ""
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue