Skip to content

Commit

Permalink
chore(tests): added tests for empty/nil content file in static viewen…
Browse files Browse the repository at this point in the history
…gine
  • Loading branch information
cnlangzi committed Dec 20, 2024
1 parent da35f13 commit 71e80e9
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 126 deletions.
12 changes: 7 additions & 5 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand All @@ -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)
Expand Down
28 changes: 28 additions & 0 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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) {
Expand Down
130 changes: 130 additions & 0 deletions html_template.go
Original file line number Diff line number Diff line change
@@ -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, " ", "")
//<!--layout:home-->\n
if layoutName != "" && strings.HasSuffix(layoutName, "-->\n") && strings.HasPrefix(layoutName, "<!--layout:") {
layoutName = "layouts/" + layoutName[11:len(layoutName)-4]

layout, ok := templates[layoutName]
if ok {
_, err = nt.AddParseTree(layoutName, layout.template.Tree)
if err != nil {
return err
}

layout.dependents[t.name] = t

for tn := range layout.dependencies {
dependencies[tn] = struct{}{}
}

}

for tn := range t.dependencies {
it, ok := templates[tn]
if ok {
_, err = nt.AddParseTree(tn, it.template.Tree)
if err != nil {
return err
}

it.dependents[t.name] = t
}
}

t.layout = layoutName
} else {
t.layout = ""
}

return nil
}

func (t *HtmlTemplate) Reload(fsys fs.FS, templates map[string]*HtmlTemplate) error {
err := t.Load(fsys, templates)
if err != nil {
return err
}

for _, it := range t.dependents {
err := it.Reload(fsys, templates)
if err != nil {
return err
}
}
return nil
}

func (t *HtmlTemplate) Execute(wr io.Writer, data any) error {
if t.layout != "" {
return t.template.ExecuteTemplate(wr, t.layout, data)
}
return t.template.Execute(wr, data)
}
123 changes: 3 additions & 120 deletions viewengine_html.go
Original file line number Diff line number Diff line change
@@ -1,140 +1,23 @@
package htmx

import (
"bufio"
"bytes"
"html/template"
"io"
"io/fs"
"path/filepath"
"strings"

"github.com/yaitoo/htmx/fsnotify"
)

type Template struct {
template *template.Template

name string
path string
layout string

dependencies map[string]struct{}
dependents map[string]*Template
}

func NewHtmlTemplate(name, path string) *Template {
return &Template{
name: name,
path: path,
dependencies: make(map[string]struct{}),
dependents: make(map[string]*Template),
}
}

func (t *Template) Load(fsys fs.FS, templates map[string]*Template) error {
buf, err := fs.ReadFile(fsys, t.path)
if err != nil {
return err
}

nt, err := template.New(t.name).Parse(string(buf))
if err != nil {
return err
}

dependencies := make(map[string]struct{})

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, " ", "")
//<!--layout:home-->\n
if layoutName != "" && strings.HasSuffix(layoutName, "-->\n") && strings.HasPrefix(layoutName, "<!--layout:") {
layoutName = "layouts/" + layoutName[11:len(layoutName)-4]

layout, ok := templates[layoutName]
if ok {
_, err = nt.AddParseTree(layoutName, layout.template.Tree)
if err != nil {
return err
}

layout.dependents[t.name] = t

for tn := range layout.dependencies {
dependencies[tn] = struct{}{}
}

}

for tn := range t.dependencies {
it, ok := templates[tn]
if ok {
_, err = nt.AddParseTree(tn, it.template.Tree)
if err != nil {
return err
}

it.dependents[t.name] = t
}
}

t.layout = layoutName
} else {
t.layout = ""
}

t.template = nt
t.dependencies = dependencies

return nil
}

func (t *Template) Reload(fsys fs.FS, templates map[string]*Template) error {
err := t.Load(fsys, templates)
if err != nil {
return err
}

for _, it := range t.dependents {
err := it.Reload(fsys, templates)
if err != nil {
return err
}
}
return nil
}

func (t *Template) Execute(wr io.Writer, data any) error {
if t.layout != "" {
return t.template.ExecuteTemplate(wr, t.layout, data)
}
return t.template.Execute(wr, data)
}

type HtmlViewEngine struct {
fsys fs.FS
app *App

templates map[string]*Template
templates map[string]*HtmlTemplate
}

func (ve *HtmlViewEngine) Load(fsys fs.FS, app *App) error {
if ve.templates == nil {
ve.templates = map[string]*Template{}
ve.templates = map[string]*HtmlTemplate{}
}

ve.fsys = fsys
Expand Down Expand Up @@ -206,7 +89,7 @@ func (ve *HtmlViewEngine) loadComponents() error {

}

func (ve *HtmlViewEngine) loadTemplate(path string) (*Template, error) {
func (ve *HtmlViewEngine) loadTemplate(path string) (*HtmlTemplate, error) {
name := path[:len(path)-5]

t := NewHtmlTemplate(name, path)
Expand Down
2 changes: 1 addition & 1 deletion viewer_html.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func init() {
}

type HtmlViewer struct {
template *Template
template *HtmlTemplate
}

func (*HtmlViewer) MimeType() string {
Expand Down

0 comments on commit 71e80e9

Please sign in to comment.