From c65ac2d90d271d52994985cb41587ce74258c30c Mon Sep 17 00:00:00 2001 From: Laura Brehm Date: Mon, 9 Sep 2024 13:35:17 +0100 Subject: [PATCH 1/2] volume/update: require 1 argument/fix panic This command was declaring that it requires at least 1 argument, when it needs exactly 1 argument. This was causing the CLI to panic when the command was invoked with no argument: `docker volume update` Signed-off-by: Laura Brehm (cherry picked from commit daea277ee839742be94e1f41d5c477f114a81273) Signed-off-by: Sebastiaan van Stijn --- cli/command/volume/update.go | 2 +- cli/command/volume/update_test.go | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 cli/command/volume/update_test.go diff --git a/cli/command/volume/update.go b/cli/command/volume/update.go index c04c2ff8f507..42ce9ac586be 100644 --- a/cli/command/volume/update.go +++ b/cli/command/volume/update.go @@ -18,7 +18,7 @@ func newUpdateCommand(dockerCli command.Cli) *cobra.Command { cmd := &cobra.Command{ Use: "update [OPTIONS] [VOLUME]", Short: "Update a volume (cluster volumes only)", - Args: cli.RequiresMaxArgs(1), + Args: cli.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { return runUpdate(cmd.Context(), dockerCli, args[0], availability, cmd.Flags()) }, diff --git a/cli/command/volume/update_test.go b/cli/command/volume/update_test.go new file mode 100644 index 000000000000..cffee60468ee --- /dev/null +++ b/cli/command/volume/update_test.go @@ -0,0 +1,22 @@ +package volume + +import ( + "io" + "testing" + + "github.com/docker/cli/internal/test" + "gotest.tools/v3/assert" +) + +func TestUpdateCmd(t *testing.T) { + cmd := newUpdateCommand( + test.NewFakeCli(&fakeClient{}), + ) + cmd.SetArgs([]string{}) + cmd.SetOut(io.Discard) + cmd.SetErr(io.Discard) + + err := cmd.Execute() + + assert.ErrorContains(t, err, "requires 1 argument") +} From 965699ba0f3316260fb621a78d5cdc05667da331 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Wed, 11 Sep 2024 13:26:06 +0200 Subject: [PATCH 2/2] cli/command/volume TestUpdateCmd: adjust for older error messages The error-message changed in newer versions, and no longer includes "exactly". This patch adjusts the test in the meantime. 59.13 === FAIL: cli/command/volume TestUpdateCmd (0.00s) 59.13 update_test.go:21: assertion failed: expected error to contain "requires 1 argument", got "\"update\" requires exactly 1 argument.\nSee 'update --help'.\n\nUsage: update [OPTIONS] [VOLUME] [flags]\n\nUpdate a volume (cluster volumes only)" Signed-off-by: Sebastiaan van Stijn --- cli/command/volume/update_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/command/volume/update_test.go b/cli/command/volume/update_test.go index cffee60468ee..13051a0056fa 100644 --- a/cli/command/volume/update_test.go +++ b/cli/command/volume/update_test.go @@ -18,5 +18,5 @@ func TestUpdateCmd(t *testing.T) { err := cmd.Execute() - assert.ErrorContains(t, err, "requires 1 argument") + assert.ErrorContains(t, err, "requires exactly 1 argument") }