verify that DisableFlagsInUseLine is set for all commands
This replaces the visitAll recursive function with a test that verifies that the option is set for all commands and subcommands, so that it doesn't have to be modified at runtime. We currently still have to loop over all functions for the setValidateArgs call, but that can be looked at separately. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
@ -163,11 +163,7 @@ func newDockerCommand(dockerCli *command.DockerCli) *cli.TopLevelCommand {
|
||||
cmd.SetOut(dockerCli.Out())
|
||||
commands.AddCommands(cmd, dockerCli)
|
||||
|
||||
visitAll(cmd,
|
||||
setValidateArgs(dockerCli),
|
||||
// prevent adding "[flags]" to the end of the usage line.
|
||||
func(c *cobra.Command) { c.DisableFlagsInUseLine = true },
|
||||
)
|
||||
visitAll(cmd, setValidateArgs(dockerCli))
|
||||
|
||||
// flags must be the top-level command flags, not cmd.Flags()
|
||||
return cli.NewTopLevelCommand(cmd, dockerCli, opts, cmd.Flags())
|
||||
|
||||
@ -11,6 +11,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/docker/cli/cli/command"
|
||||
"github.com/docker/cli/cli/command/commands"
|
||||
"github.com/docker/cli/cli/debug"
|
||||
platformsignals "github.com/docker/cli/cmd/docker/internal/signals"
|
||||
"github.com/sirupsen/logrus"
|
||||
@ -19,6 +20,22 @@ import (
|
||||
is "gotest.tools/v3/assert/cmp"
|
||||
)
|
||||
|
||||
func TestDisableFlagsInUseLineIsSet(t *testing.T) {
|
||||
dockerCli, err := command.NewDockerCli(command.WithBaseContext(context.TODO()))
|
||||
assert.NilError(t, err)
|
||||
rootCmd := &cobra.Command{DisableFlagsInUseLine: true}
|
||||
commands.AddCommands(rootCmd, dockerCli)
|
||||
|
||||
var errs []error
|
||||
visitAll(rootCmd, func(c *cobra.Command) {
|
||||
if !c.DisableFlagsInUseLine {
|
||||
errs = append(errs, errors.New("DisableFlagsInUseLine is not set for "+c.CommandPath()))
|
||||
}
|
||||
})
|
||||
err = errors.Join(errs...)
|
||||
assert.NilError(t, err)
|
||||
}
|
||||
|
||||
func TestClientDebugEnabled(t *testing.T) {
|
||||
defer debug.Disable()
|
||||
ctx, cancel := context.WithCancel(context.TODO())
|
||||
|
||||
Reference in New Issue
Block a user