Merge pull request #1850 from ijc/include-plugins-in-badopt-help

Include plugins in `docker --badopt` help output
This commit is contained in:
Silvin Lubecki 2019-05-13 15:50:44 +02:00 committed by GitHub
commit 245bfd15cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 11 deletions

View File

@ -69,16 +69,22 @@ func newDockerCommand(dockerCli *command.DockerCli) *cli.TopLevelCommand {
return cli.NewTopLevelCommand(cmd, dockerCli, opts, flags) 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 // When invoking `docker stack --nonsense`, we need to make sure FlagErrorFunc return appropriate
// output if the feature is not supported. // 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 // As above cli.SetupRootCommand(cmd) have already setup the FlagErrorFunc, we will add a pre-check before the FlagErrorFunc
// is called. // is called.
flagErrorFunc := cmd.FlagErrorFunc() flagErrorFunc := cmd.FlagErrorFunc()
cmd.SetFlagErrorFunc(func(cmd *cobra.Command, err error) error { 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 { if err := isSupported(cmd, dockerCli); err != nil {
return err return err
} }
if err := hideUnsupportedFeatures(cmd, dockerCli); err != nil {
return err
}
return flagErrorFunc(cmd, err) return flagErrorFunc(cmd, err)
}) })
} }

View File

@ -74,18 +74,36 @@ func TestGlobalHelp(t *testing.T) {
assert.Assert(t, is.Equal(badmetacount, 1)) assert.Assert(t, is.Equal(badmetacount, 1))
// Running with `--help` should produce the same. // Running with `--help` should produce the same.
res2 := icmd.RunCmd(run("--help")) t.Run("help_flag", func(t *testing.T) {
res2.Assert(t, icmd.Expected{ res2 := icmd.RunCmd(run("--help"))
ExitCode: 0, 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. // Running just `docker` (without `help` nor `--help`) should produce the same thing, except on Stderr.
res2 = icmd.RunCmd(run()) t.Run("bare", func(t *testing.T) {
res2.Assert(t, icmd.Expected{ res2 := icmd.RunCmd(run())
ExitCode: 0, 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()))
} }