disableKinds_test.go - hugo - [fork] hugo port for 9front
HTML git clone https://git.drkhsh.at/hugo.git
DIR Log
DIR Files
DIR Refs
DIR Submodules
DIR README
DIR LICENSE
---
disableKinds_test.go (14536B)
---
1 // Copyright 2019 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 "github.com/gohugoio/hugo/resources/page"
22 )
23
24 func TestDisable(t *testing.T) {
25 c := qt.New(t)
26
27 newSitesBuilder := func(c *qt.C, disableKind string) *sitesBuilder {
28 config := fmt.Sprintf(`
29 baseURL = "http://example.com/blog"
30 enableRobotsTXT = true
31 ignoreErrors = ["error-disable-taxonomy"]
32 disableKinds = [%q]
33 `, disableKind)
34
35 b := newTestSitesBuilder(c)
36 b.WithTemplatesAdded("_default/single.html", `single`)
37 b.WithConfigFile("toml", config).WithContent("sect/page.md", `
38 ---
39 title: Page
40 categories: ["mycat"]
41 tags: ["mytag"]
42 ---
43
44 `, "sect/no-list.md", `
45 ---
46 title: No List
47 build:
48 list: false
49 ---
50
51 `, "sect/no-render.md", `
52 ---
53 title: No List
54 build:
55 render: false
56 ---
57 `,
58 "sect/no-render-link.md", `
59 ---
60 title: No Render Link
61 aliases: ["/link-alias"]
62 build:
63 render: link
64 ---
65 `,
66 "sect/no-publishresources/index.md", `
67 ---
68 title: No Publish Resources
69 build:
70 publishResources: false
71 ---
72
73 `, "sect/headlessbundle/index.md", `
74 ---
75 title: Headless
76 headless: true
77 ---
78
79
80 `, "headless-local/_index.md", `
81 ---
82 title: Headless Local Lists
83 cascade:
84 build:
85 render: false
86 list: local
87 publishResources: false
88 ---
89
90 `, "headless-local/headless-local-page.md", "---\ntitle: Headless Local Page\n---",
91 "headless-local/sub/_index.md", `
92 ---
93 title: Headless Local Lists Sub
94 ---
95
96 `, "headless-local/sub/headless-local-sub-page.md", "---\ntitle: Headless Local Sub Page\n---",
97 )
98
99 b.WithSourceFile("content/sect/headlessbundle/data.json", "DATA")
100 b.WithSourceFile("content/sect/no-publishresources/data.json", "DATA")
101
102 return b
103 }
104
105 getPage := func(b *sitesBuilder, ref string) page.Page {
106 b.Helper()
107 p, err := b.H.Sites[0].getPage(nil, ref)
108 b.Assert(err, qt.IsNil)
109 return p
110 }
111
112 getPageInSitePages := func(b *sitesBuilder, ref string) page.Page {
113 b.Helper()
114 for _, pages := range []page.Pages{b.H.Sites[0].Pages(), b.H.Sites[0].RegularPages()} {
115 for _, p := range pages {
116 if ref == p.Path() {
117 return p
118 }
119 }
120 }
121 return nil
122 }
123
124 getPageInPagePages := func(p page.Page, ref string, pageCollections ...page.Pages) page.Page {
125 if len(pageCollections) == 0 {
126 pageCollections = []page.Pages{p.Pages(), p.RegularPages(), p.RegularPagesRecursive(), p.Sections()}
127 }
128 for _, pages := range pageCollections {
129 for _, p := range pages {
130 if ref == p.Path() {
131 return p
132 }
133 }
134 }
135 return nil
136 }
137
138 disableKind := kinds.KindPage
139 c.Run("Disable "+disableKind, func(c *qt.C) {
140 b := newSitesBuilder(c, disableKind)
141 b.Build(BuildCfg{})
142 s := b.H.Sites[0]
143 b.Assert(getPage(b, "/sect/page.md"), qt.IsNil)
144 b.Assert(b.CheckExists("public/sect/page/index.html"), qt.Equals, false)
145 b.Assert(getPageInSitePages(b, "/sect/page.md"), qt.IsNil)
146 b.Assert(getPageInPagePages(getPage(b, "/"), "/sect/page.md"), qt.IsNil)
147
148 // Also check the side effects
149 b.Assert(b.CheckExists("public/categories/mycat/index.html"), qt.Equals, false)
150 b.Assert(len(s.Taxonomies()["categories"]), qt.Equals, 0)
151 })
152
153 disableKind = kinds.KindTerm
154 c.Run("Disable "+disableKind, func(c *qt.C) {
155 b := newSitesBuilder(c, disableKind)
156 b.Build(BuildCfg{})
157 s := b.H.Sites[0]
158 b.Assert(b.CheckExists("public/categories/index.html"), qt.Equals, true)
159 b.Assert(b.CheckExists("public/categories/mycat/index.html"), qt.Equals, false)
160 b.Assert(len(s.Taxonomies()["categories"]), qt.Equals, 0)
161 b.Assert(getPage(b, "/categories"), qt.Not(qt.IsNil))
162 b.Assert(getPage(b, "/categories/mycat"), qt.IsNil)
163 })
164
165 disableKind = kinds.KindTaxonomy
166 c.Run("Disable "+disableKind, func(c *qt.C) {
167 b := newSitesBuilder(c, disableKind)
168 b.Build(BuildCfg{})
169 s := b.H.Sites[0]
170 b.Assert(b.CheckExists("public/categories/mycat/index.html"), qt.Equals, true)
171 b.Assert(b.CheckExists("public/categories/index.html"), qt.Equals, false)
172 b.Assert(len(s.Taxonomies()["categories"]), qt.Equals, 1)
173 b.Assert(getPage(b, "/categories/mycat"), qt.Not(qt.IsNil))
174 categories := getPage(b, "/categories")
175 b.Assert(categories, qt.Not(qt.IsNil))
176 b.Assert(categories.RelPermalink(), qt.Equals, "")
177 b.Assert(getPageInSitePages(b, "/categories"), qt.IsNil)
178 b.Assert(getPageInPagePages(getPage(b, "/"), "/categories"), qt.IsNil)
179 })
180
181 disableKind = kinds.KindHome
182 c.Run("Disable "+disableKind, func(c *qt.C) {
183 b := newSitesBuilder(c, disableKind)
184 b.Build(BuildCfg{})
185 b.Assert(b.CheckExists("public/index.html"), qt.Equals, false)
186 home := getPage(b, "/")
187 b.Assert(home, qt.Not(qt.IsNil))
188 b.Assert(home.RelPermalink(), qt.Equals, "")
189 b.Assert(getPageInSitePages(b, "/"), qt.IsNil)
190 b.Assert(getPageInPagePages(home, "/"), qt.IsNil)
191 b.Assert(getPage(b, "/sect/page.md"), qt.Not(qt.IsNil))
192 })
193
194 disableKind = kinds.KindSection
195 c.Run("Disable "+disableKind, func(c *qt.C) {
196 b := newSitesBuilder(c, disableKind)
197 b.Build(BuildCfg{})
198 b.Assert(b.CheckExists("public/sect/index.html"), qt.Equals, false)
199 sect := getPage(b, "/sect")
200 b.Assert(sect, qt.Not(qt.IsNil))
201 b.Assert(sect.RelPermalink(), qt.Equals, "")
202 b.Assert(getPageInSitePages(b, "/sect"), qt.IsNil)
203 home := getPage(b, "/")
204 b.Assert(getPageInPagePages(home, "/sect"), qt.IsNil)
205 b.Assert(home.OutputFormats(), qt.HasLen, 2)
206 page := getPage(b, "/sect/page.md")
207 b.Assert(page, qt.Not(qt.IsNil))
208 b.Assert(page.CurrentSection(), qt.Equals, sect)
209 b.Assert(getPageInPagePages(sect, "/sect/page"), qt.Not(qt.IsNil))
210 b.AssertFileContent("public/sitemap.xml", "sitemap")
211 b.AssertFileContent("public/index.xml", "rss")
212 })
213
214 disableKind = kinds.KindRSS
215 c.Run("Disable "+disableKind, func(c *qt.C) {
216 b := newSitesBuilder(c, disableKind)
217 b.Build(BuildCfg{})
218 b.Assert(b.CheckExists("public/index.xml"), qt.Equals, false)
219 home := getPage(b, "/")
220 b.Assert(home.OutputFormats(), qt.HasLen, 1)
221 })
222
223 disableKind = kinds.KindSitemap
224 c.Run("Disable "+disableKind, func(c *qt.C) {
225 b := newSitesBuilder(c, disableKind)
226 b.Build(BuildCfg{})
227 b.Assert(b.CheckExists("public/sitemap.xml"), qt.Equals, false)
228 })
229
230 disableKind = kinds.KindStatus404
231 c.Run("Disable "+disableKind, func(c *qt.C) {
232 b := newSitesBuilder(c, disableKind)
233 b.Build(BuildCfg{})
234 b.Assert(b.CheckExists("public/404.html"), qt.Equals, false)
235 })
236
237 disableKind = kinds.KindRobotsTXT
238 c.Run("Disable "+disableKind, func(c *qt.C) {
239 b := newSitesBuilder(c, disableKind)
240 b.WithTemplatesAdded("robots.txt", "myrobots")
241 b.Build(BuildCfg{})
242 b.Assert(b.CheckExists("public/robots.txt"), qt.Equals, false)
243 })
244
245 c.Run("Headless bundle", func(c *qt.C) {
246 b := newSitesBuilder(c, disableKind)
247 b.Build(BuildCfg{})
248 b.Assert(b.CheckExists("public/sect/headlessbundle/index.html"), qt.Equals, false)
249 b.Assert(b.CheckExists("public/sect/headlessbundle/data.json"), qt.Equals, true)
250 bundle := getPage(b, "/sect/headlessbundle/index.md")
251 b.Assert(bundle, qt.Not(qt.IsNil))
252 b.Assert(bundle.RelPermalink(), qt.Equals, "")
253 resource := bundle.Resources()[0]
254 b.Assert(resource.RelPermalink(), qt.Equals, "/blog/sect/headlessbundle/data.json")
255 b.Assert(bundle.OutputFormats(), qt.HasLen, 0)
256 b.Assert(bundle.AlternativeOutputFormats(), qt.HasLen, 0)
257 })
258
259 c.Run("Build config, no list", func(c *qt.C) {
260 b := newSitesBuilder(c, disableKind)
261 b.Build(BuildCfg{})
262 ref := "/sect/no-list.md"
263 b.Assert(b.CheckExists("public/sect/no-list/index.html"), qt.Equals, true)
264 p := getPage(b, ref)
265 b.Assert(p, qt.Not(qt.IsNil))
266 b.Assert(p.RelPermalink(), qt.Equals, "/blog/sect/no-list/")
267 b.Assert(getPageInSitePages(b, ref), qt.IsNil)
268 sect := getPage(b, "/sect")
269 b.Assert(getPageInPagePages(sect, ref), qt.IsNil)
270 })
271
272 c.Run("Build config, local list", func(c *qt.C) {
273 b := newSitesBuilder(c, disableKind)
274 b.Build(BuildCfg{})
275 ref := "/headless-local"
276 sect := getPage(b, ref)
277 b.Assert(sect, qt.Not(qt.IsNil))
278 b.Assert(getPageInSitePages(b, ref), qt.IsNil)
279
280 b.Assert(getPageInSitePages(b, "/headless-local"), qt.IsNil)
281 b.Assert(getPageInSitePages(b, "/headless-local/headless-local-page"), qt.IsNil)
282
283 localPageRef := ref + "/headless-local-page"
284
285 b.Assert(getPageInPagePages(sect, localPageRef, sect.RegularPages()), qt.Not(qt.IsNil))
286 b.Assert(getPageInPagePages(sect, localPageRef, sect.RegularPagesRecursive()), qt.Not(qt.IsNil))
287 b.Assert(getPageInPagePages(sect, localPageRef, sect.Pages()), qt.Not(qt.IsNil))
288
289 ref = "/headless-local/sub"
290
291 sect = getPage(b, ref)
292 b.Assert(sect, qt.Not(qt.IsNil))
293
294 localPageRef = ref + "/headless-local-sub-page"
295 b.Assert(getPageInPagePages(sect, localPageRef), qt.Not(qt.IsNil))
296 })
297
298 c.Run("Build config, no render", func(c *qt.C) {
299 b := newSitesBuilder(c, disableKind)
300 b.Build(BuildCfg{})
301 ref := "/sect/no-render"
302 b.Assert(b.CheckExists("public/sect/no-render/index.html"), qt.Equals, false)
303 p := getPage(b, ref)
304 b.Assert(p, qt.Not(qt.IsNil))
305 b.Assert(p.RelPermalink(), qt.Equals, "")
306 b.Assert(p.OutputFormats(), qt.HasLen, 0)
307 b.Assert(getPageInSitePages(b, ref), qt.Not(qt.IsNil))
308 sect := getPage(b, "/sect")
309 b.Assert(getPageInPagePages(sect, ref), qt.Not(qt.IsNil))
310 })
311
312 c.Run("Build config, no render link", func(c *qt.C) {
313 b := newSitesBuilder(c, disableKind)
314 b.Build(BuildCfg{})
315 ref := "/sect/no-render-link"
316 b.Assert(b.CheckExists("public/sect/no-render/index.html"), qt.Equals, false)
317 p := getPage(b, ref)
318 b.Assert(p, qt.Not(qt.IsNil))
319 b.Assert(p.RelPermalink(), qt.Equals, "/blog/sect/no-render-link/")
320 b.Assert(p.OutputFormats(), qt.HasLen, 1)
321 b.Assert(getPageInSitePages(b, ref), qt.Not(qt.IsNil))
322 sect := getPage(b, "/sect")
323 b.Assert(getPageInPagePages(sect, ref), qt.Not(qt.IsNil))
324
325 // https://github.com/gohugoio/hugo/issues/7832
326 // It should still render any aliases.
327 b.AssertFileContent("public/link-alias/index.html", "refresh")
328 })
329
330 c.Run("Build config, no publish resources", func(c *qt.C) {
331 b := newSitesBuilder(c, disableKind)
332 b.Build(BuildCfg{})
333 b.Assert(b.CheckExists("public/sect/no-publishresources/index.html"), qt.Equals, true)
334 b.Assert(b.CheckExists("public/sect/no-publishresources/data.json"), qt.Equals, false)
335 bundle := getPage(b, "/sect/no-publishresources/index.md")
336 b.Assert(bundle, qt.Not(qt.IsNil))
337 b.Assert(bundle.RelPermalink(), qt.Equals, "/blog/sect/no-publishresources/")
338 b.Assert(bundle.Resources(), qt.HasLen, 1)
339 resource := bundle.Resources()[0]
340 b.Assert(resource.RelPermalink(), qt.Equals, "/blog/sect/no-publishresources/data.json")
341 })
342 }
343
344 // https://github.com/gohugoio/hugo/issues/6897#issuecomment-587947078
345 func TestDisableRSSWithRSSInCustomOutputs(t *testing.T) {
346 b := newTestSitesBuilder(t).WithConfigFile("toml", `
347 disableKinds = ["term", "taxonomy", "RSS"]
348 [outputs]
349 home = [ "HTML", "RSS" ]
350 `).Build(BuildCfg{})
351
352 // The config above is a little conflicting, but it exists in the real world.
353 // In Hugo 0.65 we consolidated the code paths and made RSS a pure output format,
354 // but we should make sure to not break existing sites.
355 b.Assert(b.CheckExists("public/index.xml"), qt.Equals, false)
356 }
357
358 func TestBundleNoPublishResources(t *testing.T) {
359 b := newTestSitesBuilder(t)
360 b.WithTemplates("index.html", `
361 {{ $bundle := site.GetPage "section/bundle-false" }}
362 {{ $data1 := $bundle.Resources.GetMatch "data1*" }}
363 Data1: {{ $data1.RelPermalink }}
364
365 `)
366
367 b.WithContent("section/bundle-false/index.md", `---\ntitle: BundleFalse
368 build:
369 publishResources: false
370 ---`,
371 "section/bundle-false/data1.json", "Some data1",
372 "section/bundle-false/data2.json", "Some data2",
373 )
374
375 b.WithContent("section/bundle-true/index.md", `---\ntitle: BundleTrue
376 ---`,
377 "section/bundle-true/data3.json", "Some data 3",
378 )
379
380 b.Build(BuildCfg{})
381 b.AssertFileContent("public/index.html", `Data1: /section/bundle-false/data1.json`)
382 b.AssertFileContent("public/section/bundle-false/data1.json", `Some data1`)
383 b.Assert(b.CheckExists("public/section/bundle-false/data2.json"), qt.Equals, false)
384 b.AssertFileContent("public/section/bundle-true/data3.json", `Some data 3`)
385 }
386
387 func TestNoRenderAndNoPublishResources(t *testing.T) {
388 noRenderPage := `
389 ---
390 title: %s
391 build:
392 render: false
393 publishResources: false
394 ---
395 `
396 b := newTestSitesBuilder(t)
397 b.WithTemplatesAdded("index.html", `
398 {{ $page := site.GetPage "sect/no-render" }}
399 {{ $sect := site.GetPage "sect-no-render" }}
400
401 Page: {{ $page.Title }}|RelPermalink: {{ $page.RelPermalink }}|Outputs: {{ len $page.OutputFormats }}
402 Section: {{ $sect.Title }}|RelPermalink: {{ $sect.RelPermalink }}|Outputs: {{ len $sect.OutputFormats }}
403
404
405 `)
406 b.WithContent("sect-no-render/_index.md", fmt.Sprintf(noRenderPage, "MySection"))
407 b.WithContent("sect/no-render.md", fmt.Sprintf(noRenderPage, "MyPage"))
408
409 b.Build(BuildCfg{})
410
411 b.AssertFileContent("public/index.html", `
412 Page: MyPage|RelPermalink: |Outputs: 0
413 Section: MySection|RelPermalink: |Outputs: 0
414 `)
415
416 b.Assert(b.CheckExists("public/sect/no-render/index.html"), qt.Equals, false)
417 b.Assert(b.CheckExists("public/sect-no-render/index.html"), qt.Equals, false)
418 }
419
420 func TestDisableOneOfThreeLanguages(t *testing.T) {
421 files := `
422 -- hugo.toml --
423 baseURL = "https://example.com"
424 defaultContentLanguage = "en"
425 defaultContentLanguageInSubdir = true
426 [languages]
427 [languages.en]
428 weight = 1
429 title = "English"
430 [languages.nn]
431 weight = 2
432 title = "Nynorsk"
433 disabled = true
434 [languages.nb]
435 weight = 3
436 title = "Bokmål"
437 -- content/p1.nn.md --
438 ---
439 title: "Page 1 nn"
440 ---
441 -- content/p1.nb.md --
442 ---
443 title: "Page 1 nb"
444 ---
445 -- content/p1.en.md --
446 ---
447 title: "Page 1 en"
448 ---
449 -- content/p2.nn.md --
450 ---
451 title: "Page 2 nn"
452 ---
453 -- layouts/_default/single.html --
454 {{ .Title }}
455 `
456 b := Test(t, files)
457
458 b.Assert(len(b.H.Sites), qt.Equals, 2)
459 b.AssertFileContent("public/en/p1/index.html", "Page 1 en")
460 b.AssertFileContent("public/nb/p1/index.html", "Page 1 nb")
461
462 b.AssertFileExists("public/en/p2/index.html", false)
463 b.AssertFileExists("public/nn/p1/index.html", false)
464 b.AssertFileExists("public/nn/p2/index.html", false)
465 }