Skip to content

Commit fb64e0c

Browse files
committed
refactor: refactor template loading to use helper functions and add tests
- Refactor `LoadTemplates` to utilize the new `loadTemplatesFromFS` helper function. - Remove complex logic from `LoadTemplates` function. - Introduce new `LoadTemplatesFromDir` function for loading templates from a directory. - Create helper function `loadTemplatesFromFS` to handle loading templates from the filesystem. - Add test for `LoadTemplates` to verify loading templates from an embedded filesystem. - Add new embedded filesystem sample template in tests. - Change test template name from `test.tmpl` to `foo.tmpl`. - Create new template file `test.tmpl` with content "Hello, {{.Name}}!". Signed-off-by: appleboy <[email protected]>
1 parent 2a39894 commit fb64e0c

File tree

3 files changed

+53
-21
lines changed

3 files changed

+53
-21
lines changed

util/template.go

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -68,38 +68,34 @@ func GetTemplateByBytes(name string, data map[string]interface{}) ([]byte, error
6868
// LoadTemplates loads all the templates found in the templates directory from the embedded filesystem.
6969
// It returns an error if reading the directory or parsing any template fails.
7070
func LoadTemplates(files embed.FS) error {
71-
tmplFiles, err := fs.ReadDir(files, templatesDir)
72-
if err != nil {
73-
return err
74-
}
75-
76-
for _, tmpl := range tmplFiles {
77-
if tmpl.IsDir() {
78-
continue
79-
}
80-
81-
pt, err := template.ParseFS(files, templatesDir+"/"+tmpl.Name())
82-
if err != nil {
83-
return err
84-
}
85-
86-
templates[tmpl.Name()] = pt
87-
}
88-
return nil
71+
return loadTemplatesFromFS(files, templatesDir)
8972
}
9073

74+
// LoadTemplatesFromDir loads all the templates found in the specified directory from the filesystem.
75+
// It returns an error if reading the directory or parsing any template fails.
9176
func LoadTemplatesFromDir(dir string) error {
92-
tmplFiles, err := fs.ReadDir(os.DirFS(dir), ".")
77+
return loadTemplatesFromFS(os.DirFS(dir), ".")
78+
}
79+
80+
// loadTemplatesFromFS is a helper function that loads templates from the given filesystem and directory.
81+
// It returns an error if reading the directory or parsing any template fails.
82+
func loadTemplatesFromFS(fsys fs.FS, dir string) error {
83+
tmplFiles, err := fs.ReadDir(fsys, dir)
9384
if err != nil {
9485
return err
9586
}
9687

88+
pattern := dir + "/"
89+
if dir == "." {
90+
pattern = ""
91+
}
92+
9793
for _, tmpl := range tmplFiles {
9894
if tmpl.IsDir() {
9995
continue
10096
}
10197

102-
pt, err := template.ParseFS(os.DirFS(dir), tmpl.Name())
98+
pt, err := template.ParseFS(fsys, pattern+tmpl.Name())
10399
if err != nil {
104100
return err
105101
}

util/template_test.go

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package util
22

33
import (
44
"bytes"
5+
"embed"
56
"html/template"
67
"os"
78
"testing"
@@ -45,7 +46,7 @@ func TestNewTemplateByStringWithCustomVars(t *testing.T) {
4546

4647
func TestProcessTemplate(t *testing.T) {
4748
// Set up test data
48-
testTemplateName := "test.tmpl"
49+
testTemplateName := "foo.tmpl"
4950
testTemplateText := "Hello {{.Name}}!"
5051
testData := Data{"Name": "World"}
5152

@@ -110,3 +111,37 @@ func TestLoadTemplatesFromDir(t *testing.T) {
110111
t.Errorf("Unexpected output. Got: %v, Want: %v", buf.String(), expected)
111112
}
112113
}
114+
115+
// Create an embedded filesystem with a sample template
116+
//
117+
//go:embed templates/*
118+
var testFiles embed.FS
119+
120+
func TestLoadTemplates(t *testing.T) {
121+
// Load templates from the embedded filesystem
122+
err := LoadTemplates(testFiles)
123+
if err != nil {
124+
t.Fatalf("Failed to load templates from embedded filesystem: %v", err)
125+
}
126+
127+
// Check if the template was loaded correctly
128+
templateFile := "test.tmpl"
129+
tmpl, ok := templates[templateFile]
130+
if !ok {
131+
t.Fatalf("Template %s not found in loaded templates", templateFile)
132+
}
133+
134+
// Process the loaded template
135+
data := Data{"Name": "World"}
136+
var buf bytes.Buffer
137+
err = tmpl.Execute(&buf, data)
138+
if err != nil {
139+
t.Fatalf("Failed to execute loaded template: %v", err)
140+
}
141+
142+
// Check the output
143+
expected := "Hello, World!"
144+
if buf.String() != expected {
145+
t.Errorf("Unexpected output. Got: %v, Want: %v", buf.String(), expected)
146+
}
147+
}

util/templates/test.tmpl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello, {{.Name}}!

0 commit comments

Comments
 (0)