Debian Codegraph @

config/allconfig/allconfig_integration_test.go

raw
1 package allconfig_test
2
3 import (
4 "path/filepath"
5 "strings"
6 "testing"
7
8 qt "github.com/frankban/quicktest"
9 "github.com/gohugoio/hugo/common/hugo"
10 "github.com/gohugoio/hugo/config/allconfig"
11 "github.com/gohugoio/hugo/hugolib"
12 "github.com/gohugoio/hugo/media"
13 )
14
15 func TestDirsMount(t *testing.T) {
16 files := `
17 -- hugo.toml --
18 baseURL = "https://example.com"
19 disableKinds = ["taxonomy", "term"]
20 [languages]
21 [languages.en]
22 weight = 1
23 [languages.sv]
24 weight = 2
25 [[module.mounts]]
26 source = 'content/en'
27 target = 'content'
28 [module.mounts.sites.matrix]
29 languages = 'en'
30 [[module.mounts]]
31 source = 'content/sv'
32 target = 'content'
33 [module.mounts.sites.matrix]
34 languages = 'sv'
35 -- content/en/p1.md --
36 ---
37 title: "p1"
38 ---
39 -- content/sv/p1.md --
40 ---
41 title: "p1"
42 ---
43 -- layouts/single.html --
44 Title: {{ .Title }}
45 `
46
47 b := hugolib.Test(t, files)
48
49 // b.AssertFileContent("public/p1/index.html", "Title: p1")
50
51 sites := b.H.Sites
52 b.Assert(len(sites), qt.Equals, 2)
53
54 configs := b.H.Configs
55 mods := configs.Modules
56 b.Assert(len(mods), qt.Equals, 1)
57 mod := mods[0]
58 b.Assert(mod.Mounts(), qt.HasLen, 8)
59
60 enConcp := sites[0].Conf
61 enConf := enConcp.GetConfig().(*allconfig.Config)
62
63 b.Assert(enConcp.BaseURL().String(), qt.Equals, "https://example.com/")
64 modConf := enConf.Module
65 b.Assert(modConf.Mounts, qt.HasLen, 8)
66 b.Assert(modConf.Mounts[0].Source, qt.Equals, filepath.FromSlash("content/en"))
67 b.Assert(modConf.Mounts[0].Target, qt.Equals, "content")
68 b.Assert(modConf.Mounts[0].Lang, qt.Equals, "")
69 b.Assert(modConf.Mounts[0].Sites.Matrix.Languages, qt.DeepEquals, []string{"en"})
70 b.Assert(modConf.Mounts[1].Source, qt.Equals, filepath.FromSlash("content/sv"))
71 b.Assert(modConf.Mounts[1].Target, qt.Equals, "content")
72 b.Assert(modConf.Mounts[1].Lang, qt.Equals, "")
73 b.Assert(modConf.Mounts[1].Sites.Matrix.Languages, qt.DeepEquals, []string{"sv"})
74 }
75
76 func TestConfigAliases(t *testing.T) {
77 files := `
78 -- hugo.toml --
79 baseURL = "https://example.com"
80 logI18nWarnings = true
81 logPathWarnings = true
82 `
83 b := hugolib.Test(t, files)
84
85 conf := b.H.Configs.Base
86
87 b.Assert(conf.PrintI18nWarnings, qt.Equals, true)
88 b.Assert(conf.PrintPathWarnings, qt.Equals, true)
89 }
90
91 func TestRedefineContentTypes(t *testing.T) {
92 files := `
93 -- hugo.toml --
94 baseURL = "https://example.com"
95 [mediaTypes]
96 [mediaTypes."text/html"]
97 suffixes = ["html", "xhtml"]
98 `
99
100 b := hugolib.Test(t, files)
101
102 conf := b.H.Configs.Base
103 contentTypes := conf.ContentTypes.Config
104
105 b.Assert(contentTypes.HTML.Suffixes(), qt.DeepEquals, []string{"html", "xhtml"})
106 b.Assert(contentTypes.Markdown.Suffixes(), qt.DeepEquals, []string{"md", "mdown", "markdown"})
107 }
108
109 func TestPaginationConfig(t *testing.T) {
110 files := `
111 -- hugo.toml --
112 [languages.en]
113 weight = 1
114 [languages.en.pagination]
115 pagerSize = 20
116 [languages.de]
117 weight = 2
118 [languages.de.pagination]
119 path = "page-de"
120
121 `
122
123 b := hugolib.Test(t, files)
124
125 confEn := b.H.Sites[0].Conf.Pagination()
126 confDe := b.H.Sites[1].Conf.Pagination()
127
128 b.Assert(confEn.Path, qt.Equals, "page")
129 b.Assert(confEn.PagerSize, qt.Equals, 20)
130 b.Assert(confDe.Path, qt.Equals, "page-de")
131 b.Assert(confDe.PagerSize, qt.Equals, 10)
132 }
133
134 func TestPaginationConfigDisableAliases(t *testing.T) {
135 files := `
136 -- hugo.toml --
137 disableKinds = ["taxonomy", "term"]
138 [pagination]
139 disableAliases = true
140 pagerSize = 2
141 -- layouts/list.html --
142 {{ $paginator := .Paginate site.RegularPages }}
143 {{ template "_internal/pagination.html" . }}
144 {{ range $paginator.Pages }}
145 {{ .Title }}
146 {{ end }}
147 -- content/p1.md --
148 ---
149 title: "p1"
150 ---
151 -- content/p2.md --
152 ---
153 title: "p2"
154 ---
155 -- content/p3.md --
156 ---
157 title: "p3"
158 ---
159 `
160
161 b := hugolib.Test(t, files)
162
163 b.AssertFileExists("public/page/1/index.html", false)
164 b.AssertFileContent("public/page/2/index.html", "pagination-default")
165 }
166
167 func TestMapUglyURLs(t *testing.T) {
168 files := `
169 -- hugo.toml --
170 [uglyurls]
171 posts = true
172 `
173
174 b := hugolib.Test(t, files)
175
176 c := b.H.Configs.Base
177
178 b.Assert(c.C.IsUglyURLSection("posts"), qt.IsTrue)
179 b.Assert(c.C.IsUglyURLSection("blog"), qt.IsFalse)
180 }
181
182 // Issue 13199
183 func TestInvalidOutputFormat(t *testing.T) {
184 t.Parallel()
185
186 files := `
187 -- hugo.toml --
188 disableKinds = ['page','rss','section','sitemap','taxonomy','term']
189 [outputs]
190 home = ['html','foo']
191 -- layouts/home.html --
192 x
193 `
194
195 b, err := hugolib.TestE(t, files)
196 b.Assert(err, qt.IsNotNil)
197 b.Assert(err.Error(), qt.Contains, `failed to create config: unknown output format "foo" for kind "home"`)
198 }
199
200 func TestContentTypesDefault(t *testing.T) {
201 files := `
202 -- hugo.toml --
203 baseURL = "https://example.com"
204
205
206 `
207
208 b := hugolib.Test(t, files)
209
210 ct := b.H.Configs.Base.ContentTypes
211 c := ct.Config
212 s := ct.SourceStructure.(map[string]media.ContentTypeConfig)
213
214 b.Assert(c.IsContentFile("foo.md"), qt.Equals, true)
215 b.Assert(len(s), qt.Equals, 6)
216 }
217
218 func TestMergeDeep(t *testing.T) {
219 t.Parallel()
220
221 files := `
222 -- hugo.toml --
223 baseURL = "https://example.com"
224 theme = ["theme1", "theme2"]
225 _merge = "deep"
226 -- themes/theme1/hugo.toml --
227 [sitemap]
228 filename = 'mysitemap.xml'
229 [services]
230 [services.googleAnalytics]
231 id = 'foo bar'
232 [taxonomies]
233 foo = 'bars'
234 -- themes/theme2/config/_default/hugo.toml --
235 [taxonomies]
236 bar = 'baz'
237 -- layouts/home.html --
238 GA ID: {{ site.Config.Services.GoogleAnalytics.ID }}.
239
240 `
241
242 b := hugolib.Test(t, files)
243
244 conf := b.H.Configs
245 base := conf.Base
246
247 b.Assert(base.Environment, qt.Equals, hugo.EnvironmentProduction)
248 b.Assert(base.BaseURL, qt.Equals, "https://example.com")
249 b.Assert(base.Sitemap.Filename, qt.Equals, "mysitemap.xml")
250 b.Assert(base.Taxonomies, qt.DeepEquals, map[string]string{"bar": "baz", "foo": "bars"})
251
252 b.AssertFileContent("public/index.html", "GA ID: foo bar.")
253 }
254
255 func TestMergeDeepBuildStats(t *testing.T) {
256 t.Parallel()
257
258 files := `
259 -- hugo.toml --
260 baseURL = "https://example.com"
261 title = "Theme 1"
262 _merge = "deep"
263 [module]
264 [module.hugoVersion]
265 [[module.imports]]
266 path = "theme1"
267 -- themes/theme1/hugo.toml --
268 [build]
269 [build.buildStats]
270 disableIDs = true
271 enable = true
272 -- layouts/home.html --
273 Home.
274
275 `
276
277 b := hugolib.Test(t, files, hugolib.TestOptOsFs())
278
279 conf := b.H.Configs
280 base := conf.Base
281
282 b.Assert(base.Title, qt.Equals, "Theme 1")
283 b.Assert(len(base.Module.Imports), qt.Equals, 1)
284 b.Assert(base.Build.BuildStats.Enable, qt.Equals, true)
285 b.AssertFileExists("/hugo_stats.json", true)
286 }
287
288 func TestMergeDeepBuildStatsTheme(t *testing.T) {
289 t.Parallel()
290
291 files := `
292 -- hugo.toml --
293 baseURL = "https://example.com"
294 _merge = "deep"
295 theme = ["theme1"]
296 -- themes/theme1/hugo.toml --
297 title = "Theme 1"
298 [build]
299 [build.buildStats]
300 disableIDs = true
301 enable = true
302 -- layouts/home.html --
303 Home.
304
305 `
306
307 b := hugolib.Test(t, files, hugolib.TestOptOsFs())
308
309 conf := b.H.Configs
310 base := conf.Base
311
312 b.Assert(base.Title, qt.Equals, "Theme 1")
313 b.Assert(len(base.Module.Imports), qt.Equals, 1)
314 b.Assert(base.Build.BuildStats.Enable, qt.Equals, true)
315 b.AssertFileExists("/hugo_stats.json", true)
316 }
317
318 func TestDefaultConfigLanguageBlankWhenNoEnglishExists(t *testing.T) {
319 t.Parallel()
320
321 files := `
322 -- hugo.toml --
323 baseURL = "https://example.com"
324 [languages]
325 [languages.nn]
326 weight = 20
327 [languages.sv]
328 weight = 10
329 [languages.sv.taxonomies]
330 tag = "taggar"
331 -- layouts/all.html --
332 All.
333 `
334
335 b := hugolib.Test(t, files)
336
337 b.Assert(b.H.Conf.DefaultContentLanguage(), qt.Equals, "sv")
338 }
339
340 func TestDefaultConfigEnvDisableLanguagesIssue13707(t *testing.T) {
341 t.Parallel()
342
343 files := `
344 -- hugo.toml --
345 disableLanguages = []
346 [languages]
347 [languages.en]
348 weight = 1
349 [languages.nn]
350 weight = 2
351 [languages.sv]
352 weight = 3
353 `
354
355 b := hugolib.Test(t, files, hugolib.TestOptWithConfig(func(conf *hugolib.IntegrationTestConfig) {
356 conf.Environ = []string{`HUGO_DISABLELANGUAGES=sv nn`}
357 }))
358
359 b.Assert(len(b.H.Sites), qt.Equals, 1)
360 }
361
362 // Issue 13535
363 // We changed enablement of the embedded link and image render hooks from
364 // booleans to enums in v0.148.0. This should throw error with v0.163.0 and later.
365 func TestLegacyEmbeddedRenderHookEnablement(t *testing.T) {
366 files := `
367 -- hugo.toml --
368 [markup.goldmark.renderHooks.image]
369 #KEY_VALUE_IMAGE
370
371 [markup.goldmark.renderHooks.link]
372 #KEY_VALUE_LINK
373 `
374
375 replacer := strings.NewReplacer(
376 "#KEY_VALUE_IMAGE", "enableDefault = false",
377 "#KEY_VALUE_LINK", "",
378 )
379 f := replacer.Replace(files)
380 b, _ := hugolib.TestE(t, f)
381 b.AssertLogContains("ERROR deprecated")
382
383 replacer = strings.NewReplacer(
384 "#KEY_VALUE_IMAGE", "enableDefault = true",
385 "#KEY_VALUE_LINK", "",
386 )
387 f = replacer.Replace(files)
388 b, _ = hugolib.TestE(t, f)
389 b.AssertLogContains("ERROR deprecated")
390
391 replacer = strings.NewReplacer(
392 "#KEY_VALUE_IMAGE", "",
393 "#KEY_VALUE_LINK", "enableDefault = false",
394 )
395 f = replacer.Replace(files)
396 b, _ = hugolib.TestE(t, f)
397 b.AssertLogContains("ERROR deprecated")
398
399 replacer = strings.NewReplacer(
400 "#KEY_VALUE_IMAGE", "",
401 "#KEY_VALUE_LINK", "enableDefault = true",
402 )
403 f = replacer.Replace(files)
404 b, _ = hugolib.TestE(t, f)
405 b.AssertLogContains("ERROR deprecated")
406 }