Skip to content

Commit d29b785

Browse files
committed
align tests
1 parent df9c7f2 commit d29b785

File tree

6 files changed

+93
-89
lines changed

6 files changed

+93
-89
lines changed

cmd/admin_user.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ var subcmdUser = &cli.Command{
1111
Name: "user",
1212
Usage: "Modify users",
1313
Commands: []*cli.Command{
14-
microcmdUserCreate,
14+
microcmdUserCreate(),
1515
microcmdUserList,
1616
microcmdUserChangePassword,
1717
microcmdUserDelete,

cmd/admin_user_create.go

Lines changed: 76 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -19,73 +19,84 @@ import (
1919
"github.com/urfave/cli/v3"
2020
)
2121

22-
var microcmdUserCreate = &cli.Command{
23-
Name: "create",
24-
Usage: "Create a new user in database",
25-
Action: runCreateUser,
26-
Flags: []cli.Flag{
27-
&cli.StringFlag{
28-
Name: "name",
29-
Usage: "Username. DEPRECATED: use username instead",
22+
func microcmdUserCreate() *cli.Command {
23+
return &cli.Command{
24+
Name: "create",
25+
Usage: "Create a new user in database",
26+
Action: runCreateUser,
27+
MutuallyExclusiveFlags: []cli.MutuallyExclusiveFlags{
28+
{
29+
Flags: [][]cli.Flag{
30+
[]cli.Flag{
31+
&cli.StringFlag{
32+
Name: "name",
33+
Usage: "Username. DEPRECATED: use username instead",
34+
},
35+
&cli.StringFlag{
36+
Name: "username",
37+
Usage: "Username",
38+
},
39+
},
40+
},
41+
Required: true,
42+
},
3043
},
31-
&cli.StringFlag{
32-
Name: "username",
33-
Usage: "Username",
44+
Flags: []cli.Flag{
45+
&cli.StringFlag{
46+
Name: "user-type",
47+
Usage: "Set user's type: individual or bot",
48+
Value: "individual",
49+
},
50+
&cli.StringFlag{
51+
Name: "password",
52+
Usage: "User password",
53+
},
54+
&cli.StringFlag{
55+
Name: "email",
56+
Usage: "User email address",
57+
},
58+
&cli.BoolFlag{
59+
Name: "admin",
60+
Usage: "User is an admin",
61+
},
62+
&cli.BoolFlag{
63+
Name: "random-password",
64+
Usage: "Generate a random password for the user",
65+
},
66+
&cli.BoolFlag{
67+
Name: "must-change-password",
68+
Usage: "User must change password after initial login, defaults to true for all users except the first one (can be disabled by --must-change-password=false)",
69+
HideDefault: true,
70+
},
71+
&cli.IntFlag{
72+
Name: "random-password-length",
73+
Usage: "Length of the random password to be generated",
74+
Value: 12,
75+
},
76+
&cli.BoolFlag{
77+
Name: "access-token",
78+
Usage: "Generate access token for the user",
79+
},
80+
&cli.StringFlag{
81+
Name: "access-token-name",
82+
Usage: `Name of the generated access token`,
83+
Value: "gitea-admin",
84+
},
85+
&cli.StringFlag{
86+
Name: "access-token-scopes",
87+
Usage: `Scopes of the generated access token, comma separated. Examples: "all", "public-only,read:issue", "write:repository,write:user"`,
88+
Value: "all",
89+
},
90+
&cli.BoolFlag{
91+
Name: "restricted",
92+
Usage: "Make a restricted user account",
93+
},
94+
&cli.StringFlag{
95+
Name: "fullname",
96+
Usage: `The full, human-readable name of the user`,
97+
},
3498
},
35-
&cli.StringFlag{
36-
Name: "user-type",
37-
Usage: "Set user's type: individual or bot",
38-
Value: "individual",
39-
},
40-
&cli.StringFlag{
41-
Name: "password",
42-
Usage: "User password",
43-
},
44-
&cli.StringFlag{
45-
Name: "email",
46-
Usage: "User email address",
47-
},
48-
&cli.BoolFlag{
49-
Name: "admin",
50-
Usage: "User is an admin",
51-
},
52-
&cli.BoolFlag{
53-
Name: "random-password",
54-
Usage: "Generate a random password for the user",
55-
},
56-
&cli.BoolFlag{
57-
Name: "must-change-password",
58-
Usage: "User must change password after initial login, defaults to true for all users except the first one (can be disabled by --must-change-password=false)",
59-
DefaultText: "",
60-
},
61-
&cli.IntFlag{
62-
Name: "random-password-length",
63-
Usage: "Length of the random password to be generated",
64-
Value: 12,
65-
},
66-
&cli.BoolFlag{
67-
Name: "access-token",
68-
Usage: "Generate access token for the user",
69-
},
70-
&cli.StringFlag{
71-
Name: "access-token-name",
72-
Usage: `Name of the generated access token`,
73-
Value: "gitea-admin",
74-
},
75-
&cli.StringFlag{
76-
Name: "access-token-scopes",
77-
Usage: `Scopes of the generated access token, comma separated. Examples: "all", "public-only,read:issue", "write:repository,write:user"`,
78-
Value: "all",
79-
},
80-
&cli.BoolFlag{
81-
Name: "restricted",
82-
Usage: "Make a restricted user account",
83-
},
84-
&cli.StringFlag{
85-
Name: "fullname",
86-
Usage: `The full, human-readable name of the user`,
87-
},
88-
},
99+
}
89100
}
90101

91102
func runCreateUser(ctx context.Context, c *cli.Command) error {
@@ -113,12 +124,6 @@ func runCreateUser(ctx context.Context, c *cli.Command) error {
113124
return errors.New("password can only be set for individual users")
114125
}
115126
}
116-
if c.IsSet("name") && c.IsSet("username") {
117-
return errors.New("cannot set both --name and --username flags")
118-
}
119-
if !c.IsSet("name") && !c.IsSet("username") {
120-
return errors.New("one of --name or --username flags must be set")
121-
}
122127

123128
if c.IsSet("password") && c.IsSet("random-password") {
124129
return errors.New("cannot set both -random-password and -password flags")

cmd/admin_user_create_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
)
1919

2020
func TestAdminUserCreate(t *testing.T) {
21-
app := NewMainApp(AppVersion{})
2221

2322
reset := func() {
2423
require.NoError(t, db.TruncateBeans(db.DefaultContext, &user_model.User{}))
@@ -31,8 +30,9 @@ func TestAdminUserCreate(t *testing.T) {
3130
IsAdmin bool
3231
MustChangePassword bool
3332
}
33+
3434
createCheck := func(name, args string) check {
35-
require.NoError(t, app.Run(t.Context(), strings.Fields(fmt.Sprintf("./gitea admin user create --username %s --email %[email protected] %s --password foobar", name, name, args))))
35+
require.NoError(t, microcmdUserCreate().Run(t.Context(), strings.Fields(fmt.Sprintf("create --username %s --email %[email protected] %s --password foobar", name, name, args))))
3636
u := unittest.AssertExistsAndLoadBean(t, &user_model.User{LowerName: name})
3737
return check{IsAdmin: u.IsAdmin, MustChangePassword: u.MustChangePassword}
3838
}
@@ -51,7 +51,7 @@ func TestAdminUserCreate(t *testing.T) {
5151
})
5252

5353
createUser := func(name string, args ...string) error {
54-
return app.Run(t.Context(), append([]string{"./gitea", "admin", "user", "create", "--username", name, "--email", name + "@gitea.local"}, args...))
54+
return microcmdUserCreate().Run(t.Context(), append([]string{"create", "--username", name, "--email", name + "@gitea.local"}, args...))
5555
}
5656

5757
t.Run("UserType", func(t *testing.T) {

cmd/docs.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ import (
99
"os"
1010
"strings"
1111

12-
"github.com/urfave/cli/v3"
13-
1412
cli_docs "github.com/urfave/cli-docs/v3"
13+
"github.com/urfave/cli/v3"
1514
)
1615

1716
// CmdDocs represents the available docs sub-command.

cmd/main.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,18 @@ func cmdHelp() *cli.Command {
2323
Aliases: []string{"h"},
2424
Usage: "Shows a list of commands or help for one command",
2525
ArgsUsage: "[command]",
26-
Action: func(_ context.Context, c *cli.Command) (err error) {
27-
lineage := c.Lineage() // The order is from child to parent: help, doctor, Gitea, {Command:nil}
26+
Action: func(ctx context.Context, c *cli.Command) (err error) {
27+
lineage := c.Lineage() // The order is from child to parent: help, doctor, Gitea
2828
targetCmdIdx := 0
2929
if c.Name == "help" {
3030
targetCmdIdx = 1
3131
}
32-
if lineage[targetCmdIdx+1] != nil {
33-
err = cli.ShowCommandHelp(context.Background(), lineage[targetCmdIdx+1], lineage[targetCmdIdx].Name)
32+
if lineage[targetCmdIdx].Name != "Gitea" {
33+
err = cli.ShowCommandHelp(ctx, lineage[targetCmdIdx], lineage[targetCmdIdx].Name)
3434
} else {
3535
err = cli.ShowAppHelp(c)
3636
}
37-
_, _ = fmt.Fprintf(c.Writer, `
37+
_, _ = fmt.Fprintf(c.Root().Writer, `
3838
DEFAULT CONFIGURATION:
3939
AppPath: %s
4040
WorkPath: %s

cmd/main_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func makePathOutput(workPath, customPath, customConf string) string {
2828
return fmt.Sprintf("WorkPath=%s\nCustomPath=%s\nCustomConf=%s", workPath, customPath, customConf)
2929
}
3030

31-
func newTestApp(testCmdAction func(ctx context.Context, cmd *cli.Command) error) *cli.Command {
31+
func newTestApp(testCmdAction cli.ActionFunc) *cli.Command {
3232
app := NewMainApp(AppVersion{})
3333
testCmd := &cli.Command{Name: "test-cmd", Action: testCmdAction}
3434
prepareSubcommandWithConfig(testCmd, appGlobalFlags())
@@ -110,12 +110,12 @@ func TestCliCmd(t *testing.T) {
110110
},
111111
}
112112

113-
app := newTestApp(func(ctx context.Context, cmd *cli.Command) error {
114-
_, _ = fmt.Fprint(cmd.Writer, makePathOutput(setting.AppWorkPath, setting.CustomPath, setting.CustomConf))
115-
return nil
116-
})
117113
for _, c := range cases {
118114
t.Run(c.cmd, func(t *testing.T) {
115+
app := newTestApp(func(ctx context.Context, cmd *cli.Command) error {
116+
_, _ = fmt.Fprint(cmd.Root().Writer, makePathOutput(setting.AppWorkPath, setting.CustomPath, setting.CustomConf))
117+
return nil
118+
})
119119
for k, v := range c.env {
120120
t.Setenv(k, v)
121121
}
@@ -147,8 +147,8 @@ func TestCliCmdError(t *testing.T) {
147147
r, err = runTestApp(app, "./gitea", "test-cmd", "--no-such")
148148
assert.Error(t, err)
149149
assert.Equal(t, 1, r.ExitCode)
150-
assert.Equal(t, "Incorrect Usage: flag provided but not defined: -no-such\n\n", r.Stdout)
151-
assert.Empty(t, r.Stderr) // the cli package's strange behavior, the error message is not in stderr ....
150+
assert.Empty(t, r.Stdout)
151+
assert.Equal(t, "Incorrect Usage: flag provided but not defined: -no-such\n\n", r.Stderr)
152152

153153
app = newTestApp(func(ctx context.Context, cmd *cli.Command) error { return nil })
154154
r, err = runTestApp(app, "./gitea", "test-cmd")

0 commit comments

Comments
 (0)