Skip to content
Merged
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: 7 additions & 1 deletion cmd/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
Expand Down
38 changes: 28 additions & 10 deletions e2e/cli-plugins/help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
}