diff options
-rw-r--r-- | templater/templater.go | 4 | ||||
-rw-r--r-- | templater/templater_test.go | 17 | ||||
-rw-r--r-- | templater/testdata/test-template.txt | 1 |
3 files changed, 21 insertions, 1 deletions
diff --git a/templater/templater.go b/templater/templater.go index e6f8a92ee3a1..e9e8c11e927a 100644 --- a/templater/templater.go +++ b/templater/templater.go @@ -16,6 +16,8 @@ import ( "github.com/tazjin/kontemplate/util" ) +const failOnMissingKeys string = "missingkey=error" + // Error that is caused by non-existent template files being specified type TemplateNotFoundError struct { meep.AllTraits @@ -78,7 +80,7 @@ func processFiles(c *context.Context, rs *context.ResourceSet, rp string, files } func templateFile(c *context.Context, rs *context.ResourceSet, filename string) (string, error) { - tpl, err := template.New(path.Base(filename)).Funcs(templateFuncs()).ParseFiles(filename) + tpl, err := template.New(path.Base(filename)).Funcs(templateFuncs()).Option(failOnMissingKeys).ParseFiles(filename) if err != nil { return "", meep.New( diff --git a/templater/templater_test.go b/templater/templater_test.go index b262787ae9ba..4aeee254d1c2 100644 --- a/templater/templater_test.go +++ b/templater/templater_test.go @@ -3,6 +3,7 @@ package templater import ( "github.com/tazjin/kontemplate/context" "reflect" + "strings" "testing" ) @@ -136,3 +137,19 @@ func TestApplyLimitsExcludeIncludePrecedence(t *testing.T) { t.Fail() } } + +func TestFailOnMissingKeys(t *testing.T) { + ctx := context.Context{} + resourceSet := context.ResourceSet{} + + _, err := templateFile(&ctx, &resourceSet, "testdata/test-template.txt") + + if err == nil { + t.Errorf("Template with missing keys should have failed.\n") + t.Fail() + } + + if !strings.Contains(err.Error(), "map has no entry for key \"testName\"") { + t.Errorf("Templating failed with unexpected error: %v\n", err) + } +} diff --git a/templater/testdata/test-template.txt b/templater/testdata/test-template.txt new file mode 100644 index 000000000000..06f1cfc630c3 --- /dev/null +++ b/templater/testdata/test-template.txt @@ -0,0 +1 @@ +Template for test {{ .testName }} |