hugo/hugolib/segments/segments_test.go
Bjørn Erik Pedersen 1f1c62e6c7 Add segments config + --renderSegments flag
Named segments can be defined in `hugo.toml`.

* Eeach segment consists of zero or more `exclude` filters and zero or more `include` filters.
* Eeach filter consists of one or more field Glob matchers.
* Eeach filter in a section (`exclude` or `include`) is ORed together, each matcher in a filter is ANDed together.

The current list of fields that can be filtered are:

* path as defined in https://gohugo.io/methods/page/path/
* kind
* lang
* output (output format, e.g. html).

It is recommended to put coarse grained filters (e.g. for language and output format) in the excludes section, e.g.:

```toml
[segments.segment1]
  [[segments.segment1.excludes]]
    lang = "n*"
  [[segments.segment1.excludes]]
    no     = "en"
    output = "rss"
  [[segments.segment1.includes]]
    term = "{home,term,taxonomy}"
  [[segments.segment1.includes]]
    path = "{/docs,/docs/**}"
```

By default, Hugo will render all segments, but you can enable filters by setting the `renderSegments` option or `--renderSegments` flag, e.g:

```
hugo --renderSegments segment1,segment2
```

For segment `segment1` in the configuration above, this will:

* Skip rendering of all languages matching `n*`, e.g. `no`.
* Skip rendering of the output format `rss` for the `en` language.
* It will render all pages of kind `home`, `term` or `taxonomy`
* It will render the `/docs` section and all pages below.

Fixes #10106
2024-03-16 15:53:26 +01:00

116 lines
2.8 KiB
Go

package segments
import (
"testing"
qt "github.com/frankban/quicktest"
)
func TestCompileSegments(t *testing.T) {
c := qt.New(t)
c.Run("excludes", func(c *qt.C) {
fields := []SegmentMatcherFields{
{
Lang: "n*",
Output: "rss",
},
}
match, err := compileSegments(fields)
c.Assert(err, qt.IsNil)
check := func() {
c.Assert(match, qt.IsNotNil)
c.Assert(match(SegmentMatcherFields{Lang: "no"}), qt.Equals, false)
c.Assert(match(SegmentMatcherFields{Lang: "no", Kind: "page"}), qt.Equals, false)
c.Assert(match(SegmentMatcherFields{Lang: "no", Output: "rss"}), qt.Equals, true)
c.Assert(match(SegmentMatcherFields{Lang: "no", Output: "html"}), qt.Equals, false)
c.Assert(match(SegmentMatcherFields{Kind: "page"}), qt.Equals, false)
c.Assert(match(SegmentMatcherFields{Lang: "no", Output: "rss", Kind: "page"}), qt.Equals, true)
}
check()
fields = []SegmentMatcherFields{
{
Path: "/blog/**",
},
{
Lang: "n*",
Output: "rss",
},
}
match, err = compileSegments(fields)
c.Assert(err, qt.IsNil)
check()
c.Assert(match(SegmentMatcherFields{Path: "/blog/foo"}), qt.Equals, true)
})
c.Run("includes", func(c *qt.C) {
fields := []SegmentMatcherFields{
{
Path: "/docs/**",
},
{
Lang: "no",
Output: "rss",
},
}
match, err := compileSegments(fields)
c.Assert(err, qt.IsNil)
c.Assert(match, qt.IsNotNil)
c.Assert(match(SegmentMatcherFields{Lang: "no"}), qt.Equals, false)
c.Assert(match(SegmentMatcherFields{Kind: "page"}), qt.Equals, false)
c.Assert(match(SegmentMatcherFields{Kind: "page", Path: "/blog/foo"}), qt.Equals, false)
c.Assert(match(SegmentMatcherFields{Lang: "en"}), qt.Equals, false)
c.Assert(match(SegmentMatcherFields{Lang: "no", Output: "rss"}), qt.Equals, true)
c.Assert(match(SegmentMatcherFields{Lang: "no", Output: "html"}), qt.Equals, false)
c.Assert(match(SegmentMatcherFields{Kind: "page", Path: "/docs/foo"}), qt.Equals, true)
})
c.Run("includes variant1", func(c *qt.C) {
c.Skip()
fields := []SegmentMatcherFields{
{
Kind: "home",
},
{
Path: "{/docs,/docs/**}",
},
}
match, err := compileSegments(fields)
c.Assert(err, qt.IsNil)
c.Assert(match, qt.IsNotNil)
c.Assert(match(SegmentMatcherFields{Path: "/blog/foo"}), qt.Equals, false)
c.Assert(match(SegmentMatcherFields{Kind: "page", Path: "/docs/foo"}), qt.Equals, true)
c.Assert(match(SegmentMatcherFields{Kind: "home", Path: "/"}), qt.Equals, true)
})
}
func BenchmarkSegmentsMatch(b *testing.B) {
fields := []SegmentMatcherFields{
{
Path: "/docs/**",
},
{
Lang: "no",
Output: "rss",
},
}
match, err := compileSegments(fields)
if err != nil {
b.Fatal(err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
match(SegmentMatcherFields{Lang: "no", Output: "rss"})
}
}