Debian Codegraph @

hugolib/disableKinds_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 package hugolib
14
15 import (
16 "fmt"
17 "testing"
18
19 qt "github.com/frankban/quicktest"
20 "github.com/gohugoio/hugo/resources/kinds"
21 )
22
23 func TestDisableKinds(t *testing.T) {
24 filesForDisabledKind := func(disableKind string) string {
25 return fmt.Sprintf(`
26 -- hugo.toml --
27 baseURL = "http://example.com/blog"
28 enableRobotsTXT = true
29 ignoreErrors = ["error-disable-taxonomy"]
30 disableKinds = ["%s"]
31 -- layouts/single.html --
32 single
33 -- content/sect/page.md --
34 ---
35 title: Page
36 categories: ["mycat"]
37 tags: ["mytag"]
38 ---
39 -- content/sect/no-list.md --
40 ---
41 title: No List
42 build:
43 list: false
44 ---
45 -- content/sect/no-render.md --
46 ---
47 title: No List
48 build:
49 render: false
50 ---
51 -- content/sect/no-render-link.md --
52 ---
53 title: No Render Link
54 aliases: ["/link-alias"]
55 build:
56 render: link
57 ---
58 -- content/sect/no-publishresources/index.md --
59 ---
60 title: No Publish Resources
61 build:
62 publishResources: false
63 ---
64 -- content/sect/headlessbundle/index.md --
65 ---
66 title: Headless
67 headless: true
68 ---
69 -- content/headless-local/_index.md --
70 ---
71 title: Headless Local Lists
72 cascade:
73 build:
74 render: false
75 list: local
76 publishResources: false
77 ---
78 -- content/headless-local/headless-local-page.md --
79 ---
80 title: Headless Local Page
81 ---
82 -- content/headless-local/sub/_index.md --
83 ---
84 title: Headless Local Lists Sub
85 ---
86 -- content/headless-local/sub/headless-local-sub-page.md --
87 ---
88 title: Headless Local Sub Page
89 ---
90 -- content/sect/headlessbundle/data.json --
91 DATA
92 -- content/sect/no-publishresources/data.json --
93 DATA
94 `, disableKind)
95 }
96 t.Run("Disable "+kinds.KindPage, func(t *testing.T) {
97 files := filesForDisabledKind(kinds.KindPage)
98 b := Test(t, files)
99 s := b.H.Sites[0]
100 b.AssertFileExists("public/sect/page/index.html", false)
101 b.AssertFileExists("public/categories/mycat/index.html", false)
102 b.Assert(len(s.Taxonomies()["categories"]), qt.Equals, 0)
103 })
104
105 t.Run("Disable "+kinds.KindTerm, func(t *testing.T) {
106 files := filesForDisabledKind(kinds.KindTerm)
107 b := Test(t, files)
108 s := b.H.Sites[0]
109 b.AssertFileExists("public/categories/index.html", false)
110 b.AssertFileExists("public/categories/mycat/index.html", false)
111 b.Assert(len(s.Taxonomies()["categories"]), qt.Equals, 0)
112 })
113
114 t.Run("Disable "+kinds.KindTaxonomy, func(t *testing.T) {
115 files := filesForDisabledKind(kinds.KindTaxonomy)
116 b := Test(t, files)
117 s := b.H.Sites[0]
118 b.AssertFileExists("public/categories/mycat/index.html", false)
119 b.AssertFileExists("public/categories/index.html", false)
120 b.Assert(len(s.Taxonomies()["categories"]), qt.Equals, 1)
121 })
122
123 t.Run("Disable "+kinds.KindHome, func(t *testing.T) {
124 files := filesForDisabledKind(kinds.KindHome)
125 b := Test(t, files)
126 b.AssertFileExists("public/index.html", false)
127 })
128
129 t.Run("Disable "+kinds.KindSection, func(t *testing.T) {
130 files := filesForDisabledKind(kinds.KindSection)
131 b := Test(t, files)
132 b.AssertFileExists("public/sect/index.html", false)
133 b.AssertFileContent("public/sitemap.xml", "sitemap")
134 b.AssertFileContent("public/index.xml", "rss")
135 })
136
137 t.Run("Disable "+kinds.KindRSS, func(t *testing.T) {
138 files := filesForDisabledKind(kinds.KindRSS)
139 b := Test(t, files)
140 b.AssertFileExists("public/index.xml", false)
141 })
142
143 t.Run("Disable "+kinds.KindSitemap, func(t *testing.T) {
144 files := filesForDisabledKind(kinds.KindSitemap)
145 b := Test(t, files)
146 b.AssertFileExists("public/sitemap.xml", false)
147 })
148
149 t.Run("Disable "+kinds.KindStatus404, func(t *testing.T) {
150 files := filesForDisabledKind(kinds.KindStatus404)
151 b := Test(t, files)
152 b.AssertFileExists("public/404.html", false)
153 })
154
155 t.Run("Disable "+kinds.KindRobotsTXT, func(t *testing.T) {
156 files := filesForDisabledKind(kinds.KindRobotsTXT)
157 b := Test(t, files)
158 b.AssertFileExists("public/robots.txt", false)
159 })
160
161 t.Run("Headless bundle", func(t *testing.T) {
162 files := filesForDisabledKind("")
163 b := Test(t, files)
164 b.AssertFileExists("public/sect/headlessbundle/index.html", false)
165 b.AssertFileExists("public/sect/headlessbundle/data.json", true)
166 })
167
168 t.Run("Build config, no list", func(t *testing.T) {
169 files := filesForDisabledKind("")
170 b := Test(t, files)
171 b.AssertFileExists("public/sect/no-list/index.html", true)
172 })
173
174 t.Run("Build config, local list", func(t *testing.T) {
175 files := filesForDisabledKind("")
176 b := Test(t, files)
177 // Assert that the pages are not rendered to disk, as list:local implies.
178 b.AssertFileExists("public/headless-local/index.html", false)
179 b.AssertFileExists("public/headless-local/headless-local-page/index.html", false)
180 b.AssertFileExists("public/headless-local/sub/index.html", false)
181 b.AssertFileExists("public/headless-local/sub/headless-local-sub-page/index.html", false)
182 })
183
184 t.Run("Build config, no render", func(t *testing.T) {
185 files := filesForDisabledKind("")
186 b := Test(t, files)
187 b.AssertFileExists("public/sect/no-render/index.html", false)
188 })
189
190 t.Run("Build config, no render link", func(t *testing.T) {
191 files := filesForDisabledKind("")
192 b := Test(t, files)
193 b.AssertFileExists("public/sect/no-render-link/index.html", false)
194 b.AssertFileExists("public/link-alias/index.html", false)
195 })
196
197 t.Run("Build config, no publish resources", func(t *testing.T) {
198 files := filesForDisabledKind("")
199 b := Test(t, files)
200 b.AssertFileExists("public/sect/no-publishresources/index.html", true)
201 b.AssertFileExists("public/sect/no-publishresources/data.json", false)
202 })
203 }
204
205 // https://github.com/gohugoio/hugo/issues/6897#issuecomment-587947078
206 func TestDisableRSSWithRSSInCustomOutputs(t *testing.T) {
207 files := `
208 -- hugo.toml --
209 disableKinds = ["term", "taxonomy", "RSS"]
210 [outputs]
211 home = [ "HTML", "RSS" ]
212 -- layouts/home.html --
213 Home
214 `
215 b := Test(t, files)
216
217 // The config above is a little conflicting, but it exists in the real world.
218 // In Hugo 0.65 we consolidated the code paths and made RSS a pure output format,
219 // but we should make sure to not break existing sites.
220 b.AssertFileExists("public/index.xml", false)
221 }
222
223 func TestBundleNoPublishResources(t *testing.T) {
224 files := `
225 -- hugo.toml --
226 baseURL = "http://example.com"
227 -- layouts/home.html --
228 {{ $bundle := site.GetPage "section/bundle-false" }}
229 {{ $data1 := $bundle.Resources.GetMatch "data1*" }}
230 Data1: {{ $data1.RelPermalink }}
231 -- content/section/bundle-false/index.md --
232 ---
233 title: BundleFalse
234 build:
235 publishResources: false
236 ---
237 -- content/section/bundle-false/data1.json --
238 Some data1
239 -- content/section/bundle-false/data2.json --
240 Some data2
241 -- content/section/bundle-true/index.md --
242 ---
243 title: BundleTrue
244 ---
245 -- content/section/bundle-true/data3.json --
246 Some data 3
247 `
248 b := Test(t, files)
249 b.AssertFileContent("public/index.html", `Data1: /section/bundle-false/data1.json`)
250 b.AssertFileContent("public/section/bundle-false/data1.json", `Some data1`)
251 b.AssertFileExists("public/section/bundle-false/data2.json", false)
252 b.AssertFileContent("public/section/bundle-true/data3.json", `Some data 3`)
253 }
254
255 func TestNoRenderAndNoPublishResources(t *testing.T) {
256 files := `
257 -- hugo.toml --
258 baseURL = "http://example.com"
259 -- layouts/home.html --
260 {{ $page := site.GetPage "sect/no-render" }}
261 {{ $sect := site.GetPage "sect-no-render" }}
262
263 Page: {{ $page.Title }}|RelPermalink: {{ $page.RelPermalink }}|Outputs: {{ len $page.OutputFormats }}
264 Section: {{ $sect.Title }}|RelPermalink: {{ $sect.RelPermalink }}|Outputs: {{ len $sect.OutputFormats }}
265 -- content/sect-no-render/_index.md --
266 ---
267 title: MySection
268 build:
269 render: false
270 publishResources: false
271 ---
272 -- content/sect/no-render.md --
273 ---
274 title: MyPage
275 build:
276 render: false
277 publishResources: false
278 ---
279 `
280 b := Test(t, files)
281
282 b.AssertFileContent("public/index.html", `
283 Page: MyPage|RelPermalink: |Outputs: 0
284 Section: MySection|RelPermalink: |Outputs: 0
285 `)
286
287 b.AssertFileExists("public/sect/no-render/index.html", false)
288 b.AssertFileExists("public/sect-no-render/index.html", false)
289 }
290
291 func TestDisableOneOfThreeLanguages(t *testing.T) {
292 files := `
293 -- hugo.toml --
294 baseURL = "https://example.com"
295 defaultContentLanguage = "en"
296 defaultContentLanguageInSubdir = true
297 [languages]
298 [languages.en]
299 weight = 1
300 title = "English"
301 [languages.nn]
302 weight = 2
303 title = "Nynorsk"
304 disabled = true
305 [languages.nb]
306 weight = 3
307 title = "Bokmål"
308 -- content/p1.nn.md --
309 ---
310 title: "Page 1 nn"
311 ---
312 -- content/p1.nb.md --
313 ---
314 title: "Page 1 nb"
315 ---
316 -- content/p1.en.md --
317 ---
318 title: "Page 1 en"
319 ---
320 -- content/p2.nn.md --
321 ---
322 title: "Page 2 nn"
323 ---
324 -- layouts/single.html --
325 {{ .Title }}
326 `
327 b := Test(t, files)
328
329 b.Assert(len(b.H.Sites), qt.Equals, 2)
330 b.AssertFileContent("public/en/p1/index.html", "Page 1 en")
331 b.AssertFileContent("public/nb/p1/index.html", "Page 1 nb")
332
333 b.AssertFileExists("public/en/p2/index.html", false)
334 b.AssertFileExists("public/nn/p1/index.html", false)
335 b.AssertFileExists("public/nn/p2/index.html", false)
336 }
337
338 // Issue #14550
339 // disableKinds = [] after [taxonomies] is inside the taxonomies TOML table,
340 // creating a phantom taxonomy that causes .Ancestors to hang.
341 func TestDisableKindsEmptySliceAncestors(t *testing.T) {
342 files := `
343 -- hugo.toml --
344 baseURL = "http://example.com/"
345 title = "Bug repro"
346 [taxonomies]
347 tag = "tags"
348 disableKinds = []
349 -- content/posts/hello.md --
350 ---
351 title: Hello
352 tags: [demo]
353 ---
354 Hello.
355 -- layouts/page.html --
356 Ancestors: {{ len .Ancestors }}|{{ range .Ancestors }}{{ .Kind }}|{{ end }}
357 -- layouts/list.html --
358 {{ .Title }}
359 `
360 b := Test(t, files)
361 b.AssertFileContent("public/posts/hello/index.html", "Ancestors: 2|section|home|")
362 }