aboutsummaryrefslogtreecommitdiffstats
path: root/services
diff options
context:
space:
mode:
authorwxiaoguang <wxiaoguang@gmail.com>2023-04-22 04:32:25 +0800
committerGitHub <noreply@github.com>2023-04-21 16:32:25 -0400
commit911975059a50e71632d33567da7000a97aa28f22 (patch)
tree336ccc3a6ca64ab0967daf4cbbd8f908f4dac078 /services
parent65fe0fb22cfb264f0b756065d0c3ce7a17d7e55b (diff)
downloadgitea-911975059a50e71632d33567da7000a97aa28f22.tar.gz
gitea-911975059a50e71632d33567da7000a97aa28f22.zip
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.
Diffstat (limited to 'services')
-rw-r--r--services/migrations/gitea_uploader_test.go67
1 files changed, 28 insertions, 39 deletions
diff --git a/services/migrations/gitea_uploader_test.go b/services/migrations/gitea_uploader_test.go
index 6a942b9b57..b59ccb7c44 100644
--- a/services/migrations/gitea_uploader_test.go
+++ b/services/migrations/gitea_uploader_test.go
@@ -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)
}
})
}