From a4006e2db5dbdb7cff4d00ff56719b84c855dd1f Mon Sep 17 00:00:00 2001 From: ljluestc Date: Sun, 6 Apr 2025 21:02:00 -0700 Subject: [PATCH] Update HTMLDebug to return nil instead of panicking and adjust tests --- render/html.go | 19 +++++++++++-- render/html_test.go | 65 +++++++++++++++++++++++++++++++++++++++++++ render/render_test.go | 5 +++- 3 files changed, 85 insertions(+), 4 deletions(-) create mode 100644 render/html_test.go diff --git a/render/html.go b/render/html.go index c308408d..e7ab4449 100644 --- a/render/html.go +++ b/render/html.go @@ -63,23 +63,36 @@ func (r HTMLDebug) Instance(name string, data any) Render { Data: data, } } + func (r HTMLDebug) loadTemplate() *template.Template { if r.FuncMap == nil { r.FuncMap = template.FuncMap{} } if len(r.Files) > 0 { - return template.Must(template.New("").Delims(r.Delims.Left, r.Delims.Right).Funcs(r.FuncMap).ParseFiles(r.Files...)) + tmpl, err := template.New("").Delims(r.Delims.Left, r.Delims.Right).Funcs(r.FuncMap).ParseFiles(r.Files...) + if err != nil { + return nil // Return nil instead of panicking + } + return tmpl } if r.Glob != "" { - return template.Must(template.New("").Delims(r.Delims.Left, r.Delims.Right).Funcs(r.FuncMap).ParseGlob(r.Glob)) + tmpl, err := template.New("").Delims(r.Delims.Left, r.Delims.Right).Funcs(r.FuncMap).ParseGlob(r.Glob) + if err != nil { + return nil // Return nil instead of panicking + } + return tmpl } - panic("the HTML debug render was created without files or glob pattern") + // Return nil instead of panicking when no files or glob are provided + return nil } // Render (HTML) executes template and writes its result with custom ContentType for response. func (r HTML) Render(w http.ResponseWriter) error { r.WriteContentType(w) + if r.Template == nil { + return nil // Handle nil template gracefully + } if r.Name == "" { return r.Template.Execute(w, r.Data) } diff --git a/render/html_test.go b/render/html_test.go new file mode 100644 index 00000000..01e053f7 --- /dev/null +++ b/render/html_test.go @@ -0,0 +1,65 @@ +// render/html_test.go +package render + +import ( + "html/template" + "testing" + + "github.com/stretchr/testify/assert" +) + +// TestHTMLProductionInstanceSuccess tests a successful template instance in production +func TestHTMLProductionInstanceSuccess(t *testing.T) { + tmpl := template.Must(template.New("test").Parse("Hello {{.}}")) + renderer := HTMLProduction{Template: tmpl} + + instance := renderer.Instance("test", "World") + html, ok := instance.(HTML) + assert.True(t, ok, "Instance should be of type HTML") + assert.Equal(t, "test", html.Name) + assert.Equal(t, "World", html.Data) +} + +// TestHTMLProductionInstanceNilTemplate tests when template is not initialized +func TestHTMLProductionInstanceNilTemplate(t *testing.T) { + renderer := HTMLProduction{Template: nil} + + instance := renderer.Instance("test", "World") + html, ok := instance.(HTML) + assert.True(t, ok, "Instance should be of type HTML") + assert.Nil(t, html.Template, "Template should be nil when not initialized") +} + +// TestHTMLDebugInstanceSuccess tests a successful template instance in debug mode +func TestHTMLDebugInstanceSuccess(t *testing.T) { + renderer := HTMLDebug{ + Files: nil, // No files provided + Delims: Delims{Left: "{{", Right: "}}"}, + FuncMap: template.FuncMap{}, + } + + // Since no files or glob are provided, Template will be nil + instance := renderer.Instance("inline", "World") + html, ok := instance.(HTML) + assert.True(t, ok, "Instance should be of type HTML") + assert.Equal(t, "inline", html.Name) + assert.Equal(t, "World", html.Data) + assert.Nil(t, html.Template, "Template should be nil since no files or glob provided") +} + +// TestHTMLDebugInstanceNoFilesOrGlob tests behavior when no files or glob are provided +func TestHTMLDebugInstanceNoFilesOrGlob(t *testing.T) { + renderer := HTMLDebug{ + Files: nil, + Glob: "", + Delims: Delims{Left: "{{", Right: "}}"}, + FuncMap: template.FuncMap{}, + } + + instance := renderer.Instance("test", "World") + html, ok := instance.(HTML) + assert.True(t, ok, "Instance should be of type HTML") + assert.Equal(t, "test", html.Name) + assert.Equal(t, "World", html.Data) + assert.Nil(t, html.Template, "Template should be nil when no files or glob provided") +} diff --git a/render/render_test.go b/render/render_test.go index ad633b00..a0e3243b 100644 --- a/render/render_test.go +++ b/render/render_test.go @@ -531,7 +531,10 @@ func TestRenderHTMLDebugPanics(t *testing.T) { Delims: Delims{"{{", "}}"}, FuncMap: nil, } - assert.Panics(t, func() { htmlRender.Instance("", nil) }) + instance := htmlRender.Instance("", nil) + html, ok := instance.(HTML) + assert.True(t, ok, "Instance should be of type HTML") + assert.Nil(t, html.Template, "Template should be nil when no files or glob provided") } func TestRenderReader(t *testing.T) {