// Copyright 2019 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package hugolib
import (
"bytes"
"fmt"
"path"
"strings"
"testing"
"github.com/gohugoio/hugo/common/maps"
qt "github.com/frankban/quicktest"
"github.com/gohugoio/hugo/parser"
"github.com/gohugoio/hugo/parser/metadecoders"
)
func BenchmarkCascade(b *testing.B) {
allLangs := []string{"en", "nn", "nb", "sv", "ab", "aa", "af", "sq", "kw", "da"}
for i := 1; i <= len(allLangs); i += 2 {
langs := allLangs[0:i]
b.Run(fmt.Sprintf("langs-%d", len(langs)), func(b *testing.B) {
c := qt.New(b)
b.StopTimer()
builders := make([]*sitesBuilder, b.N)
for i := 0; i < b.N; i++ {
builders[i] = newCascadeTestBuilder(b, langs)
}
b.StartTimer()
for i := 0; i < b.N; i++ {
builder := builders[i]
err := builder.BuildE(BuildCfg{})
c.Assert(err, qt.IsNil)
first := builder.H.Sites[0]
c.Assert(first, qt.Not(qt.IsNil))
}
})
}
}
func BenchmarkCascadeTarget(b *testing.B) {
files := `
-- content/_index.md --
background = 'yosemite.jpg'
[cascade._target]
kind = '{section,term}'
-- content/posts/_index.md --
-- content/posts/funny/_index.md --
`
for i := 1; i < 100; i++ {
files += fmt.Sprintf("\n-- content/posts/p%d.md --\n", i+1)
}
for i := 1; i < 100; i++ {
files += fmt.Sprintf("\n-- content/posts/funny/pf%d.md --\n", i+1)
}
b.Run("Kind", func(b *testing.B) {
cfg := IntegrationTestConfig{
T: b,
TxtarString: files,
}
builders := make([]*IntegrationTestBuilder, b.N)
for i := range builders {
builders[i] = NewIntegrationTestBuilder(cfg)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
builders[i].Build()
}
})
}
func TestCascadeConfig(t *testing.T) {
c := qt.New(t)
// Make sure the cascade from config gets applied even if we're not
// having a content file for the home page.
for _, withHomeContent := range []bool{true, false} {
testName := "Home content file"
if !withHomeContent {
testName = "No home content file"
}
c.Run(testName, func(c *qt.C) {
b := newTestSitesBuilder(c)
b.WithConfigFile("toml", `
baseURL="https://example.org"
[cascade]
img1 = "img1-config.jpg"
imgconfig = "img-config.jpg"
`)
if withHomeContent {
b.WithContent("_index.md", `
---
title: "Home"
cascade:
img1: "img1-home.jpg"
img2: "img2-home.jpg"
---
`)
}
b.WithContent("p1.md", ``)
b.Build(BuildCfg{})
p1 := b.H.Sites[0].getPageOldVersion("p1")
if withHomeContent {
b.Assert(p1.Params(), qt.DeepEquals, maps.Params{
"imgconfig": "img-config.jpg",
"draft": bool(false),
"iscjklanguage": bool(false),
"img1": "img1-home.jpg",
<