chore: improve notification template tests' resilience (#14196)

This commit is contained in:
Danny Kopping
2024-08-07 11:33:26 +02:00
committed by GitHub
parent e09ad1ddc1
commit c6076d2d0d
6 changed files with 114 additions and 16 deletions

View File

@ -9,10 +9,19 @@ import (
"github.com/coder/coder/v2/coderd/notifications/types"
)
// NoValue is used when a template variable is not found.
// This string is not exported as a const from the text/template.
const NoValue = "<no value>"
// GoTemplate attempts to substitute the given payload into the given template using Go's templating syntax.
// TODO: memoize templates for memory efficiency?
func GoTemplate(in string, payload types.MessagePayload, extraFuncs template.FuncMap) (string, error) {
tmpl, err := template.New("text").Funcs(extraFuncs).Parse(in)
tmpl, err := template.New("text").
Funcs(extraFuncs).
// text/template substitutes a missing label with "<no value>".
// NOTE: html/template does not, for obvious reasons.
Option("missingkey=invalid").
Parse(in)
if err != nil {
return "", xerrors.Errorf("template parse: %w", err)
}