1. feat: package fake: add func Exit 2. test: package log: use fake.Exit

pull/2/head
q191201771 5 years ago
parent 8e5c9947f1
commit 3bee539a75

1
.gitignore vendored

@ -11,6 +11,7 @@ profile*.pdf
/pre-commit.sh
/TODO.md
/pkg/snowflake
/pkg/tag
/pkg/nazatime
/demo/samefile

@ -45,7 +45,7 @@ pkg/ ...... 源码包
|-- ratelimit/ ...... 限流器,令牌桶
|-- ic/ ...... 将整型切片压缩成二进制字节切片
|-- unique/ ...... 对象唯一 ID
|-- fake/ ...... 实现一些常用的接口,辅助测试其它代码
|-- fake/ ...... stub和mock相关实现一些常用的接口,辅助测试其它代码
demo/ ...... 示例相关的代码
bin/ ...... 可执行文件编译输出目录
```

@ -25,7 +25,7 @@ import (
var licenseTmpl = `
> **** [https://pengrl.com/p/%s/](https://pengrl.com/p/%s/)
> **** [yoko blog](https://pengrl.com) (https://pengrl.com)
> **** yoko
> **** [yoko](https://github.com/q191201771) (https://github.com/q191201771)
> **** `
func main() {
@ -46,13 +46,13 @@ func main() {
lines := bytes.Split(content, []byte{'\n'})
//if bytes.Index(lines[len(lines)-1], []byte("声明")) != -1 {
// res, err := filebatch.DeleteLines(content, filebatch.LineRange{From: -2, To: -1})
// res, err := filebatch.DeleteLines(content, filebatch.LineRange{From: -4, To: -1})
// nazalog.Debugf("%s -2", info.Name())
// nazalog.FatalIfErrorNotNil(err)
// return res
//}
//if bytes.Index(lines[len(lines)-2], []byte("声明")) != -1 {
// res, err := filebatch.DeleteLines(content, filebatch.LineRange{From: -3, To: -1})
// res, err := filebatch.DeleteLines(content, filebatch.LineRange{From: -5, To: -1})
// nazalog.Debugf("%s -3", info.Name())
// nazalog.FatalIfErrorNotNil(err)
// return res

@ -0,0 +1,48 @@
// Copyright 2019, Chef. All rights reserved.
// https://github.com/q191201771/naza
//
// Use of this source code is governed by a MIT-style license
// that can be found in the License file.
//
// Author: Chef (191201771@qq.com)
package fake
import "os"
var (
exit = os.Exit
)
type ExitResult struct {
HasExit bool
ExitCode int
}
var exitResult ExitResult
// 正常情况下,调用 os.Exit单元测试时可通过调用 WithFakeExit 配置为不调用 os.Exit
func Exit(code int) {
exit(code)
}
func WithFakeExit(fn func()) ExitResult {
startFakeExit()
fn()
stopFakeExit()
return exitResult
}
func startFakeExit() {
exitResult.HasExit = false
exitResult.ExitCode = 0
exit = func(code int) {
exitResult.HasExit = true
exitResult.ExitCode = code
}
}
func stopFakeExit() {
exit = os.Exit
}

@ -0,0 +1,35 @@
// Copyright 2019, Chef. All rights reserved.
// https://github.com/q191201771/naza
//
// Use of this source code is governed by a MIT-style license
// that can be found in the License file.
//
// Author: Chef (191201771@qq.com)
package fake_test
import (
"testing"
"github.com/q191201771/naza/pkg/assert"
"github.com/q191201771/naza/pkg/fake"
)
func TestWithFakeExit(t *testing.T) {
var er fake.ExitResult
er = fake.WithFakeExit(func() {
fake.Exit(1)
})
assert.Equal(t, true, er.HasExit)
assert.Equal(t, 1, er.ExitCode)
er = fake.WithFakeExit(func() {
})
assert.Equal(t, false, er.HasExit)
er = fake.WithFakeExit(func() {
fake.Exit(2)
})
assert.Equal(t, true, er.HasExit)
assert.Equal(t, 2, er.ExitCode)
}

@ -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)
}
}

@ -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)
}
}

@ -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()

Loading…
Cancel
Save