mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-29 20:56:19 +01:00
36997a48e3
(cherry picked from commit08be2b226e
) (cherry picked from commitb6cfa88c6e
) (cherry picked from commit59704200de
) [CLI] implement forgejo-cli actions generate-secret (cherry picked from commit6f7905c8ec
) (cherry picked from commite085d6d273
) [CLI] implement forgejo-cli actions generate-secret (squash) NoInit (cherry picked from commit962c944eb2
) [CLI] implement forgejo-cli actions register (cherry picked from commit2f95143000
) (cherry picked from commit42f2f8731e
) [CLI] implement forgejo-cli actions register (squash) no private Do not go through the private API, directly modify the database (cherry picked from commit1ba7c0d39d
) [CLI] implement forgejo-cli actions (cherry picked from commit6f7905c8ec
) (cherry picked from commite085d6d273
) [CLI] implement forgejo-cli actions generate-secret (squash) NoInit (cherry picked from commit962c944eb2
) (cherry picked from commit4c121ef022
) Conflicts: cmd/forgejo/actions.go tests/integration/cmd_forgejo_actions_test.go
127 lines
3.1 KiB
Go
127 lines
3.1 KiB
Go
// Copyright 2022 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"code.gitea.io/gitea/models/unittest"
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/urfave/cli/v2"
|
|
)
|
|
|
|
func TestMain(m *testing.M) {
|
|
unittest.MainTest(m, &unittest.TestOptions{
|
|
GiteaRootPath: "..",
|
|
})
|
|
}
|
|
|
|
func newTestApp() *cli.App {
|
|
app := NewMainApp()
|
|
testCmd := &cli.Command{
|
|
Name: "test-cmd",
|
|
Action: func(ctx *cli.Context) error {
|
|
_, _ = fmt.Fprint(app.Writer, makePathOutput(setting.AppWorkPath, setting.CustomPath, setting.CustomConf))
|
|
return nil
|
|
},
|
|
}
|
|
prepareSubcommandWithConfig(testCmd, appGlobalFlags())
|
|
app.Commands = append(app.Commands, testCmd)
|
|
app.DefaultCommand = testCmd.Name
|
|
return app
|
|
}
|
|
|
|
func TestCliCmd(t *testing.T) {
|
|
defaultWorkPath := filepath.Dir(setting.AppPath)
|
|
defaultCustomPath := filepath.Join(defaultWorkPath, "custom")
|
|
defaultCustomConf := filepath.Join(defaultCustomPath, "conf/app.ini")
|
|
|
|
cli.CommandHelpTemplate = "(command help template)"
|
|
cli.AppHelpTemplate = "(app help template)"
|
|
cli.SubcommandHelpTemplate = "(subcommand help template)"
|
|
|
|
cases := []struct {
|
|
env map[string]string
|
|
cmd string
|
|
exp string
|
|
}{
|
|
// main command help
|
|
{
|
|
cmd: "./gitea help",
|
|
exp: "DEFAULT CONFIGURATION:",
|
|
},
|
|
|
|
// parse paths
|
|
{
|
|
cmd: "./gitea test-cmd",
|
|
exp: makePathOutput(defaultWorkPath, defaultCustomPath, defaultCustomConf),
|
|
},
|
|
{
|
|
cmd: "./gitea -c /tmp/app.ini test-cmd",
|
|
exp: makePathOutput(defaultWorkPath, defaultCustomPath, "/tmp/app.ini"),
|
|
},
|
|
{
|
|
cmd: "./gitea test-cmd -c /tmp/app.ini",
|
|
exp: makePathOutput(defaultWorkPath, defaultCustomPath, "/tmp/app.ini"),
|
|
},
|
|
{
|
|
env: map[string]string{"GITEA_WORK_DIR": "/tmp"},
|
|
cmd: "./gitea test-cmd",
|
|
exp: makePathOutput("/tmp", "/tmp/custom", "/tmp/custom/conf/app.ini"),
|
|
},
|
|
{
|
|
env: map[string]string{"GITEA_WORK_DIR": "/tmp"},
|
|
cmd: "./gitea test-cmd --work-path /tmp/other",
|
|
exp: makePathOutput("/tmp/other", "/tmp/other/custom", "/tmp/other/custom/conf/app.ini"),
|
|
},
|
|
{
|
|
env: map[string]string{"GITEA_WORK_DIR": "/tmp"},
|
|
cmd: "./gitea test-cmd --config /tmp/app-other.ini",
|
|
exp: makePathOutput("/tmp", "/tmp/custom", "/tmp/app-other.ini"),
|
|
},
|
|
}
|
|
|
|
app := newTestApp()
|
|
var envBackup []string
|
|
for _, s := range os.Environ() {
|
|
if strings.HasPrefix(s, "GITEA_") && strings.Contains(s, "=") {
|
|
envBackup = append(envBackup, s)
|
|
}
|
|
}
|
|
clearGiteaEnv := func() {
|
|
for _, s := range os.Environ() {
|
|
if strings.HasPrefix(s, "GITEA_") {
|
|
_ = os.Unsetenv(s)
|
|
}
|
|
}
|
|
}
|
|
defer func() {
|
|
clearGiteaEnv()
|
|
for _, s := range envBackup {
|
|
k, v, _ := strings.Cut(s, "=")
|
|
_ = os.Setenv(k, v)
|
|
}
|
|
}()
|
|
|
|
for _, c := range cases {
|
|
clearGiteaEnv()
|
|
for k, v := range c.env {
|
|
_ = os.Setenv(k, v)
|
|
}
|
|
args := strings.Split(c.cmd, " ") // for test only, "split" is good enough
|
|
out := new(strings.Builder)
|
|
app.Writer = out
|
|
err := app.Run(args)
|
|
assert.NoError(t, err, c.cmd)
|
|
assert.NotEmpty(t, c.exp, c.cmd)
|
|
outStr := out.String()
|
|
assert.Contains(t, outStr, c.exp, c.cmd)
|
|
}
|
|
}
|