[TESTS] prevent overriding testlogger when calling mainApp
(cherry picked from commit 5785ae72c7
)
This commit is contained in:
parent
c01935e9d0
commit
966faddee4
|
@ -128,6 +128,11 @@ func (w *testLoggerWriterCloser) recordError(msg string) {
|
|||
err = w.errs[len(w.errs)-1]
|
||||
}
|
||||
|
||||
if len(w.t) > 0 {
|
||||
// format error message to easily add it to the ignore list
|
||||
msg = fmt.Sprintf("// %s\n\t`%s`,", w.t[len(w.t)-1].Name(), msg)
|
||||
}
|
||||
|
||||
err = errors.Join(err, errors.New(msg))
|
||||
|
||||
if len(w.errs) > 0 {
|
||||
|
|
|
@ -4,8 +4,11 @@ package integration
|
|||
|
||||
import (
|
||||
gocontext "context"
|
||||
"errors"
|
||||
"io"
|
||||
"net/url"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
|
@ -19,16 +22,18 @@ import (
|
|||
|
||||
func Test_CmdForgejo_Actions(t *testing.T) {
|
||||
onGiteaRun(t, func(*testing.T, *url.URL) {
|
||||
token, err := cmdForgejoCaptureOutput(t, []string{"forgejo", "forgejo-cli", "actions", "generate-runner-token"})
|
||||
token, err := runMainApp("forgejo-cli", "actions", "generate-runner-token")
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, 40, len(token))
|
||||
|
||||
secret, err := cmdForgejoCaptureOutput(t, []string{"forgejo", "forgejo-cli", "actions", "generate-secret"})
|
||||
secret, err := runMainApp("forgejo-cli", "actions", "generate-secret")
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, 40, len(secret))
|
||||
|
||||
_, err = cmdForgejoCaptureOutput(t, []string{"forgejo", "forgejo-cli", "actions", "register"})
|
||||
assert.ErrorContains(t, err, "at least one of the --secret")
|
||||
_, err = runMainApp("forgejo-cli", "actions", "register")
|
||||
var exitErr *exec.ExitError
|
||||
assert.True(t, errors.As(err, &exitErr))
|
||||
assert.Contains(t, string(exitErr.Stderr), "at least one of the --secret")
|
||||
|
||||
for _, testCase := range []struct {
|
||||
testName string
|
||||
|
@ -62,10 +67,12 @@ func Test_CmdForgejo_Actions(t *testing.T) {
|
|||
},
|
||||
} {
|
||||
t.Run(testCase.testName, func(t *testing.T) {
|
||||
cmd := []string{"forgejo", "forgejo-cli", "actions", "register", "--secret", testCase.secret, "--scope", testCase.scope}
|
||||
output, err := cmdForgejoCaptureOutput(t, cmd)
|
||||
assert.ErrorContains(t, err, testCase.errorMessage)
|
||||
output, err := runMainApp("forgejo-cli", "actions", "register", "--secret", testCase.secret, "--scope", testCase.scope)
|
||||
assert.EqualValues(t, "", output)
|
||||
|
||||
var exitErr *exec.ExitError
|
||||
assert.True(t, errors.As(err, &exitErr))
|
||||
assert.Contains(t, string(exitErr.Stderr), testCase.errorMessage)
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -75,7 +82,7 @@ func Test_CmdForgejo_Actions(t *testing.T) {
|
|||
for _, testCase := range []struct {
|
||||
testName string
|
||||
secretOption func() string
|
||||
stdin []string
|
||||
stdin io.Reader
|
||||
}{
|
||||
{
|
||||
testName: "secret from argument",
|
||||
|
@ -88,7 +95,7 @@ func Test_CmdForgejo_Actions(t *testing.T) {
|
|||
secretOption: func() string {
|
||||
return "--secret-stdin"
|
||||
},
|
||||
stdin: []string{secret},
|
||||
stdin: strings.NewReader(secret),
|
||||
},
|
||||
{
|
||||
testName: "secret from file",
|
||||
|
@ -100,8 +107,7 @@ func Test_CmdForgejo_Actions(t *testing.T) {
|
|||
},
|
||||
} {
|
||||
t.Run(testCase.testName, func(t *testing.T) {
|
||||
cmd := []string{"forgejo", "forgejo-cli", "actions", "register", testCase.secretOption(), "--scope=org26"}
|
||||
uuid, err := cmdForgejoCaptureOutput(t, cmd, testCase.stdin...)
|
||||
uuid, err := runMainAppWithStdin(testCase.stdin, "forgejo-cli", "actions", "register", testCase.secretOption(), "--scope=org26")
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, expecteduuid, uuid)
|
||||
})
|
||||
|
@ -161,7 +167,7 @@ func Test_CmdForgejo_Actions(t *testing.T) {
|
|||
} {
|
||||
t.Run(testCase.testName, func(t *testing.T) {
|
||||
cmd := []string{
|
||||
"forgejo", "forgejo-cli", "actions", "register",
|
||||
"actions", "register",
|
||||
"--secret", testCase.secret, "--scope", testCase.scope,
|
||||
}
|
||||
if testCase.name != "" {
|
||||
|
@ -177,7 +183,7 @@ func Test_CmdForgejo_Actions(t *testing.T) {
|
|||
// Run twice to verify it is idempotent
|
||||
//
|
||||
for i := 0; i < 2; i++ {
|
||||
uuid, err := cmdForgejoCaptureOutput(t, cmd)
|
||||
uuid, err := runMainApp("forgejo-cli", cmd...)
|
||||
assert.NoError(t, err)
|
||||
if assert.EqualValues(t, testCase.uuid, uuid) {
|
||||
ownerName, repoName, found := strings.Cut(testCase.scope, "/")
|
||||
|
|
|
@ -1,36 +0,0 @@
|
|||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package integration
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"code.gitea.io/gitea/cmd/forgejo"
|
||||
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
func cmdForgejoCaptureOutput(t *testing.T, args []string, stdin ...string) (string, error) {
|
||||
buf := new(bytes.Buffer)
|
||||
|
||||
app := cli.NewApp()
|
||||
app.Writer = buf
|
||||
app.ErrWriter = buf
|
||||
ctx := context.Background()
|
||||
ctx = forgejo.ContextSetNoInit(ctx, true)
|
||||
ctx = forgejo.ContextSetNoExit(ctx, true)
|
||||
ctx = forgejo.ContextSetStdout(ctx, buf)
|
||||
ctx = forgejo.ContextSetStderr(ctx, buf)
|
||||
if len(stdin) > 0 {
|
||||
ctx = forgejo.ContextSetStdin(ctx, strings.NewReader(strings.Join(stdin, "")))
|
||||
}
|
||||
app.Commands = []*cli.Command{
|
||||
forgejo.CmdForgejo(ctx),
|
||||
}
|
||||
err := app.Run(args)
|
||||
|
||||
return buf.String(), err
|
||||
}
|
|
@ -4,16 +4,15 @@
|
|||
package integration
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"net/url"
|
||||
"os/exec"
|
||||
"testing"
|
||||
|
||||
"code.gitea.io/gitea/cmd"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
func Test_CmdKeys(t *testing.T) {
|
||||
|
@ -24,30 +23,30 @@ func Test_CmdKeys(t *testing.T) {
|
|||
wantErr bool
|
||||
expectedOutput string
|
||||
}{
|
||||
{"test_empty_1", []string{"keys", "--username=git", "--type=test", "--content=test"}, true, ""},
|
||||
{"test_empty_2", []string{"keys", "-e", "git", "-u", "git", "-t", "test", "-k", "test"}, true, ""},
|
||||
{"test_empty_1", []string{"--username=git", "--type=test", "--content=test"}, true, ""},
|
||||
{"test_empty_2", []string{"-e", "git", "-u", "git", "-t", "test", "-k", "test"}, true, ""},
|
||||
{
|
||||
"with_key",
|
||||
[]string{"keys", "-e", "git", "-u", "git", "-t", "ssh-rsa", "-k", "AAAAB3NzaC1yc2EAAAADAQABAAABgQDWVj0fQ5N8wNc0LVNA41wDLYJ89ZIbejrPfg/avyj3u/ZohAKsQclxG4Ju0VirduBFF9EOiuxoiFBRr3xRpqzpsZtnMPkWVWb+akZwBFAx8p+jKdy4QXR/SZqbVobrGwip2UjSrri1CtBxpJikojRIZfCnDaMOyd9Jp6KkujvniFzUWdLmCPxUE9zhTaPu0JsEP7MW0m6yx7ZUhHyfss+NtqmFTaDO+QlMR7L2QkDliN2Jl3Xa3PhuWnKJfWhdAq1Cw4oraKUOmIgXLkuiuxVQ6mD3AiFupkmfqdHq6h+uHHmyQqv3gU+/sD8GbGAhf6ftqhTsXjnv1Aj4R8NoDf9BS6KRkzkeun5UisSzgtfQzjOMEiJtmrep2ZQrMGahrXa+q4VKr0aKJfm+KlLfwm/JztfsBcqQWNcTURiCFqz+fgZw0Ey/de0eyMzldYTdXXNRYCKjs9bvBK+6SSXRM7AhftfQ0ZuoW5+gtinPrnmoOaSCEJbAiEiTO/BzOHgowiM="},
|
||||
[]string{"-e", "git", "-u", "git", "-t", "ssh-rsa", "-k", "AAAAB3NzaC1yc2EAAAADAQABAAABgQDWVj0fQ5N8wNc0LVNA41wDLYJ89ZIbejrPfg/avyj3u/ZohAKsQclxG4Ju0VirduBFF9EOiuxoiFBRr3xRpqzpsZtnMPkWVWb+akZwBFAx8p+jKdy4QXR/SZqbVobrGwip2UjSrri1CtBxpJikojRIZfCnDaMOyd9Jp6KkujvniFzUWdLmCPxUE9zhTaPu0JsEP7MW0m6yx7ZUhHyfss+NtqmFTaDO+QlMR7L2QkDliN2Jl3Xa3PhuWnKJfWhdAq1Cw4oraKUOmIgXLkuiuxVQ6mD3AiFupkmfqdHq6h+uHHmyQqv3gU+/sD8GbGAhf6ftqhTsXjnv1Aj4R8NoDf9BS6KRkzkeun5UisSzgtfQzjOMEiJtmrep2ZQrMGahrXa+q4VKr0aKJfm+KlLfwm/JztfsBcqQWNcTURiCFqz+fgZw0Ey/de0eyMzldYTdXXNRYCKjs9bvBK+6SSXRM7AhftfQ0ZuoW5+gtinPrnmoOaSCEJbAiEiTO/BzOHgowiM="},
|
||||
false,
|
||||
"# gitea public key\ncommand=\"" + setting.AppPath + " --config=" + util.ShellEscape(setting.CustomConf) + " serv key-1\",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty,no-user-rc,restrict ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDWVj0fQ5N8wNc0LVNA41wDLYJ89ZIbejrPfg/avyj3u/ZohAKsQclxG4Ju0VirduBFF9EOiuxoiFBRr3xRpqzpsZtnMPkWVWb+akZwBFAx8p+jKdy4QXR/SZqbVobrGwip2UjSrri1CtBxpJikojRIZfCnDaMOyd9Jp6KkujvniFzUWdLmCPxUE9zhTaPu0JsEP7MW0m6yx7ZUhHyfss+NtqmFTaDO+QlMR7L2QkDliN2Jl3Xa3PhuWnKJfWhdAq1Cw4oraKUOmIgXLkuiuxVQ6mD3AiFupkmfqdHq6h+uHHmyQqv3gU+/sD8GbGAhf6ftqhTsXjnv1Aj4R8NoDf9BS6KRkzkeun5UisSzgtfQzjOMEiJtmrep2ZQrMGahrXa+q4VKr0aKJfm+KlLfwm/JztfsBcqQWNcTURiCFqz+fgZw0Ey/de0eyMzldYTdXXNRYCKjs9bvBK+6SSXRM7AhftfQ0ZuoW5+gtinPrnmoOaSCEJbAiEiTO/BzOHgowiM= user2@localhost\n",
|
||||
},
|
||||
{"invalid", []string{"keys", "--not-a-flag=git"}, true, "Incorrect Usage: flag provided but not defined: -not-a-flag\n\n"},
|
||||
{"invalid", []string{"--not-a-flag=git"}, true, "Incorrect Usage: flag provided but not defined: -not-a-flag\n\n"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
out := new(bytes.Buffer)
|
||||
app := cli.NewApp()
|
||||
app.Writer = out
|
||||
app.Commands = []*cli.Command{cmd.CmdKeys}
|
||||
cmd.CmdKeys.HideHelp = true
|
||||
err := app.Run(append([]string{"prog"}, tt.args...))
|
||||
out, err := runMainApp("keys", tt.args...)
|
||||
|
||||
var exitErr *exec.ExitError
|
||||
if errors.As(err, &exitErr) {
|
||||
t.Log(string(exitErr.Stderr))
|
||||
}
|
||||
if tt.wantErr {
|
||||
assert.Error(t, err)
|
||||
} else {
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
assert.Equal(t, tt.expectedOutput, out.String())
|
||||
assert.Equal(t, tt.expectedOutput, out)
|
||||
})
|
||||
}
|
||||
})
|
||||
|
|
|
@ -17,6 +17,7 @@ import (
|
|||
"net/http/httptest"
|
||||
"net/url"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
@ -24,6 +25,7 @@ import (
|
|||
"testing"
|
||||
"time"
|
||||
|
||||
"code.gitea.io/gitea/cmd"
|
||||
"code.gitea.io/gitea/models/auth"
|
||||
"code.gitea.io/gitea/models/db"
|
||||
repo_model "code.gitea.io/gitea/models/repo"
|
||||
|
@ -93,7 +95,43 @@ func NewNilResponseHashSumRecorder() *NilResponseHashSumRecorder {
|
|||
}
|
||||
}
|
||||
|
||||
// runMainApp runs the subcommand and returns its standard output. Any returned error will usually be of type *ExitError. If c.Stderr was nil, Output populates ExitError.Stderr.
|
||||
func runMainApp(subcommand string, args ...string) (string, error) {
|
||||
return runMainAppWithStdin(nil, subcommand, args...)
|
||||
}
|
||||
|
||||
// runMainAppWithStdin runs the subcommand and returns its standard output. Any returned error will usually be of type *ExitError. If c.Stderr was nil, Output populates ExitError.Stderr.
|
||||
func runMainAppWithStdin(stdin io.Reader, subcommand string, args ...string) (string, error) {
|
||||
// running the main app directly will very likely mess with the testing setup (logger & co.)
|
||||
// hence we run it as a subprocess and capture its output
|
||||
args = append([]string{subcommand}, args...)
|
||||
cmd := exec.Command(os.Args[0], args...)
|
||||
cmd.Env = append(os.Environ(),
|
||||
"GITEA_TEST_CLI=true",
|
||||
"GITEA_CONF="+setting.CustomConf,
|
||||
"GITEA_WORK_DIR="+setting.AppWorkPath)
|
||||
cmd.Stdin = stdin
|
||||
out, err := cmd.Output()
|
||||
return string(out), err
|
||||
}
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
// GITEA_TEST_CLI is set by runMainAppWithStdin
|
||||
// inspired by https://abhinavg.net/2022/05/15/hijack-testmain/
|
||||
if testCLI := os.Getenv("GITEA_TEST_CLI"); testCLI == "true" {
|
||||
app := cmd.NewMainApp("test-version", "integration-test")
|
||||
args := append([]string{
|
||||
"executable-name", // unused, but expected at position 1
|
||||
"--config", os.Getenv("GITEA_CONF"),
|
||||
},
|
||||
os.Args[1:]..., // skip the executable name
|
||||
)
|
||||
if err := cmd.RunMainApp(app, args...); err != nil {
|
||||
panic(err) // should never happen since RunMainApp exits on error
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
defer log.GetManager().Close()
|
||||
|
||||
managerCtx, cancel := context.WithCancel(context.Background())
|
||||
|
|
Loading…
Reference in New Issue