| 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 segments_test |
| 15 |
|
| 16 |
import ( |
| 17 |
"testing" |
| 18 |
|
| 19 |
qt "github.com/frankban/quicktest" |
| 20 |
"github.com/gohugoio/hugo/hugolib" |
| 21 |
) |
| 22 |
|
| 23 |
func TestSegments(t *testing.T) { |
| 24 |
files := ` |
| 25 |
-- hugo.toml -- |
| 26 |
baseURL = "https://example.org/" |
| 27 |
renderSegments = ["docs"] |
| 28 |
[languages] |
| 29 |
[languages.en] |
| 30 |
weight = 1 |
| 31 |
[languages.no] |
| 32 |
weight = 2 |
| 33 |
[languages.nb] |
| 34 |
weight = 3 |
| 35 |
[segments] |
| 36 |
[segments.docs] |
| 37 |
[[segments.docs.includes]] |
| 38 |
kind = "{home,taxonomy,term}" |
| 39 |
[[segments.docs.includes]] |
| 40 |
path = "{/docs,/docs/**}" |
| 41 |
[[segments.docs.excludes]] |
| 42 |
path = "/blog/**" |
| 43 |
[[segments.docs.excludes]] |
| 44 |
lang = "n*" |
| 45 |
output = "rss" |
| 46 |
[[segments.docs.excludes]] |
| 47 |
output = "json" |
| 48 |
-- layouts/single.html -- |
| 49 |
Single: {{ .Title }}|{{ .RelPermalink }}| |
| 50 |
-- layouts/list.html -- |
| 51 |
List: {{ .Title }}|{{ .RelPermalink }}| |
| 52 |
-- content/docs/_index.md -- |
| 53 |
-- content/docs/section1/_index.md -- |
| 54 |
-- content/docs/section1/page1.md -- |
| 55 |
--- |
| 56 |
title: "Docs Page 1" |
| 57 |
tags: ["tag1", "tag2"] |
| 58 |
--- |
| 59 |
-- content/blog/_index.md -- |
| 60 |
-- content/blog/section1/page1.md -- |
| 61 |
--- |
| 62 |
title: "Blog Page 1" |
| 63 |
tags: ["tag1", "tag2"] |
| 64 |
--- |
| 65 |
` |
| 66 |
|
| 67 |
b := hugolib.Test(t, files, hugolib.TestOptInfo()) |
| 68 |
b.AssertLogContains("deprecated") // lang => sites.matrix in v0.152.0 |
| 69 |
b.Assert(b.H.Configs.Base.RootConfig.RenderSegments, qt.DeepEquals, []string{"docs"}) |
| 70 |
|
| 71 |
b.AssertFileContent("public/docs/section1/page1/index.html", "Docs Page 1") |
| 72 |
b.AssertFileExists("public/blog/section1/page1/index.html", false) |
| 73 |
b.AssertFileExists("public/index.html", true) |
| 74 |
b.AssertFileExists("public/index.xml", true) |
| 75 |
b.AssertFileExists("public/no/index.html", true) |
| 76 |
b.AssertFileExists("public/no/index.xml", false) |
| 77 |
} |
| 78 |
|
| 79 |
// See issue 14939. |
| 80 |
func TestRenderSegmentsMergesHugoStatsJSON(t *testing.T) { |
| 81 |
files := ` |
| 82 |
-- hugo.toml -- |
| 83 |
baseURL = "https://example.org/" |
| 84 |
renderSegments = ["docs"] |
| 85 |
[build.buildStats] |
| 86 |
enable = true |
| 87 |
[segments] |
| 88 |
[segments.docs] |
| 89 |
[[segments.docs.includes]] |
| 90 |
path = "{/docs,/docs/**}" |
| 91 |
-- hugo_stats.json -- |
| 92 |
{ |
| 93 |
"htmlElements": { |
| 94 |
"tags": [ |
| 95 |
"section" |
| 96 |
], |
| 97 |
"classes": [ |
| 98 |
"from-previous-build" |
| 99 |
], |
| 100 |
"ids": [ |
| 101 |
"previous-id" |
| 102 |
] |
| 103 |
} |
| 104 |
} |
| 105 |
-- layouts/single.html -- |
| 106 |
<div id="docs-id" class="docs-class">{{ .Title }}</div> |
| 107 |
-- layouts/list.html -- |
| 108 |
<div id="list-id" class="list-class">{{ .Title }}</div> |
| 109 |
-- content/docs/section1/page1.md -- |
| 110 |
--- |
| 111 |
title: "Docs Page 1" |
| 112 |
--- |
| 113 |
-- content/blog/section1/page1.md -- |
| 114 |
--- |
| 115 |
title: "Blog Page 1" |
| 116 |
--- |
| 117 |
` |
| 118 |
|
| 119 |
b := hugolib.Test(t, files, hugolib.TestOptOsFs()) |
| 120 |
|
| 121 |
b.AssertFileContent("public/docs/section1/page1/index.html", "Docs Page 1") |
| 122 |
b.AssertFileExists("public/blog/section1/page1/index.html", false) |
| 123 |
|
| 124 |
b.AssertFileContent("hugo_stats.json", |
| 125 |
"from-previous-build", |
| 126 |
"previous-id", |
| 127 |
"section", |
| 128 |
"docs-class", |
| 129 |
) |
| 130 |
} |