Skip to content

Commit

Permalink
Fix cascade-pattern-with-extension for cascade in site config
Browse files Browse the repository at this point in the history
Also clean up the log handling in the integration tester, most notably lost logs during the config loading.

Fixes #12151
  • Loading branch information
bep committed Feb 26, 2024
1 parent d4be164 commit 1736ef7
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 26 deletions.
9 changes: 8 additions & 1 deletion config/allconfig/allconfig.go
Expand Up @@ -671,6 +671,13 @@ type Configs struct {
configLangs []config.AllProvider
}

func (c *Configs) Validate(logger loggers.Logger) error {
for p := range c.Base.Cascade.Config {
page.CheckCascadePattern(logger, p)
}
return nil
}

// transientErr returns the last transient error found during config compilation.
func (c *Configs) transientErr() error {
for _, l := range c.LanguageConfigSlice {
Expand Down Expand Up @@ -969,7 +976,7 @@ func decodeConfigFromParams(fs afero.Fs, logger loggers.Logger, bcfg config.Base
})

for _, v := range decoderSetups {
p := decodeConfig{p: p, c: target, fs: fs, logger: logger, bcfg: bcfg}
p := decodeConfig{p: p, c: target, fs: fs, bcfg: bcfg}
if err := v.decode(v, p); err != nil {
return fmt.Errorf("failed to decode %q: %w", v.key, err)
}
Expand Down
12 changes: 5 additions & 7 deletions config/allconfig/alldecoders.go
Expand Up @@ -18,7 +18,6 @@ import (
"strings"

"github.com/gohugoio/hugo/cache/filecache"
"github.com/gohugoio/hugo/common/loggers"
"github.com/gohugoio/hugo/common/maps"
"github.com/gohugoio/hugo/common/types"
"github.com/gohugoio/hugo/config"
Expand All @@ -43,11 +42,10 @@ import (
)

type decodeConfig struct {
p config.Provider
c *Config
fs afero.Fs
logger loggers.Logger
bcfg config.BaseConfig
p config.Provider
c *Config
fs afero.Fs
bcfg config.BaseConfig
}

type decodeWeight struct {
Expand Down Expand Up @@ -293,7 +291,7 @@ var allDecoderSetups = map[string]decodeWeight{
key: "cascade",
decode: func(d decodeWeight, p decodeConfig) error {
var err error
p.c.Cascade, err = page.DecodeCascadeConfig(p.logger, p.p.Get(d.key))
p.c.Cascade, err = page.DecodeCascadeConfig(nil, p.p.Get(d.key))
return err
},
},
Expand Down
66 changes: 66 additions & 0 deletions hugolib/cascade_test.go
Expand Up @@ -674,6 +674,8 @@ S1|p1:|p2:p2|

// Issue 11977.
func TestCascadeExtensionInPath(t *testing.T) {
t.Parallel()

files := `
-- hugo.toml --
baseURL = "https://example.org"
Expand All @@ -700,3 +702,67 @@ title: "Post 1"
b.Assert(err, qt.IsNotNil)
b.AssertLogContains(`cascade target path "/posts/post-1.de.md" looks like a path with an extension; since Hugo v0.123.0 this will not match anything, see https://gohugo.io/methods/page/path/`)
}

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

files := `
-- hugo.toml --
baseURL = "https://example.org"
ignoreLogs = ['cascade-pattern-with-extension']
[languages]
[languages.en]
weight = 1
[languages.de]
-- content/_index.de.md --
+++
[[cascade]]
[cascade.params]
foo = 'bar'
[cascade._target]
path = '/posts/post-1.de.md'
+++
-- content/posts/post-1.de.md --
---
title: "Post 1"
---
-- layouts/_default/single.html --
{{ .Title }}|{{ .Params.foo }}$
`
b := Test(t, files)
b.AssertLogNotContains(`looks like a path with an extension`)
}

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

files := `
-- hugo.toml --
baseURL = "https://example.org"
[[cascade]]
[cascade.params]
foo = 'bar'
[cascade._target]
path = '/p1.md'
`
b, err := TestE(t, files)
b.Assert(err, qt.IsNotNil)
b.AssertLogContains(`looks like a path with an extension`)
}

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

files := `
-- hugo.toml --
baseURL = "https://example.org"
ignoreLogs = ['cascade-pattern-with-extension']
[[cascade]]
[cascade.params]
foo = 'bar'
[cascade._target]
path = '/p1.md'
`
b := Test(t, files)
b.AssertLogNotContains(`looks like a path with an extension`)
}
29 changes: 14 additions & 15 deletions hugolib/integrationtest_builder.go
Expand Up @@ -169,10 +169,11 @@ type IntegrationTestBuilder struct {
renamedFiles []string
renamedDirs []string

buildCount int
GCCount int
counters *buildCounters
logBuff lockingBuffer
buildCount int
GCCount int
counters *buildCounters
logBuff lockingBuffer
lastBuildLog string

builderInit sync.Once
}
Expand All @@ -192,21 +193,21 @@ func (b *lockingBuffer) Write(p []byte) (n int, err error) {
func (s *IntegrationTestBuilder) AssertLogContains(els ...string) {
s.Helper()
for _, el := range els {
s.Assert(s.logBuff.String(), qt.Contains, el)
s.Assert(s.lastBuildLog, qt.Contains, el)
}
}

func (s *IntegrationTestBuilder) AssertLogNotContains(els ...string) {
s.Helper()
for _, el := range els {
s.Assert(s.logBuff.String(), qt.Not(qt.Contains), el)
s.Assert(s.lastBuildLog, qt.Not(qt.Contains), el)
}
}

func (s *IntegrationTestBuilder) AssertLogMatches(expression string) {
s.Helper()
re := regexp.MustCompile(expression)
s.Assert(re.MatchString(s.logBuff.String()), qt.IsTrue, qt.Commentf(s.logBuff.String()))
s.Assert(re.MatchString(s.lastBuildLog), qt.IsTrue, qt.Commentf(s.lastBuildLog))
}

func (s *IntegrationTestBuilder) AssertBuildCountData(count int) {
Expand Down Expand Up @@ -341,7 +342,7 @@ func (s *IntegrationTestBuilder) Build() *IntegrationTestBuilder {
s.Helper()
_, err := s.BuildE()
if s.Cfg.Verbose || err != nil {
fmt.Println(s.logBuff.String())
fmt.Println(s.lastBuildLog)
if s.H != nil && err == nil {
for _, s := range s.H.Sites {
m := s.pageMap
Expand All @@ -352,7 +353,7 @@ func (s *IntegrationTestBuilder) Build() *IntegrationTestBuilder {
}
}
} else if s.Cfg.LogLevel <= logg.LevelDebug {
fmt.Println(s.logBuff.String())
fmt.Println(s.lastBuildLog)
}
s.Assert(err, qt.IsNil)
if s.Cfg.RunGC {
Expand All @@ -364,7 +365,7 @@ func (s *IntegrationTestBuilder) Build() *IntegrationTestBuilder {
}

func (s *IntegrationTestBuilder) LogString() string {
return s.logBuff.String()
return s.lastBuildLog
}

func (s *IntegrationTestBuilder) BuildE() (*IntegrationTestBuilder, error) {
Expand All @@ -381,6 +382,7 @@ func (s *IntegrationTestBuilder) Init() *IntegrationTestBuilder {
if err := s.initBuilder(); err != nil {
s.Fatalf("Failed to init builder: %s", err)
}
s.lastBuildLog = s.logBuff.String()
return s
}

Expand Down Expand Up @@ -626,10 +628,11 @@ func (s *IntegrationTestBuilder) build(cfg BuildCfg) error {
s.Helper()
defer func() {
s.reset()
s.lastBuildLog = s.logBuff.String()
s.logBuff.Reset()
}()

changeEvents := s.changeEvents()
s.logBuff.Reset()
s.counters = &buildCounters{}
cfg.testCounters = s.counters

Expand All @@ -643,10 +646,6 @@ func (s *IntegrationTestBuilder) build(cfg BuildCfg) error {
if err != nil {
return err
}
logErrorCount := s.H.NumLogErrors()
if logErrorCount > 0 {
return fmt.Errorf("logged %d error(s): %s", logErrorCount, s.logBuff.String())
}

return nil
}
Expand Down
4 changes: 4 additions & 0 deletions hugolib/site_new.go
Expand Up @@ -127,6 +127,7 @@ func NewHugoSites(cfg deps.DepsCfg) (*HugoSites, error) {
SuppressStatements: conf.IgnoredLogs(),
}
logger = loggers.New(logOpts)

}

memCache := dynacache.New(dynacache.Options{Running: conf.Running(), Log: logger})
Expand All @@ -145,6 +146,9 @@ func NewHugoSites(cfg deps.DepsCfg) (*HugoSites, error) {
}

confm := cfg.Configs
if err := confm.Validate(logger); err != nil {
return nil, err
}
var sites []*Site

ns := &contentNodeShifter{
Expand Down
10 changes: 7 additions & 3 deletions resources/page/page_matcher.go
Expand Up @@ -98,6 +98,12 @@ func isGlobWithExtension(s string) bool {
return strings.Count(last, ".") > 0
}

func CheckCascadePattern(logger loggers.Logger, m PageMatcher) {
if logger != nil && isGlobWithExtension(m.Path) {
logger.Erroridf("cascade-pattern-with-extension", "cascade target path %q looks like a path with an extension; since Hugo v0.123.0 this will not match anything, see https://gohugo.io/methods/page/path/", m.Path)
}
}

func DecodeCascadeConfig(logger loggers.Logger, in any) (*config.ConfigNamespace[[]PageMatcherParamsConfig, map[PageMatcher]maps.Params], error) {
buildConfig := func(in any) (map[PageMatcher]maps.Params, any, error) {
cascade := make(map[PageMatcher]maps.Params)
Expand Down Expand Up @@ -127,9 +133,7 @@ func DecodeCascadeConfig(logger loggers.Logger, in any) (*config.ConfigNamespace

for _, cfg := range cfgs {
m := cfg.Target
if isGlobWithExtension(m.Path) {
logger.Erroridf("cascade-pattern-with-extension", "cascade target path %q looks like a path with an extension; since Hugo v0.123.0 this will not match anything, see https://gohugo.io/methods/page/path/", m.Path)
}
CheckCascadePattern(logger, m)
c, found := cascade[m]
if found {
// Merge
Expand Down

0 comments on commit 1736ef7

Please sign in to comment.