github.com/neohugo/neohugo@v0.123.8/hugolib/site_stats_test.go (about) 1 // Copyright 2017 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 "bytes" 18 "fmt" 19 "io" 20 "testing" 21 22 "github.com/neohugo/neohugo/helpers" 23 24 qt "github.com/frankban/quicktest" 25 ) 26 27 func TestSiteStats(t *testing.T) { 28 t.Parallel() 29 30 c := qt.New(t) 31 32 siteConfig := ` 33 baseURL = "http://example.com/blog" 34 35 paginate = 1 36 defaultContentLanguage = "nn" 37 38 [languages] 39 [languages.nn] 40 languageName = "Nynorsk" 41 weight = 1 42 title = "Hugo på norsk" 43 44 [languages.en] 45 languageName = "English" 46 weight = 2 47 title = "Hugo in English" 48 49 ` 50 51 pageTemplate := `--- 52 title: "T%d" 53 tags: 54 %s 55 categories: 56 %s 57 aliases: [/Ali%d] 58 --- 59 # Doc 60 ` 61 62 b := newTestSitesBuilder(t).WithConfigFile("toml", siteConfig) 63 64 b.WithTemplates( 65 "_default/single.html", "Single|{{ .Title }}|{{ .Content }}", 66 "_default/list.html", `List|{{ .Title }}|Pages: {{ .Paginator.TotalPages }}|{{ .Content }}`, 67 "_default/terms.html", "Terms List|{{ .Title }}|{{ .Content }}", 68 ) 69 70 for i := 0; i < 2; i++ { 71 for j := 0; j < 2; j++ { 72 pageID := i + j + 1 73 b.WithContent(fmt.Sprintf("content/sect/p%d.md", pageID), 74 fmt.Sprintf(pageTemplate, pageID, fmt.Sprintf("- tag%d", j), fmt.Sprintf("- category%d", j), pageID)) 75 } 76 } 77 78 for i := 0; i < 5; i++ { 79 b.WithContent(fmt.Sprintf("assets/image%d.png", i+1), "image") 80 } 81 82 b.Build(BuildCfg{}) 83 h := b.H 84 85 stats := []*helpers.ProcessingStats{ 86 h.Sites[0].PathSpec.ProcessingStats, 87 h.Sites[1].PathSpec.ProcessingStats, 88 } 89 90 stats[0].Table(io.Discard) 91 stats[1].Table(io.Discard) 92 93 var buff bytes.Buffer 94 95 helpers.ProcessingStatsTable(&buff, stats...) 96 97 c.Assert(buff.String(), qt.Contains, "Pages | 21 | 7") 98 } 99 100 func TestSiteLastmod(t *testing.T) { 101 t.Parallel() 102 103 files := ` 104 -- hugo.toml -- 105 baseURL = "https://example.com/" 106 -- content/_index.md -- 107 --- 108 date: 2023-01-01 109 --- 110 -- content/posts/_index.md -- 111 --- 112 date: 2023-02-01 113 --- 114 -- content/posts/post-1.md -- 115 --- 116 date: 2023-03-01 117 --- 118 -- content/posts/post-2.md -- 119 --- 120 date: 2023-04-01 121 --- 122 -- layouts/index.html -- 123 site.Lastmod: {{ .Site.Lastmod.Format "2006-01-02" }} 124 site.LastChange: {{ .Site.LastChange.Format "2006-01-02" }} 125 home.Lastmod: {{ site.Home.Lastmod.Format "2006-01-02" }} 126 127 ` 128 b := Test(t, files) 129 130 b.AssertFileContent("public/index.html", "site.Lastmod: 2023-04-01\nsite.LastChange: 2023-04-01\nhome.Lastmod: 2023-01-01") 131 }