From 71e80e909e68ca0fcdcee2ef17eaf85aa6e3b72d Mon Sep 17 00:00:00 2001 From: Lz Date: Fri, 20 Dec 2024 09:13:23 +0800 Subject: [PATCH] chore(tests): added tests for empty/nil content file in static viewengine --- app.go | 12 +++-- app_test.go | 28 ++++++++++ html_template.go | 130 +++++++++++++++++++++++++++++++++++++++++++++ viewengine_html.go | 123 ++---------------------------------------- viewer_html.go | 2 +- 5 files changed, 169 insertions(+), 126 deletions(-) create mode 100644 html_template.go diff --git a/app.go b/app.go index 629bd26..40793d0 100644 --- a/app.go +++ b/app.go @@ -38,16 +38,11 @@ func New(opts ...Option) *App { routes: make(map[string]*Routing), viewers: make(map[string]Viewer), viewer: &JsonViewer{}, - viewEngines: []ViewEngine{ - &StaticViewEngine{}, - // &HtmlViewEngine{}, - }, } for _, o := range opts { o(app) } - if app.logger == nil { app.logger = slog.Default() } @@ -56,6 +51,13 @@ func New(opts ...Option) *App { app.mux = http.DefaultServeMux } + if app.viewEngines == nil { + app.viewEngines = []ViewEngine{ + &StaticViewEngine{}, + &HtmlViewEngine{}, + } + } + if app.fsys != nil { for _, ve := range app.viewEngines { err := ve.Load(app.fsys, app) diff --git a/app_test.go b/app_test.go index 3d1d1f9..67728fb 100644 --- a/app_test.go +++ b/app_test.go @@ -151,6 +151,12 @@ func TestStaticViewer(t *testing.T) { background: red; }`), }, + "public/assets/empty.js": &fstest.MapFile{ + Data: []byte(``), + }, + "public/assets/nil.js": &fstest.MapFile{ + Data: nil, + }, } mux := http.NewServeMux() @@ -197,6 +203,28 @@ func TestStaticViewer(t *testing.T) { require.Equal(t, fsys["public/assets/skin.css"].Data, buf) + req, err = http.NewRequest("GET", srv.URL+"/assets/empty.js", nil) + require.NoError(t, err) + resp, err = client.Do(req) + require.NoError(t, err) + + buf, err = io.ReadAll(resp.Body) + require.NoError(t, err) + resp.Body.Close() + + require.Equal(t, 0, len(buf)) + + req, err = http.NewRequest("GET", srv.URL+"/assets/nil.js", nil) + require.NoError(t, err) + resp, err = client.Do(req) + require.NoError(t, err) + + buf, err = io.ReadAll(resp.Body) + require.NoError(t, err) + resp.Body.Close() + + require.Equal(t, 0, len(buf)) + } func TestApp(t *testing.T) { diff --git a/html_template.go b/html_template.go new file mode 100644 index 0000000..18f9c53 --- /dev/null +++ b/html_template.go @@ -0,0 +1,130 @@ +package htmx + +import ( + "bufio" + "bytes" + "io" + "io/fs" + "strings" + "text/template" +) + +type HtmlTemplate struct { + template *template.Template + + name string + path string + layout string + + dependencies map[string]struct{} + dependents map[string]*HtmlTemplate +} + +func NewHtmlTemplate(name, path string) *HtmlTemplate { + return &HtmlTemplate{ + name: name, + path: path, + dependencies: make(map[string]struct{}), + dependents: make(map[string]*HtmlTemplate), + } +} + +func (t *HtmlTemplate) Load(fsys fs.FS, templates map[string]*HtmlTemplate) error { + buf, err := fs.ReadFile(fsys, t.path) + if err != nil { + return err + } + + nt := template.New(t.name) + dependencies := make(map[string]struct{}) + + defer func() { + t.template = nt + t.dependencies = dependencies + }() + + if len(buf) == 0 { + return nil + } + + nt, err = nt.Parse(string(buf)) + if err != nil { + return err + } + + for _, it := range nt.Templates() { + tn := it.Name() + if strings.EqualFold(tn, t.name) { + continue + } + + dependencies[tn] = struct{}{} + } + + r := bufio.NewReader(bytes.NewReader(buf)) + layoutName, err := r.ReadString('\n') + if err != nil { + return err + } + + layoutName = strings.ReplaceAll(layoutName, " ", "") + //\n + if layoutName != "" && strings.HasSuffix(layoutName, "-->\n") && strings.HasPrefix(layoutName, "\n - if layoutName != "" && strings.HasSuffix(layoutName, "-->\n") && strings.HasPrefix(layoutName, "