Skip to content

Commit

Permalink
package nazalog: 1. use fake.Exit 2. bugfix: missing Exit while Fatal
Browse files Browse the repository at this point in the history
  • Loading branch information
q191201771 committed Dec 3, 2019
1 parent a78344e commit 32e0c62
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pkg/fake/exit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
7 changes: 5 additions & 2 deletions pkg/nazalog/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ package nazalog

import (
"fmt"
"os"

"github.com/q191201771/naza/pkg/fake"
)

var global Logger
Expand All @@ -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{}) {
Expand Down Expand Up @@ -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{}) {
Expand All @@ -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)
}
}

Expand Down
8 changes: 5 additions & 3 deletions pkg/nazalog/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
"runtime"
"sync"
"time"

"github.com/q191201771/naza/pkg/fake"
)

const (
Expand Down Expand Up @@ -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{}) {
Expand Down Expand Up @@ -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{}) {
Expand All @@ -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)
}
}

Expand Down
38 changes: 38 additions & 0 deletions pkg/nazalog/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
"os"
"testing"

"github.com/q191201771/naza/pkg/fake"

"github.com/q191201771/naza/pkg/assert"
)

Expand Down Expand Up @@ -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()

Expand Down

0 comments on commit 32e0c62

Please sign in to comment.