| 1 |
// Copyright 2025 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package hugolib |
| 15 |
|
| 16 |
import ( |
| 17 |
"strings" |
| 18 |
"testing" |
| 19 |
) |
| 20 |
|
| 21 |
func TestNextInSectionNested(t *testing.T) { |
| 22 |
t.Parallel() |
| 23 |
|
| 24 |
files := ` |
| 25 |
-- hugo.toml -- |
| 26 |
baseURL = "https://example.com/" |
| 27 |
-- content/blog/page1.md -- |
| 28 |
--- |
| 29 |
weight: 1 |
| 30 |
--- |
| 31 |
-- content/blog/page2.md -- |
| 32 |
--- |
| 33 |
weight: 2 |
| 34 |
--- |
| 35 |
-- content/blog/cool/_index.md -- |
| 36 |
--- |
| 37 |
weight: 1 |
| 38 |
--- |
| 39 |
-- content/blog/cool/cool1.md -- |
| 40 |
--- |
| 41 |
weight: 1 |
| 42 |
--- |
| 43 |
-- content/blog/cool/cool2.md -- |
| 44 |
--- |
| 45 |
weight: 2 |
| 46 |
--- |
| 47 |
-- content/root1.md -- |
| 48 |
--- |
| 49 |
weight: 1 |
| 50 |
--- |
| 51 |
-- content/root2.md -- |
| 52 |
--- |
| 53 |
weight: 2 |
| 54 |
--- |
| 55 |
-- layouts/single.html -- |
| 56 |
Prev: {{ with .PrevInSection }}{{ .RelPermalink }}{{ end }}| |
| 57 |
Next: {{ with .NextInSection }}{{ .RelPermalink }}{{ end }}| |
| 58 |
` |
| 59 |
|
| 60 |
b := Test(t, files) |
| 61 |
|
| 62 |
b.AssertFileContent("public/root1/index.html", |
| 63 |
"Prev: /root2/|", "Next: |") |
| 64 |
b.AssertFileContent("public/root2/index.html", |
| 65 |
"Prev: |", "Next: /root1/|") |
| 66 |
b.AssertFileContent("public/blog/page1/index.html", |
| 67 |
"Prev: /blog/page2/|", "Next: |") |
| 68 |
b.AssertFileContent("public/blog/page2/index.html", |
| 69 |
"Prev: |", "Next: /blog/page1/|") |
| 70 |
b.AssertFileContent("public/blog/cool/cool1/index.html", |
| 71 |
"Prev: /blog/cool/cool2/|", "Next: |") |
| 72 |
b.AssertFileContent("public/blog/cool/cool2/index.html", |
| 73 |
"Prev: |", "Next: /blog/cool/cool1/|") |
| 74 |
} |
| 75 |
|
| 76 |
func TestSectionEntries(t *testing.T) { |
| 77 |
t.Parallel() |
| 78 |
|
| 79 |
files := ` |
| 80 |
-- hugo.toml -- |
| 81 |
baseURL = "https://example.com/" |
| 82 |
-- content/myfirstsection/p1.md -- |
| 83 |
--- |
| 84 |
title: "P1" |
| 85 |
--- |
| 86 |
P1 |
| 87 |
-- content/a/b/c/_index.md -- |
| 88 |
--- |
| 89 |
title: "C" |
| 90 |
--- |
| 91 |
C |
| 92 |
-- content/a/b/c/mybundle/index.md -- |
| 93 |
--- |
| 94 |
title: "My Bundle" |
| 95 |
--- |
| 96 |
-- layouts/list.html -- |
| 97 |
Kind: {{ .Kind }}|RelPermalink: {{ .RelPermalink }}|SectionsPath: {{ .SectionsPath }}|SectionsEntries: {{ .SectionsEntries }}|Len: {{ len .SectionsEntries }}| |
| 98 |
-- layouts/single.html -- |
| 99 |
Kind: {{ .Kind }}|RelPermalink: {{ .RelPermalink }}|SectionsPath: {{ .SectionsPath }}|SectionsEntries: {{ .SectionsEntries }}|Len: {{ len .SectionsEntries }}| |
| 100 |
` |
| 101 |
|
| 102 |
b := Test(t, files) |
| 103 |
|
| 104 |
b.AssertFileContent("public/myfirstsection/p1/index.html", "RelPermalink: /myfirstsection/p1/|SectionsPath: /myfirstsection|SectionsEntries: [myfirstsection]|Len: 1") |
| 105 |
b.AssertFileContent("public/a/b/c/index.html", "RelPermalink: /a/b/c/|SectionsPath: /a/b/c|SectionsEntries: [a b c]|Len: 3") |
| 106 |
b.AssertFileContent("public/a/b/c/mybundle/index.html", "Kind: page|RelPermalink: /a/b/c/mybundle/|SectionsPath: /a/b/c|SectionsEntries: [a b c]|Len: 3") |
| 107 |
b.AssertFileContent("public/index.html", "Kind: home|RelPermalink: /|SectionsPath: /|SectionsEntries: []|Len: 0") |
| 108 |
} |
| 109 |
|
| 110 |
func TestParentWithPageOverlap(t *testing.T) { |
| 111 |
files := ` |
| 112 |
-- hugo.toml -- |
| 113 |
baseURL = "https://example.com/" |
| 114 |
-- content/docs/_index.md -- |
| 115 |
-- content/docs/logs/_index.md -- |
| 116 |
-- content/docs/logs/sdk.md -- |
| 117 |
-- content/docs/logs/sdk_exporters/stdout.md -- |
| 118 |
-- layouts/list.html -- |
| 119 |
{{ .RelPermalink }}|{{ with .Parent}}{{ .RelPermalink }}{{ end }}| |
| 120 |
-- layouts/single.html -- |
| 121 |
{{ .RelPermalink }}|{{ with .Parent}}{{ .RelPermalink }}{{ end }}| |
| 122 |
|
| 123 |
` |
| 124 |
b := Test(t, files) |
| 125 |
|
| 126 |
b.AssertFileContent("public/index.html", "/||") |
| 127 |
b.AssertFileContent("public/docs/index.html", "/docs/|/|") |
| 128 |
b.AssertFileContent("public/docs/logs/index.html", "/docs/logs/|/docs/|") |
| 129 |
b.AssertFileContent("public/docs/logs/sdk/index.html", "/docs/logs/sdk/|/docs/logs/|") |
| 130 |
b.AssertFileContent("public/docs/logs/sdk_exporters/stdout/index.html", "/docs/logs/sdk_exporters/stdout/|/docs/logs/|") |
| 131 |
} |
| 132 |
|
| 133 |
func TestNestedSectionsEmpty(t *testing.T) { |
| 134 |
t.Parallel() |
| 135 |
|
| 136 |
files := ` |
| 137 |
-- hugo.toml -- |
| 138 |
baseURL = "https://example.com/" |
| 139 |
-- content/a/b/c/_index.md -- |
| 140 |
--- |
| 141 |
title: "C" |
| 142 |
--- |
| 143 |
-- layouts/all.html -- |
| 144 |
All: {{ .Title }}|{{ .Kind }}| |
| 145 |
` |
| 146 |
b := Test(t, files) |
| 147 |
|
| 148 |
b.AssertFileContent("public/a/index.html", "All: As|section|") |
| 149 |
b.AssertFileExists("public/a/b/index.html", false) |
| 150 |
b.AssertFileContent("public/a/b/c/index.html", "All: C|section|") |
| 151 |
} |
| 152 |
|
| 153 |
// See issue 15046. |
| 154 |
func TestSectionAndSiblingPageURLs(t *testing.T) { |
| 155 |
t.Parallel() |
| 156 |
|
| 157 |
files := ` |
| 158 |
-- hugo.toml -- |
| 159 |
disableKinds = ['home','rss','sitemap','taxonomy','term'] |
| 160 |
uglyURLs = false |
| 161 |
-- content/s1/p1.md -- |
| 162 |
--- |
| 163 |
title: content/s1/p1.md |
| 164 |
--- |
| 165 |
-- content/s1.md -- |
| 166 |
--- |
| 167 |
title: content/s1.md |
| 168 |
--- |
| 169 |
-- layouts/all.html -- |
| 170 |
{{ .Title }} |
| 171 |
` |
| 172 |
|
| 173 |
b := Test(t, files) |
| 174 |
|
| 175 |
b.AssertFileContent("public/s1/index.html", "content/s1.md") |
| 176 |
b.AssertFileContent("public/s1/p1/index.html", "content/s1/p1.md") |
| 177 |
|
| 178 |
files = strings.ReplaceAll(files, "uglyURLs = false", "uglyURLs = true") |
| 179 |
|
| 180 |
b = Test(t, files) |
| 181 |
|
| 182 |
b.AssertFileContent("public/s1.html", "content/s1.md") |
| 183 |
b.AssertFileContent("public/s1/p1.html", "content/s1/p1.md") |
| 184 |
} |