diff --git a/cmd/docker/docker.go b/cmd/docker/docker.go index 22b978de9b..dafaea0703 100644 --- a/cmd/docker/docker.go +++ b/cmd/docker/docker.go @@ -69,16 +69,22 @@ func newDockerCommand(dockerCli *command.DockerCli) *cli.TopLevelCommand { return cli.NewTopLevelCommand(cmd, dockerCli, opts, flags) } -func setFlagErrorFunc(dockerCli *command.DockerCli, cmd *cobra.Command) { +func setFlagErrorFunc(dockerCli command.Cli, cmd *cobra.Command) { // When invoking `docker stack --nonsense`, we need to make sure FlagErrorFunc return appropriate // output if the feature is not supported. // As above cli.SetupRootCommand(cmd) have already setup the FlagErrorFunc, we will add a pre-check before the FlagErrorFunc // is called. flagErrorFunc := cmd.FlagErrorFunc() cmd.SetFlagErrorFunc(func(cmd *cobra.Command, err error) error { + if err := pluginmanager.AddPluginCommandStubs(dockerCli, cmd.Root()); err != nil { + return err + } if err := isSupported(cmd, dockerCli); err != nil { return err } + if err := hideUnsupportedFeatures(cmd, dockerCli); err != nil { + return err + } return flagErrorFunc(cmd, err) }) } diff --git a/e2e/cli-plugins/help_test.go b/e2e/cli-plugins/help_test.go index 8e57ccc876..b73054d4a3 100644 --- a/e2e/cli-plugins/help_test.go +++ b/e2e/cli-plugins/help_test.go @@ -74,18 +74,36 @@ func TestGlobalHelp(t *testing.T) { assert.Assert(t, is.Equal(badmetacount, 1)) // Running with `--help` should produce the same. - res2 := icmd.RunCmd(run("--help")) - res2.Assert(t, icmd.Expected{ - ExitCode: 0, + t.Run("help_flag", func(t *testing.T) { + res2 := icmd.RunCmd(run("--help")) + res2.Assert(t, icmd.Expected{ + ExitCode: 0, + }) + assert.Assert(t, is.Equal(res2.Stdout(), res.Stdout())) + assert.Assert(t, is.Equal(res2.Stderr(), "")) }) - assert.Assert(t, is.Equal(res2.Stdout(), res.Stdout())) - assert.Assert(t, is.Equal(res2.Stderr(), "")) // Running just `docker` (without `help` nor `--help`) should produce the same thing, except on Stderr. - res2 = icmd.RunCmd(run()) - res2.Assert(t, icmd.Expected{ - ExitCode: 0, + t.Run("bare", func(t *testing.T) { + res2 := icmd.RunCmd(run()) + res2.Assert(t, icmd.Expected{ + ExitCode: 0, + }) + assert.Assert(t, is.Equal(res2.Stdout(), "")) + assert.Assert(t, is.Equal(res2.Stderr(), res.Stdout())) + }) + + t.Run("badopt", func(t *testing.T) { + // Running `docker --badopt` should also produce the + // same thing, give or take the leading error message + // and a trailing carriage return (due to main() using + // Println in the error case). + res2 := icmd.RunCmd(run("--badopt")) + res2.Assert(t, icmd.Expected{ + ExitCode: 125, + }) + assert.Assert(t, is.Equal(res2.Stdout(), "")) + exp := "unknown flag: --badopt\nSee 'docker --help'.\n" + res.Stdout() + "\n" + assert.Assert(t, is.Equal(res2.Stderr(), exp)) }) - assert.Assert(t, is.Equal(res2.Stdout(), "")) - assert.Assert(t, is.Equal(res2.Stderr(), res.Stdout())) }