Skip to content

Commit e57ac93

Browse files
committed
fix
1 parent aeb3830 commit e57ac93

File tree

3 files changed

+62
-35
lines changed

3 files changed

+62
-35
lines changed

cmd/doctor.go

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ import (
88
golog "log"
99
"os"
1010
"path/filepath"
11-
"strings"
1211
"text/tabwriter"
1312

1413
"code.gitea.io/gitea/models/db"
1514
"code.gitea.io/gitea/models/migrations"
1615
migrate_base "code.gitea.io/gitea/models/migrations/base"
16+
"code.gitea.io/gitea/modules/container"
1717
"code.gitea.io/gitea/modules/doctor"
1818
"code.gitea.io/gitea/modules/log"
1919
"code.gitea.io/gitea/modules/setting"
@@ -22,6 +22,19 @@ import (
2222
"xorm.io/xorm"
2323
)
2424

25+
// CmdDoctor represents the available doctor sub-command.
26+
var CmdDoctor = &cli.Command{
27+
Name: "doctor",
28+
Usage: "Diagnose and optionally fix problems",
29+
Description: "A command to diagnose problems with the current Gitea instance according to the given configuration. Some problems can optionally be fixed by modifying the database or data storage.",
30+
31+
Subcommands: []*cli.Command{
32+
cmdDoctorCheck,
33+
cmdRecreateTable,
34+
cmdDoctorConvert,
35+
},
36+
}
37+
2538
var cmdDoctorCheck = &cli.Command{
2639
Name: "check",
2740
Usage: "Diagnose and optionally fix problems",
@@ -60,19 +73,6 @@ var cmdDoctorCheck = &cli.Command{
6073
},
6174
}
6275

63-
// CmdDoctor represents the available doctor sub-command.
64-
var CmdDoctor = &cli.Command{
65-
Name: "doctor",
66-
Usage: "Diagnose and optionally fix problems",
67-
Description: "A command to diagnose problems with the current Gitea instance according to the given configuration. Some problems can optionally be fixed by modifying the database or data storage.",
68-
69-
Subcommands: []*cli.Command{
70-
cmdDoctorCheck,
71-
cmdRecreateTable,
72-
cmdDoctorConvert,
73-
},
74-
}
75-
7676
var cmdRecreateTable = &cli.Command{
7777
Name: "recreate-table",
7878
Usage: "Recreate tables from XORM definitions and copy the data.",
@@ -177,6 +177,7 @@ func runDoctorCheck(ctx *cli.Context) error {
177177
if ctx.IsSet("list") {
178178
w := tabwriter.NewWriter(os.Stdout, 0, 8, 1, '\t', 0)
179179
_, _ = w.Write([]byte("Default\tName\tTitle\n"))
180+
doctor.SortChecks(doctor.Checks)
180181
for _, check := range doctor.Checks {
181182
if check.IsDefault {
182183
_, _ = w.Write([]byte{'*'})
@@ -192,33 +193,26 @@ func runDoctorCheck(ctx *cli.Context) error {
192193

193194
var checks []*doctor.Check
194195
if ctx.Bool("all") {
195-
checks = doctor.Checks
196+
checks = make([]*doctor.Check, len(doctor.Checks))
197+
copy(checks, doctor.Checks)
196198
} else if ctx.IsSet("run") {
197199
addDefault := ctx.Bool("default")
198-
names := ctx.StringSlice("run")
199-
for i, name := range names {
200-
names[i] = strings.ToLower(strings.TrimSpace(name))
201-
}
202-
200+
runNamesSet := container.SetOf(ctx.StringSlice("run")...)
203201
for _, check := range doctor.Checks {
204-
if addDefault && check.IsDefault {
202+
if (addDefault && check.IsDefault) || runNamesSet.Contains(check.Name) {
205203
checks = append(checks, check)
206-
continue
207-
}
208-
for _, name := range names {
209-
if name == check.Name {
210-
checks = append(checks, check)
211-
break
212-
}
204+
runNamesSet.Remove(check.Name)
213205
}
214206
}
207+
if len(runNamesSet) > 0 {
208+
return fmt.Errorf("unknown checks: %v", runNamesSet.Values())
209+
}
215210
} else {
216211
for _, check := range doctor.Checks {
217212
if check.IsDefault {
218213
checks = append(checks, check)
219214
}
220215
}
221216
}
222-
223217
return doctor.RunChecks(stdCtx, colorize, ctx.Bool("fix"), checks)
224218
}

cmd/doctor_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2023 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package cmd
5+
6+
import (
7+
"context"
8+
"testing"
9+
10+
"code.gitea.io/gitea/modules/doctor"
11+
"code.gitea.io/gitea/modules/log"
12+
13+
"github.com/stretchr/testify/assert"
14+
)
15+
16+
func TestDoctorRun(t *testing.T) {
17+
app := newTestApp(nil)
18+
doctor.Register(&doctor.Check{
19+
Title: "Test Check",
20+
Name: "test-check",
21+
Run: func(ctx context.Context, logger log.Logger, autofix bool) error { return nil },
22+
})
23+
_, err := runTestApp(app, "./gitea", "doctor", "check", "--run", "test-check")
24+
assert.NoError(t, err)
25+
_, err = runTestApp(app, "./gitea", "doctor", "check", "--run", "no-such")
26+
assert.ErrorContains(t, err, "unknown check: [no-such]")
27+
_, err = runTestApp(app, "./gitea", "doctor", "check", "--run", "test-check,no-such")
28+
assert.ErrorContains(t, err, "unknown check: [no-such]")
29+
}

modules/doctor/doctor.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ var Checks []*Check
7979

8080
// RunChecks runs the doctor checks for the provided list
8181
func RunChecks(ctx context.Context, colorize, autofix bool, checks []*Check) error {
82+
SortChecks(checks)
8283
// the checks output logs by a special logger, they do not use the default logger
8384
logger := log.BaseLoggerToGeneralLogger(&doctorCheckLogger{colorize: colorize})
8485
loggerStep := log.BaseLoggerToGeneralLogger(&doctorCheckStepLogger{colorize: colorize})
@@ -104,20 +105,23 @@ func RunChecks(ctx context.Context, colorize, autofix bool, checks []*Check) err
104105
logger.Info("OK")
105106
}
106107
}
107-
logger.Info("\nAll done.")
108+
logger.Info("\nAll done (checks: %d).", len(checks))
108109
return nil
109110
}
110111

111112
// Register registers a command with the list
112113
func Register(command *Check) {
113114
Checks = append(Checks, command)
114-
sort.SliceStable(Checks, func(i, j int) bool {
115-
if Checks[i].Priority == Checks[j].Priority {
116-
return Checks[i].Name < Checks[j].Name
115+
}
116+
117+
func SortChecks(checks []*Check) {
118+
sort.SliceStable(checks, func(i, j int) bool {
119+
if checks[i].Priority == checks[j].Priority {
120+
return checks[i].Name < checks[j].Name
117121
}
118-
if Checks[i].Priority == 0 {
122+
if checks[i].Priority == 0 {
119123
return false
120124
}
121-
return Checks[i].Priority < Checks[j].Priority
125+
return checks[i].Priority < checks[j].Priority
122126
})
123127
}

0 commit comments

Comments
 (0)