From f3b487f11271711cbdce2b6f0557cb917cc434c4 Mon Sep 17 00:00:00 2001 From: Earl Warren Date: Thu, 25 Jan 2024 11:35:29 +0100 Subject: [PATCH] [TESTS] add 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 --- modules/test/logchecker.go | 6 ++++-- modules/test/logchecker_test.go | 17 +++++++++++++++-- services/migrations/gitea_uploader_test.go | 2 +- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/modules/test/logchecker.go b/modules/test/logchecker.go index 7bf234f560..0f12257f3e 100644 --- a/modules/test/logchecker.go +++ b/modules/test/logchecker.go @@ -55,13 +55,15 @@ func (lc *LogChecker) checkLogEvent(event *log.EventFormatted) { 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) newCheckerIndex := atomic.AddInt64(&checkerIndex, 1) writerName := namePrefix + "-" + fmt.Sprint(newCheckerIndex) 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) return lc, func() { _ = logger.RemoveWriter(writerName) } } diff --git a/modules/test/logchecker_test.go b/modules/test/logchecker_test.go index 6b093ab1b3..0f410fed12 100644 --- a/modules/test/logchecker_test.go +++ b/modules/test/logchecker_test.go @@ -12,8 +12,8 @@ import ( "github.com/stretchr/testify/assert" ) -func TestLogChecker(t *testing.T) { - lc, cleanup := NewLogChecker(log.DEFAULT) +func TestLogCheckerInfo(t *testing.T) { + lc, cleanup := NewLogChecker(log.DEFAULT, log.INFO) defer cleanup() lc.Filter("First", "Third").StopMark("End") @@ -24,11 +24,13 @@ func TestLogChecker(t *testing.T) { assert.False(t, stopped) log.Info("First") + log.Debug("Third") filtered, stopped = lc.Check(100 * time.Millisecond) assert.ElementsMatch(t, []bool{true, false}, filtered) assert.False(t, stopped) log.Info("Second") + log.Debug("Third") filtered, stopped = lc.Check(100 * time.Millisecond) assert.ElementsMatch(t, []bool{true, false}, filtered) assert.False(t, stopped) @@ -43,3 +45,14 @@ func TestLogChecker(t *testing.T) { assert.ElementsMatch(t, []bool{true, true}, filtered) 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) +} diff --git a/services/migrations/gitea_uploader_test.go b/services/migrations/gitea_uploader_test.go index 4ec0361dfb..11f9f44c76 100644 --- a/services/migrations/gitea_uploader_test.go +++ b/services/migrations/gitea_uploader_test.go @@ -495,7 +495,7 @@ func TestGiteaUploadUpdateGitForPullRequest(t *testing.T) { t.Run(testCase.name, func(t *testing.T) { 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) defer cleanup()