update test

This commit is contained in:
yedf2 2021-08-04 15:57:51 +08:00
parent e4392bac43
commit 9050833805
3 changed files with 26 additions and 9 deletions

View File

@ -88,9 +88,7 @@ func InitConfig(config interface{}) {
break
}
}
if cont == nil {
dtmcli.LogFatalf("no config file conf.yml/conf.sample.yml found in current and parent path: %s", MustGetwd())
}
dtmcli.LogIfFatalf(cont == nil, "no config file conf.yml/conf.sample.yml found in current and parent path: %s", MustGetwd())
dtmcli.Logf("cont is: \n%s", string(cont))
err := yaml.Unmarshal(cont, config)
dtmcli.FatalIfError(err)

View File

@ -126,19 +126,25 @@ func LogRedf(fmt string, args ...interface{}) {
Logf("\x1b[31m\n"+fmt+"\x1b[0m\n", args...)
}
// FatalExitFunc Fatal退出函数测试时被替换
var FatalExitFunc = func() { os.Exit(1) }
// LogFatalf 采用红色打印错误类信息, 并退出
func LogFatalf(fmt string, args ...interface{}) {
Logf("\x1b[31m\n"+fmt+"\x1b[0m\n", args...)
os.Exit(1)
FatalExitFunc()
}
// LogIfFatalf 采用红色打印错误类信息, 并退出
func LogIfFatalf(condition bool, fmt string, args ...interface{}) {
if condition {
LogFatalf(fmt, args...)
}
}
// FatalIfError 采用红色打印错误类信息, 并退出
func FatalIfError(err error) {
if err == nil {
return
}
Logf("\x1b[31m\nFatal error: %v\x1b[0m\n", err)
os.Exit(1)
LogIfFatalf(err == nil, "Fatal error: %v", err)
}
// RestyClient the resty object

View File

@ -2,6 +2,7 @@ package dtmcli
import (
"errors"
"fmt"
"os"
"strings"
"testing"
@ -76,3 +77,15 @@ func TestSome(t *testing.T) {
s2 := MayReplaceLocalhost("http://localhost")
assert.Equal(t, "http://localhost", s2)
}
func TestFatal(t *testing.T) {
old := FatalExitFunc
defer func() {
FatalExitFunc = old
}()
FatalExitFunc = func() { panic(fmt.Errorf("fatal")) }
err := CatchP(func() {
LogIfFatalf(true, "")
})
assert.Error(t, err, fmt.Errorf("fatal"))
}