diff --git a/pkg/gui/container_env_test.go b/pkg/gui/container_env_test.go new file mode 100644 index 00000000..b28c85c2 --- /dev/null +++ b/pkg/gui/container_env_test.go @@ -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) +} diff --git a/pkg/gui/containers_panel.go b/pkg/gui/containers_panel.go index a638ee29..24c131b5 100644 --- a/pkg/gui/containers_panel.go +++ b/pkg/gui/containers_panel.go @@ -3,6 +3,7 @@ package gui import ( "context" "fmt" + "sort" "strings" "time" @@ -155,7 +156,12 @@ func (gui *Gui) containerEnv(container *commands.Container) string { 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) key := splitEnv[0] value := ""