Skip to content

Commit de70cd3

Browse files
authored
Fix cli "Before" handling (go-gitea#35797)
Regression of go-gitea#34973 Fix go-gitea#35796
1 parent ef90bef commit de70cd3

File tree

5 files changed

+55
-16
lines changed

5 files changed

+55
-16
lines changed

cmd/cmd.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,12 @@ func globalBool(c *cli.Command, name string) bool {
121121
// Any log appears in git stdout pipe will break the git protocol, eg: client can't push and hangs forever.
122122
func PrepareConsoleLoggerLevel(defaultLevel log.Level) func(context.Context, *cli.Command) (context.Context, error) {
123123
return func(ctx context.Context, c *cli.Command) (context.Context, error) {
124+
if setting.InstallLock {
125+
// During config loading, there might also be logs (for example: deprecation warnings).
126+
// It must make sure that console logger is set up before config is loaded.
127+
log.Error("Config is loaded before console logger is setup, it will cause bugs. Please fix it.")
128+
return nil, errors.New("console logger must be setup before config is loaded")
129+
}
124130
level := defaultLevel
125131
if globalBool(c, "quiet") {
126132
level = log.FATAL

cmd/keys.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
var CmdKeys = &cli.Command{
2020
Name: "keys",
2121
Usage: "(internal) Should only be called by SSH server",
22-
Hidden: true, // internal commands shouldn't not be visible
22+
Hidden: true, // internal commands shouldn't be visible
2323
Description: "Queries the Gitea database to get the authorized command for a given ssh key fingerprint",
2424
Before: PrepareConsoleLoggerLevel(log.FATAL),
2525
Action: runKeys,

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(), any("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+
}

tests/integration/cmd_keys_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"code.gitea.io/gitea/cmd"
1212
"code.gitea.io/gitea/modules/setting"
13+
"code.gitea.io/gitea/modules/test"
1314
"code.gitea.io/gitea/modules/util"
1415

1516
"github.com/stretchr/testify/assert"
@@ -36,13 +37,15 @@ func Test_CmdKeys(t *testing.T) {
3637
}
3738
for _, tt := range tests {
3839
t.Run(tt.name, func(t *testing.T) {
40+
// FIXME: this test is not quite right. Each "command run" always re-initializes settings
41+
defer test.MockVariableValue(&cmd.CmdKeys.Before, nil)() // don't re-initialize logger during the test
42+
3943
var stdout, stderr bytes.Buffer
4044
app := &cli.Command{
4145
Writer: &stdout,
4246
ErrWriter: &stderr,
4347
Commands: []*cli.Command{cmd.CmdKeys},
4448
}
45-
cmd.CmdKeys.HideHelp = true
4649
err := app.Run(t.Context(), append([]string{"prog"}, tt.args...))
4750
if tt.wantErr {
4851
assert.Error(t, err)

0 commit comments

Comments
 (0)