From 176bce040622aea68deeb0ef536265eab22e949a Mon Sep 17 00:00:00 2001 From: arsalanyavari Date: Sat, 5 Dec 2026 01:06:37 +0330 Subject: [PATCH] context update: allow clearing the description `docker context update --description ''` reported success but kept the old description, because an empty value was treated the same as the flag not being set. Check whether the flag was passed instead. Signed-off-by: arsalanyavari --- cli/command/context/update.go | 8 +++-- cli/command/context/update_test.go | 50 +++++++++++++++++++++++++++++- 2 files changed, 54 insertions(+), 4 deletions(-) diff --git a/cli/command/context/update.go b/cli/command/context/update.go index cdd79e578d33..2440bded160e 100644 --- a/cli/command/context/update.go +++ b/cli/command/context/update.go @@ -14,8 +14,9 @@ import ( // updateOptions are the options used to update a context. type updateOptions struct { - description string - endpoint map[string]string + description string + endpoint map[string]string + descriptionChanged bool } func longUpdateDescription() string { @@ -38,6 +39,7 @@ func newUpdateCommand(dockerCLI command.Cli) *cobra.Command { Short: "Update a context", Args: cli.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { + opts.descriptionChanged = cmd.Flags().Changed("description") return runUpdate(dockerCLI, args[0], opts) }, Long: longUpdateDescription(), @@ -64,7 +66,7 @@ func runUpdate(dockerCLI command.Cli, name string, opts updateOptions) error { if err != nil { return err } - if opts.description != "" { + if opts.descriptionChanged { dockerContext.Description = opts.description } diff --git a/cli/command/context/update_test.go b/cli/command/context/update_test.go index c2fc968c2ab5..76bd3981c024 100644 --- a/cli/command/context/update_test.go +++ b/cli/command/context/update_test.go @@ -1,6 +1,7 @@ package context import ( + "io" "testing" "github.com/docker/cli/cli/command" @@ -18,7 +19,8 @@ func TestUpdateDescriptionOnly(t *testing.T) { cli.OutBuffer().Reset() cli.ErrBuffer().Reset() assert.NilError(t, runUpdate(cli, "test", updateOptions{ - description: "description", + description: "description", + descriptionChanged: true, })) c, err := cli.ContextStore().GetMetadata("test") assert.NilError(t, err) @@ -30,6 +32,52 @@ func TestUpdateDescriptionOnly(t *testing.T) { assert.Equal(t, "Successfully updated context \"test\"\n", cli.ErrBuffer().String()) } +func TestUpdateDescriptionFlag(t *testing.T) { + tests := []struct { + doc string + args []string + expectedDescription string + }{ + { + doc: "description not set keeps existing description", + args: []string{"test"}, + expectedDescription: "description of test", + }, + { + doc: "empty description clears existing description", + args: []string{"test", "--description="}, + expectedDescription: "", + }, + { + doc: "empty description as separate argument clears existing description", + args: []string{"test", "--description", ""}, + expectedDescription: "", + }, + { + doc: "description replaces existing description", + args: []string{"test", "--description=new description"}, + expectedDescription: "new description", + }, + } + for _, tc := range tests { + t.Run(tc.doc, func(t *testing.T) { + cli := makeFakeCli(t) + createTestContext(t, cli, "test", nil) + cmd := newUpdateCommand(cli) + cmd.SetArgs(tc.args) + cmd.SetOut(io.Discard) + cmd.SetErr(io.Discard) + assert.NilError(t, cmd.Execute()) + + c, err := cli.ContextStore().GetMetadata("test") + assert.NilError(t, err) + dc, err := command.GetDockerContext(c) + assert.NilError(t, err) + assert.Equal(t, dc.Description, tc.expectedDescription) + }) + } +} + func TestUpdateDockerOnly(t *testing.T) { cli := makeFakeCli(t) createTestContext(t, cli, "test", nil)