Skip to content

resources: Refix resources.GetMatch/Match case-sensitivity bug #10320

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions hugofs/glob.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ package hugofs

import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"

Expand Down Expand Up @@ -69,6 +71,15 @@ func Glob(fs afero.Fs, pattern string, handle func(fi FileMetaInfo) (bool, error
return nil
}

root, err = CanonicalizeFilepath(fs, root)

if os.IsNotExist(err) {
// TODO logger
return nil
} else if err != nil {
return err
}

w := NewWalkway(WalkwayConfig{
Root: root,
Fs: fs,
Expand All @@ -83,3 +94,77 @@ func Glob(fs afero.Fs, pattern string, handle func(fi FileMetaInfo) (bool, error

return nil
}

// return case-matched path from case-insensitive input
// before using this, you have to apply `NormalizePath`
func CanonicalizeFilepath(fs afero.Fs, path string) (string, error) {
if path == "." || path == "" {
return path, nil
}
paths := strings.Split(path, "/")

var ret []string

for _, p := range paths {
dir := filepath.Join(ret...)
joined := filepath.Join(dir, p)
fi, ok, err := lstatIfPossible(fs, joined)

if !os.IsNotExist(err) {
return "", err
}

if ok {
ret = append(ret, fi.Name())
continue
}

fi, err = statWithCaseInsensitiveName(fs, dir, p)
if err != nil {
return "", err
}

ret = append(ret, fi.Name())
// ret = append(ret, p)
}

return filepath.Join(ret...), nil
}

// case-insenstively search in parent dir, and return os.FileInfo
func statWithCaseInsensitiveName(fs afero.Fs, parent, name string) (os.FileInfo, error) {
fi, err := fs.Stat(parent)

if err != nil {
return nil, err
}

if !fi.IsDir() {
return nil, fmt.Errorf("%s is not directory", parent)
}

f, err := fs.Open(parent)
if err != nil {
return nil, err
}
defer f.Close()

names, err := f.Readdirnames(-1)

if err != nil {
return nil, err
}

baseLowered := strings.ToLower(name)
for _, name := range names {
if baseLowered == strings.ToLower(name) {
p := filepath.Join(parent, name)
fi, _, err := lstatIfPossible(fs, p)
if err != nil {
return nil, err
}
return fi, err
}
}
return nil, os.ErrNotExist
}
8 changes: 6 additions & 2 deletions resources/resource_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,11 @@ func (c *ResourceCache) Contains(key string) bool {
}

func (c *ResourceCache) cleanKey(key string) string {
return strings.TrimPrefix(path.Clean(strings.ToLower(key)), "/")
return strings.ToLower(c.cleanKeyNoLower(key))
}

func (c *ResourceCache) cleanKeyNoLower(key string) string {
return strings.TrimPrefix(path.Clean(key), "/")
}

func (c *ResourceCache) get(key string) (any, bool) {
Expand All @@ -175,7 +179,7 @@ func (c *ResourceCache) GetOrCreateResources(key string, f func() (resource.Reso
}

func (c *ResourceCache) getOrCreate(key string, f func() (any, error)) (any, error) {
key = c.cleanKey(key)
key = c.cleanKeyNoLower(key)
// First check in-memory cache.
r, found := c.get(key)
if found {
Expand Down
2 changes: 1 addition & 1 deletion resources/resource_factories/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (c *Client) GetMatch(pattern string) (resource.Resource, error) {
}

func (c *Client) match(name, pattern string, matchFunc func(r resource.Resource) bool, firstOnly bool) (resource.Resources, error) {
pattern = glob.NormalizePath(pattern)
pattern = glob.NormalizePathNoLower(pattern)
partitions := glob.FilterGlobParts(strings.Split(pattern, "/"))
if len(partitions) == 0 {
partitions = []string{resources.CACHE_OTHER}
Expand Down
156 changes: 156 additions & 0 deletions tpl/resources/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,159 @@ func TestCopyPageShouldFail(t *testing.T) {
b.Assert(err, qt.IsNotNil)

}

func TestMatchPatternCaseSensitivity(t *testing.T) {
t.Parallel()

files := `
-- config.toml --
baseURL = "http://example.com/blog"
-- assets/DIR1/sub/x.txt --
content in x.txt
-- assets/dir2/SUB/y.txt --
content in y.txt
-- layouts/index.html --
{{ with resources.GetMatch "does/not/exist.txt" }}
GetMatch "does/not/exist.txt" matched
{{ else }}
GetMatch "does/not/exist.txt" unmatched
{{ end }}

{{ with resources.GetMatch "DIR1/sub/x.txt" }}
GetMatch "DIR1/sub/x.txt" | .Content => {{ .Content }}
GetMatch "DIR1/sub/x.txt" | .Name => {{ .Name }}
{{ end }}

{{ with resources.GetMatch "dir2/SUB/y.txt" }}
GetMatch "dir2/SUB/y.txt" | .Content => {{ .Content }}
GetMatch "dir2/SUB/y.txt" | .Name => {{ .Name }}
{{ end }}

{{ with resources.GetMatch "dir1/sub/x.txt" }}
GetMatch "dir1/sub/x.txt" | .Content => {{ .Content }}
GetMatch "dir1/sub/x.txt" | .Name => {{ .Name }}
{{ end }}

{{ with resources.GetMatch "dir2/sub/y.txt" }}
GetMatch "dir2/sub/y.txt" | .Content => {{ .Content }}
GetMatch "dir2/sub/y.txt" | .Name => {{ .Name }}
{{ end }}

{{ with resources.GetMatch "DIR1/SUB/X.TXT" }}
GetMatch "DIR1/SUB/X.TXT" | .Content => {{ .Content }}
GetMatch "DIR1/SUB/X.TXT" | .Name => {{ .Name }}
{{ end }}

{{ with resources.GetMatch "DIR2/SUB/Y.TXT" }}
GetMatch "DIR2/SUB/Y.TXT" | .Content => {{ .Content }}
GetMatch "DIR2/SUB/Y.TXT" | .Name => {{ .Name }}
{{ end }}

{{ with resources.GetMatch "DIR1/*/x.txt" }}
GetMatch "DIR1/*/x.txt" | .Content => {{ .Content }}
GetMatch "DIR1/*/x.txt" | .Name => {{ .Name }}
{{ end }}

{{ with resources.GetMatch "dir2/*/y.txt" }}
GetMatch "dir2/*/y.txt" | .Content => {{ .Content }}
GetMatch "dir2/*/y.txt" | .Name => {{ .Name }}
{{ end }}

{{ with resources.GetMatch "dir1/*/x.txt" }}
GetMatch "dir1/*/x.txt" | .Content => {{ .Content }}
GetMatch "dir1/*/x.txt" | .Name => {{ .Name }}
{{ end }}

{{ with resources.GetMatch "DIR2/*/Y.TXT" }}
GetMatch "DIR2/*/Y.TXT" | .Content => {{ .Content }}
GetMatch "DIR2/*/Y.TXT" | .Name => {{ .Name }}
{{ end }}

{{ with resources.GetMatch "DIR1/**/x.txt" }}
GetMatch "DIR1/**/x.txt" | .Content => {{ .Content }}
GetMatch "DIR1/**/x.txt" | .Name => {{ .Name }}
{{ end }}

{{ with resources.GetMatch "dir2/**/y.txt" }}
GetMatch "dir2/**/y.txt" | .Content => {{ .Content }}
GetMatch "dir2/**/y.txt" | .Name => {{ .Name }}
{{ end }}

{{ with resources.GetMatch "dir1/**/x.txt" }}
GetMatch "dir1/**/x.txt" | .Content => {{ .Content }}
GetMatch "dir1/**/x.txt" | .Name => {{ .Name }}
{{ end }}

{{ with resources.GetMatch "DIR2/**/Y.TXT" }}
GetMatch "DIR2/**/Y.TXT" | .Content => {{ .Content }}
GetMatch "DIR2/**/Y.TXT" | .Name => {{ .Name }}
{{ end }}

{{ with resources.Match "**/*.txt" }}
Match "**/*.txt" | len => {{ len . }}
{{ end }}

{{ with resources.Match "*/*/*.txt" }}
Match "*/*/*.txt" | len => {{ len . }}
{{ end }}

`

b := hugolib.NewIntegrationTestBuilder(
hugolib.IntegrationTestConfig{
T: t,
TxtarString: files,
}).Build()

want := `
GetMatch "does/not/exist.txt" unmatched

GetMatch "DIR1/sub/x.txt" | .Content => content in x.txt
GetMatch "DIR1/sub/x.txt" | .Name => DIR1/sub/x.txt

GetMatch "dir2/SUB/y.txt" | .Content => content in y.txt
GetMatch "dir2/SUB/y.txt" | .Name => dir2/SUB/y.txt

GetMatch "dir1/sub/x.txt" | .Content => content in x.txt
GetMatch "dir1/sub/x.txt" | .Name => DIR1/sub/x.txt

GetMatch "dir2/sub/y.txt" | .Content => content in y.txt
GetMatch "dir2/sub/y.txt" | .Name => dir2/SUB/y.txt

GetMatch "DIR1/SUB/X.TXT" | .Content => content in x.txt
GetMatch "DIR1/SUB/X.TXT" | .Name => DIR1/sub/x.txt

GetMatch "DIR2/SUB/Y.TXT" | .Content => content in y.txt
GetMatch "DIR2/SUB/Y.TXT" | .Name => dir2/SUB/y.txt

GetMatch "DIR1/*/x.txt" | .Content => content in x.txt
GetMatch "DIR1/*/x.txt" | .Name => DIR1/sub/x.txt

GetMatch "dir2/*/y.txt" | .Content => content in y.txt
GetMatch "dir2/*/y.txt" | .Name => dir2/SUB/y.txt

GetMatch "dir1/*/x.txt" | .Content => content in x.txt
GetMatch "dir1/*/x.txt" | .Name => DIR1/sub/x.txt

GetMatch "DIR2/*/Y.TXT" | .Content => content in y.txt
GetMatch "DIR2/*/Y.TXT" | .Name => dir2/SUB/y.txt

GetMatch "DIR1/**/x.txt" | .Content => content in x.txt
GetMatch "DIR1/**/x.txt" | .Name => DIR1/sub/x.txt

GetMatch "dir2/**/y.txt" | .Content => content in y.txt
GetMatch "dir2/**/y.txt" | .Name => dir2/SUB/y.txt

GetMatch "dir1/**/x.txt" | .Content => content in x.txt
GetMatch "dir1/**/x.txt" | .Name => DIR1/sub/x.txt

GetMatch "DIR2/**/Y.TXT" | .Content => content in y.txt
GetMatch "DIR2/**/Y.TXT" | .Name => dir2/SUB/y.txt

Match "*/*/*.txt" | len => 2
Match "**/*.txt" | len => 2
`

b.AssertFileContent("public/index.html", want)

}