Debian Codegraph @

hugolib/hugo_sites_build_test.go

raw
1 package hugolib
2
3 import (
4 "fmt"
5 "path/filepath"
6 "strings"
7 "testing"
8
9 qt "github.com/frankban/quicktest"
10 "github.com/gohugoio/hugo/common/paths"
11 "github.com/gohugoio/hugo/resources/kinds"
12
13 "github.com/spf13/afero"
14 )
15
16 func TestMultiSitesMainLangInRoot(t *testing.T) {
17 files := `
18 -- hugo.toml --
19 defaultContentLanguage = "fr"
20 defaultContentLanguageInSubdir = false
21 disableKinds = ["taxonomy", "term"]
22 [languages]
23 [languages.en]
24 weight = 1
25 [languages.fr]
26 weight = 2
27 -- content/sect/doc1.en.md --
28 ---
29 title: doc1 en
30 ---
31 -- content/sect/doc1.fr.md --
32 ---
33 title: doc1 fr
34 slug: doc1-fr
35 ---
36 -- layouts/single.html --
37 Single: {{ .Title }}|{{ .Lang }}|{{ .RelPermalink }}|
38
39 `
40 b := Test(t, files)
41 b.AssertFileContent("public/sect/doc1-fr/index.html", "Single: doc1 fr|fr|/sect/doc1-fr/|")
42 b.AssertFileContent("public/en/sect/doc1/index.html", "Single: doc1 en|en|/en/sect/doc1/|")
43 }
44
45 func TestMultiSitesWithTwoLanguages(t *testing.T) {
46 t.Parallel()
47
48 files := `
49 -- hugo.toml --
50 defaultContentLanguage = "nn"
51
52 [languages]
53 [languages.nn]
54 label = "Nynorsk"
55 weight = 1
56 title = "Tittel på Nynorsk"
57 [languages.nn.params]
58 p1 = "p1nn"
59
60 [languages.en]
61 title = "Title in English"
62 label = "English"
63 weight = 2
64 [languages.en.params]
65 p1 = "p1en"
66 `
67 b := Test(t, files, TestOptSkipRender())
68 sites := b.H.Sites
69
70 c := qt.New(t)
71 c.Assert(len(sites), qt.Equals, 2)
72
73 nnSite := sites[0]
74 nnHome := nnSite.getPageOldVersion(kinds.KindHome)
75 c.Assert(len(nnHome.AllTranslations()), qt.Equals, 2)
76 c.Assert(len(nnHome.Translations()), qt.Equals, 1)
77 c.Assert(nnHome.IsTranslated(), qt.Equals, true)
78
79 enHome := sites[1].getPageOldVersion(kinds.KindHome)
80
81 p1, err := enHome.Param("p1")
82 c.Assert(err, qt.IsNil)
83 c.Assert(p1, qt.Equals, "p1en")
84
85 p1, err = nnHome.Param("p1")
86 c.Assert(err, qt.IsNil)
87 c.Assert(p1, qt.Equals, "p1nn")
88 }
89
90 func writeToFs(t testing.TB, fs afero.Fs, filename, content string) {
91 t.Helper()
92 if err := afero.WriteFile(fs, filepath.FromSlash(filename), []byte(content), 0o755); err != nil {
93 t.Fatalf("Failed to write file: %s", err)
94 }
95 }
96
97 func TestBenchmarkAssembleDeepSiteWithManySections(t *testing.T) {
98 t.Parallel()
99
100 b := createBenchmarkAssembleDeepSiteWithManySectionsBuilder(t, false, 2, 3, 4).Build()
101 b.AssertFileContent("public/index.html", "Num regular pages recursive: 48|")
102 }
103
104 func createBenchmarkAssembleDeepSiteWithManySectionsBuilder(t testing.TB, skipRender bool, sectionDepth, sectionsPerLevel, pagesPerSection int) *IntegrationTestBuilder {
105 t.Helper()
106
107 const contentTemplate = `---
108 title: P%d
109 ---
110
111 ## A title
112
113 Some content with a shortcode: {{< foo >}}.
114
115 Some more content and then another shortcode: {{< foo >}}.
116
117 Some final content.
118 `
119
120 const filesTemplate = `
121 -- hugo.toml --
122 baseURL = "http://example.org/"
123 disableKinds = ["taxonomy", "term", "rss", "sitemap", "robotsTXT", "404"]
124 -- layouts/all.html --
125 All.{{ .Title }}|{{ .Content }}|Num pages: {{ len .Pages }}|Num sections: {{ len .Sections }}|Num regular pages recursive: {{ len .RegularPagesRecursive }}|
126 Sections: {{ range .Sections }}{{ .Title }}|{{ end }}|
127 RegularPagesRecursive: {{ range .RegularPagesRecursive }}{{ .RelPermalink }}|{{ end }}|
128 -- layouts/_shortcodes/foo.html --
129 `
130 page := func(section string, i int) string {
131 return fmt.Sprintf("\n-- content/%s/p%d.md --\n"+contentTemplate, section, i, i)
132 }
133
134 section := func(section string, i int) string {
135 if section != "" {
136 section = paths.AddTrailingSlash(section)
137 }
138 return fmt.Sprintf("\n-- content/%ss%d/_index.md --\n"+contentTemplate, section, i, i)
139 }
140
141 var sb strings.Builder
142
143 // s0
144 // s0/s0
145 // s0/s1
146 // etc.
147 var (
148 pageCount int
149 sectionCount int
150 )
151 var createSections func(currentSection string, currentDepth int)
152 createSections = func(currentSection string, currentDepth int) {
153 if currentDepth > sectionDepth {
154 return
155 }
156
157 for i := range sectionsPerLevel {
158 sectionCount++
159 sectionName := fmt.Sprintf("s%d", i)
160 sectionPath := sectionName
161 if currentSection != "" {
162 sectionPath = currentSection + "/" + sectionName
163 }
164 sb.WriteString(section(currentSection, i))
165
166 // Pages in this section
167 for j := range pagesPerSection {
168 pageCount++
169 sb.WriteString(page(sectionPath, j))
170 }
171
172 // Recurse
173 createSections(sectionPath, currentDepth+1)
174 }
175 }
176
177 createSections("", 1)
178
179 sb.WriteString(filesTemplate)
180
181 files := sb.String()
182
183 return NewIntegrationTestBuilder(
184 IntegrationTestConfig{
185 T: t,
186 TxtarString: files,
187 BuildCfg: BuildCfg{
188 SkipRender: skipRender,
189 },
190 },
191 )
192 }
193
194 func BenchmarkAssembleDeepSiteWithManySections(b *testing.B) {
195 runOne := func(sectionDepth, sectionsPerLevel, pagesPerSection int) {
196 name := fmt.Sprintf("depth=%d/sectionsPerLevel=%d/pagesPerSection=%d", sectionDepth, sectionsPerLevel, pagesPerSection)
197 b.Run(name, func(b *testing.B) {
198 for b.Loop() {
199 b.StopTimer()
200 bt := createBenchmarkAssembleDeepSiteWithManySectionsBuilder(b, true, sectionDepth, sectionsPerLevel, pagesPerSection)
201 b.StartTimer()
202 bt.Build()
203 }
204 })
205 }
206
207 runOne(1, 1, 50)
208 runOne(1, 6, 100)
209 runOne(1, 6, 500)
210 runOne(2, 6, 100)
211 runOne(4, 2, 100)
212 }