From 32e0c621fbdbd24873f7ed394b4d7d7583ebc6ff Mon Sep 17 00:00:00 2001 From: q191201771 <191201771@qq.com> Date: Tue, 3 Dec 2019 17:58:00 +0800 Subject: [PATCH] package nazalog: 1. use fake.Exit 2. bugfix: missing Exit while Fatal --- pkg/fake/exit.go | 2 +- pkg/nazalog/global.go | 7 +++++-- pkg/nazalog/log.go | 8 +++++--- pkg/nazalog/log_test.go | 38 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 49 insertions(+), 6 deletions(-) diff --git a/pkg/fake/exit.go b/pkg/fake/exit.go index 19673e8..c2a950f 100644 --- a/pkg/fake/exit.go +++ b/pkg/fake/exit.go @@ -21,7 +21,7 @@ type ExitResult struct { var exitResult ExitResult -// 正常情况下,调用 os.Exit,单元测试时,可配置为不调用 os.Exit +// 正常情况下,调用 os.Exit,单元测试时,可通过调用 WithFakeExit 配置为不调用 os.Exit func Exit(code int) { exit(code) } diff --git a/pkg/nazalog/global.go b/pkg/nazalog/global.go index addb6c5..3ee3fb6 100644 --- a/pkg/nazalog/global.go +++ b/pkg/nazalog/global.go @@ -10,7 +10,8 @@ package nazalog import ( "fmt" - "os" + + "github.com/q191201771/naza/pkg/fake" ) var global Logger @@ -37,6 +38,7 @@ func Errorf(format string, v ...interface{}) { func Fatalf(format string, v ...interface{}) { global.Out(LevelFatal, 3, fmt.Sprintf(format, v...)) + fake.Exit(1) } func Panicf(format string, v ...interface{}) { @@ -65,6 +67,7 @@ func Error(v ...interface{}) { func Fatal(v ...interface{}) { global.Out(LevelFatal, 3, fmt.Sprint(v...)) + fake.Exit(1) } func Panic(v ...interface{}) { @@ -74,7 +77,7 @@ func Panic(v ...interface{}) { func FatalIfErrorNotNil(err error) { if err != nil { global.Out(LevelError, 3, fmt.Sprintf("fatal since error not nil. err=%+v", err)) - os.Exit(1) + fake.Exit(1) } } diff --git a/pkg/nazalog/log.go b/pkg/nazalog/log.go index ef9f86d..466aa0a 100644 --- a/pkg/nazalog/log.go +++ b/pkg/nazalog/log.go @@ -16,6 +16,8 @@ import ( "runtime" "sync" "time" + + "github.com/q191201771/naza/pkg/fake" ) const ( @@ -87,7 +89,7 @@ func (l *logger) Errorf(format string, v ...interface{}) { func (l *logger) Fatalf(format string, v ...interface{}) { l.Out(LevelFatal, 3, fmt.Sprintf(format, v...)) - os.Exit(1) + fake.Exit(1) } func (l *logger) Panicf(format string, v ...interface{}) { @@ -117,7 +119,7 @@ func (l *logger) Error(v ...interface{}) { func (l *logger) Fatal(v ...interface{}) { l.Out(LevelFatal, 3, fmt.Sprint(v...)) - os.Exit(1) + fake.Exit(1) } func (l *logger) Panic(v ...interface{}) { @@ -128,7 +130,7 @@ func (l *logger) Panic(v ...interface{}) { func (l *logger) FatalIfErrorNotNil(err error) { if err != nil { l.Out(LevelError, 3, fmt.Sprintf("fatal since error not nil. err=%+v", err)) - os.Exit(1) + fake.Exit(1) } } diff --git a/pkg/nazalog/log_test.go b/pkg/nazalog/log_test.go index 0462775..eed893e 100644 --- a/pkg/nazalog/log_test.go +++ b/pkg/nazalog/log_test.go @@ -15,6 +15,8 @@ import ( "os" "testing" + "github.com/q191201771/naza/pkg/fake" + "github.com/q191201771/naza/pkg/assert" ) @@ -150,6 +152,42 @@ func TestPanic(t *testing.T) { }) } +func TestFatal(t *testing.T) { + var er fake.ExitResult + er = fake.WithFakeExit(func() { + FatalIfErrorNotNil(errors.New("fxxk")) + }) + assert.Equal(t, true, er.HasExit) + assert.Equal(t, 1, er.ExitCode) + er = fake.WithFakeExit(func() { + Fatal("Fatal") + }) + assert.Equal(t, true, er.HasExit) + assert.Equal(t, 1, er.ExitCode) + er = fake.WithFakeExit(func() { + Fatalf("Fatalf%s", ".") + }) + assert.Equal(t, true, er.HasExit) + assert.Equal(t, 1, er.ExitCode) + + logger, _ := New() + er = fake.WithFakeExit(func() { + logger.FatalIfErrorNotNil(errors.New("fxxk")) + }) + assert.Equal(t, true, er.HasExit) + assert.Equal(t, 1, er.ExitCode) + er = fake.WithFakeExit(func() { + logger.Fatal("Fatal") + }) + assert.Equal(t, true, er.HasExit) + assert.Equal(t, 1, er.ExitCode) + er = fake.WithFakeExit(func() { + logger.Fatalf("Fatalf%s", ".") + }) + assert.Equal(t, true, er.HasExit) + assert.Equal(t, 1, er.ExitCode) +} + func BenchmarkStdout(b *testing.B) { b.ReportAllocs()