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