mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-05-16 23:12:43 +00:00
Improve test logger (#24235)
Before, there was a `log/buffer.go`, but that design is not general, and it introduces a lot of irrelevant `Content() (string, error) ` and `return "", fmt.Errorf("not supported")` . And the old `log/buffer.go` is difficult to use, developers have to write a lot of `Contains` and `Sleep` code. The new `LogChecker` is designed to be a general approach to help to assert some messages appearing or not appearing in logs.
This commit is contained in:
parent
65fe0fb22c
commit
911975059a
13 changed files with 195 additions and 214 deletions
|
@ -10,7 +10,6 @@ import (
|
|||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
|
@ -24,6 +23,7 @@ import (
|
|||
"code.gitea.io/gitea/modules/log"
|
||||
base "code.gitea.io/gitea/modules/migration"
|
||||
"code.gitea.io/gitea/modules/structs"
|
||||
"code.gitea.io/gitea/modules/test"
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
@ -312,10 +312,11 @@ func TestGiteaUploadUpdateGitForPullRequest(t *testing.T) {
|
|||
}))
|
||||
|
||||
for _, testCase := range []struct {
|
||||
name string
|
||||
head string
|
||||
assertContent func(t *testing.T, content string)
|
||||
pr base.PullRequest
|
||||
name string
|
||||
head string
|
||||
logFilter []string
|
||||
logFiltered []bool
|
||||
pr base.PullRequest
|
||||
}{
|
||||
{
|
||||
name: "fork, good Head.SHA",
|
||||
|
@ -362,9 +363,8 @@ func TestGiteaUploadUpdateGitForPullRequest(t *testing.T) {
|
|||
OwnerName: forkRepo.OwnerName,
|
||||
},
|
||||
},
|
||||
assertContent: func(t *testing.T, content string) {
|
||||
assert.Contains(t, content, "Fetch branch from")
|
||||
},
|
||||
logFilter: []string{"Fetch branch from"},
|
||||
logFiltered: []bool{true},
|
||||
},
|
||||
{
|
||||
name: "invalid fork CloneURL",
|
||||
|
@ -388,9 +388,8 @@ func TestGiteaUploadUpdateGitForPullRequest(t *testing.T) {
|
|||
OwnerName: "WRONG",
|
||||
},
|
||||
},
|
||||
assertContent: func(t *testing.T, content string) {
|
||||
assert.Contains(t, content, "AddRemote")
|
||||
},
|
||||
logFilter: []string{"AddRemote"},
|
||||
logFiltered: []bool{true},
|
||||
},
|
||||
{
|
||||
name: "no fork, good Head.SHA",
|
||||
|
@ -437,10 +436,8 @@ func TestGiteaUploadUpdateGitForPullRequest(t *testing.T) {
|
|||
OwnerName: fromRepo.OwnerName,
|
||||
},
|
||||
},
|
||||
assertContent: func(t *testing.T, content string) {
|
||||
assert.Contains(t, content, "Empty reference")
|
||||
assert.NotContains(t, content, "Cannot remove local head")
|
||||
},
|
||||
logFilter: []string{"Empty reference", "Cannot remove local head"},
|
||||
logFiltered: []bool{true, false},
|
||||
},
|
||||
{
|
||||
name: "no fork, invalid Head.SHA",
|
||||
|
@ -464,9 +461,8 @@ func TestGiteaUploadUpdateGitForPullRequest(t *testing.T) {
|
|||
OwnerName: fromRepo.OwnerName,
|
||||
},
|
||||
},
|
||||
assertContent: func(t *testing.T, content string) {
|
||||
assert.Contains(t, content, "Deprecated local head")
|
||||
},
|
||||
logFilter: []string{"Deprecated local head"},
|
||||
logFiltered: []bool{true},
|
||||
},
|
||||
{
|
||||
name: "no fork, not found Head.SHA",
|
||||
|
@ -490,36 +486,29 @@ func TestGiteaUploadUpdateGitForPullRequest(t *testing.T) {
|
|||
OwnerName: fromRepo.OwnerName,
|
||||
},
|
||||
},
|
||||
assertContent: func(t *testing.T, content string) {
|
||||
assert.Contains(t, content, "Deprecated local head")
|
||||
assert.NotContains(t, content, "Cannot remove local head")
|
||||
},
|
||||
logFilter: []string{"Deprecated local head", "Cannot remove local head"},
|
||||
logFiltered: []bool{true, false},
|
||||
},
|
||||
} {
|
||||
t.Run(testCase.name, func(t *testing.T) {
|
||||
logger, ok := log.NamedLoggers.Load(log.DEFAULT)
|
||||
assert.True(t, ok)
|
||||
logger.SetLogger("buffer", "buffer", "{}")
|
||||
defer logger.DelLogger("buffer")
|
||||
stopMark := fmt.Sprintf(">>>>>>>>>>>>>STOP: %s<<<<<<<<<<<<<<<", testCase.name)
|
||||
|
||||
logChecker, cleanup := test.NewLogChecker(log.DEFAULT)
|
||||
logChecker.Filter(testCase.logFilter...).StopMark(stopMark)
|
||||
defer cleanup()
|
||||
|
||||
testCase.pr.EnsuredSafe = true
|
||||
|
||||
head, err := uploader.updateGitForPullRequest(&testCase.pr)
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, testCase.head, head)
|
||||
if testCase.assertContent != nil {
|
||||
fence := fmt.Sprintf(">>>>>>>>>>>>>FENCE %s<<<<<<<<<<<<<<<", testCase.name)
|
||||
log.Error(fence)
|
||||
var content string
|
||||
for i := 0; i < 5000; i++ {
|
||||
content, err = logger.GetLoggerProviderContent("buffer")
|
||||
assert.NoError(t, err)
|
||||
if strings.Contains(content, fence) {
|
||||
break
|
||||
}
|
||||
time.Sleep(1 * time.Millisecond)
|
||||
}
|
||||
testCase.assertContent(t, content)
|
||||
|
||||
log.Info(stopMark)
|
||||
|
||||
logFiltered, logStopped := logChecker.Check(5 * time.Second)
|
||||
assert.True(t, logStopped)
|
||||
if len(testCase.logFilter) > 0 {
|
||||
assert.EqualValues(t, testCase.logFiltered, logFiltered, "for log message filters: %v", testCase.logFilter)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue