From 014df1c8fc074fd68a0b6418317185e53434aace Mon Sep 17 00:00:00 2001 From: NoahLan <6995syu@163.com> Date: Thu, 20 Jul 2023 17:27:33 +0800 Subject: [PATCH] chore: add more tests --- nlog/logs_test.go | 22 ++++++++++++++++++++++ nlog/rotatelogger.go | 4 ++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/nlog/logs_test.go b/nlog/logs_test.go index c965715..ac1d853 100644 --- a/nlog/logs_test.go +++ b/nlog/logs_test.go @@ -24,6 +24,10 @@ var ( _ Writer = (*mockWriter)(nil) ) +func init() { + ExitOnFatal.Set(false) +} + type mockWriter struct { lock sync.Mutex builder strings.Builder @@ -208,6 +212,12 @@ func TestFileLineConsoleMode(t *testing.T) { assert.True(t, w.Contains(fmt.Sprintf("%s:%d", file, line+1))) } +func TestMust(t *testing.T) { + assert.Panics(t, func() { + Must(errors.New("foo")) + }) +} + func TestStructedLogAlert(t *testing.T) { w := new(mockWriter) old := writer.Swap(w) @@ -574,26 +584,38 @@ func TestSetup(t *testing.T) { atomic.StoreUint32(&encoding, jsonEncodingType) }() + setupOnce = sync.Once{} + MustSetup(LogConf{ + ServiceName: "any", + Mode: "console", + Encoding: "json", + TimeFormat: timeFormat, + }) + setupOnce = sync.Once{} MustSetup(LogConf{ ServiceName: "any", Mode: "console", TimeFormat: timeFormat, }) + setupOnce = sync.Once{} MustSetup(LogConf{ ServiceName: "any", Mode: "file", Path: os.TempDir(), }) + setupOnce = sync.Once{} MustSetup(LogConf{ ServiceName: "any", Mode: "volume", Path: os.TempDir(), }) + setupOnce = sync.Once{} MustSetup(LogConf{ ServiceName: "any", Mode: "console", TimeFormat: timeFormat, }) + setupOnce = sync.Once{} MustSetup(LogConf{ ServiceName: "any", Mode: "console", diff --git a/nlog/rotatelogger.go b/nlog/rotatelogger.go index 22c25c1..f340a64 100644 --- a/nlog/rotatelogger.go +++ b/nlog/rotatelogger.go @@ -234,7 +234,7 @@ func NewLogger(filename string, rule RotateRule, compress bool) (*RotateLogger, rule: rule, compress: compress, } - if err := l.init(); err != nil { + if err := l.initialize(); err != nil { return nil, err } @@ -278,7 +278,7 @@ func (l *RotateLogger) getBackupFilename() string { return l.backup } -func (l *RotateLogger) init() error { +func (l *RotateLogger) initialize() error { l.backup = l.rule.BackupFileName() if fileInfo, err := os.Stat(l.filename); err != nil {