mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-24 22:35:39 +03:00
2cdf260f42
# The problem There were many "path tricks": * By default, Gitea uses its program directory as its work path * Gitea tries to use the "work path" to guess its "custom path" and "custom conf (app.ini)" * Users might want to use other directories as work path * The non-default work path should be passed to Gitea by GITEA_WORK_DIR or "--work-path" * But some Gitea processes are started without these values * The "serv" process started by OpenSSH server * The CLI sub-commands started by site admin * The paths are guessed by SetCustomPathAndConf again and again * The default values of "work path / custom path / custom conf" can be changed when compiling # The solution * Use `InitWorkPathAndCommonConfig` to handle these path tricks, and use test code to cover its behaviors. * When Gitea's web server runs, write the WORK_PATH to "app.ini", this value must be the most correct one, because if this value is not right, users would find that the web UI doesn't work and then they should be able to fix it. * Then all other sub-commands can use the WORK_PATH in app.ini to initialize their paths. * By the way, when Gitea starts for git protocol, it shouldn't output any log, otherwise the git protocol gets broken and client blocks forever. The "work path" priority is: WORK_PATH in app.ini > cmd arg --work-path > env var GITEA_WORK_DIR > builtin default The "app.ini" searching order is: cmd arg --config > cmd arg "work path / custom path" > env var "work path / custom path" > builtin default ## ⚠️ BREAKING If your instance's "work path / custom path / custom conf" doesn't meet the requirements (eg: work path must be absolute), Gitea will report a fatal error and exit. You need to set these values according to the error log. ---- Close #24818 Close #24222 Close #21606 Close #21498 Close #25107 Close #24981 Maybe close #24503 Replace #23301 Replace #22754 And maybe more
169 lines
4.6 KiB
Go
169 lines
4.6 KiB
Go
// Copyright 2019 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package testlogger
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"runtime"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
"code.gitea.io/gitea/modules/queue"
|
|
)
|
|
|
|
var (
|
|
prefix string
|
|
SlowTest = 10 * time.Second
|
|
SlowFlush = 5 * time.Second
|
|
)
|
|
|
|
var WriterCloser = &testLoggerWriterCloser{}
|
|
|
|
type testLoggerWriterCloser struct {
|
|
sync.RWMutex
|
|
t []testing.TB
|
|
}
|
|
|
|
func (w *testLoggerWriterCloser) pushT(t testing.TB) {
|
|
w.Lock()
|
|
w.t = append(w.t, t)
|
|
w.Unlock()
|
|
}
|
|
|
|
func (w *testLoggerWriterCloser) Write(p []byte) (int, error) {
|
|
// There was a data race problem: the logger system could still try to output logs after the runner is finished.
|
|
// So we must ensure that the "t" in stack is still valid.
|
|
w.RLock()
|
|
defer w.RUnlock()
|
|
|
|
var t testing.TB
|
|
if len(w.t) > 0 {
|
|
t = w.t[len(w.t)-1]
|
|
}
|
|
|
|
if len(p) > 0 && p[len(p)-1] == '\n' {
|
|
p = p[:len(p)-1]
|
|
}
|
|
|
|
if t == nil {
|
|
// if there is no running test, the log message should be outputted to console, to avoid losing important information.
|
|
// the "???" prefix is used to match the "===" and "+++" in PrintCurrentTest
|
|
return fmt.Fprintf(os.Stdout, "??? [TestLogger] %s\n", p)
|
|
}
|
|
|
|
t.Log(string(p))
|
|
return len(p), nil
|
|
}
|
|
|
|
func (w *testLoggerWriterCloser) popT() {
|
|
w.Lock()
|
|
if len(w.t) > 0 {
|
|
w.t = w.t[:len(w.t)-1]
|
|
}
|
|
w.Unlock()
|
|
}
|
|
|
|
func (w *testLoggerWriterCloser) Close() error {
|
|
return nil
|
|
}
|
|
|
|
func (w *testLoggerWriterCloser) Reset() {
|
|
w.Lock()
|
|
if len(w.t) > 0 {
|
|
for _, t := range w.t {
|
|
if t == nil {
|
|
continue
|
|
}
|
|
_, _ = fmt.Fprintf(os.Stdout, "Unclosed logger writer in test: %s", t.Name())
|
|
t.Errorf("Unclosed logger writer in test: %s", t.Name())
|
|
}
|
|
w.t = nil
|
|
}
|
|
w.Unlock()
|
|
}
|
|
|
|
// PrintCurrentTest prints the current test to os.Stdout
|
|
func PrintCurrentTest(t testing.TB, skip ...int) func() {
|
|
t.Helper()
|
|
start := time.Now()
|
|
actualSkip := 1
|
|
if len(skip) > 0 {
|
|
actualSkip = skip[0] + 1
|
|
}
|
|
_, filename, line, _ := runtime.Caller(actualSkip)
|
|
|
|
if log.CanColorStdout {
|
|
_, _ = fmt.Fprintf(os.Stdout, "=== %s (%s:%d)\n", fmt.Formatter(log.NewColoredValue(t.Name())), strings.TrimPrefix(filename, prefix), line)
|
|
} else {
|
|
_, _ = fmt.Fprintf(os.Stdout, "=== %s (%s:%d)\n", t.Name(), strings.TrimPrefix(filename, prefix), line)
|
|
}
|
|
WriterCloser.pushT(t)
|
|
return func() {
|
|
took := time.Since(start)
|
|
if took > SlowTest {
|
|
if log.CanColorStdout {
|
|
_, _ = fmt.Fprintf(os.Stdout, "+++ %s is a slow test (took %v)\n", fmt.Formatter(log.NewColoredValue(t.Name(), log.Bold, log.FgYellow)), fmt.Formatter(log.NewColoredValue(took, log.Bold, log.FgYellow)))
|
|
} else {
|
|
_, _ = fmt.Fprintf(os.Stdout, "+++ %s is a slow test (took %v)\n", t.Name(), took)
|
|
}
|
|
}
|
|
timer := time.AfterFunc(SlowFlush, func() {
|
|
if log.CanColorStdout {
|
|
_, _ = fmt.Fprintf(os.Stdout, "+++ %s ... still flushing after %v ...\n", fmt.Formatter(log.NewColoredValue(t.Name(), log.Bold, log.FgRed)), SlowFlush)
|
|
} else {
|
|
_, _ = fmt.Fprintf(os.Stdout, "+++ %s ... still flushing after %v ...\n", t.Name(), SlowFlush)
|
|
}
|
|
})
|
|
if err := queue.GetManager().FlushAll(context.Background(), time.Minute); err != nil {
|
|
t.Errorf("Flushing queues failed with error %v", err)
|
|
}
|
|
timer.Stop()
|
|
flushTook := time.Since(start) - took
|
|
if flushTook > SlowFlush {
|
|
if log.CanColorStdout {
|
|
_, _ = fmt.Fprintf(os.Stdout, "+++ %s had a slow clean-up flush (took %v)\n", fmt.Formatter(log.NewColoredValue(t.Name(), log.Bold, log.FgRed)), fmt.Formatter(log.NewColoredValue(flushTook, log.Bold, log.FgRed)))
|
|
} else {
|
|
_, _ = fmt.Fprintf(os.Stdout, "+++ %s had a slow clean-up flush (took %v)\n", t.Name(), flushTook)
|
|
}
|
|
}
|
|
WriterCloser.popT()
|
|
}
|
|
}
|
|
|
|
// Printf takes a format and args and prints the string to os.Stdout
|
|
func Printf(format string, args ...interface{}) {
|
|
if log.CanColorStdout {
|
|
for i := 0; i < len(args); i++ {
|
|
args[i] = log.NewColoredValue(args[i])
|
|
}
|
|
}
|
|
_, _ = fmt.Fprintf(os.Stdout, "\t"+format, args...)
|
|
}
|
|
|
|
// TestLogEventWriter is a logger which will write to the testing log
|
|
type TestLogEventWriter struct {
|
|
*log.EventWriterBaseImpl
|
|
}
|
|
|
|
// NewTestLoggerWriter creates a TestLogEventWriter as a log.LoggerProvider
|
|
func NewTestLoggerWriter(name string, mode log.WriterMode) log.EventWriter {
|
|
w := &TestLogEventWriter{}
|
|
w.EventWriterBaseImpl = log.NewEventWriterBase(name, "test-log-writer", mode)
|
|
w.OutputWriteCloser = WriterCloser
|
|
return w
|
|
}
|
|
|
|
func init() {
|
|
const relFilePath = "modules/testlogger/testlogger.go"
|
|
_, filename, _, _ := runtime.Caller(0)
|
|
if !strings.HasSuffix(filename, relFilePath) {
|
|
panic("source code file path doesn't match expected: " + relFilePath)
|
|
}
|
|
prefix = strings.TrimSuffix(filename, relFilePath)
|
|
}
|