From 2b064536b25ab0e9c54245f9e2cc5bd4766033fe Mon Sep 17 00:00:00 2001 From: Diogo Monteiro Date: Mon, 30 Oct 2017 16:14:56 -0400 Subject: [PATCH] Raising a panic in the includeIfExists helper when templates does exist but fails to render. --- default.go | 4 ++++ eval_test.go | 11 +++++++++++ testData/includeIfNotExists/broken.jet | 1 + testData/includeIfNotExists/includeBroken.jet | 1 + 4 files changed, 17 insertions(+) create mode 100644 testData/includeIfNotExists/broken.jet create mode 100644 testData/includeIfNotExists/includeBroken.jet diff --git a/default.go b/default.go index 99e5808..ddde003 100644 --- a/default.go +++ b/default.go @@ -83,6 +83,10 @@ func init() { a.RequireNumOfArguments("includeIfExists", 1, 2) t, err := a.runtime.set.GetTemplate(a.Get(0).String()) + // If template exists but returns an error then panic instead of failing silently + if t != nil && err != nil { + panic(err) + } if err != nil { return hiddenFALSE } diff --git a/eval_test.go b/eval_test.go index 907c8c3..47a3cba 100644 --- a/eval_test.go +++ b/eval_test.go @@ -364,6 +364,17 @@ func TestIncludeIfNotExists(t *testing.T) { RunJetTestWithSet(t, set, nil, nil, "notExistent", "", "") RunJetTestWithSet(t, set, nil, nil, "ifIncludeIfExits", "", "Hi, i exist!!\n Was included!!\n\n\n Was not included!!\n\n") RunJetTestWithSet(t, set, nil, "World", "wcontext", "", "Hi, Buddy!\nHi, World!") + + // Check if includeIfExists helper bubbles up runtime errors of included templates + tt, err := set.GetTemplate("includeBroken") + if err != nil { + t.Error(err) + } + buff := bytes.NewBuffer(nil) + err = tt.Execute(buff, nil, nil) + if err == nil { + t.Error("expected includeIfExists helper to fail with a runtime error but got nil") + } } func TestSet_Parse(t *testing.T) { diff --git a/testData/includeIfNotExists/broken.jet b/testData/includeIfNotExists/broken.jet new file mode 100644 index 0000000..6899e30 --- /dev/null +++ b/testData/includeIfNotExists/broken.jet @@ -0,0 +1 @@ +{{ err.break }} \ No newline at end of file diff --git a/testData/includeIfNotExists/includeBroken.jet b/testData/includeIfNotExists/includeBroken.jet new file mode 100644 index 0000000..748a895 --- /dev/null +++ b/testData/includeIfNotExists/includeBroken.jet @@ -0,0 +1 @@ +{{ includeIfExists: "broken.jet"}} \ No newline at end of file