fix(generate-entry): detect distro from the image, not the box name

Signed-off-by: Luca Di Maio <luca.dimaio1@gmail.com>
This commit is contained in:
Luca Di Maio
2026-06-25 09:07:22 +02:00
parent ca404a285d
commit cba73c8f04
2 changed files with 49 additions and 1 deletions
+14 -1
View File
@@ -126,7 +126,20 @@ func (c *GenerateEntryCommand) resolveTargets(
}
return names, images, "auto", nil
case opts.ContainerName != "":
return []string{opts.ContainerName}, nil, opts.Icon, nil
// Look up the container's image so auto-detection keys off the distro
// (image) rather than the box name: a box named "dev" running
// Ubuntu should still get the Ubuntu icon. Non-fatal if the manager
// can't be reached — we fall back to the name in Execute.
images := map[string]string{}
if listResult, err := c.listCommand.Execute(ctx); err == nil {
for _, container := range listResult.Containers {
if container.Name == opts.ContainerName {
images[opts.ContainerName] = container.Image
break
}
}
}
return []string{opts.ContainerName}, images, opts.Icon, nil
default:
return []string{defaultContainerName}, nil, opts.Icon, nil
}
+35
View File
@@ -4,6 +4,7 @@ import (
"context"
"fmt"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
@@ -11,7 +12,9 @@ import (
"github.com/89luca89/distrobox/pkg/commands"
"github.com/89luca89/distrobox/pkg/config"
"github.com/89luca89/distrobox/pkg/containermanager"
"github.com/89luca89/distrobox/pkg/containermanager/providers"
"github.com/89luca89/distrobox/pkg/internal/testutil"
)
func TestGenerateEntryCommand_Execute(t *testing.T) {
@@ -184,3 +187,35 @@ func TestGenerateAllEntriesCommand_Execute(t *testing.T) {
assert.NoFileExists(t, expectedEntryPath)
}
}
// single-container generate-entry detects the distro from the container's
// image, not its name. A box named "dev" running Ubuntu gets the Ubuntu icon.
func TestGenerateEntryCommand_SingleModeUsesImageHint(t *testing.T) {
tempDir := t.TempDir()
mock := &testutil.MockContainerManager{
ListContainersResult: []containermanager.Container{
{
Name: "dev",
Image: "docker.io/library/ubuntu:22.04",
Status: "Exited",
Labels: map[string]string{"manager": "distrobox"},
},
},
}
listCmd := commands.NewListCommand(&config.Values{}, mock)
cmd := commands.NewGenerateEntryCommand(&config.Values{}, listCmd)
err := cmd.Execute(context.Background(), &commands.GenerateEntryOptions{
ContainerName: "dev",
Icon: "auto",
DesktopEntryBaseDir: tempDir,
DistroboxPath: "/usr/bin/distrobox",
})
require.NoError(t, err)
content, err := os.ReadFile(filepath.Join(tempDir, "applications", "dev.desktop"))
require.NoError(t, err)
assert.Contains(t, string(content), "ubuntu-distrobox.png",
"single-mode must key auto-detection off the image, not the box name 'dev'")
}