Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions cli/command/image/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func runImages(ctx context.Context, dockerCLI command.Cli, options imagesOptions

images := res.Items
if !options.all {
if _, ok := filters["dangling"]; !ok {
if dangling, ok := filters["dangling"]; !ok || dangling["false"] {
images = slices.DeleteFunc(images, isDangling)
}
}
Expand All @@ -127,7 +127,6 @@ func runImages(ctx context.Context, dockerCLI command.Cli, options imagesOptions
if useTree {
return runTree(ctx, dockerCLI, treeOptions{
images: images,
all: options.all,
filters: filters,
expanded: options.tree,
})
Expand Down
82 changes: 82 additions & 0 deletions cli/command/image/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"errors"
"fmt"
"io"
"slices"
"testing"

"github.com/docker/cli/cli/config/configfile"
"github.com/docker/cli/internal/test"
"github.com/moby/moby/api/types/image"
"github.com/moby/moby/client"
"gotest.tools/v3/assert"
"gotest.tools/v3/golden"
Expand Down Expand Up @@ -116,6 +118,86 @@ func TestNewListCommandAmbiguous(t *testing.T) {
golden.Assert(t, cli.ErrBuffer().String(), "list-command-ambiguous.golden")
}

func TestImagesFilterDangling(t *testing.T) {
// Create test images with different states
items := []image.Summary{
{
ID: "sha256:87428fc522803d31065e7bce3cf03fe475096631e5e07bbd7a0fde60c4cf25c7",
RepoTags: []string{"myimage:latest"},
RepoDigests: []string{"myimage@sha256:abc123"},
},
{
ID: "sha256:0263829989b6fd954f72baaf2fc64bc2e2f01d692d4de72986ea808f6e99813f",
RepoTags: []string{},
RepoDigests: []string{},
},
{
ID: "sha256:a3a5e715f0cc574a73c3f9bebb6bc24f32ffd5b67b387244c2c909da779a1478",
RepoTags: []string{},
RepoDigests: []string{"image@sha256:a3a5e715f0cc574a73c3f9bebb6bc24f32ffd5b67b387244c2c909da779a1478"},
},
}

testCases := []struct {
name string
args []string
imageListFunc func(options client.ImageListOptions) (client.ImageListResult, error)
}{
{
name: "dangling-true",
args: []string{"-f", "dangling=true"},
imageListFunc: func(options client.ImageListOptions) (client.ImageListResult, error) {
// Verify the filter is passed to the API
assert.Check(t, options.Filters["dangling"]["true"])
// dangling=true is handled on the server side and returns only dangling images
return client.ImageListResult{Items: []image.Summary{items[1], items[2]}}, nil
},
},
{
name: "dangling-false",
args: []string{"-f", "dangling=false"},
imageListFunc: func(options client.ImageListOptions) (client.ImageListResult, error) {
// Verify the filter is passed to the API
assert.Check(t, options.Filters["dangling"]["false"])
// Return all images including dangling
return client.ImageListResult{Items: slices.Clone(items)}, nil
},
},
{
name: "no-dangling-filter",
args: []string{},
imageListFunc: func(options client.ImageListOptions) (client.ImageListResult, error) {
// Verify no dangling filter is passed to the API
_, exists := options.Filters["dangling"]
assert.Check(t, !exists)
// Return all images including dangling
return client.ImageListResult{Items: slices.Clone(items)}, nil
},
},
{
name: "all-flag",
args: []string{"--all"},
imageListFunc: func(options client.ImageListOptions) (client.ImageListResult, error) {
// Verify the All flag is set
assert.Check(t, options.All)
// Return all images including dangling
return client.ImageListResult{Items: slices.Clone(items)}, nil
},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
cli := test.NewFakeCli(&fakeClient{imageListFunc: tc.imageListFunc})
cmd := newImagesCommand(cli)
cmd.SetArgs(tc.args)
err := cmd.Execute()
assert.NilError(t, err)
golden.Assert(t, cli.OutBuffer().String(), fmt.Sprintf("list-command-filter-dangling.%s.golden", tc.name))
})
}
}

func nilToEmptySlice[T any](s []T) []T {
if s == nil {
return []T{}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
IMAGE ID DISK USAGE CONTENT SIZE EXTRA
myimage:latest 87428fc52280 0B 0B
<untagged> 0263829989b6 0B 0B
<untagged> a3a5e715f0cc 0B 0B
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
IMAGE ID DISK USAGE CONTENT SIZE EXTRA
myimage:latest 87428fc52280 0B 0B
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
IMAGE ID DISK USAGE CONTENT SIZE EXTRA
<untagged> 0263829989b6 0B 0B
<untagged> a3a5e715f0cc 0B 0B
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
IMAGE ID DISK USAGE CONTENT SIZE EXTRA
myimage:latest 87428fc52280 0B 0B
1 change: 0 additions & 1 deletion cli/command/image/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ const untaggedName = "<untagged>"

type treeOptions struct {
images []imagetypes.Summary
all bool
filters client.Filters
expanded bool
}
Expand Down
Loading