Skip to content

Commit d92b4cd

Browse files
authored
Fix incorrect CLI exit code and duplicate error message (#26346)
Follow the CLI refactoring, and add tests.
1 parent 4f51347 commit d92b4cd

File tree

4 files changed

+98
-27
lines changed

4 files changed

+98
-27
lines changed

cmd/main.go

+21-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package cmd
66
import (
77
"fmt"
88
"os"
9+
"strings"
910

1011
"code.gitea.io/gitea/modules/log"
1112
"code.gitea.io/gitea/modules/setting"
@@ -117,8 +118,12 @@ func prepareWorkPathAndCustomConf(action cli.ActionFunc) func(ctx *cli.Context)
117118
}
118119
}
119120

120-
func NewMainApp() *cli.App {
121+
func NewMainApp(version, versionExtra string) *cli.App {
121122
app := cli.NewApp()
123+
app.Name = "Gitea"
124+
app.Usage = "A painless self-hosted Git service"
125+
app.Description = `By default, Gitea will start serving using the web-server with no argument, which can alternatively be run by running the subcommand "web".`
126+
app.Version = version + versionExtra
122127
app.EnableBashCompletion = true
123128

124129
// these sub-commands need to use config file
@@ -166,3 +171,18 @@ func NewMainApp() *cli.App {
166171

167172
return app
168173
}
174+
175+
func RunMainApp(app *cli.App, args ...string) error {
176+
err := app.Run(args)
177+
if err == nil {
178+
return nil
179+
}
180+
if strings.HasPrefix(err.Error(), "flag provided but not defined:") {
181+
// the cli package should already have output the error message, so just exit
182+
cli.OsExiter(1)
183+
return err
184+
}
185+
_, _ = fmt.Fprintf(app.ErrWriter, "Command error: %v\n", err)
186+
cli.OsExiter(1)
187+
return err
188+
}

cmd/main_test.go

+64-15
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ package cmd
55

66
import (
77
"fmt"
8+
"io"
89
"os"
910
"path/filepath"
1011
"strings"
1112
"testing"
1213

1314
"code.gitea.io/gitea/models/unittest"
1415
"code.gitea.io/gitea/modules/setting"
16+
"code.gitea.io/gitea/modules/test"
1517

1618
"github.com/stretchr/testify/assert"
1719
"github.com/urfave/cli/v2"
@@ -27,21 +29,38 @@ func makePathOutput(workPath, customPath, customConf string) string {
2729
return fmt.Sprintf("WorkPath=%s\nCustomPath=%s\nCustomConf=%s", workPath, customPath, customConf)
2830
}
2931

30-
func newTestApp() *cli.App {
31-
app := NewMainApp()
32-
testCmd := &cli.Command{
33-
Name: "test-cmd",
34-
Action: func(ctx *cli.Context) error {
35-
_, _ = fmt.Fprint(app.Writer, makePathOutput(setting.AppWorkPath, setting.CustomPath, setting.CustomConf))
36-
return nil
37-
},
38-
}
32+
func newTestApp(testCmdAction func(ctx *cli.Context) error) *cli.App {
33+
app := NewMainApp("version", "version-extra")
34+
testCmd := &cli.Command{Name: "test-cmd", Action: testCmdAction}
3935
prepareSubcommandWithConfig(testCmd, appGlobalFlags())
4036
app.Commands = append(app.Commands, testCmd)
4137
app.DefaultCommand = testCmd.Name
4238
return app
4339
}
4440

41+
type runResult struct {
42+
Stdout string
43+
Stderr string
44+
ExitCode int
45+
}
46+
47+
func runTestApp(app *cli.App, args ...string) (runResult, error) {
48+
outBuf := new(strings.Builder)
49+
errBuf := new(strings.Builder)
50+
app.Writer = outBuf
51+
app.ErrWriter = errBuf
52+
exitCode := -1
53+
defer test.MockVariableValue(&cli.ErrWriter, app.ErrWriter)()
54+
defer test.MockVariableValue(&cli.OsExiter, func(code int) {
55+
if exitCode == -1 {
56+
exitCode = code // save the exit code once and then reset the writer (to simulate the exit)
57+
app.Writer, app.ErrWriter, cli.ErrWriter = io.Discard, io.Discard, io.Discard
58+
}
59+
})()
60+
err := RunMainApp(app, args...)
61+
return runResult{outBuf.String(), errBuf.String(), exitCode}, err
62+
}
63+
4564
func TestCliCmd(t *testing.T) {
4665
defaultWorkPath := filepath.Dir(setting.AppPath)
4766
defaultCustomPath := filepath.Join(defaultWorkPath, "custom")
@@ -92,7 +111,10 @@ func TestCliCmd(t *testing.T) {
92111
},
93112
}
94113

95-
app := newTestApp()
114+
app := newTestApp(func(ctx *cli.Context) error {
115+
_, _ = fmt.Fprint(ctx.App.Writer, makePathOutput(setting.AppWorkPath, setting.CustomPath, setting.CustomConf))
116+
return nil
117+
})
96118
var envBackup []string
97119
for _, s := range os.Environ() {
98120
if strings.HasPrefix(s, "GITEA_") && strings.Contains(s, "=") {
@@ -120,12 +142,39 @@ func TestCliCmd(t *testing.T) {
120142
_ = os.Setenv(k, v)
121143
}
122144
args := strings.Split(c.cmd, " ") // for test only, "split" is good enough
123-
out := new(strings.Builder)
124-
app.Writer = out
125-
err := app.Run(args)
145+
r, err := runTestApp(app, args...)
126146
assert.NoError(t, err, c.cmd)
127147
assert.NotEmpty(t, c.exp, c.cmd)
128-
outStr := out.String()
129-
assert.Contains(t, outStr, c.exp, c.cmd)
148+
assert.Contains(t, r.Stdout, c.exp, c.cmd)
130149
}
131150
}
151+
152+
func TestCliCmdError(t *testing.T) {
153+
app := newTestApp(func(ctx *cli.Context) error { return fmt.Errorf("normal error") })
154+
r, err := runTestApp(app, "./gitea", "test-cmd")
155+
assert.Error(t, err)
156+
assert.Equal(t, 1, r.ExitCode)
157+
assert.Equal(t, "", r.Stdout)
158+
assert.Equal(t, "Command error: normal error\n", r.Stderr)
159+
160+
app = newTestApp(func(ctx *cli.Context) error { return cli.Exit("exit error", 2) })
161+
r, err = runTestApp(app, "./gitea", "test-cmd")
162+
assert.Error(t, err)
163+
assert.Equal(t, 2, r.ExitCode)
164+
assert.Equal(t, "", r.Stdout)
165+
assert.Equal(t, "exit error\n", r.Stderr)
166+
167+
app = newTestApp(func(ctx *cli.Context) error { return nil })
168+
r, err = runTestApp(app, "./gitea", "test-cmd", "--no-such")
169+
assert.Error(t, err)
170+
assert.Equal(t, 1, r.ExitCode)
171+
assert.Equal(t, "Incorrect Usage: flag provided but not defined: -no-such\n\n", r.Stdout)
172+
assert.Equal(t, "", r.Stderr) // the cli package's strange behavior, the error message is not in stderr ....
173+
174+
app = newTestApp(func(ctx *cli.Context) error { return nil })
175+
r, err = runTestApp(app, "./gitea", "test-cmd")
176+
assert.NoError(t, err)
177+
assert.Equal(t, -1, r.ExitCode) // the cli.OsExiter is not called
178+
assert.Equal(t, "", r.Stdout)
179+
assert.Equal(t, "", r.Stderr)
180+
}

main.go

+7-11
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
package main
66

77
import (
8-
"fmt"
98
"os"
109
"runtime"
1110
"strings"
@@ -21,6 +20,8 @@ import (
2120
_ "code.gitea.io/gitea/modules/markup/csv"
2221
_ "code.gitea.io/gitea/modules/markup/markdown"
2322
_ "code.gitea.io/gitea/modules/markup/orgmode"
23+
24+
"github.com/urfave/cli/v2"
2425
)
2526

2627
// these flags will be set by the build flags
@@ -37,17 +38,12 @@ func init() {
3738
}
3839

3940
func main() {
40-
app := cmd.NewMainApp()
41-
app.Name = "Gitea"
42-
app.Usage = "A painless self-hosted Git service"
43-
app.Description = `By default, Gitea will start serving using the web-server with no argument, which can alternatively be run by running the subcommand "web".`
44-
app.Version = Version + formatBuiltWith()
45-
46-
err := app.Run(os.Args)
47-
if err != nil {
48-
_, _ = fmt.Fprintf(app.Writer, "\nFailed to run with %s: %v\n", os.Args, err)
41+
cli.OsExiter = func(code int) {
42+
log.GetManager().Close()
43+
os.Exit(code)
4944
}
50-
45+
app := cmd.NewMainApp(Version, formatBuiltWith())
46+
_ = cmd.RunMainApp(app, os.Args...) // all errors should have been handled by the RunMainApp
5147
log.GetManager().Close()
5248
}
5349

modules/test/utils.go

+6
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,9 @@ func RedirectURL(resp http.ResponseWriter) string {
3333
func IsNormalPageCompleted(s string) bool {
3434
return strings.Contains(s, `<footer class="page-footer"`) && strings.Contains(s, `</html>`)
3535
}
36+
37+
func MockVariableValue[T any](p *T, v T) (reset func()) {
38+
old := *p
39+
*p = v
40+
return func() { *p = old }
41+
}

0 commit comments

Comments
 (0)