Skip to content

Commit 4372fab

Browse files
committed
fix cli "Before" handling
1 parent c3472dd commit 4372fab

File tree

2 files changed

+44
-14
lines changed

2 files changed

+44
-14
lines changed

cmd/main.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,15 @@ DEFAULT CONFIGURATION:
5050

5151
func prepareSubcommandWithGlobalFlags(originCmd *cli.Command) {
5252
originBefore := originCmd.Before
53-
originCmd.Before = func(ctx context.Context, cmd *cli.Command) (context.Context, error) {
54-
prepareWorkPathAndCustomConf(cmd)
53+
originCmd.Before = func(ctxOrig context.Context, cmd *cli.Command) (ctx context.Context, err error) {
54+
ctx = ctxOrig
5555
if originBefore != nil {
56-
return originBefore(ctx, cmd)
56+
ctx, err = originBefore(ctx, cmd)
57+
if err != nil {
58+
return ctx, err
59+
}
5760
}
61+
prepareWorkPathAndCustomConf(cmd)
5862
return ctx, nil
5963
}
6064
}

cmd/main_test.go

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"code.gitea.io/gitea/models/unittest"
1616
"code.gitea.io/gitea/modules/setting"
1717
"code.gitea.io/gitea/modules/test"
18+
"code.gitea.io/gitea/modules/util"
1819

1920
"github.com/stretchr/testify/assert"
2021
"github.com/urfave/cli/v3"
@@ -28,11 +29,11 @@ func makePathOutput(workPath, customPath, customConf string) string {
2829
return fmt.Sprintf("WorkPath=%s\nCustomPath=%s\nCustomConf=%s", workPath, customPath, customConf)
2930
}
3031

31-
func newTestApp(testCmdAction cli.ActionFunc) *cli.Command {
32+
func newTestApp(testCmd cli.Command) *cli.Command {
3233
app := NewMainApp(AppVersion{})
33-
testCmd := &cli.Command{Name: "test-cmd", Action: testCmdAction}
34-
prepareSubcommandWithGlobalFlags(testCmd)
35-
app.Commands = append(app.Commands, testCmd)
34+
testCmd.Name = util.IfZero(testCmd.Name, "test-cmd")
35+
prepareSubcommandWithGlobalFlags(&testCmd)
36+
app.Commands = append(app.Commands, &testCmd)
3637
app.DefaultCommand = testCmd.Name
3738
return app
3839
}
@@ -156,9 +157,11 @@ func TestCliCmd(t *testing.T) {
156157

157158
for _, c := range cases {
158159
t.Run(c.cmd, func(t *testing.T) {
159-
app := newTestApp(func(ctx context.Context, cmd *cli.Command) error {
160-
_, _ = fmt.Fprint(cmd.Root().Writer, makePathOutput(setting.AppWorkPath, setting.CustomPath, setting.CustomConf))
161-
return nil
160+
app := newTestApp(cli.Command{
161+
Action: func(ctx context.Context, cmd *cli.Command) error {
162+
_, _ = fmt.Fprint(cmd.Root().Writer, makePathOutput(setting.AppWorkPath, setting.CustomPath, setting.CustomConf))
163+
return nil
164+
},
162165
})
163166
for k, v := range c.env {
164167
t.Setenv(k, v)
@@ -173,31 +176,54 @@ func TestCliCmd(t *testing.T) {
173176
}
174177

175178
func TestCliCmdError(t *testing.T) {
176-
app := newTestApp(func(ctx context.Context, cmd *cli.Command) error { return errors.New("normal error") })
179+
app := newTestApp(cli.Command{Action: func(ctx context.Context, cmd *cli.Command) error { return errors.New("normal error") }})
177180
r, err := runTestApp(app, "./gitea", "test-cmd")
178181
assert.Error(t, err)
179182
assert.Equal(t, 1, r.ExitCode)
180183
assert.Empty(t, r.Stdout)
181184
assert.Equal(t, "Command error: normal error\n", r.Stderr)
182185

183-
app = newTestApp(func(ctx context.Context, cmd *cli.Command) error { return cli.Exit("exit error", 2) })
186+
app = newTestApp(cli.Command{Action: func(ctx context.Context, cmd *cli.Command) error { return cli.Exit("exit error", 2) }})
184187
r, err = runTestApp(app, "./gitea", "test-cmd")
185188
assert.Error(t, err)
186189
assert.Equal(t, 2, r.ExitCode)
187190
assert.Empty(t, r.Stdout)
188191
assert.Equal(t, "exit error\n", r.Stderr)
189192

190-
app = newTestApp(func(ctx context.Context, cmd *cli.Command) error { return nil })
193+
app = newTestApp(cli.Command{Action: func(ctx context.Context, cmd *cli.Command) error { return nil }})
191194
r, err = runTestApp(app, "./gitea", "test-cmd", "--no-such")
192195
assert.Error(t, err)
193196
assert.Equal(t, 1, r.ExitCode)
194197
assert.Empty(t, r.Stdout)
195198
assert.Equal(t, "Incorrect Usage: flag provided but not defined: -no-such\n\n", r.Stderr)
196199

197-
app = newTestApp(func(ctx context.Context, cmd *cli.Command) error { return nil })
200+
app = newTestApp(cli.Command{Action: func(ctx context.Context, cmd *cli.Command) error { return nil }})
198201
r, err = runTestApp(app, "./gitea", "test-cmd")
199202
assert.NoError(t, err)
200203
assert.Equal(t, -1, r.ExitCode) // the cli.OsExiter is not called
201204
assert.Empty(t, r.Stdout)
202205
assert.Empty(t, r.Stderr)
203206
}
207+
208+
func TestCliCmdBefore(t *testing.T) {
209+
ctxNew := context.WithValue(context.Background(), "key", "value")
210+
configValues := map[string]string{}
211+
setting.CustomConf = "/tmp/any.ini"
212+
var actionCtx context.Context
213+
app := newTestApp(cli.Command{
214+
Before: func(context.Context, *cli.Command) (context.Context, error) {
215+
configValues["before"] = setting.CustomConf
216+
return ctxNew, nil
217+
},
218+
Action: func(ctx context.Context, cmd *cli.Command) error {
219+
configValues["action"] = setting.CustomConf
220+
actionCtx = ctx
221+
return nil
222+
},
223+
})
224+
_, err := runTestApp(app, "./gitea", "--config", "/dev/null", "test-cmd")
225+
assert.NoError(t, err)
226+
assert.Equal(t, ctxNew, actionCtx)
227+
assert.Equal(t, "/tmp/any.ini", configValues["before"], "BeforeFunc must be called before preparing config")
228+
assert.Equal(t, "/dev/null", configValues["action"])
229+
}

0 commit comments

Comments
 (0)