Skip to content
Open
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
8 changes: 5 additions & 3 deletions cli/command/context/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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(),
Expand All @@ -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
}

Expand Down
50 changes: 49 additions & 1 deletion cli/command/context/update_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package context

import (
"io"
"testing"

"github.com/docker/cli/cli/command"
Expand All @@ -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)
Expand All @@ -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)
Expand Down