mirror of
https://github.com/go-gitea/gitea.git
synced 2025-07-19 12:50:52 +02:00
align tests
This commit is contained in:
parent
df9c7f21a5
commit
d29b7858d7
@ -11,7 +11,7 @@ var subcmdUser = &cli.Command{
|
|||||||
Name: "user",
|
Name: "user",
|
||||||
Usage: "Modify users",
|
Usage: "Modify users",
|
||||||
Commands: []*cli.Command{
|
Commands: []*cli.Command{
|
||||||
microcmdUserCreate,
|
microcmdUserCreate(),
|
||||||
microcmdUserList,
|
microcmdUserList,
|
||||||
microcmdUserChangePassword,
|
microcmdUserChangePassword,
|
||||||
microcmdUserDelete,
|
microcmdUserDelete,
|
||||||
|
@ -19,11 +19,15 @@ import (
|
|||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
var microcmdUserCreate = &cli.Command{
|
func microcmdUserCreate() *cli.Command {
|
||||||
|
return &cli.Command{
|
||||||
Name: "create",
|
Name: "create",
|
||||||
Usage: "Create a new user in database",
|
Usage: "Create a new user in database",
|
||||||
Action: runCreateUser,
|
Action: runCreateUser,
|
||||||
Flags: []cli.Flag{
|
MutuallyExclusiveFlags: []cli.MutuallyExclusiveFlags{
|
||||||
|
{
|
||||||
|
Flags: [][]cli.Flag{
|
||||||
|
[]cli.Flag{
|
||||||
&cli.StringFlag{
|
&cli.StringFlag{
|
||||||
Name: "name",
|
Name: "name",
|
||||||
Usage: "Username. DEPRECATED: use username instead",
|
Usage: "Username. DEPRECATED: use username instead",
|
||||||
@ -32,6 +36,12 @@ var microcmdUserCreate = &cli.Command{
|
|||||||
Name: "username",
|
Name: "username",
|
||||||
Usage: "Username",
|
Usage: "Username",
|
||||||
},
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Required: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Flags: []cli.Flag{
|
||||||
&cli.StringFlag{
|
&cli.StringFlag{
|
||||||
Name: "user-type",
|
Name: "user-type",
|
||||||
Usage: "Set user's type: individual or bot",
|
Usage: "Set user's type: individual or bot",
|
||||||
@ -56,7 +66,7 @@ var microcmdUserCreate = &cli.Command{
|
|||||||
&cli.BoolFlag{
|
&cli.BoolFlag{
|
||||||
Name: "must-change-password",
|
Name: "must-change-password",
|
||||||
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)",
|
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)",
|
||||||
DefaultText: "",
|
HideDefault: true,
|
||||||
},
|
},
|
||||||
&cli.IntFlag{
|
&cli.IntFlag{
|
||||||
Name: "random-password-length",
|
Name: "random-password-length",
|
||||||
@ -86,6 +96,7 @@ var microcmdUserCreate = &cli.Command{
|
|||||||
Usage: `The full, human-readable name of the user`,
|
Usage: `The full, human-readable name of the user`,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func runCreateUser(ctx context.Context, c *cli.Command) error {
|
func runCreateUser(ctx context.Context, c *cli.Command) error {
|
||||||
@ -113,12 +124,6 @@ func runCreateUser(ctx context.Context, c *cli.Command) error {
|
|||||||
return errors.New("password can only be set for individual users")
|
return errors.New("password can only be set for individual users")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if c.IsSet("name") && c.IsSet("username") {
|
|
||||||
return errors.New("cannot set both --name and --username flags")
|
|
||||||
}
|
|
||||||
if !c.IsSet("name") && !c.IsSet("username") {
|
|
||||||
return errors.New("one of --name or --username flags must be set")
|
|
||||||
}
|
|
||||||
|
|
||||||
if c.IsSet("password") && c.IsSet("random-password") {
|
if c.IsSet("password") && c.IsSet("random-password") {
|
||||||
return errors.New("cannot set both -random-password and -password flags")
|
return errors.New("cannot set both -random-password and -password flags")
|
||||||
|
@ -18,7 +18,6 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestAdminUserCreate(t *testing.T) {
|
func TestAdminUserCreate(t *testing.T) {
|
||||||
app := NewMainApp(AppVersion{})
|
|
||||||
|
|
||||||
reset := func() {
|
reset := func() {
|
||||||
require.NoError(t, db.TruncateBeans(db.DefaultContext, &user_model.User{}))
|
require.NoError(t, db.TruncateBeans(db.DefaultContext, &user_model.User{}))
|
||||||
@ -31,8 +30,9 @@ func TestAdminUserCreate(t *testing.T) {
|
|||||||
IsAdmin bool
|
IsAdmin bool
|
||||||
MustChangePassword bool
|
MustChangePassword bool
|
||||||
}
|
}
|
||||||
|
|
||||||
createCheck := func(name, args string) check {
|
createCheck := func(name, args string) check {
|
||||||
require.NoError(t, app.Run(t.Context(), strings.Fields(fmt.Sprintf("./gitea admin user create --username %s --email %s@gitea.local %s --password foobar", name, name, args))))
|
require.NoError(t, microcmdUserCreate().Run(t.Context(), strings.Fields(fmt.Sprintf("create --username %s --email %s@gitea.local %s --password foobar", name, name, args))))
|
||||||
u := unittest.AssertExistsAndLoadBean(t, &user_model.User{LowerName: name})
|
u := unittest.AssertExistsAndLoadBean(t, &user_model.User{LowerName: name})
|
||||||
return check{IsAdmin: u.IsAdmin, MustChangePassword: u.MustChangePassword}
|
return check{IsAdmin: u.IsAdmin, MustChangePassword: u.MustChangePassword}
|
||||||
}
|
}
|
||||||
@ -51,7 +51,7 @@ func TestAdminUserCreate(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
createUser := func(name string, args ...string) error {
|
createUser := func(name string, args ...string) error {
|
||||||
return app.Run(t.Context(), append([]string{"./gitea", "admin", "user", "create", "--username", name, "--email", name + "@gitea.local"}, args...))
|
return microcmdUserCreate().Run(t.Context(), append([]string{"create", "--username", name, "--email", name + "@gitea.local"}, args...))
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Run("UserType", func(t *testing.T) {
|
t.Run("UserType", func(t *testing.T) {
|
||||||
|
@ -9,9 +9,8 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/urfave/cli/v3"
|
|
||||||
|
|
||||||
cli_docs "github.com/urfave/cli-docs/v3"
|
cli_docs "github.com/urfave/cli-docs/v3"
|
||||||
|
"github.com/urfave/cli/v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CmdDocs represents the available docs sub-command.
|
// CmdDocs represents the available docs sub-command.
|
||||||
|
10
cmd/main.go
10
cmd/main.go
@ -23,18 +23,18 @@ func cmdHelp() *cli.Command {
|
|||||||
Aliases: []string{"h"},
|
Aliases: []string{"h"},
|
||||||
Usage: "Shows a list of commands or help for one command",
|
Usage: "Shows a list of commands or help for one command",
|
||||||
ArgsUsage: "[command]",
|
ArgsUsage: "[command]",
|
||||||
Action: func(_ context.Context, c *cli.Command) (err error) {
|
Action: func(ctx context.Context, c *cli.Command) (err error) {
|
||||||
lineage := c.Lineage() // The order is from child to parent: help, doctor, Gitea, {Command:nil}
|
lineage := c.Lineage() // The order is from child to parent: help, doctor, Gitea
|
||||||
targetCmdIdx := 0
|
targetCmdIdx := 0
|
||||||
if c.Name == "help" {
|
if c.Name == "help" {
|
||||||
targetCmdIdx = 1
|
targetCmdIdx = 1
|
||||||
}
|
}
|
||||||
if lineage[targetCmdIdx+1] != nil {
|
if lineage[targetCmdIdx].Name != "Gitea" {
|
||||||
err = cli.ShowCommandHelp(context.Background(), lineage[targetCmdIdx+1], lineage[targetCmdIdx].Name)
|
err = cli.ShowCommandHelp(ctx, lineage[targetCmdIdx], lineage[targetCmdIdx].Name)
|
||||||
} else {
|
} else {
|
||||||
err = cli.ShowAppHelp(c)
|
err = cli.ShowAppHelp(c)
|
||||||
}
|
}
|
||||||
_, _ = fmt.Fprintf(c.Writer, `
|
_, _ = fmt.Fprintf(c.Root().Writer, `
|
||||||
DEFAULT CONFIGURATION:
|
DEFAULT CONFIGURATION:
|
||||||
AppPath: %s
|
AppPath: %s
|
||||||
WorkPath: %s
|
WorkPath: %s
|
||||||
|
@ -28,7 +28,7 @@ func makePathOutput(workPath, customPath, customConf string) string {
|
|||||||
return fmt.Sprintf("WorkPath=%s\nCustomPath=%s\nCustomConf=%s", workPath, customPath, customConf)
|
return fmt.Sprintf("WorkPath=%s\nCustomPath=%s\nCustomConf=%s", workPath, customPath, customConf)
|
||||||
}
|
}
|
||||||
|
|
||||||
func newTestApp(testCmdAction func(ctx context.Context, cmd *cli.Command) error) *cli.Command {
|
func newTestApp(testCmdAction cli.ActionFunc) *cli.Command {
|
||||||
app := NewMainApp(AppVersion{})
|
app := NewMainApp(AppVersion{})
|
||||||
testCmd := &cli.Command{Name: "test-cmd", Action: testCmdAction}
|
testCmd := &cli.Command{Name: "test-cmd", Action: testCmdAction}
|
||||||
prepareSubcommandWithConfig(testCmd, appGlobalFlags())
|
prepareSubcommandWithConfig(testCmd, appGlobalFlags())
|
||||||
@ -110,12 +110,12 @@ func TestCliCmd(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
app := newTestApp(func(ctx context.Context, cmd *cli.Command) error {
|
|
||||||
_, _ = fmt.Fprint(cmd.Writer, makePathOutput(setting.AppWorkPath, setting.CustomPath, setting.CustomConf))
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
for _, c := range cases {
|
for _, c := range cases {
|
||||||
t.Run(c.cmd, func(t *testing.T) {
|
t.Run(c.cmd, func(t *testing.T) {
|
||||||
|
app := newTestApp(func(ctx context.Context, cmd *cli.Command) error {
|
||||||
|
_, _ = fmt.Fprint(cmd.Root().Writer, makePathOutput(setting.AppWorkPath, setting.CustomPath, setting.CustomConf))
|
||||||
|
return nil
|
||||||
|
})
|
||||||
for k, v := range c.env {
|
for k, v := range c.env {
|
||||||
t.Setenv(k, v)
|
t.Setenv(k, v)
|
||||||
}
|
}
|
||||||
@ -147,8 +147,8 @@ func TestCliCmdError(t *testing.T) {
|
|||||||
r, err = runTestApp(app, "./gitea", "test-cmd", "--no-such")
|
r, err = runTestApp(app, "./gitea", "test-cmd", "--no-such")
|
||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
assert.Equal(t, 1, r.ExitCode)
|
assert.Equal(t, 1, r.ExitCode)
|
||||||
assert.Equal(t, "Incorrect Usage: flag provided but not defined: -no-such\n\n", r.Stdout)
|
assert.Empty(t, r.Stdout)
|
||||||
assert.Empty(t, r.Stderr) // the cli package's strange behavior, the error message is not in stderr ....
|
assert.Equal(t, "Incorrect Usage: flag provided but not defined: -no-such\n\n", r.Stderr)
|
||||||
|
|
||||||
app = newTestApp(func(ctx context.Context, cmd *cli.Command) error { return nil })
|
app = newTestApp(func(ctx context.Context, cmd *cli.Command) error { return nil })
|
||||||
r, err = runTestApp(app, "./gitea", "test-cmd")
|
r, err = runTestApp(app, "./gitea", "test-cmd")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user