Debian Codegraph @

hugolib/site_stats_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
14 package hugolib
15
16 import (
17 "bytes"
18 "fmt"
19 "strings"
20 "testing"
21
22 "github.com/gohugoio/hugo/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 var files strings.Builder
33 files.WriteString(`
34 -- hugo.toml --
35 baseURL = "http://example.com/blog"
36
37 defaultContentLanguage = "nn"
38
39 [pagination]
40 pagerSize = 1
41
42 [languages]
43 [languages.nn]
44 label = "Nynorsk"
45 weight = 1
46 title = "Hugo på norsk"
47
48 [languages.en]
49 label = "English"
50 weight = 2
51 title = "Hugo in English"
52 -- layouts/single.html --
53 Single|{{ .Title }}|{{ .Content }}
54 {{ $img1 := resources.Get "myimage1.png" }}
55 {{ $img1 = $img1.Fit "100x100" }}
56 -- layouts/list.html --
57 List|{{ .Title }}|Pages: {{ .Paginator.TotalPages }}|{{ .Content }}
58 -- layouts/terms.html --
59 Terms List|{{ .Title }}|{{ .Content }}
60
61 `)
62
63 pageTemplate := `---
64 title: "T%d"
65 tags:
66 %s
67 categories:
68 %s
69 aliases: [/Ali%d]
70 ---
71 # Doc
72 `
73
74 for i := range 2 {
75 for j := range 2 {
76 pageID := i + j + 1
77 files.WriteString(fmt.Sprintf("\n-- content/p%d.md --\n", pageID))
78 files.WriteString(fmt.Sprintf(pageTemplate, pageID, fmt.Sprintf("- tag%d", j), fmt.Sprintf("- category%d", j), pageID))
79 }
80 }
81
82 for i := range 5 {
83 files.WriteString(fmt.Sprintf("\n-- assets/myimage%d.png --\niVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==", i+1))
84 }
85
86 b := Test(t, files.String())
87 h := b.H
88
89 stats := []*helpers.ProcessingStats{
90 h.Sites[0].PathSpec.ProcessingStats,
91 h.Sites[1].PathSpec.ProcessingStats,
92 }
93
94 var buff bytes.Buffer
95
96 helpers.ProcessingStatsTable(&buff, stats...)
97 s := buff.String()
98
99 c.Assert(s, qt.Contains, "Pages │ 19 │ 7")
100 c.Assert(s, qt.Contains, "Processed images │ 1 │")
101 }
102
103 func TestSiteLastmod(t *testing.T) {
104 t.Parallel()
105
106 files := `
107 -- hugo.toml --
108 baseURL = "https://example.com/"
109 -- content/_index.md --
110 ---
111 date: 2023-01-01
112 ---
113 -- content/posts/_index.md --
114 ---
115 date: 2023-02-01
116 ---
117 -- content/posts/post-1.md --
118 ---
119 date: 2023-03-01
120 ---
121 -- content/posts/post-2.md --
122 ---
123 date: 2023-04-01
124 ---
125 -- layouts/home.html --
126 site.Lastmod: {{ .Site.Lastmod.Format "2006-01-02" }}
127 home.Lastmod: {{ site.Home.Lastmod.Format "2006-01-02" }}
128
129 `
130 b := Test(t, files)
131
132 b.AssertFileContent("public/index.html", "site.Lastmod: 2023-04-01\nhome.Lastmod: 2023-01-01")
133 }