From 3bee539a75fe4f68d2c8fe66c9c052681ca07564 Mon Sep 17 00:00:00 2001 From: q191201771 <191201771@qq.com> Date: Sat, 7 Dec 2019 12:42:33 +0800 Subject: [PATCH] 1. feat: package fake: add func Exit 2. test: package log: use fake.Exit --- .gitignore | 1 + README.md | 2 +- demo/add_blog_license/main.go | 6 ++--- pkg/fake/exit.go | 48 +++++++++++++++++++++++++++++++++++ pkg/fake/exit_test.go | 35 +++++++++++++++++++++++++ pkg/nazalog/global.go | 7 +++-- pkg/nazalog/log.go | 8 +++--- pkg/nazalog/log_test.go | 38 +++++++++++++++++++++++++++ 8 files changed, 136 insertions(+), 9 deletions(-) create mode 100644 pkg/fake/exit.go create mode 100644 pkg/fake/exit_test.go diff --git a/.gitignore b/.gitignore index 37ff4f0..132b561 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,7 @@ profile*.pdf /pre-commit.sh /TODO.md +/pkg/snowflake /pkg/tag /pkg/nazatime /demo/samefile diff --git a/README.md b/README.md index ffd1225..86c7b58 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ pkg/ ...... 源码包 |-- ratelimit/ ...... 限流器,令牌桶 |-- ic/ ...... 将整型切片压缩成二进制字节切片 |-- unique/ ...... 对象唯一 ID - |-- fake/ ...... 实现一些常用的接口,辅助测试其它代码 + |-- fake/ ...... stub和mock相关,实现一些常用的接口,辅助测试其它代码 demo/ ...... 示例相关的代码 bin/ ...... 可执行文件编译输出目录 ``` diff --git a/demo/add_blog_license/main.go b/demo/add_blog_license/main.go index 05141b2..af16d03 100644 --- a/demo/add_blog_license/main.go +++ b/demo/add_blog_license/main.go @@ -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 diff --git a/pkg/fake/exit.go b/pkg/fake/exit.go new file mode 100644 index 0000000..c2a950f --- /dev/null +++ b/pkg/fake/exit.go @@ -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 +} diff --git a/pkg/fake/exit_test.go b/pkg/fake/exit_test.go new file mode 100644 index 0000000..e1ca23e --- /dev/null +++ b/pkg/fake/exit_test.go @@ -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) +} 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()