summaryrefslogtreecommitdiff
path: root/modules/caddyhttp/templates
diff options
context:
space:
mode:
authorTim Culverhouse <tim@timculverhouse.com>2021-09-20 13:29:37 -0500
committerGitHub <noreply@github.com>2021-09-20 12:29:37 -0600
commit16f752125fb5d6fe46b9dea95eec8804bfdb24f8 (patch)
tree48f5db584f54a00dedf738ad09a5eed7ce06ee42 /modules/caddyhttp/templates
parent0a5f7a677f00e1406247fb77c6a8f45898532ae5 (diff)
templates: Add tests for funcInclude and funcImport (#4357)
* Update tplcontext.go Add {{ render "/path/to/file.ext" $data }} via funcRender * Update tplcontext.go * Refactor funcInclude, add funcImport to enable {{block}} and {{template}} * Fix funcImport return of nil showing up in html * Update godocs for and * Add tests for funcInclude * Add tests for funcImport * os.RemoveAll -> os.Remove for TestFuncInclude and TestFuncImport
Diffstat (limited to 'modules/caddyhttp/templates')
-rw-r--r--modules/caddyhttp/templates/tplcontext_test.go134
1 files changed, 134 insertions, 0 deletions
diff --git a/modules/caddyhttp/templates/tplcontext_test.go b/modules/caddyhttp/templates/tplcontext_test.go
index efee3be..1d15ca7 100644
--- a/modules/caddyhttp/templates/tplcontext_test.go
+++ b/modules/caddyhttp/templates/tplcontext_test.go
@@ -91,6 +91,140 @@ func TestCookie(t *testing.T) {
}
}
+func TestImport(t *testing.T) {
+ for i, test := range []struct {
+ fileContent string
+ fileName string
+ shouldErr bool
+ expect string
+ }{
+ {
+ // file exists, template is defined
+ fileContent: `{{ define "imported" }}text{{end}}`,
+ fileName: "file1",
+ shouldErr: false,
+ expect: `"imported"`,
+ },
+ {
+ // file does not exit
+ fileContent: "",
+ fileName: "",
+ shouldErr: true,
+ },
+ } {
+ context := getContextOrFail(t)
+ var absFilePath string
+
+ // create files for test case
+ if test.fileName != "" {
+ absFilePath := filepath.Join(fmt.Sprintf("%s", context.Root), test.fileName)
+ if err := ioutil.WriteFile(absFilePath, []byte(test.fileContent), os.ModePerm); err != nil {
+ os.Remove(absFilePath)
+ t.Fatalf("Test %d: Expected no error creating file, got: '%s'", i, err.Error())
+ }
+ }
+
+ // perform test
+ context.NewTemplate("parent")
+ actual, err := context.funcImport(test.fileName)
+ templateWasDefined := strings.Contains(context.tpl.DefinedTemplates(), test.expect)
+ if err != nil {
+ if !test.shouldErr {
+ t.Errorf("Test %d: Expected no error, got: '%s'", i, err)
+ }
+ } else if test.shouldErr {
+ t.Errorf("Test %d: Expected error but had none", i)
+ } else if !templateWasDefined && actual != "" {
+ // template should be defined, return value should be an empty string
+ t.Errorf("Test %d: Expected template %s to be define but got %s", i, test.expect, context.tpl.DefinedTemplates())
+
+ }
+
+ if absFilePath != "" {
+ if err := os.Remove(absFilePath); err != nil && !os.IsNotExist(err) {
+ t.Fatalf("Test %d: Expected no error removing temporary test file, got: %v", i, err)
+ }
+ }
+ }
+}
+
+func TestInclude(t *testing.T) {
+ for i, test := range []struct {
+ fileContent string
+ fileName string
+ shouldErr bool
+ expect string
+ args string
+ }{
+ {
+ // file exists, content is text only
+ fileContent: "text",
+ fileName: "file1",
+ shouldErr: false,
+ expect: "text",
+ },
+ {
+ // file exists, content is template
+ fileContent: "{{ if . }}text{{ end }}",
+ fileName: "file1",
+ shouldErr: false,
+ expect: "text",
+ },
+ {
+ // file does not exit
+ fileContent: "",
+ fileName: "",
+ shouldErr: true,
+ },
+ {
+ // args
+ fileContent: "{{ index .Args 0 }}",
+ fileName: "file1",
+ shouldErr: false,
+ args: "text",
+ expect: "text",
+ },
+ {
+ // args, reference arg out of range
+ fileContent: "{{ index .Args 1 }}",
+ fileName: "file1",
+ shouldErr: true,
+ args: "text",
+ },
+ } {
+ context := getContextOrFail(t)
+ var absFilePath string
+
+ // create files for test case
+ if test.fileName != "" {
+ absFilePath := filepath.Join(fmt.Sprintf("%s", context.Root), test.fileName)
+ if err := ioutil.WriteFile(absFilePath, []byte(test.fileContent), os.ModePerm); err != nil {
+ os.Remove(absFilePath)
+ t.Fatalf("Test %d: Expected no error creating file, got: '%s'", i, err.Error())
+ }
+ }
+
+ // perform test
+ actual, err := context.funcInclude(test.fileName, test.args)
+ if err != nil {
+ if !test.shouldErr {
+ t.Errorf("Test %d: Expected no error, got: '%s'", i, err)
+ }
+ } else if test.shouldErr {
+ t.Errorf("Test %d: Expected error but had none", i)
+ } else if actual != test.expect {
+ t.Errorf("Test %d: Expected %s but got %s", i, test.expect, actual)
+
+ }
+
+ if absFilePath != "" {
+ if err := os.Remove(absFilePath); err != nil && !os.IsNotExist(err) {
+ t.Fatalf("Test %d: Expected no error removing temporary test file, got: %v", i, err)
+ }
+ }
+ }
+}
+
func TestCookieMultipleCookies(t *testing.T) {
context := getContextOrFail(t)