fix(generate-entry): refuse a container that does not exist

Signed-off-by: Luca Di Maio <luca.dimaio1@gmail.com>
This commit is contained in:
Luca Di Maio committed 2026-06-25 09:07:22 +02:00
1 parent 348b79050f
commit cc11c2d48b
2 files changed
+46

No files matched your search

+5
View File
@@ -146,12 +146,17 @@ func (c *GenerateEntryCommand) resolveTargets(
// 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 {
found := false
for _, container := range listResult.Containers {
if container.Name == opts.ContainerName {
images[opts.ContainerName] = container.Image
found = true
break
}
}
if !found && !opts.Delete {
return nil, nil, "", fmt.Errorf("cannot find container %s, please create it first", opts.ContainerName)
}
}
return []string{opts.ContainerName}, images, opts.Icon, nil
default:
+41
View File
@@ -225,3 +225,44 @@ func TestGenerateEntryCommand_SingleModeUsesImageHint(t *testing.T) {
assert.Contains(t, string(content), "ubuntu-distrobox.png",
"single-mode must key auto-detection off the image (cached icon path), not the box name 'dev'")
}
// generate-entry refuses to write an entry for a container that does not
// exist (matches the shell, distrobox-generate-entry:285-288).
func TestGenerateEntryCommand_RefusesNonExistentContainer(t *testing.T) {
tempDir := t.TempDir()
mock := &testutil.MockContainerManager{
ListContainersResult: []containermanager.Container{
{Name: "other", Image: "alpine", 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: "ghost",
DesktopEntryBaseDir: tempDir,
DistroboxPath: "/usr/bin/distrobox",
})
require.Error(t, err)
assert.Contains(t, err.Error(), "cannot find container ghost")
assert.NoFileExists(t, filepath.Join(tempDir, "applications", "ghost.desktop"))
}
// --delete must NOT require the container to exist (its box may already
// be gone when rm cleans up the entry).
func TestGenerateEntryCommand_DeleteNonExistentNoError(t *testing.T) {
tempDir := t.TempDir()
mock := &testutil.MockContainerManager{
ListContainersResult: []containermanager.Container{},
}
listCmd := commands.NewListCommand(&config.Values{}, mock)
cmd := commands.NewGenerateEntryCommand(&config.Values{}, listCmd)
err := cmd.Execute(context.Background(), &commands.GenerateEntryOptions{
ContainerName: "ghost",
Delete: true,
DesktopEntryBaseDir: tempDir,
DistroboxPath: "/usr/bin/distrobox",
})
require.NoError(t, err)
}