(t *testing.T)
| 27 | ) |
| 28 | |
| 29 | func TestLoadTextTemplate(t *testing.T) { |
| 30 | executeTextTemplate := func(t *testing.T, dir, name, pattern string, model map[string]interface{}) string { |
| 31 | _, reg := pkg.NewFastRegistryWithMocks(t) |
| 32 | tp, err := template.LoadText(t.Context(), reg, os.DirFS(dir), name, pattern, model, "") |
| 33 | require.NoError(t, err) |
| 34 | return tp |
| 35 | } |
| 36 | |
| 37 | executeHTMLTemplate := func(t *testing.T, dir, name, pattern string, model map[string]interface{}) string { |
| 38 | _, reg := pkg.NewFastRegistryWithMocks(t) |
| 39 | tp, err := template.LoadHTML(t.Context(), reg, os.DirFS(dir), name, pattern, model, "") |
| 40 | require.NoError(t, err) |
| 41 | return tp |
| 42 | } |
| 43 | |
| 44 | t.Run("method=from bundled", func(t *testing.T) { |
| 45 | actual := executeTextTemplate(t, "courier/builtin/templates/test_stub", "email.body.gotmpl", "", nil) |
| 46 | assert.Contains(t, actual, "stub email") |
| 47 | }) |
| 48 | |
| 49 | t.Run("method=fallback to bundled", func(t *testing.T) { |
| 50 | template.Cache, _ = lru.New[string, template.Template](16) // prevent Cache hit |
| 51 | actual := executeTextTemplate(t, "some/inexistent/dir", "test_stub/email.body.gotmpl", "", nil) |
| 52 | assert.Contains(t, actual, "stub email") |
| 53 | }) |
| 54 | |
| 55 | t.Run("method=with Sprig functions", func(t *testing.T) { |
| 56 | template.Cache, _ = lru.New[string, template.Template](16) // prevent Cache hit |
| 57 | m := map[string]interface{}{"input": "hello world"} // create a simple model |
| 58 | actual := executeTextTemplate(t, "courier/builtin/templates/test_stub", "email.body.sprig.gotmpl", "", m) |
| 59 | assert.Contains(t, actual, "HelloWorld,HELLOWORLD") |
| 60 | }) |
| 61 | |
| 62 | t.Run("method=sprig should not support non-hermetic", func(t *testing.T) { |
| 63 | template.Cache, _ = lru.New[string, template.Template](16) |
| 64 | _, reg := pkg.NewFastRegistryWithMocks(t) |
| 65 | |
| 66 | nonhermetic := []string{"date", "date_in_zone", "date_modify", "now", "htmlDate", "htmlDateInZone", "dateInZone", "dateModify", "env", "expandenv", "getHostByName", "uuidv4", "randNumeric", "randAscii", "randAlpha", "randAlphaNum"} |
| 67 | |
| 68 | for _, tc := range nonhermetic { |
| 69 | t.Run("case=should not support function: "+tc, func(t *testing.T) { |
| 70 | _, err := template.LoadText(t.Context(), reg, x.NewStubFS(tc, []byte(fmt.Sprintf("{{ %s }}", tc))), tc, "", map[string]interface{}{}, "") |
| 71 | assert.ErrorContains(t, err, fmt.Sprintf("function %q not defined", tc)) |
| 72 | }) |
| 73 | } |
| 74 | }) |
| 75 | |
| 76 | t.Run("method=html with nested templates", func(t *testing.T) { |
| 77 | template.Cache, _ = lru.New[string, template.Template](16) // prevent Cache hit |
| 78 | m := map[string]interface{}{"lang": "en_US"} // create a simple model |
| 79 | actual := executeHTMLTemplate(t, "courier/builtin/templates/test_stub", "email.body.html.gotmpl", "email.body.html*", m) |
| 80 | assert.Contains(t, actual, "lang=en_US") |
| 81 | }) |
| 82 | |
| 83 | t.Run("method=Cache works", func(t *testing.T) { |
| 84 | dir := os.TempDir() |
| 85 | name := x.NewUUID().String() + ".body.gotmpl" |
| 86 | fp := filepath.Join(dir, name) |
nothing calls this directly
no test coverage detected