Debian Codegraph @

hugolib/segments/segments_integration_test.go

raw
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 "strings"
18 "testing"
19
20 qt "github.com/frankban/quicktest"
21 "github.com/gohugoio/hugo/hugolib"
22 )
23
24 func TestSegments(t *testing.T) {
25 files := `
26 -- hugo.toml --
27 baseURL = "https://example.org/"
28 renderSegments = ["docs"]
29 [languages]
30 [languages.en]
31 weight = 1
32 [languages.no]
33 weight = 2
34 [languages.nb]
35 weight = 3
36 [segments]
37 [segments.docs]
38 [[segments.docs.includes]]
39 kind = "{home,taxonomy,term}"
40 [[segments.docs.includes]]
41 path = "{/docs,/docs/**}"
42 [[segments.docs.excludes]]
43 path = "/blog/**"
44 [[segments.docs.excludes]]
45 lang = "n*"
46 output = "rss"
47 [[segments.docs.excludes]]
48 output = "json"
49 -- layouts/single.html --
50 Single: {{ .Title }}|{{ .RelPermalink }}|
51 -- layouts/list.html --
52 List: {{ .Title }}|{{ .RelPermalink }}|
53 -- content/docs/_index.md --
54 -- content/docs/section1/_index.md --
55 -- content/docs/section1/page1.md --
56 ---
57 title: "Docs Page 1"
58 tags: ["tag1", "tag2"]
59 ---
60 -- content/blog/_index.md --
61 -- content/blog/section1/page1.md --
62 ---
63 title: "Blog Page 1"
64 tags: ["tag1", "tag2"]
65 ---
66 `
67
68 b := hugolib.Test(t, files, hugolib.TestOptInfo())
69 b.AssertLogContains("deprecated") // lang => sites.matrix in v0.152.0
70 b.Assert(b.H.Configs.Base.RootConfig.RenderSegments, qt.DeepEquals, []string{"docs"})
71
72 b.AssertFileContent("public/docs/section1/page1/index.html", "Docs Page 1")
73 b.AssertFileExists("public/blog/section1/page1/index.html", false)
74 b.AssertFileExists("public/index.html", true)
75 b.AssertFileExists("public/index.xml", true)
76 b.AssertFileExists("public/no/index.html", true)
77 b.AssertFileExists("public/no/index.xml", false)
78 }
79
80 // See issue 15024.
81 func TestSegmentsMultiple(t *testing.T) {
82 filesTemplate := `
83 -- hugo.toml --
84 renderSegments = SEGMENTS
85 disableKinds = ["home", "taxonomy", "term", "page"]
86 [outputs]
87 section = ['html', 'json']
88 [segments]
89 [segments.excludeallkinds]
90 [[segments.excludeallkinds.excludes]]
91 kind = "**"
92 [segments.blog]
93 [[segments.blog.includes]]
94 path = "{/blog,/blog/**}"
95 [[segments.blog.excludes]]
96 output = 'json'
97 [segments.news]
98 [[segments.news.includes]]
99 path = "{/news,/news/**}"
100 -- layouts/all.html --
101 {{ .Kind }}: {{ .Title }}|{{ .RelPermalink }}|
102 -- layouts/all.json --
103 {{ .Kind }}: {{ .Title }}|{{ .RelPermalink }}|
104 -- content/blog/_index.md --
105 -- content/blog/page1.md --
106 ---
107 title: "Blog Page 1"
108 tags: ["tag1", "tag2"]
109 ---
110 -- content/news/_index.md --
111 -- content/news/page1.md --
112 ---
113 title: "News Page 1"
114 tags: ["tag1", "tag2"]
115 ---
116 `
117 files := strings.ReplaceAll(filesTemplate, "SEGMENTS", `["excludeallkinds", "blog", "news"]`)
118
119 b := hugolib.Test(t, files, hugolib.TestOptInfo())
120
121 b.AssertPublishDir(`
122 blog/index.html
123 ! blog/index.json
124 news/index.html
125 news/index.json
126 `)
127
128 files = strings.ReplaceAll(filesTemplate, "SEGMENTS", `["excludeallkinds"]`)
129
130 b = hugolib.Test(t, files, hugolib.TestOptInfo())
131
132 b.AssertPublishDir(`
133 ! json
134 ! html
135 `)
136 }
137
138 // See issue 14939.
139 func TestRenderSegmentsMergesHugoStatsJSON(t *testing.T) {
140 files := `
141 -- hugo.toml --
142 baseURL = "https://example.org/"
143 renderSegments = ["docs"]
144 [build.buildStats]
145 enable = true
146 [segments]
147 [segments.docs]
148 [[segments.docs.includes]]
149 path = "{/docs,/docs/**}"
150 -- hugo_stats.json --
151 {
152 "htmlElements": {
153 "tags": [
154 "section"
155 ],
156 "classes": [
157 "from-previous-build"
158 ],
159 "ids": [
160 "previous-id"
161 ]
162 }
163 }
164 -- layouts/single.html --
165 <div id="docs-id" class="docs-class">{{ .Title }}</div>
166 -- layouts/list.html --
167 <div id="list-id" class="list-class">{{ .Title }}</div>
168 -- content/docs/section1/page1.md --
169 ---
170 title: "Docs Page 1"
171 ---
172 -- content/blog/section1/page1.md --
173 ---
174 title: "Blog Page 1"
175 ---
176 `
177
178 b := hugolib.Test(t, files, hugolib.TestOptOsFs())
179
180 b.AssertFileContent("public/docs/section1/page1/index.html", "Docs Page 1")
181 b.AssertFileExists("public/blog/section1/page1/index.html", false)
182
183 b.AssertFileContent("hugo_stats.json",
184 "from-previous-build",
185 "previous-id",
186 "section",
187 "docs-class",
188 )
189 }