diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2018-01-20 18:07:41 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2018-01-22 10:28:12 +0100 |
commit | 9421380168f66620cb73203e1267814b3086d805 (patch) | |
tree | 0946c9982ebfd61bb74268e69302e2364171d6f2 /resource/resource_test.go | |
parent | 5d03086981b4a7d4bc450269a6a2e0fd22dbeed7 (diff) |
resource: Add Match and GetMatch
These methods takes a glob pattern as argument:
* by default matching from the bundle root
* matching is case insensitive and the separator is Unix style slashes: "/"
* the bundle root does (by default) not start with a leading slash
* if you renames the `Name` for the rsource in front matter (`src=...`), then that is the value used in `Match`.
* double asterisk matches beyond directory borders, so "**.jpg" will match any JPEG image in the bundle
See https://github.com/gobwas/glob
This commit also deprecates `ByPrefix` and `GetByPrefix`.
This should also be more effective, given a fair amount of reuse of the glob patterns:
```bash
BenchmarkResourcesByPrefix-4 300000 4284 ns/op 1130 B/op 7 allocs/op
BenchmarkResourcesMatch-4 300000 5220 ns/op 505 B/op 3 allocs/op
```
Fixes #4301
Diffstat (limited to 'resource/resource_test.go')
-rw-r--r-- | resource/resource_test.go | 117 |
1 files changed, 116 insertions, 1 deletions
diff --git a/resource/resource_test.go b/resource/resource_test.go index 4670ef632..b4cf3ebed 100644 --- a/resource/resource_test.go +++ b/resource/resource_test.go @@ -15,9 +15,12 @@ package resource import ( "fmt" + "math/rand" "path" "path/filepath" + "strings" "testing" + "time" "github.com/stretchr/testify/require" ) @@ -137,6 +140,52 @@ func TestResourcesGetByPrefix(t *testing.T) { } +func TestResourcesGetMatch(t *testing.T) { + assert := require.New(t) + spec := newTestResourceSpec(assert) + resources := Resources{ + spec.newGenericResource(nil, nil, "/public", "/a/foo1.css", "foo1.css", "css"), + spec.newGenericResource(nil, nil, "/public", "/a/logo1.png", "logo1.png", "image"), + spec.newGenericResource(nil, nil, "/public", "/b/Logo2.png", "Logo2.png", "image"), + spec.newGenericResource(nil, nil, "/public", "/b/foo2.css", "foo2.css", "css"), + spec.newGenericResource(nil, nil, "/public", "/b/foo3.css", "foo3.css", "css"), + spec.newGenericResource(nil, nil, "/public", "/b/c/foo4.css", "c/foo4.css", "css"), + spec.newGenericResource(nil, nil, "/public", "/b/c/foo5.css", "c/foo5.css", "css"), + spec.newGenericResource(nil, nil, "/public", "/b/c/d/foo6.css", "c/d/foo6.css", "css"), + } + + assert.Equal("/logo1.png", resources.GetMatch("logo*").RelPermalink()) + assert.Equal("/logo1.png", resources.GetMatch("loGo*").RelPermalink()) + assert.Equal("/Logo2.png", resources.GetMatch("logo2*").RelPermalink()) + assert.Equal("/foo2.css", resources.GetMatch("foo2*").RelPermalink()) + assert.Equal("/foo1.css", resources.GetMatch("foo1*").RelPermalink()) + assert.Equal("/foo1.css", resources.GetMatch("foo1*").RelPermalink()) + assert.Equal("/c/foo4.css", resources.GetMatch("*/foo*").RelPermalink()) + + assert.Nil(resources.GetMatch("asdfasdf")) + + assert.Equal(2, len(resources.Match("Logo*"))) + assert.Equal(1, len(resources.Match("logo2*"))) + assert.Equal(2, len(resources.Match("c/*"))) + + assert.Equal(6, len(resources.Match("**.css"))) + assert.Equal(3, len(resources.Match("**/*.css"))) + assert.Equal(1, len(resources.Match("c/**/*.css"))) + + // Matches only CSS files in c/ + assert.Equal(3, len(resources.Match("c/**.css"))) + + // Matches all CSS files below c/ (including in c/d/) + assert.Equal(3, len(resources.Match("c/**.css"))) + + // Patterns beginning with a slash will not match anything. + // We could maybe consider trimming that slash, but let's be explicit about this. + // (it is possible for users to do a rename) + // This is analogous to standing in a directory and doing "ls *.*". + assert.Equal(0, len(resources.Match("/c/**.css"))) + +} + func TestAssignMetadata(t *testing.T) { assert := require.New(t) spec := newTestResourceSpec(assert) @@ -290,6 +339,73 @@ func TestAssignMetadata(t *testing.T) { } +func BenchmarkResourcesByPrefix(b *testing.B) { + resources := benchResources(b) + prefixes := []string{"abc", "jkl", "nomatch", "sub/"} + rnd := rand.New(rand.NewSource(time.Now().Unix())) + + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + resources.ByPrefix(prefixes[rnd.Intn(len(prefixes))]) + } + }) +} + +func BenchmarkResourcesMatch(b *testing.B) { + resources := benchResources(b) + prefixes := []string{"abc*", "jkl*", "nomatch*", "sub/*"} + rnd := rand.New(rand.NewSource(time.Now().Unix())) + + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + resources.Match(prefixes[rnd.Intn(len(prefixes))]) + } + }) +} + +// This adds a benchmark for the a100 test case as described by Russ Cox here: +// https://research.swtch.com/glob (really interesting article) +// I don't expect Hugo users to "stumble upon" this problem, so this is more to satisfy +// my own curiosity. +func BenchmarkResourcesMatchA100(b *testing.B) { + assert := require.New(b) + spec := newTestResourceSpec(assert) + a100 := strings.Repeat("a", 100) + pattern := "a*a*a*a*a*a*a*a*b" + + resources := Resources{spec.newGenericResource(nil, nil, "/public", "/a/"+a100, a100, "css")} + + b.ResetTimer() + for i := 0; i < b.N; i++ { + resources.Match(pattern) + } + +} + +func benchResources(b *testing.B) Resources { + assert := require.New(b) + spec := newTestResourceSpec(assert) + var resources Resources + + for i := 0; i < 30; i++ { + name := fmt.Sprintf("abcde%d_%d.css", i%5, i) + resources = append(resources, spec.newGenericResource(nil, nil, "/public", "/a/"+name, name, "css")) + } + + for i := 0; i < 30; i++ { + name := fmt.Sprintf("efghi%d_%d.css", i%5, i) + resources = append(resources, spec.newGenericResource(nil, nil, "/public", "/a/"+name, name, "css")) + } + + for i := 0; i < 30; i++ { + name := fmt.Sprintf("jklmn%d_%d.css", i%5, i) + resources = append(resources, spec.newGenericResource(nil, nil, "/public", "/b/sub/"+name, "sub/"+name, "css")) + } + + return resources + +} + func BenchmarkAssignMetadata(b *testing.B) { assert := require.New(b) spec := newTestResourceSpec(assert) @@ -320,5 +436,4 @@ func BenchmarkAssignMetadata(b *testing.B) { } } - } |