From d708cada43f31b79462e6d7325fbcfd75b47ae29 Mon Sep 17 00:00:00 2001 From: Akihiro Suda Date: Wed, 10 Oct 2018 18:16:27 +0900 Subject: [PATCH] Fix `docker invalid-subcommand` regression Starting with a3fe7d62b8274d69930286fb14653cccf37acadb, `docker invalid-subcommand` did not exit with non-zero status. Fix #1428 Signed-off-by: Akihiro Suda --- cmd/docker/docker.go | 3 +++ cmd/docker/docker_test.go | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/cmd/docker/docker.go b/cmd/docker/docker.go index a2bcd2a2cd..4b0c37c822 100644 --- a/cmd/docker/docker.go +++ b/cmd/docker/docker.go @@ -33,6 +33,9 @@ func newDockerCommand(dockerCli *command.DockerCli) *cobra.Command { SilenceErrors: true, TraverseChildren: true, Args: noArgs, + RunE: func(cmd *cobra.Command, args []string) error { + return command.ShowHelp(dockerCli.Err())(cmd, args) + }, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { // flags must be the top-level command flags, not cmd.Flags() opts.Common.SetDefaultOptions(flags) diff --git a/cmd/docker/docker_test.go b/cmd/docker/docker_test.go index 7f7336b63e..7c6e4526f4 100644 --- a/cmd/docker/docker_test.go +++ b/cmd/docker/docker_test.go @@ -1,6 +1,7 @@ package main import ( + "bytes" "io/ioutil" "os" "testing" @@ -31,3 +32,20 @@ func TestExitStatusForInvalidSubcommandWithHelpFlag(t *testing.T) { err := cmd.Execute() assert.Error(t, err, "unknown help topic: invalid") } + +func TestExitStatusForInvalidSubcommand(t *testing.T) { + discard := ioutil.Discard + cmd := newDockerCommand(command.NewDockerCli(os.Stdin, discard, discard, false, nil)) + cmd.SetArgs([]string{"invalid"}) + err := cmd.Execute() + assert.Check(t, is.ErrorContains(err, "docker: 'invalid' is not a docker command.")) +} + +func TestVersion(t *testing.T) { + var b bytes.Buffer + cmd := newDockerCommand(command.NewDockerCli(os.Stdin, &b, &b, false, nil)) + cmd.SetArgs([]string{"--version"}) + err := cmd.Execute() + assert.NilError(t, err) + assert.Check(t, is.Contains(b.String(), "Docker version")) +}