Skip to content

Commit

Permalink
add test coverage for bad embed comments
Browse files Browse the repository at this point in the history
  • Loading branch information
crhntr committed Aug 18, 2024
1 parent d53fe3b commit ec1c1b2
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
12 changes: 7 additions & 5 deletions internal/source/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,12 @@ func embedFSFilepaths(dir string, fileSet *token.FileSet, files []*ast.File, exp
continue
}
var comment strings.Builder
readComments(&comment, decl.Doc, spec.Doc)
commentNode := readComments(&comment, decl.Doc, spec.Doc)
patterns, err := parsePatterns(comment.String())
if err != nil {
return nil, err
}
absMat, err := embeddedFilesMatchingPatternList(patterns, embeddedFiles)
absMat, err := embeddedFilesMatchingPatternList(dir, fileSet, commentNode, patterns, embeddedFiles)
if err != nil {
return nil, err
}
Expand All @@ -185,7 +185,7 @@ func embedFSFilepaths(dir string, fileSet *token.FileSet, files []*ast.File, exp
return nil, fmt.Errorf("variable %s not found", fsIdent.Name)
}

func embeddedFilesMatchingPatternList(patterns, embeddedFiles []string) ([]string, error) {
func embeddedFilesMatchingPatternList(dir string, set *token.FileSet, comment ast.Node, patterns, embeddedFiles []string) ([]string, error) {
var matches []string
for _, fp := range embeddedFiles {
for _, pattern := range patterns {
Expand All @@ -198,7 +198,7 @@ func embeddedFilesMatchingPatternList(patterns, embeddedFiles []string) ([]strin
}
}
if matched, err := filepath.Match(pat, fp); err != nil {
return nil, err
return nil, contextError(dir, set, comment.Pos(), fmt.Errorf("embed comment malformed: %w", err))
} else if matched {
matches = append(matches, fp)
}
Expand All @@ -209,7 +209,7 @@ func embeddedFilesMatchingPatternList(patterns, embeddedFiles []string) ([]strin

const goEmbedCommentPrefix = "//go:embed"

func readComments(s *strings.Builder, groups ...*ast.CommentGroup) {
func readComments(s *strings.Builder, groups ...*ast.CommentGroup) ast.Node {
for _, c := range groups {
if c == nil {
continue
Expand All @@ -221,7 +221,9 @@ func readComments(s *strings.Builder, groups ...*ast.CommentGroup) {
s.WriteString(strings.TrimSpace(strings.TrimPrefix(line.Text, goEmbedCommentPrefix)))
s.WriteByte(' ')
}
return c
}
return nil
}

func parsePatterns(input string) ([]string, error) {
Expand Down
8 changes: 8 additions & 0 deletions internal/source/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,14 @@ func TestTemplates(t *testing.T) {
assert.Nil(t, tsGoHTML.Lookup("script.html"))
assert.Nil(t, tsGoHTML.Lookup("console_log"))
})
t.Run("call bad embed pattern", func(t *testing.T) {
dir := createTestDir(t, filepath.FromSlash("testdata/bad_embed_pattern.txtar"))
goFiles, fileSet := parseGo(t, dir)
_, err := source.Templates(dir, "templates", fileSet, goFiles, []string{
filepath.Join(dir, "greeting.gohtml"), // null must not be in a path
})
require.ErrorContains(t, err, `template.go:9:2: embed comment malformed: syntax error in pattern`)
})
}

func createTestDir(t *testing.T, filename string) string {
Expand Down
17 changes: 17 additions & 0 deletions internal/source/testdata/bad_embed_pattern.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-- template.go --
package main

import (
"embed"
"html/template"
)

var (
//go:embed "[fail"
assetsFS embed.FS

templates = template.Must(template.ParseFS(assetsFS, "*"))
)

-- greeting.gohtml --
Hello, friend!
2 changes: 2 additions & 0 deletions internal/source/testdata/template_ParseFS.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ var (

templates = template.Must(template.ParseFS(templateSource, "*"))

// allHTML used by templatesHTML and templatesGoHTML to test pattern filtering
// this comment ensures the comment parser skips lines not preceded by go:embed
//go:embed *html
allHTML embed.FS

Expand Down

0 comments on commit ec1c1b2

Please sign in to comment.