[TESTS] log.Level to test.NewLogChecker

So the caller can check log events at the desired level instead of
being limited to the default level log.INFO

(cherry picked from commit f3b487f112)
This commit is contained in:
Earl Warren 2024-01-25 11:35:29 +01:00
parent 4592919fe3
commit 0f8376b0cf
No known key found for this signature in database
GPG key ID: 0579CB2928A78A00
3 changed files with 20 additions and 5 deletions

View file

@ -55,13 +55,15 @@ func (lc *LogChecker) checkLogEvent(event *log.EventFormatted) {
var checkerIndex int64 var checkerIndex int64
func NewLogChecker(namePrefix string) (logChecker *LogChecker, cancel func()) { func NewLogChecker(namePrefix string, level log.Level) (logChecker *LogChecker, cancel func()) {
logger := log.GetManager().GetLogger(namePrefix) logger := log.GetManager().GetLogger(namePrefix)
newCheckerIndex := atomic.AddInt64(&checkerIndex, 1) newCheckerIndex := atomic.AddInt64(&checkerIndex, 1)
writerName := namePrefix + "-" + fmt.Sprint(newCheckerIndex) writerName := namePrefix + "-" + fmt.Sprint(newCheckerIndex)
lc := &LogChecker{} lc := &LogChecker{}
lc.EventWriterBaseImpl = log.NewEventWriterBase(writerName, "test-log-checker", log.WriterMode{}) lc.EventWriterBaseImpl = log.NewEventWriterBase(writerName, "test-log-checker", log.WriterMode{
Level: level,
})
logger.AddWriters(lc) logger.AddWriters(lc)
return lc, func() { _ = logger.RemoveWriter(writerName) } return lc, func() { _ = logger.RemoveWriter(writerName) }
} }

View file

@ -12,8 +12,8 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
func TestLogChecker(t *testing.T) { func TestLogCheckerInfo(t *testing.T) {
lc, cleanup := NewLogChecker(log.DEFAULT) lc, cleanup := NewLogChecker(log.DEFAULT, log.INFO)
defer cleanup() defer cleanup()
lc.Filter("First", "Third").StopMark("End") lc.Filter("First", "Third").StopMark("End")
@ -24,11 +24,13 @@ func TestLogChecker(t *testing.T) {
assert.False(t, stopped) assert.False(t, stopped)
log.Info("First") log.Info("First")
log.Debug("Third")
filtered, stopped = lc.Check(100 * time.Millisecond) filtered, stopped = lc.Check(100 * time.Millisecond)
assert.ElementsMatch(t, []bool{true, false}, filtered) assert.ElementsMatch(t, []bool{true, false}, filtered)
assert.False(t, stopped) assert.False(t, stopped)
log.Info("Second") log.Info("Second")
log.Debug("Third")
filtered, stopped = lc.Check(100 * time.Millisecond) filtered, stopped = lc.Check(100 * time.Millisecond)
assert.ElementsMatch(t, []bool{true, false}, filtered) assert.ElementsMatch(t, []bool{true, false}, filtered)
assert.False(t, stopped) assert.False(t, stopped)
@ -43,3 +45,14 @@ func TestLogChecker(t *testing.T) {
assert.ElementsMatch(t, []bool{true, true}, filtered) assert.ElementsMatch(t, []bool{true, true}, filtered)
assert.True(t, stopped) assert.True(t, stopped)
} }
func TestLogCheckerDebug(t *testing.T) {
lc, cleanup := NewLogChecker(log.DEFAULT, log.DEBUG)
defer cleanup()
lc.StopMark("End")
log.Debug("End")
_, stopped := lc.Check(100 * time.Millisecond)
assert.True(t, stopped)
}

View file

@ -496,7 +496,7 @@ func TestGiteaUploadUpdateGitForPullRequest(t *testing.T) {
t.Run(testCase.name, func(t *testing.T) { t.Run(testCase.name, func(t *testing.T) {
stopMark := fmt.Sprintf(">>>>>>>>>>>>>STOP: %s<<<<<<<<<<<<<<<", testCase.name) stopMark := fmt.Sprintf(">>>>>>>>>>>>>STOP: %s<<<<<<<<<<<<<<<", testCase.name)
logChecker, cleanup := test.NewLogChecker(log.DEFAULT) logChecker, cleanup := test.NewLogChecker(log.DEFAULT, log.INFO)
logChecker.Filter(testCase.logFilter...).StopMark(stopMark) logChecker.Filter(testCase.logFilter...).StopMark(stopMark)
defer cleanup() defer cleanup()