github.com/olliephillips/hugo@v0.42.2/hugolib/site_test.go (about) 1 // Copyright 2016 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 hugolib 15 16 import ( 17 "fmt" 18 "path/filepath" 19 "strings" 20 "testing" 21 22 "github.com/markbates/inflect" 23 24 "github.com/gohugoio/hugo/helpers" 25 26 "github.com/gohugoio/hugo/deps" 27 "github.com/gohugoio/hugo/hugofs" 28 "github.com/stretchr/testify/assert" 29 "github.com/stretchr/testify/require" 30 ) 31 32 const ( 33 templateMissingFunc = "{{ .Title | funcdoesnotexists }}" 34 templateWithURLAbs = "<a href=\"/foobar.jpg\">Going</a>" 35 ) 36 37 func init() { 38 testMode = true 39 } 40 41 func pageMust(p *Page, err error) *Page { 42 if err != nil { 43 panic(err) 44 } 45 return p 46 } 47 48 func TestRenderWithInvalidTemplate(t *testing.T) { 49 t.Parallel() 50 cfg, fs := newTestCfg() 51 52 writeSource(t, fs, filepath.Join("content", "foo.md"), "foo") 53 54 withTemplate := createWithTemplateFromNameValues("missing", templateMissingFunc) 55 56 buildSingleSiteExpected(t, true, deps.DepsCfg{Fs: fs, Cfg: cfg, WithTemplate: withTemplate}, BuildCfg{}) 57 58 } 59 60 func TestDraftAndFutureRender(t *testing.T) { 61 t.Parallel() 62 sources := [][2]string{ 63 {filepath.FromSlash("sect/doc1.md"), "---\ntitle: doc1\ndraft: true\npublishdate: \"2414-05-29\"\n---\n# doc1\n*some content*"}, 64 {filepath.FromSlash("sect/doc2.md"), "---\ntitle: doc2\ndraft: true\npublishdate: \"2012-05-29\"\n---\n# doc2\n*some content*"}, 65 {filepath.FromSlash("sect/doc3.md"), "---\ntitle: doc3\ndraft: false\npublishdate: \"2414-05-29\"\n---\n# doc3\n*some content*"}, 66 {filepath.FromSlash("sect/doc4.md"), "---\ntitle: doc4\ndraft: false\npublishdate: \"2012-05-29\"\n---\n# doc4\n*some content*"}, 67 } 68 69 siteSetup := func(t *testing.T, configKeyValues ...interface{}) *Site { 70 cfg, fs := newTestCfg() 71 72 cfg.Set("baseURL", "http://auth/bub") 73 74 for i := 0; i < len(configKeyValues); i += 2 { 75 cfg.Set(configKeyValues[i].(string), configKeyValues[i+1]) 76 } 77 78 for _, src := range sources { 79 writeSource(t, fs, filepath.Join("content", src[0]), src[1]) 80 81 } 82 83 return buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{}) 84 } 85 86 // Testing Defaults.. Only draft:true and publishDate in the past should be rendered 87 s := siteSetup(t) 88 if len(s.RegularPages) != 1 { 89 t.Fatal("Draft or Future dated content published unexpectedly") 90 } 91 92 // only publishDate in the past should be rendered 93 s = siteSetup(t, "buildDrafts", true) 94 if len(s.RegularPages) != 2 { 95 t.Fatal("Future Dated Posts published unexpectedly") 96 } 97 98 // drafts should not be rendered, but all dates should 99 s = siteSetup(t, 100 "buildDrafts", false, 101 "buildFuture", true) 102 103 if len(s.RegularPages) != 2 { 104 t.Fatal("Draft posts published unexpectedly") 105 } 106 107 // all 4 should be included 108 s = siteSetup(t, 109 "buildDrafts", true, 110 "buildFuture", true) 111 112 if len(s.RegularPages) != 4 { 113 t.Fatal("Drafts or Future posts not included as expected") 114 } 115 116 } 117 118 func TestFutureExpirationRender(t *testing.T) { 119 t.Parallel() 120 sources := [][2]string{ 121 {filepath.FromSlash("sect/doc3.md"), "---\ntitle: doc1\nexpirydate: \"2400-05-29\"\n---\n# doc1\n*some content*"}, 122 {filepath.FromSlash("sect/doc4.md"), "---\ntitle: doc2\nexpirydate: \"2000-05-29\"\n---\n# doc2\n*some content*"}, 123 } 124 125 siteSetup := func(t *testing.T) *Site { 126 cfg, fs := newTestCfg() 127 cfg.Set("baseURL", "http://auth/bub") 128 129 for _, src := range sources { 130 writeSource(t, fs, filepath.Join("content", src[0]), src[1]) 131 132 } 133 134 return buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{}) 135 } 136 137 s := siteSetup(t) 138 139 if len(s.AllPages) != 1 { 140 if len(s.RegularPages) > 1 { 141 t.Fatal("Expired content published unexpectedly") 142 } 143 144 if len(s.RegularPages) < 1 { 145 t.Fatal("Valid content expired unexpectedly") 146 } 147 } 148 149 if s.AllPages[0].title == "doc2" { 150 t.Fatal("Expired content published unexpectedly") 151 } 152 } 153 154 func TestLastChange(t *testing.T) { 155 t.Parallel() 156 157 cfg, fs := newTestCfg() 158 159 writeSource(t, fs, filepath.Join("content", "sect/doc1.md"), "---\ntitle: doc1\nweight: 1\ndate: 2014-05-29\n---\n# doc1\n*some content*") 160 writeSource(t, fs, filepath.Join("content", "sect/doc2.md"), "---\ntitle: doc2\nweight: 2\ndate: 2015-05-29\n---\n# doc2\n*some content*") 161 writeSource(t, fs, filepath.Join("content", "sect/doc3.md"), "---\ntitle: doc3\nweight: 3\ndate: 2017-05-29\n---\n# doc3\n*some content*") 162 writeSource(t, fs, filepath.Join("content", "sect/doc4.md"), "---\ntitle: doc4\nweight: 4\ndate: 2016-05-29\n---\n# doc4\n*some content*") 163 writeSource(t, fs, filepath.Join("content", "sect/doc5.md"), "---\ntitle: doc5\nweight: 3\n---\n# doc5\n*some content*") 164 165 s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{SkipRender: true}) 166 167 require.False(t, s.Info.LastChange.IsZero(), "Site.LastChange is zero") 168 require.Equal(t, 2017, s.Info.LastChange.Year(), "Site.LastChange should be set to the page with latest Lastmod (year 2017)") 169 } 170 171 // Issue #_index 172 func TestPageWithUnderScoreIndexInFilename(t *testing.T) { 173 t.Parallel() 174 175 cfg, fs := newTestCfg() 176 177 writeSource(t, fs, filepath.Join("content", "sect/my_index_file.md"), "---\ntitle: doc1\nweight: 1\ndate: 2014-05-29\n---\n# doc1\n*some content*") 178 179 s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{SkipRender: true}) 180 181 require.Len(t, s.RegularPages, 1) 182 183 } 184 185 // Issue #957 186 func TestCrossrefs(t *testing.T) { 187 t.Parallel() 188 for _, uglyURLs := range []bool{true, false} { 189 for _, relative := range []bool{true, false} { 190 doTestCrossrefs(t, relative, uglyURLs) 191 } 192 } 193 } 194 195 func doTestCrossrefs(t *testing.T, relative, uglyURLs bool) { 196 197 baseURL := "http://foo/bar" 198 199 var refShortcode string 200 var expectedBase string 201 var expectedURLSuffix string 202 var expectedPathSuffix string 203 204 if relative { 205 refShortcode = "relref" 206 expectedBase = "/bar" 207 } else { 208 refShortcode = "ref" 209 expectedBase = baseURL 210 } 211 212 if uglyURLs { 213 expectedURLSuffix = ".html" 214 expectedPathSuffix = ".html" 215 } else { 216 expectedURLSuffix = "/" 217 expectedPathSuffix = "/index.html" 218 } 219 220 doc3Slashed := filepath.FromSlash("/sect/doc3.md") 221 222 sources := [][2]string{ 223 { 224 filepath.FromSlash("sect/doc1.md"), 225 fmt.Sprintf(`Ref 2: {{< %s "sect/doc2.md" >}}`, refShortcode), 226 }, 227 // Issue #1148: Make sure that no P-tags is added around shortcodes. 228 { 229 filepath.FromSlash("sect/doc2.md"), 230 fmt.Sprintf(`**Ref 1:** 231 232 {{< %s "sect/doc1.md" >}} 233 234 THE END.`, refShortcode), 235 }, 236 // Issue #1753: Should not add a trailing newline after shortcode. 237 { 238 filepath.FromSlash("sect/doc3.md"), 239 fmt.Sprintf(`**Ref 1:**{{< %s "sect/doc3.md" >}}.`, refShortcode), 240 }, 241 // Issue #3703 242 { 243 filepath.FromSlash("sect/doc4.md"), 244 fmt.Sprintf(`**Ref 1:**{{< %s "%s" >}}.`, refShortcode, doc3Slashed), 245 }, 246 } 247 248 cfg, fs := newTestCfg() 249 250 cfg.Set("baseURL", baseURL) 251 cfg.Set("uglyURLs", uglyURLs) 252 cfg.Set("verbose", true) 253 254 for _, src := range sources { 255 writeSource(t, fs, filepath.Join("content", src[0]), src[1]) 256 } 257 258 s := buildSingleSite( 259 t, 260 deps.DepsCfg{ 261 Fs: fs, 262 Cfg: cfg, 263 WithTemplate: createWithTemplateFromNameValues("_default/single.html", "{{.Content}}")}, 264 BuildCfg{}) 265 266 require.Len(t, s.RegularPages, 4) 267 268 th := testHelper{s.Cfg, s.Fs, t} 269 270 tests := []struct { 271 doc string 272 expected string 273 }{ 274 {filepath.FromSlash(fmt.Sprintf("public/sect/doc1%s", expectedPathSuffix)), fmt.Sprintf("<p>Ref 2: %s/sect/doc2%s</p>\n", expectedBase, expectedURLSuffix)}, 275 {filepath.FromSlash(fmt.Sprintf("public/sect/doc2%s", expectedPathSuffix)), fmt.Sprintf("<p><strong>Ref 1:</strong></p>\n\n%s/sect/doc1%s\n\n<p>THE END.</p>\n", expectedBase, expectedURLSuffix)}, 276 {filepath.FromSlash(fmt.Sprintf("public/sect/doc3%s", expectedPathSuffix)), fmt.Sprintf("<p><strong>Ref 1:</strong>%s/sect/doc3%s.</p>\n", expectedBase, expectedURLSuffix)}, 277 {filepath.FromSlash(fmt.Sprintf("public/sect/doc4%s", expectedPathSuffix)), fmt.Sprintf("<p><strong>Ref 1:</strong>%s/sect/doc3%s.</p>\n", expectedBase, expectedURLSuffix)}, 278 } 279 280 for _, test := range tests { 281 th.assertFileContent(test.doc, test.expected) 282 283 } 284 285 } 286 287 // Issue #939 288 // Issue #1923 289 func TestShouldAlwaysHaveUglyURLs(t *testing.T) { 290 t.Parallel() 291 for _, uglyURLs := range []bool{true, false} { 292 doTestShouldAlwaysHaveUglyURLs(t, uglyURLs) 293 } 294 } 295 296 func doTestShouldAlwaysHaveUglyURLs(t *testing.T, uglyURLs bool) { 297 298 cfg, fs := newTestCfg() 299 300 cfg.Set("verbose", true) 301 cfg.Set("baseURL", "http://auth/bub") 302 cfg.Set("rssURI", "index.xml") 303 cfg.Set("blackfriday", 304 map[string]interface{}{ 305 "plainIDAnchors": true}) 306 307 cfg.Set("uglyURLs", uglyURLs) 308 309 sources := [][2]string{ 310 {filepath.FromSlash("sect/doc1.md"), "---\nmarkup: markdown\n---\n# title\nsome *content*"}, 311 {filepath.FromSlash("sect/doc2.md"), "---\nurl: /ugly.html\nmarkup: markdown\n---\n# title\ndoc2 *content*"}, 312 } 313 314 for _, src := range sources { 315 writeSource(t, fs, filepath.Join("content", src[0]), src[1]) 316 } 317 318 writeSource(t, fs, filepath.Join("layouts", "index.html"), "Home Sweet {{ if.IsHome }}Home{{ end }}.") 319 writeSource(t, fs, filepath.Join("layouts", "_default/single.html"), "{{.Content}}{{ if.IsHome }}This is not home!{{ end }}") 320 writeSource(t, fs, filepath.Join("layouts", "404.html"), "Page Not Found.{{ if.IsHome }}This is not home!{{ end }}") 321 writeSource(t, fs, filepath.Join("layouts", "rss.xml"), "<root>RSS</root>") 322 writeSource(t, fs, filepath.Join("layouts", "sitemap.xml"), "<root>SITEMAP</root>") 323 324 s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{}) 325 326 var expectedPagePath string 327 if uglyURLs { 328 expectedPagePath = "public/sect/doc1.html" 329 } else { 330 expectedPagePath = "public/sect/doc1/index.html" 331 } 332 333 tests := []struct { 334 doc string 335 expected string 336 }{ 337 {filepath.FromSlash("public/index.html"), "Home Sweet Home."}, 338 {filepath.FromSlash(expectedPagePath), "\n\n<h1 id=\"title\">title</h1>\n\n<p>some <em>content</em></p>\n"}, 339 {filepath.FromSlash("public/404.html"), "Page Not Found."}, 340 {filepath.FromSlash("public/index.xml"), "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\" ?>\n<root>RSS</root>"}, 341 {filepath.FromSlash("public/sitemap.xml"), "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\" ?>\n<root>SITEMAP</root>"}, 342 // Issue #1923 343 {filepath.FromSlash("public/ugly.html"), "\n\n<h1 id=\"title\">title</h1>\n\n<p>doc2 <em>content</em></p>\n"}, 344 } 345 346 for _, p := range s.RegularPages { 347 assert.False(t, p.IsHome()) 348 } 349 350 for _, test := range tests { 351 content := readDestination(t, fs, test.doc) 352 353 if content != test.expected { 354 t.Errorf("%s content expected:\n%q\ngot:\n%q", test.doc, test.expected, content) 355 } 356 } 357 358 } 359 360 func TestNewSiteDefaultLang(t *testing.T) { 361 t.Parallel() 362 s, err := NewSiteDefaultLang() 363 require.NoError(t, err) 364 require.Equal(t, hugofs.Os, s.Fs.Source) 365 require.Equal(t, hugofs.Os, s.Fs.Destination) 366 } 367 368 // Issue #3355 369 func TestShouldNotWriteZeroLengthFilesToDestination(t *testing.T) { 370 cfg, fs := newTestCfg() 371 372 writeSource(t, fs, filepath.Join("content", "simple.html"), "simple") 373 writeSource(t, fs, filepath.Join("layouts", "_default/single.html"), "{{.Content}}") 374 writeSource(t, fs, filepath.Join("layouts", "_default/list.html"), "") 375 376 s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{}) 377 th := testHelper{s.Cfg, s.Fs, t} 378 379 th.assertFileNotExist(filepath.Join("public", "index.html")) 380 } 381 382 // Issue #1176 383 func TestSectionNaming(t *testing.T) { 384 t.Parallel() 385 for _, canonify := range []bool{true, false} { 386 for _, uglify := range []bool{true, false} { 387 for _, pluralize := range []bool{true, false} { 388 t.Run(fmt.Sprintf("canonify=%t,uglify=%t,pluralize=%t", canonify, uglify, pluralize), func(t *testing.T) { 389 doTestSectionNaming(t, canonify, uglify, pluralize) 390 }) 391 } 392 } 393 } 394 } 395 396 func doTestSectionNaming(t *testing.T, canonify, uglify, pluralize bool) { 397 398 var expectedPathSuffix string 399 400 if uglify { 401 expectedPathSuffix = ".html" 402 } else { 403 expectedPathSuffix = "/index.html" 404 } 405 406 sources := [][2]string{ 407 {filepath.FromSlash("sect/doc1.html"), "doc1"}, 408 // Add one more page to sect to make sure sect is picked in mainSections 409 {filepath.FromSlash("sect/sect.html"), "sect"}, 410 {filepath.FromSlash("Fish and Chips/doc2.html"), "doc2"}, 411 {filepath.FromSlash("ラーメン/doc3.html"), "doc3"}, 412 } 413 414 cfg, fs := newTestCfg() 415 416 cfg.Set("baseURL", "http://auth/sub/") 417 cfg.Set("uglyURLs", uglify) 418 cfg.Set("pluralizeListTitles", pluralize) 419 cfg.Set("canonifyURLs", canonify) 420 421 for _, src := range sources { 422 writeSource(t, fs, filepath.Join("content", src[0]), src[1]) 423 } 424 425 writeSource(t, fs, filepath.Join("layouts", "_default/single.html"), "{{.Content}}") 426 writeSource(t, fs, filepath.Join("layouts", "_default/list.html"), "{{.Title}}") 427 428 s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{}) 429 430 mainSections, err := s.Info.Param("mainSections") 431 require.NoError(t, err) 432 require.Equal(t, []string{"sect"}, mainSections) 433 434 th := testHelper{s.Cfg, s.Fs, t} 435 tests := []struct { 436 doc string 437 pluralAware bool 438 expected string 439 }{ 440 {filepath.FromSlash(fmt.Sprintf("sect/doc1%s", expectedPathSuffix)), false, "doc1"}, 441 {filepath.FromSlash(fmt.Sprintf("sect%s", expectedPathSuffix)), true, "Sect"}, 442 {filepath.FromSlash(fmt.Sprintf("fish-and-chips/doc2%s", expectedPathSuffix)), false, "doc2"}, 443 {filepath.FromSlash(fmt.Sprintf("fish-and-chips%s", expectedPathSuffix)), true, "Fish and Chips"}, 444 {filepath.FromSlash(fmt.Sprintf("ラーメン/doc3%s", expectedPathSuffix)), false, "doc3"}, 445 {filepath.FromSlash(fmt.Sprintf("ラーメン%s", expectedPathSuffix)), true, "ラーメン"}, 446 } 447 448 for _, test := range tests { 449 450 if test.pluralAware && pluralize { 451 test.expected = inflect.Pluralize(test.expected) 452 } 453 454 th.assertFileContent(filepath.Join("public", test.doc), test.expected) 455 } 456 457 } 458 func TestSkipRender(t *testing.T) { 459 t.Parallel() 460 sources := [][2]string{ 461 {filepath.FromSlash("sect/doc1.html"), "---\nmarkup: markdown\n---\n# title\nsome *content*"}, 462 {filepath.FromSlash("sect/doc2.html"), "<!doctype html><html><body>more content</body></html>"}, 463 {filepath.FromSlash("sect/doc3.md"), "# doc3\n*some* content"}, 464 {filepath.FromSlash("sect/doc4.md"), "---\ntitle: doc4\n---\n# doc4\n*some content*"}, 465 {filepath.FromSlash("sect/doc5.html"), "<!doctype html><html>{{ template \"head\" }}<body>body5</body></html>"}, 466 {filepath.FromSlash("sect/doc6.html"), "<!doctype html><html>{{ template \"head_abs\" }}<body>body5</body></html>"}, 467 {filepath.FromSlash("doc7.html"), "<html><body>doc7 content</body></html>"}, 468 {filepath.FromSlash("sect/doc8.html"), "---\nmarkup: md\n---\n# title\nsome *content*"}, 469 // Issue #3021 470 {filepath.FromSlash("doc9.html"), "<html><body>doc9: {{< myshortcode >}}</body></html>"}, 471 } 472 473 cfg, fs := newTestCfg() 474 475 cfg.Set("verbose", true) 476 cfg.Set("canonifyURLs", true) 477 cfg.Set("uglyURLs", true) 478 cfg.Set("baseURL", "http://auth/bub") 479 480 for _, src := range sources { 481 writeSource(t, fs, filepath.Join("content", src[0]), src[1]) 482 483 } 484 485 writeSource(t, fs, filepath.Join("layouts", "_default/single.html"), "{{.Content}}") 486 writeSource(t, fs, filepath.Join("layouts", "head"), "<head><script src=\"script.js\"></script></head>") 487 writeSource(t, fs, filepath.Join("layouts", "head_abs"), "<head><script src=\"/script.js\"></script></head>") 488 writeSource(t, fs, filepath.Join("layouts", "shortcodes", "myshortcode.html"), "SHORT") 489 490 buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{}) 491 492 tests := []struct { 493 doc string 494 expected string 495 }{ 496 {filepath.FromSlash("public/sect/doc1.html"), "\n\n<h1 id=\"title\">title</h1>\n\n<p>some <em>content</em></p>\n"}, 497 {filepath.FromSlash("public/sect/doc2.html"), "<!doctype html><html><body>more content</body></html>"}, 498 {filepath.FromSlash("public/sect/doc3.html"), "\n\n<h1 id=\"doc3\">doc3</h1>\n\n<p><em>some</em> content</p>\n"}, 499 {filepath.FromSlash("public/sect/doc4.html"), "\n\n<h1 id=\"doc4\">doc4</h1>\n\n<p><em>some content</em></p>\n"}, 500 {filepath.FromSlash("public/sect/doc5.html"), "<!doctype html><html><head><script src=\"script.js\"></script></head><body>body5</body></html>"}, 501 {filepath.FromSlash("public/sect/doc6.html"), "<!doctype html><html><head><script src=\"http://auth/bub/script.js\"></script></head><body>body5</body></html>"}, 502 {filepath.FromSlash("public/doc7.html"), "<html><body>doc7 content</body></html>"}, 503 {filepath.FromSlash("public/sect/doc8.html"), "\n\n<h1 id=\"title\">title</h1>\n\n<p>some <em>content</em></p>\n"}, 504 {filepath.FromSlash("public/doc9.html"), "<html><body>doc9: SHORT</body></html>"}, 505 } 506 507 for _, test := range tests { 508 file, err := fs.Destination.Open(test.doc) 509 if err != nil { 510 t.Fatalf("Did not find %s in target.", test.doc) 511 } 512 513 content := helpers.ReaderToString(file) 514 515 if content != test.expected { 516 t.Errorf("%s content expected:\n%q\ngot:\n%q", test.doc, test.expected, content) 517 } 518 } 519 } 520 521 func TestAbsURLify(t *testing.T) { 522 t.Parallel() 523 sources := [][2]string{ 524 {filepath.FromSlash("sect/doc1.html"), "<!doctype html><html><head></head><body><a href=\"#frag1\">link</a></body></html>"}, 525 {filepath.FromSlash("blue/doc2.html"), "---\nf: t\n---\n<!doctype html><html><body>more content</body></html>"}, 526 } 527 for _, baseURL := range []string{"http://auth/bub", "http://base", "//base"} { 528 for _, canonify := range []bool{true, false} { 529 530 cfg, fs := newTestCfg() 531 532 cfg.Set("uglyURLs", true) 533 cfg.Set("canonifyURLs", canonify) 534 cfg.Set("baseURL", baseURL) 535 536 for _, src := range sources { 537 writeSource(t, fs, filepath.Join("content", src[0]), src[1]) 538 539 } 540 541 writeSource(t, fs, filepath.Join("layouts", "blue/single.html"), templateWithURLAbs) 542 543 s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{}) 544 th := testHelper{s.Cfg, s.Fs, t} 545 546 tests := []struct { 547 file, expected string 548 }{ 549 {"public/blue/doc2.html", "<a href=\"%s/foobar.jpg\">Going</a>"}, 550 {"public/sect/doc1.html", "<!doctype html><html><head></head><body><a href=\"#frag1\">link</a></body></html>"}, 551 } 552 553 for _, test := range tests { 554 555 expected := test.expected 556 557 if strings.Contains(expected, "%s") { 558 expected = fmt.Sprintf(expected, baseURL) 559 } 560 561 if !canonify { 562 expected = strings.Replace(expected, baseURL, "", -1) 563 } 564 565 th.assertFileContent(test.file, expected) 566 567 } 568 } 569 } 570 } 571 572 var weightedPage1 = `+++ 573 weight = "2" 574 title = "One" 575 my_param = "foo" 576 my_date = 1979-05-27T07:32:00Z 577 +++ 578 Front Matter with Ordered Pages` 579 580 var weightedPage2 = `+++ 581 weight = "6" 582 title = "Two" 583 publishdate = "2012-03-05" 584 my_param = "foo" 585 +++ 586 Front Matter with Ordered Pages 2` 587 588 var weightedPage3 = `+++ 589 weight = "4" 590 title = "Three" 591 date = "2012-04-06" 592 publishdate = "2012-04-06" 593 my_param = "bar" 594 only_one = "yes" 595 my_date = 2010-05-27T07:32:00Z 596 +++ 597 Front Matter with Ordered Pages 3` 598 599 var weightedPage4 = `+++ 600 weight = "4" 601 title = "Four" 602 date = "2012-01-01" 603 publishdate = "2012-01-01" 604 my_param = "baz" 605 my_date = 2010-05-27T07:32:00Z 606 categories = [ "hugo" ] 607 +++ 608 Front Matter with Ordered Pages 4. This is longer content` 609 610 var weightedSources = [][2]string{ 611 {filepath.FromSlash("sect/doc1.md"), weightedPage1}, 612 {filepath.FromSlash("sect/doc2.md"), weightedPage2}, 613 {filepath.FromSlash("sect/doc3.md"), weightedPage3}, 614 {filepath.FromSlash("sect/doc4.md"), weightedPage4}, 615 } 616 617 func TestOrderedPages(t *testing.T) { 618 t.Parallel() 619 cfg, fs := newTestCfg() 620 cfg.Set("baseURL", "http://auth/bub") 621 622 for _, src := range weightedSources { 623 writeSource(t, fs, filepath.Join("content", src[0]), src[1]) 624 625 } 626 627 s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{SkipRender: true}) 628 629 if s.getPage(KindSection, "sect").Pages[1].title != "Three" || s.getPage(KindSection, "sect").Pages[2].title != "Four" { 630 t.Error("Pages in unexpected order.") 631 } 632 633 bydate := s.RegularPages.ByDate() 634 635 if bydate[0].title != "One" { 636 t.Errorf("Pages in unexpected order. First should be '%s', got '%s'", "One", bydate[0].title) 637 } 638 639 rev := bydate.Reverse() 640 if rev[0].title != "Three" { 641 t.Errorf("Pages in unexpected order. First should be '%s', got '%s'", "Three", rev[0].title) 642 } 643 644 bypubdate := s.RegularPages.ByPublishDate() 645 646 if bypubdate[0].title != "One" { 647 t.Errorf("Pages in unexpected order. First should be '%s', got '%s'", "One", bypubdate[0].title) 648 } 649 650 rbypubdate := bypubdate.Reverse() 651 if rbypubdate[0].title != "Three" { 652 t.Errorf("Pages in unexpected order. First should be '%s', got '%s'", "Three", rbypubdate[0].title) 653 } 654 655 bylength := s.RegularPages.ByLength() 656 if bylength[0].title != "One" { 657 t.Errorf("Pages in unexpected order. First should be '%s', got '%s'", "One", bylength[0].title) 658 } 659 660 rbylength := bylength.Reverse() 661 if rbylength[0].title != "Four" { 662 t.Errorf("Pages in unexpected order. First should be '%s', got '%s'", "Four", rbylength[0].title) 663 } 664 } 665 666 var groupedSources = [][2]string{ 667 {filepath.FromSlash("sect1/doc1.md"), weightedPage1}, 668 {filepath.FromSlash("sect1/doc2.md"), weightedPage2}, 669 {filepath.FromSlash("sect2/doc3.md"), weightedPage3}, 670 {filepath.FromSlash("sect3/doc4.md"), weightedPage4}, 671 } 672 673 func TestGroupedPages(t *testing.T) { 674 t.Parallel() 675 defer func() { 676 if r := recover(); r != nil { 677 fmt.Println("Recovered in f", r) 678 } 679 }() 680 681 cfg, fs := newTestCfg() 682 cfg.Set("baseURL", "http://auth/bub") 683 684 writeSourcesToSource(t, "content", fs, groupedSources...) 685 s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{}) 686 687 rbysection, err := s.RegularPages.GroupBy("Section", "desc") 688 if err != nil { 689 t.Fatalf("Unable to make PageGroup array: %s", err) 690 } 691 692 if rbysection[0].Key != "sect3" { 693 t.Errorf("PageGroup array in unexpected order. First group key should be '%s', got '%s'", "sect3", rbysection[0].Key) 694 } 695 if rbysection[1].Key != "sect2" { 696 t.Errorf("PageGroup array in unexpected order. Second group key should be '%s', got '%s'", "sect2", rbysection[1].Key) 697 } 698 if rbysection[2].Key != "sect1" { 699 t.Errorf("PageGroup array in unexpected order. Third group key should be '%s', got '%s'", "sect1", rbysection[2].Key) 700 } 701 if rbysection[0].Pages[0].title != "Four" { 702 t.Errorf("PageGroup has an unexpected page. First group's pages should have '%s', got '%s'", "Four", rbysection[0].Pages[0].title) 703 } 704 if len(rbysection[2].Pages) != 2 { 705 t.Errorf("PageGroup has unexpected number of pages. Third group should have '%d' pages, got '%d' pages", 2, len(rbysection[2].Pages)) 706 } 707 708 bytype, err := s.RegularPages.GroupBy("Type", "asc") 709 if err != nil { 710 t.Fatalf("Unable to make PageGroup array: %s", err) 711 } 712 if bytype[0].Key != "sect1" { 713 t.Errorf("PageGroup array in unexpected order. First group key should be '%s', got '%s'", "sect1", bytype[0].Key) 714 } 715 if bytype[1].Key != "sect2" { 716 t.Errorf("PageGroup array in unexpected order. Second group key should be '%s', got '%s'", "sect2", bytype[1].Key) 717 } 718 if bytype[2].Key != "sect3" { 719 t.Errorf("PageGroup array in unexpected order. Third group key should be '%s', got '%s'", "sect3", bytype[2].Key) 720 } 721 if bytype[2].Pages[0].title != "Four" { 722 t.Errorf("PageGroup has an unexpected page. Third group's data should have '%s', got '%s'", "Four", bytype[0].Pages[0].title) 723 } 724 if len(bytype[0].Pages) != 2 { 725 t.Errorf("PageGroup has unexpected number of pages. First group should have '%d' pages, got '%d' pages", 2, len(bytype[2].Pages)) 726 } 727 728 bydate, err := s.RegularPages.GroupByDate("2006-01", "asc") 729 if err != nil { 730 t.Fatalf("Unable to make PageGroup array: %s", err) 731 } 732 if bydate[0].Key != "0001-01" { 733 t.Errorf("PageGroup array in unexpected order. First group key should be '%s', got '%s'", "0001-01", bydate[0].Key) 734 } 735 if bydate[1].Key != "2012-01" { 736 t.Errorf("PageGroup array in unexpected order. Second group key should be '%s', got '%s'", "2012-01", bydate[1].Key) 737 } 738 739 bypubdate, err := s.RegularPages.GroupByPublishDate("2006") 740 if err != nil { 741 t.Fatalf("Unable to make PageGroup array: %s", err) 742 } 743 if bypubdate[0].Key != "2012" { 744 t.Errorf("PageGroup array in unexpected order. First group key should be '%s', got '%s'", "2012", bypubdate[0].Key) 745 } 746 if bypubdate[1].Key != "0001" { 747 t.Errorf("PageGroup array in unexpected order. Second group key should be '%s', got '%s'", "0001", bypubdate[1].Key) 748 } 749 if bypubdate[0].Pages[0].title != "Three" { 750 t.Errorf("PageGroup has an unexpected page. Third group's pages should have '%s', got '%s'", "Three", bypubdate[0].Pages[0].title) 751 } 752 if len(bypubdate[0].Pages) != 3 { 753 t.Errorf("PageGroup has unexpected number of pages. First group should have '%d' pages, got '%d' pages", 3, len(bypubdate[0].Pages)) 754 } 755 756 byparam, err := s.RegularPages.GroupByParam("my_param", "desc") 757 if err != nil { 758 t.Fatalf("Unable to make PageGroup array: %s", err) 759 } 760 if byparam[0].Key != "foo" { 761 t.Errorf("PageGroup array in unexpected order. First group key should be '%s', got '%s'", "foo", byparam[0].Key) 762 } 763 if byparam[1].Key != "baz" { 764 t.Errorf("PageGroup array in unexpected order. Second group key should be '%s', got '%s'", "baz", byparam[1].Key) 765 } 766 if byparam[2].Key != "bar" { 767 t.Errorf("PageGroup array in unexpected order. Third group key should be '%s', got '%s'", "bar", byparam[2].Key) 768 } 769 if byparam[2].Pages[0].title != "Three" { 770 t.Errorf("PageGroup has an unexpected page. Third group's pages should have '%s', got '%s'", "Three", byparam[2].Pages[0].title) 771 } 772 if len(byparam[0].Pages) != 2 { 773 t.Errorf("PageGroup has unexpected number of pages. First group should have '%d' pages, got '%d' pages", 2, len(byparam[0].Pages)) 774 } 775 776 _, err = s.RegularPages.GroupByParam("not_exist") 777 if err == nil { 778 t.Errorf("GroupByParam didn't return an expected error") 779 } 780 781 byOnlyOneParam, err := s.RegularPages.GroupByParam("only_one") 782 if err != nil { 783 t.Fatalf("Unable to make PageGroup array: %s", err) 784 } 785 if len(byOnlyOneParam) != 1 { 786 t.Errorf("PageGroup array has unexpected elements. Group length should be '%d', got '%d'", 1, len(byOnlyOneParam)) 787 } 788 if byOnlyOneParam[0].Key != "yes" { 789 t.Errorf("PageGroup array in unexpected order. First group key should be '%s', got '%s'", "yes", byOnlyOneParam[0].Key) 790 } 791 792 byParamDate, err := s.RegularPages.GroupByParamDate("my_date", "2006-01") 793 if err != nil { 794 t.Fatalf("Unable to make PageGroup array: %s", err) 795 } 796 if byParamDate[0].Key != "2010-05" { 797 t.Errorf("PageGroup array in unexpected order. First group key should be '%s', got '%s'", "2010-05", byParamDate[0].Key) 798 } 799 if byParamDate[1].Key != "1979-05" { 800 t.Errorf("PageGroup array in unexpected order. Second group key should be '%s', got '%s'", "1979-05", byParamDate[1].Key) 801 } 802 if byParamDate[1].Pages[0].title != "One" { 803 t.Errorf("PageGroup has an unexpected page. Second group's pages should have '%s', got '%s'", "One", byParamDate[1].Pages[0].title) 804 } 805 if len(byParamDate[0].Pages) != 2 { 806 t.Errorf("PageGroup has unexpected number of pages. First group should have '%d' pages, got '%d' pages", 2, len(byParamDate[2].Pages)) 807 } 808 } 809 810 var pageWithWeightedTaxonomies1 = `+++ 811 tags = [ "a", "b", "c" ] 812 tags_weight = 22 813 categories = ["d"] 814 title = "foo" 815 categories_weight = 44 816 +++ 817 Front Matter with weighted tags and categories` 818 819 var pageWithWeightedTaxonomies2 = `+++ 820 tags = "a" 821 tags_weight = 33 822 title = "bar" 823 categories = [ "d", "e" ] 824 categories_weight = 11 825 alias = "spf13" 826 date = 1979-05-27T07:32:00Z 827 +++ 828 Front Matter with weighted tags and categories` 829 830 var pageWithWeightedTaxonomies3 = `+++ 831 title = "bza" 832 categories = [ "e" ] 833 categories_weight = 11 834 alias = "spf13" 835 date = 2010-05-27T07:32:00Z 836 +++ 837 Front Matter with weighted tags and categories` 838 839 func TestWeightedTaxonomies(t *testing.T) { 840 t.Parallel() 841 sources := [][2]string{ 842 {filepath.FromSlash("sect/doc1.md"), pageWithWeightedTaxonomies2}, 843 {filepath.FromSlash("sect/doc2.md"), pageWithWeightedTaxonomies1}, 844 {filepath.FromSlash("sect/doc3.md"), pageWithWeightedTaxonomies3}, 845 } 846 taxonomies := make(map[string]string) 847 848 taxonomies["tag"] = "tags" 849 taxonomies["category"] = "categories" 850 851 cfg, fs := newTestCfg() 852 853 cfg.Set("baseURL", "http://auth/bub") 854 cfg.Set("taxonomies", taxonomies) 855 856 writeSourcesToSource(t, "content", fs, sources...) 857 s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{}) 858 859 if s.Taxonomies["tags"]["a"][0].Page.title != "foo" { 860 t.Errorf("Pages in unexpected order, 'foo' expected first, got '%v'", s.Taxonomies["tags"]["a"][0].Page.title) 861 } 862 863 if s.Taxonomies["categories"]["d"][0].Page.title != "bar" { 864 t.Errorf("Pages in unexpected order, 'bar' expected first, got '%v'", s.Taxonomies["categories"]["d"][0].Page.title) 865 } 866 867 if s.Taxonomies["categories"]["e"][0].Page.title != "bza" { 868 t.Errorf("Pages in unexpected order, 'bza' expected first, got '%v'", s.Taxonomies["categories"]["e"][0].Page.title) 869 } 870 } 871 872 func setupLinkingMockSite(t *testing.T) *Site { 873 sources := [][2]string{ 874 {filepath.FromSlash("level2/unique.md"), ""}, 875 {filepath.FromSlash("rootfile.md"), ""}, 876 {filepath.FromSlash("root-image.png"), ""}, 877 878 {filepath.FromSlash("level2/2-root.md"), ""}, 879 {filepath.FromSlash("level2/common.md"), ""}, 880 881 {filepath.FromSlash("level2/2-image.png"), ""}, 882 {filepath.FromSlash("level2/common.png"), ""}, 883 884 {filepath.FromSlash("level2/level3/start.md"), ""}, 885 {filepath.FromSlash("level2/level3/3-root.md"), ""}, 886 {filepath.FromSlash("level2/level3/common.md"), ""}, 887 {filepath.FromSlash("level2/level3/3-image.png"), ""}, 888 {filepath.FromSlash("level2/level3/common.png"), ""}, 889 } 890 891 cfg, fs := newTestCfg() 892 893 cfg.Set("baseURL", "http://auth/") 894 cfg.Set("uglyURLs", false) 895 cfg.Set("outputs", map[string]interface{}{ 896 "page": []string{"HTML", "AMP"}, 897 }) 898 cfg.Set("pluralizeListTitles", false) 899 cfg.Set("canonifyURLs", false) 900 cfg.Set("blackfriday", 901 map[string]interface{}{}) 902 writeSourcesToSource(t, "content", fs, sources...) 903 return buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{}) 904 905 } 906 907 func TestRefLinking(t *testing.T) { 908 t.Parallel() 909 site := setupLinkingMockSite(t) 910 911 currentPage := site.getPage(KindPage, "level2/level3/start.md") 912 if currentPage == nil { 913 t.Fatalf("failed to find current page in site") 914 } 915 916 for i, test := range []struct { 917 link string 918 outputFormat string 919 relative bool 920 expected string 921 }{ 922 {"unique.md", "", true, "/level2/unique/"}, 923 {"level2/common.md", "", true, "/level2/common/"}, 924 {"3-root.md", "", true, "/level2/level3/3-root/"}, 925 } { 926 if out, err := site.Info.refLink(test.link, currentPage, test.relative, test.outputFormat); err != nil || out != test.expected { 927 t.Errorf("[%d] Expected %s to resolve to (%s), got (%s) - error: %s", i, test.link, test.expected, out, err) 928 } 929 } 930 931 // TODO: and then the failure cases. 932 }