Skip to content

Commit

Permalink
fix dir pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
crhntr committed Aug 18, 2024
1 parent 3f6ee0f commit d53fe3b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 10 deletions.
8 changes: 3 additions & 5 deletions internal/source/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,9 @@ func embeddedFilesMatchingPatternList(patterns, embeddedFiles []string) ([]strin
for _, pattern := range patterns {
pat := filepath.FromSlash(pattern)
if !strings.ContainsAny(pat, "*[]") {
prefix := filepath.FromSlash(pat) + "/"
if i := slices.IndexFunc(embeddedFiles, func(file string) bool {
return strings.HasPrefix(file, prefix)
}); i >= 0 {
matches = append(matches, embeddedFiles[i])
prefix := filepath.FromSlash(pat + "/")
if strings.HasPrefix(fp, prefix) {
matches = append(matches, fp)
continue
}
}
Expand Down
25 changes: 20 additions & 5 deletions internal/source/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"go/ast"
"go/parser"
"go/token"
"log"
"os"
"path/filepath"
"slices"
Expand Down Expand Up @@ -42,6 +43,22 @@ func TestTemplates(t *testing.T) {
assert.Equal(t, []string{"create", "form.gohtml", "home", "index.gohtml", "update"}, names)
})

t.Run("call ParseFS with assets dir", func(t *testing.T) {
dir := createTestDir(t, filepath.FromSlash("testdata/assets_dir.txtar"))
goFiles, fileSet := parseGo(t, dir)
ts, err := source.Templates(dir, "templates", fileSet, goFiles, []string{
filepath.Join(dir, filepath.FromSlash("assets/index.gohtml")),
filepath.Join(dir, filepath.FromSlash("assets/form.gohtml")),
})
require.NoError(t, err)
var names []string
for _, t := range ts.Templates() {
names = append(names, t.Name())
}
slices.Sort(names)
assert.Equal(t, []string{"create", "form.gohtml", "home", "index.gohtml", "update"}, names)
})

t.Run("call New", func(t *testing.T) {
dir := createTestDir(t, filepath.FromSlash("testdata/templates.txtar"))
goFiles, fileSet := parseGo(t, dir)
Expand Down Expand Up @@ -301,11 +318,9 @@ func createTestDir(t *testing.T, filename string) string {
if err != nil {
t.Fatal(err)
}
for _, file := range archive.Files {
output := filepath.Join(dir, filepath.FromSlash(file.Name))
if err := os.WriteFile(output, file.Data, 0o666); err != nil {
t.Fatal(err)
}
tfs, err := txtar.FS(archive)
if err := os.CopyFS(dir, tfs); err != nil {
log.Fatal(err)
}
return dir
}
Expand Down
24 changes: 24 additions & 0 deletions internal/source/testdata/assets_dir.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
-- template.go --
package main

import (
"embed"
"html/template"
)

var (
//go:embed assets
assetsFS embed.FS

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

-- assets/index.gohtml --

{{define "home"}}{{end}}

-- assets/form.gohtml --

{{define "create"}}{{end}}

{{define "update"}}{{end}}

0 comments on commit d53fe3b

Please sign in to comment.