Browse Source

update test

topic
yedf2 5 years ago
parent
commit
9050833805
  1. 4
      common/utils.go
  2. 18
      dtmcli/utils.go
  3. 13
      dtmcli/utils_test.go

4
common/utils.go

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

18
dtmcli/utils.go

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

13
dtmcli/utils_test.go

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

Loading…
Cancel
Save