aboutsummaryrefslogtreecommitdiffhomepage
path: root/hugolib/site_test.go
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <[email protected]>2023-01-04 18:24:36 +0100
committerBjørn Erik Pedersen <[email protected]>2023-05-16 18:01:29 +0200
commit241b21b0fd34d91fccb2ce69874110dceae6f926 (patch)
treed4e0118eac7e9c42f065815447a70805f8d6ad3e /hugolib/site_test.go
parent6aededf6b42011c3039f5f66487a89a8dd65e0e7 (diff)
downloadhugo-241b21b0fd34d91fccb2ce69874110dceae6f926.tar.gz
hugo-241b21b0fd34d91fccb2ce69874110dceae6f926.zip
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
Diffstat (limited to 'hugolib/site_test.go')
-rw-r--r--hugolib/site_test.go204
1 files changed, 162 insertions, 42 deletions
diff --git a/hugolib/site_test.go b/hugolib/site_test.go
index 494c41c88..be59b17a7 100644
--- a/hugolib/site_test.go
+++ b/hugolib/site_test.go
@@ -36,19 +36,10 @@ const (
templateWithURLAbs = "<a href=\"/foobar.jpg\">Going</a>"
)
-func TestRenderWithInvalidTemplate(t *testing.T) {
- t.Parallel()
- cfg, fs := newTestCfg()
-
- writeSource(t, fs, filepath.Join("content", "foo.md"), "foo")
-
- withTemplate := createWithTemplateFromNameValues("missing", templateMissingFunc)
-
- buildSingleSiteExpected(t, true, false, deps.DepsCfg{Fs: fs, Cfg: cfg, WithTemplate: withTemplate}, BuildCfg{})
-}
-
func TestDraftAndFutureRender(t *testing.T) {
t.Parallel()
+ c := qt.New(t)
+
sources := [][2]string{
{filepath.FromSlash("sect/doc1.md"), "---\ntitle: doc1\ndraft: true\npublishdate: \"2414-05-29\"\n---\n# doc1\n*some content*"},
{filepath.FromSlash("sect/doc2.md"), "---\ntitle: doc2\ndraft: true\npublishdate: \"2012-05-29\"\n---\n# doc2\n*some content*"},
@@ -64,12 +55,14 @@ func TestDraftAndFutureRender(t *testing.T) {
for i := 0; i < len(configKeyValues); i += 2 {
cfg.Set(configKeyValues[i].(string), configKeyValues[i+1])
}
+ configs, err := loadTestConfigFromProvider(cfg)
+ c.Assert(err, qt.IsNil)
for _, src := range sources {
writeSource(t, fs, filepath.Join("content", src[0]), src[1])
}
- return buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{})
+ return buildSingleSite(t, deps.DepsCfg{Fs: fs, Configs: configs}, BuildCfg{})
}
// Testing Defaults.. Only draft:true and publishDate in the past should be rendered
@@ -105,6 +98,7 @@ func TestDraftAndFutureRender(t *testing.T) {
func TestFutureExpirationRender(t *testing.T) {
t.Parallel()
+ c := qt.New(t)
sources := [][2]string{
{filepath.FromSlash("sect/doc3.md"), "---\ntitle: doc1\nexpirydate: \"2400-05-29\"\n---\n# doc1\n*some content*"},
{filepath.FromSlash("sect/doc4.md"), "---\ntitle: doc2\nexpirydate: \"2000-05-29\"\n---\n# doc2\n*some content*"},
@@ -114,11 +108,14 @@ func TestFutureExpirationRender(t *testing.T) {
cfg, fs := newTestCfg()
cfg.Set("baseURL", "http://auth/bub")
+ configs, err := loadTestConfigFromProvider(cfg)
+ c.Assert(err, qt.IsNil)
+
for _, src := range sources {
writeSource(t, fs, filepath.Join("content", src[0]), src[1])
}
- return buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{})
+ return buildSingleSite(t, deps.DepsCfg{Fs: fs, Configs: configs}, BuildCfg{})
}
s := siteSetup(t)
@@ -143,6 +140,8 @@ func TestLastChange(t *testing.T) {
cfg, fs := newTestCfg()
c := qt.New(t)
+ configs, err := loadTestConfigFromProvider(cfg)
+ c.Assert(err, qt.IsNil)
writeSource(t, fs, filepath.Join("content", "sect/doc1.md"), "---\ntitle: doc1\nweight: 1\ndate: 2014-05-29\n---\n# doc1\n*some content*")
writeSource(t, fs, filepath.Join("content", "sect/doc2.md"), "---\ntitle: doc2\nweight: 2\ndate: 2015-05-29\n---\n# doc2\n*some content*")
@@ -150,22 +149,24 @@ func TestLastChange(t *testing.T) {
writeSource(t, fs, filepath.Join("content", "sect/doc4.md"), "---\ntitle: doc4\nweight: 4\ndate: 2016-05-29\n---\n# doc4\n*some content*")
writeSource(t, fs, filepath.Join("content", "sect/doc5.md"), "---\ntitle: doc5\nweight: 3\n---\n# doc5\n*some content*")
- s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{SkipRender: true})
+ s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Configs: configs}, BuildCfg{SkipRender: true})
- c.Assert(s.Info.LastChange().IsZero(), qt.Equals, false)
- c.Assert(s.Info.LastChange().Year(), qt.Equals, 2017)
+ c.Assert(s.LastChange().IsZero(), qt.Equals, false)
+ c.Assert(s.LastChange().Year(), qt.Equals, 2017)
}
// Issue #_index
func TestPageWithUnderScoreIndexInFilename(t *testing.T) {
t.Parallel()
+ c := qt.New(t)
cfg, fs := newTestCfg()
- c := qt.New(t)
+ configs, err := loadTestConfigFromProvider(cfg)
+ c.Assert(err, qt.IsNil)
writeSource(t, fs, filepath.Join("content", "sect/my_index_file.md"), "---\ntitle: doc1\nweight: 1\ndate: 2014-05-29\n---\n# doc1\n*some content*")
- s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{SkipRender: true})
+ s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Configs: configs}, BuildCfg{SkipRender: true})
c.Assert(len(s.RegularPages()), qt.Equals, 1)
}
@@ -239,23 +240,25 @@ THE END.`, refShortcode),
cfg.Set("baseURL", baseURL)
cfg.Set("uglyURLs", uglyURLs)
cfg.Set("verbose", true)
+ configs, err := loadTestConfigFromProvider(cfg)
+ c.Assert(err, qt.IsNil)
for _, src := range sources {
writeSource(t, fs, filepath.Join("content", src[0]), src[1])
}
+ writeSource(t, fs, filepath.Join("layouts", "_default", "single.html"), "{{.Content}}")
s := buildSingleSite(
t,
deps.DepsCfg{
- Fs: fs,
- Cfg: cfg,
- WithTemplate: createWithTemplateFromNameValues("_default/single.html", "{{.Content}}"),
+ Fs: fs,
+ Configs: configs,
},
BuildCfg{})
c.Assert(len(s.RegularPages()), qt.Equals, 4)
- th := newTestHelper(s.Cfg, s.Fs, t)
+ th := newTestHelper(s.conf, s.Fs, t)
tests := []struct {
doc string
@@ -289,6 +292,9 @@ func doTestShouldAlwaysHaveUglyURLs(t *testing.T, uglyURLs bool) {
cfg.Set("baseURL", "http://auth/bub")
cfg.Set("uglyURLs", uglyURLs)
+ configs, err := loadTestConfigFromProvider(cfg)
+ c.Assert(err, qt.IsNil)
+
sources := [][2]string{
{filepath.FromSlash("sect/doc1.md"), "---\nmarkup: markdown\n---\n# title\nsome *content*"},
{filepath.FromSlash("sect/doc2.md"), "---\nurl: /ugly.html\nmarkup: markdown\n---\n# title\ndoc2 *content*"},
@@ -304,7 +310,7 @@ func doTestShouldAlwaysHaveUglyURLs(t *testing.T, uglyURLs bool) {
writeSource(t, fs, filepath.Join("layouts", "rss.xml"), "<root>RSS</root>")
writeSource(t, fs, filepath.Join("layouts", "sitemap.xml"), "<root>SITEMAP</root>")
- s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{})
+ s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Configs: configs}, BuildCfg{})
var expectedPagePath string
if uglyURLs {
@@ -341,14 +347,18 @@ func doTestShouldAlwaysHaveUglyURLs(t *testing.T, uglyURLs bool) {
// Issue #3355
func TestShouldNotWriteZeroLengthFilesToDestination(t *testing.T) {
+ c := qt.New(t)
+
cfg, fs := newTestCfg()
+ configs, err := loadTestConfigFromProvider(cfg)
+ c.Assert(err, qt.IsNil)
writeSource(t, fs, filepath.Join("content", "simple.html"), "simple")
writeSource(t, fs, filepath.Join("layouts", "_default/single.html"), "{{.Content}}")
writeSource(t, fs, filepath.Join("layouts", "_default/list.html"), "")
- s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{})
- th := newTestHelper(s.Cfg, s.Fs, t)
+ s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Configs: configs}, BuildCfg{})
+ th := newTestHelper(s.conf, s.Fs, t)
th.assertFileNotExist(filepath.Join("public", "index.html"))
}
@@ -357,7 +367,7 @@ func TestMainSections(t *testing.T) {
c := qt.New(t)
for _, paramSet := range []bool{false, true} {
c.Run(fmt.Sprintf("param-%t", paramSet), func(c *qt.C) {
- v := config.NewWithTestDefaults()
+ v := config.New()
if paramSet {
v.Set("params", map[string]any{
"mainSections": []string{"a1", "a2"},
@@ -407,6 +417,101 @@ Main section page: {{ .RelPermalink }}
}
}
+func TestMainSectionsMoveToSite(t *testing.T) {
+
+ t.Run("defined in params", func(t *testing.T) {
+ t.Parallel()
+
+ files := `
+-- config.toml --
+disableKinds = ['RSS','sitemap','taxonomy','term']
+[params]
+mainSections=["a", "b"]
+-- content/mysect/page1.md --
+-- layouts/index.html --
+{{/* Behaviour before Hugo 0.112.0. */}}
+MainSections Params: {{ site.Params.mainSections }}|
+MainSections Site method: {{ site.MainSections }}|
+
+
+ `
+
+ b := NewIntegrationTestBuilder(
+ IntegrationTestConfig{
+ T: t,
+ TxtarString: files,
+ },
+ ).Build()
+
+ b.AssertFileContent("public/index.html", `
+MainSections Params: [a b]|
+MainSections Site method: [a b]|
+ `)
+ })
+
+ t.Run("defined in top level config", func(t *testing.T) {
+ t.Parallel()
+
+ files := `
+-- config.toml --
+disableKinds = ['RSS','sitemap','taxonomy','term']
+mainSections=["a", "b"]
+[params]
+[params.sub]
+mainSections=["c", "d"]
+-- content/mysect/page1.md --
+-- layouts/index.html --
+{{/* Behaviour before Hugo 0.112.0. */}}
+MainSections Params: {{ site.Params.mainSections }}|
+MainSections Param sub: {{ site.Params.sub.mainSections }}|
+MainSections Site method: {{ site.MainSections }}|
+
+
+`
+
+ b := NewIntegrationTestBuilder(
+ IntegrationTestConfig{
+ T: t,
+ TxtarString: files,
+ },
+ ).Build()
+
+ b.AssertFileContent("public/index.html", `
+MainSections Params: [a b]|
+MainSections Param sub: [c d]|
+MainSections Site method: [a b]|
+`)
+ })
+
+ t.Run("guessed from pages", func(t *testing.T) {
+ t.Parallel()
+
+ files := `
+-- config.toml --
+disableKinds = ['RSS','sitemap','taxonomy','term']
+-- content/mysect/page1.md --
+-- layouts/index.html --
+MainSections Params: {{ site.Params.mainSections }}|
+MainSections Site method: {{ site.MainSections }}|
+
+
+ `
+
+ b := NewIntegrationTestBuilder(
+ IntegrationTestConfig{
+ T: t,
+ TxtarString: files,
+ },
+ ).Build()
+
+ b.AssertFileContent("public/index.html", `
+MainSections Params: [mysect]|
+MainSections Site method: [mysect]|
+ `)
+ })
+
+}
+
// Issue #1176
func TestSectionNaming(t *testing.T) {
for _, canonify := range []bool{true, false} {
@@ -450,6 +555,9 @@ func doTestSectionNaming(t *testing.T, canonify, uglify, pluralize bool) {
cfg.Set("pluralizeListTitles", pluralize)
cfg.Set("canonifyURLs", canonify)
+ configs, err := loadTestConfigFromProvider(cfg)
+ c.Assert(err, qt.IsNil)
+
for _, src := range sources {
writeSource(t, fs, filepath.Join("content", src[0]), src[1])
}
@@ -457,13 +565,11 @@ func doTestSectionNaming(t *testing.T, canonify, uglify, pluralize bool) {
writeSource(t, fs, filepath.Join("layouts", "_default/single.html"), "{{.Content}}")
writeSource(t, fs, filepath.Join("layouts", "_default/list.html"), "{{ .Kind }}|{{.Title}}")
- s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{})
+ s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Configs: configs}, BuildCfg{})
- mainSections, err := s.Info.Param("mainSections")
- c.Assert(err, qt.IsNil)
- c.Assert(mainSections, qt.DeepEquals, []string{"sect"})
+ c.Assert(s.MainSections(), qt.DeepEquals, []string{"sect"})
- th := newTestHelper(s.Cfg, s.Fs, t)
+ th := newTestHelper(s.conf, s.Fs, t)
tests := []struct {
doc string
pluralAware bool
@@ -489,6 +595,7 @@ func doTestSectionNaming(t *testing.T, canonify, uglify, pluralize bool) {
func TestAbsURLify(t *testing.T) {
t.Parallel()
+ c := qt.New(t)
sources := [][2]string{
{filepath.FromSlash("sect/doc1.html"), "<!doctype html><html><head></head><body><a href=\"#frag1\">link</a></body></html>"},
{filepath.FromSlash("blue/doc2.html"), "---\nf: t\n---\n<!doctype html><html><body>more content</body></html>"},
@@ -502,14 +609,17 @@ func TestAbsURLify(t *testing.T) {
cfg.Set("canonifyURLs", canonify)
cfg.Set("baseURL", baseURL)
+ configs, err := loadTestConfigFromProvider(cfg)
+ c.Assert(err, qt.IsNil)
+
for _, src := range sources {
writeSource(t, fs, filepath.Join("content", src[0]), src[1])
}
writeSource(t, fs, filepath.Join("layouts", "blue/single.html"), templateWithURLAbs)
- s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{})
- th := newTestHelper(s.Cfg, s.Fs, t)
+ s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Configs: configs}, BuildCfg{})
+ th := newTestHelper(s.conf, s.Fs, t)
tests := []struct {
file, expected string
@@ -595,14 +705,17 @@ var weightedSources = [][2]string{
func TestOrderedPages(t *testing.T) {
t.Parallel()
+ c := qt.New(t)
cfg, fs := newTestCfg()
cfg.Set("baseURL", "http://auth/bub")
+ configs, err := loadTestConfigFromProvider(cfg)
+ c.Assert(err, qt.IsNil)
for _, src := range weightedSources {
writeSource(t, fs, filepath.Join("content", src[0]), src[1])
}
- s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{SkipRender: true})
+ s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Configs: configs}, BuildCfg{SkipRender: true})
if s.getPage(page.KindSection, "sect").Pages()[1].Title() != "Three" || s.getPage(page.KindSection, "sect").Pages()[2].Title() != "Four" {
t.Error("Pages in unexpected order.")
@@ -650,17 +763,15 @@ var groupedSources = [][2]string{
func TestGroupedPages(t *testing.T) {
t.Parallel()
- defer func() {
- if r := recover(); r != nil {
- fmt.Println("Recovered in f", r)
- }
- }()
+ c := qt.New(t)
cfg, fs := newTestCfg()
cfg.Set("baseURL", "http://auth/bub")
+ configs, err := loadTestConfigFromProvider(cfg)
+ c.Assert(err, qt.IsNil)
writeSourcesToSource(t, "content", fs, groupedSources...)
- s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{})
+ s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Configs: configs}, BuildCfg{})
rbysection, err := s.RegularPages().GroupBy(context.Background(), "Section", "desc")
if err != nil {
@@ -816,6 +927,8 @@ Front Matter with weighted tags and categories`
func TestWeightedTaxonomies(t *testing.T) {
t.Parallel()
+ c := qt.New(t)
+
sources := [][2]string{
{filepath.FromSlash("sect/doc1.md"), pageWithWeightedTaxonomies2},
{filepath.FromSlash("sect/doc2.md"), pageWithWeightedTaxonomies1},
@@ -830,9 +943,11 @@ func TestWeightedTaxonomies(t *testing.T) {
cfg.Set("baseURL", "http://auth/bub")
cfg.Set("taxonomies", taxonomies)
+ configs, err := loadTestConfigFromProvider(cfg)
+ c.Assert(err, qt.IsNil)
writeSourcesToSource(t, "content", fs, sources...)
- s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{})
+ s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Configs: configs}, BuildCfg{})
if s.Taxonomies()["tags"]["a"][0].Page.Title() != "foo" {
t.Errorf("Pages in unexpected order, 'foo' expected first, got '%v'", s.Taxonomies()["tags"]["a"][0].Page.Title())
@@ -882,8 +997,13 @@ func setupLinkingMockSite(t *testing.T) *Site {
})
cfg.Set("pluralizeListTitles", false)
cfg.Set("canonifyURLs", false)
+ configs, err := loadTestConfigFromProvider(cfg)
+ if err != nil {
+ t.Fatal(err)
+ }
+
writeSourcesToSource(t, "content", fs, sources...)
- return buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{})
+ return buildSingleSite(t, deps.DepsCfg{Fs: fs, Configs: configs}, BuildCfg{})
}
func TestRefLinking(t *testing.T) {