From 22439e119bbb1a01986cbdac57cb30cad646b20b Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Mon, 29 Jun 2026 10:01:07 +0800 Subject: [PATCH] TestCliCmdCompletion --- cmd/cmdtest/cmd_test.go | 11 +++++++++++ cmd/main.go | 5 ++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/cmd/cmdtest/cmd_test.go b/cmd/cmdtest/cmd_test.go index 18aaebe6e8c..f41793c8e44 100644 --- a/cmd/cmdtest/cmd_test.go +++ b/cmd/cmdtest/cmd_test.go @@ -235,3 +235,14 @@ func TestCliCmdBefore(t *testing.T) { assert.Equal(t, "/tmp/any.ini", configValues["before"], "BeforeFunc must be called before preparing config") assert.Equal(t, "/dev/null", configValues["action"]) } + +func TestCliCmdCompletion(t *testing.T) { + app := newTestApp(cli.Command{ + Action: func(ctx context.Context, cmd *cli.Command) error { return nil }, + }) + res, err := runTestApp(app, "./gitea", "completion", "bash", "--nonexist") + assert.Error(t, err) + assert.Equal(t, 1, res.ExitCode) + assert.Equal(t, "", res.Stdout) + assert.Equal(t, "Incorrect Usage: flag provided but not defined: -nonexist\n", res.Stderr) +} diff --git a/cmd/main.go b/cmd/main.go index b028e9eebfe..0af2f324125 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -177,9 +177,8 @@ func RunMainApp(app *cli.Command, args ...string) error { ctx, cancel := installSignals() defer cancel() setCLIOnUsageError(app) - // FIXME: why it needs to set ConfigureShellCompletionCommand? What's wrong without it? - // // the completion subcommands are built during app.Run, after the Walk above, so cover them via this hook - // app.ConfigureShellCompletionCommand = setCLIOnUsageError + // the completion subcommands are built during app.Run, after the Walk above, so cover them via this hook + app.ConfigureShellCompletionCommand = setCLIOnUsageError err := app.Run(ctx, args) if err == nil { return nil