spf13--cobra/cobra_test.go
Guilhem Lettron 2b6e651ee4 feat: panic on CheckErr
os.Exit(1) kill process without defering functions.
panic can be recover if needed.

Signed-off-by: Guilhem Lettron <guilhem@barpilot.io>
2021-12-21 15:15:00 +01:00

71 lines
1.2 KiB
Go

package cobra
import (
"errors"
"testing"
"text/template"
)
func assertNoErr(t *testing.T, e error) {
if e != nil {
t.Error(e)
}
}
func TestAddTemplateFunctions(t *testing.T) {
AddTemplateFunc("t", func() bool { return true })
AddTemplateFuncs(template.FuncMap{
"f": func() bool { return false },
"h": func() string { return "Hello," },
"w": func() string { return "world." }})
c := &Command{}
c.SetUsageTemplate(`{{if t}}{{h}}{{end}}{{if f}}{{h}}{{end}} {{w}}`)
const expected = "Hello, world."
if got := c.UsageString(); got != expected {
t.Errorf("Expected UsageString: %v\nGot: %v", expected, got)
}
}
func TestCheckErr(t *testing.T) {
tests := []struct {
name string
msg interface{}
panic bool
}{
{
name: "no error",
msg: nil,
panic: false,
},
{
name: "panic string",
msg: "test",
panic: true,
},
{
name: "panic error",
msg: errors.New("test error"),
panic: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
defer func() {
r := recover()
if r != nil {
if !tt.panic {
t.Error("Didn't expect panic")
}
} else {
if tt.panic {
t.Error("Expected to panic")
}
}
}()
CheckErr(tt.msg)
})
}
}