Create a struct with all of Hugo's config options

Primary motivation is documentation, but it will also hopefully simplify the code.

Also,

* Lower case the default output format names; this is in line with the custom ones (map keys) and how
it's treated all the places. This avoids doing `stringds.EqualFold` everywhere.

Closes #10896
Closes #10620
This commit is contained in:
Bjørn Erik Pedersen
2023-01-04 18:24:36 +01:00
parent 6aededf6b4
commit 241b21b0fd
337 changed files with 13377 additions and 14898 deletions

View File

@@ -1,4 +1,4 @@
// Copyright 2015 The Hugo Authors. All rights reserved.
// Copyright 2023 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.
@@ -11,21 +11,20 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package helpers
package helpers_test
import (
"fmt"
"strings"
"testing"
qt "github.com/frankban/quicktest"
"github.com/gohugoio/hugo/hugofs"
"github.com/gohugoio/hugo/langs"
"github.com/gohugoio/hugo/config"
"github.com/gohugoio/hugo/helpers"
)
func TestURLize(t *testing.T) {
v := newTestCfg()
l := langs.NewDefaultLanguage(v)
p, _ := NewPathSpec(hugofs.NewMem(v), l, nil)
p := newTestPathSpec()
tests := []struct {
input string
@@ -61,10 +60,6 @@ func TestAbsURL(t *testing.T) {
func doTestAbsURL(t *testing.T, defaultInSubDir, addLanguage, multilingual bool, lang string) {
c := qt.New(t)
v := newTestCfg()
v.Set("multilingual", multilingual)
v.Set("defaultContentLanguage", "en")
v.Set("defaultContentLanguageInSubdir", defaultInSubDir)
tests := []struct {
input string
@@ -103,24 +98,42 @@ func doTestAbsURL(t *testing.T, defaultInSubDir, addLanguage, multilingual bool,
}
for _, test := range tests {
v.Set("baseURL", test.baseURL)
v.Set("contentDir", "content")
l := langs.NewLanguage(lang, v)
p, _ := NewPathSpec(hugofs.NewMem(v), l, nil)
output := p.AbsURL(test.input, addLanguage)
expected := test.expected
if multilingual && addLanguage {
if !defaultInSubDir && lang == "en" {
expected = strings.Replace(expected, "MULTI", "", 1)
} else {
expected = strings.Replace(expected, "MULTI", lang+"/", 1)
c.Run(fmt.Sprintf("%v/%t-%t-%t/%s", test, defaultInSubDir, addLanguage, multilingual, lang), func(c *qt.C) {
v := config.New()
if multilingual {
v.Set("languages", map[string]any{
"fr": map[string]interface{}{
"weight": 20,
},
"en": map[string]interface{}{
"weight": 10,
},
})
}
} else {
expected = strings.Replace(expected, "MULTI", "", 1)
}
v.Set("defaultContentLanguage", "en")
v.Set("defaultContentLanguageInSubdir", defaultInSubDir)
v.Set("baseURL", test.baseURL)
c.Assert(output, qt.Equals, expected)
var configLang string
if multilingual {
configLang = lang
}
p := newTestPathSpecFromCfgAndLang(v, configLang)
output := p.AbsURL(test.input, addLanguage)
expected := test.expected
if multilingual && addLanguage {
if !defaultInSubDir && lang == "en" {
expected = strings.Replace(expected, "MULTI", "", 1)
} else {
expected = strings.Replace(expected, "MULTI", lang+"/", 1)
}
} else {
expected = strings.Replace(expected, "MULTI", "", 1)
}
c.Assert(output, qt.Equals, expected)
})
}
}
@@ -137,9 +150,19 @@ func TestRelURL(t *testing.T) {
}
func doTestRelURL(t *testing.T, defaultInSubDir, addLanguage, multilingual bool, lang string) {
t.Helper()
c := qt.New(t)
v := newTestCfg()
v.Set("multilingual", multilingual)
v := config.New()
if multilingual {
v.Set("languages", map[string]any{
"fr": map[string]interface{}{
"weight": 20,
},
"en": map[string]interface{}{
"weight": 10,
},
})
}
v.Set("defaultContentLanguage", "en")
v.Set("defaultContentLanguageInSubdir", defaultInSubDir)
@@ -182,25 +205,31 @@ func doTestRelURL(t *testing.T, defaultInSubDir, addLanguage, multilingual bool,
}
for i, test := range tests {
v.Set("baseURL", test.baseURL)
v.Set("canonifyURLs", test.canonify)
l := langs.NewLanguage(lang, v)
p, _ := NewPathSpec(hugofs.NewMem(v), l, nil)
c.Run(fmt.Sprintf("%v/%t%t%t/%s", test, defaultInSubDir, addLanguage, multilingual, lang), func(c *qt.C) {
output := p.RelURL(test.input, addLanguage)
expected := test.expected
if multilingual && addLanguage {
if !defaultInSubDir && lang == "en" {
expected = strings.Replace(expected, "MULTI", "", 1)
} else {
expected = strings.Replace(expected, "MULTI", "/"+lang, 1)
v.Set("baseURL", test.baseURL)
v.Set("canonifyURLs", test.canonify)
var configLang string
if multilingual {
configLang = lang
}
} else {
expected = strings.Replace(expected, "MULTI", "", 1)
}
p := newTestPathSpecFromCfgAndLang(v, configLang)
c.Assert(output, qt.Equals, expected, qt.Commentf("[%d] %s", i, test.input))
output := p.RelURL(test.input, addLanguage)
expected := test.expected
if multilingual && addLanguage {
if !defaultInSubDir && lang == "en" {
expected = strings.Replace(expected, "MULTI", "", 1)
} else {
expected = strings.Replace(expected, "MULTI", "/"+lang, 1)
}
} else {
expected = strings.Replace(expected, "MULTI", "", 1)
}
c.Assert(output, qt.Equals, expected, qt.Commentf("[%d] %s", i, test.input))
})
}
}
@@ -216,8 +245,8 @@ func TestSanitizeURL(t *testing.T) {
}
for i, test := range tests {
o1 := SanitizeURL(test.input)
o2 := SanitizeURLKeepTrailingSlash(test.input)
o1 := helpers.SanitizeURL(test.input)
o2 := helpers.SanitizeURLKeepTrailingSlash(test.input)
expected2 := test.expected
@@ -233,28 +262,3 @@ func TestSanitizeURL(t *testing.T) {
}
}
}
func TestURLPrep(t *testing.T) {
type test struct {
ugly bool
input string
output string
}
data := []test{
{false, "/section/name.html", "/section/name/"},
{true, "/section/name/index.html", "/section/name.html"},
}
for i, d := range data {
v := newTestCfg()
v.Set("uglyURLs", d.ugly)
l := langs.NewDefaultLanguage(v)
p, _ := NewPathSpec(hugofs.NewMem(v), l, nil)
output := p.URLPrep(d.input)
if d.output != output {
t.Errorf("Test #%d failed. Expected %q got %q", i, d.output, output)
}
}
}