Merge pull request #9035 from wizbit/fail-message

Fail message is now the same as the required message.
pull/9746/head
Martin Hickey 4 years ago committed by GitHub
commit dffc2a30c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -173,6 +173,16 @@ func (e Engine) initFunMap(t *template.Template, referenceTpls map[string]render
return val, nil return val, nil
} }
// Override sprig fail function for linting and wrapping message
funcMap["fail"] = func(msg string) (string, error) {
if e.LintMode {
// Don't fail when linting
log.Printf("[INFO] Fail: %s", msg)
return "", nil
}
return "", errors.New(warnWrap(msg))
}
// If we are not linting and have a cluster connection, provide a Kubernetes-backed // If we are not linting and have a cluster connection, provide a Kubernetes-backed
// implementation. // implementation.
if !e.LintMode && e.config != nil { if !e.LintMode && e.config != nil {

@ -286,6 +286,35 @@ func TestExecErrors(t *testing.T) {
} }
} }
func TestFailErrors(t *testing.T) {
vals := chartutil.Values{"Values": map[string]interface{}{}}
failtpl := `All your base are belong to us{{ fail "This is an error" }}`
tplsFailed := map[string]renderable{
"failtpl": {tpl: failtpl, vals: vals},
}
_, err := new(Engine).render(tplsFailed)
if err == nil {
t.Fatalf("Expected failures while rendering: %s", err)
}
expected := `execution error at (failtpl:1:33): This is an error`
if err.Error() != expected {
t.Errorf("Expected '%s', got %q", expected, err.Error())
}
var e Engine
e.LintMode = true
out, err := e.render(tplsFailed)
if err != nil {
t.Fatal(err)
}
expectStr := "All your base are belong to us"
if gotStr := out["failtpl"]; gotStr != expectStr {
t.Errorf("Expected %q, got %q (%v)", expectStr, gotStr, out)
}
}
func TestAllTemplates(t *testing.T) { func TestAllTemplates(t *testing.T) {
ch1 := &chart.Chart{ ch1 := &chart.Chart{
Metadata: &chart.Metadata{Name: "ch1"}, Metadata: &chart.Metadata{Name: "ch1"},

Loading…
Cancel
Save