| 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 |
// See issue 15253. |
| 201 |
func TestInvalidDefaultOutputFormat(t *testing.T) { |
| 202 |
t.Parallel() |
| 203 |
|
| 204 |
files := ` |
| 205 |
-- hugo.toml -- |
| 206 |
disableKinds = ['page','rss','section','sitemap','taxonomy','term'] |
| 207 |
defaultOutputFormat = 'foo' |
| 208 |
-- layouts/home.html -- |
| 209 |
x |
| 210 |
` |
| 211 |
|
| 212 |
b, err := hugolib.TestE(t, files) |
| 213 |
b.Assert(err, qt.IsNotNil) |
| 214 |
b.Assert(err.Error(), qt.Contains, `failed to create config: unknown default output format "foo"`) |
| 215 |
} |
| 216 |
|
| 217 |
func TestContentTypesDefault(t *testing.T) { |
| 218 |
files := ` |
| 219 |
-- hugo.toml -- |
| 220 |
baseURL = "https://example.com" |
| 221 |
|
| 222 |
|
| 223 |
` |
| 224 |
|
| 225 |
b := hugolib.Test(t, files) |
| 226 |
|
| 227 |
ct := b.H.Configs.Base.ContentTypes |
| 228 |
c := ct.Config |
| 229 |
s := ct.SourceStructure.(map[string]media.ContentTypeConfig) |
| 230 |
|
| 231 |
b.Assert(c.IsContentFile("foo.md"), qt.Equals, true) |
| 232 |
b.Assert(len(s), qt.Equals, 6) |
| 233 |
} |
| 234 |
|
| 235 |
func TestMergeDeep(t *testing.T) { |
| 236 |
t.Parallel() |
| 237 |
|
| 238 |
files := ` |
| 239 |
-- hugo.toml -- |
| 240 |
baseURL = "https://example.com" |
| 241 |
theme = ["theme1", "theme2"] |
| 242 |
_merge = "deep" |
| 243 |
-- themes/theme1/hugo.toml -- |
| 244 |
[sitemap] |
| 245 |
filename = 'mysitemap.xml' |
| 246 |
[services] |
| 247 |
[services.googleAnalytics] |
| 248 |
id = 'foo bar' |
| 249 |
[taxonomies] |
| 250 |
foo = 'bars' |
| 251 |
-- themes/theme2/config/_default/hugo.toml -- |
| 252 |
[taxonomies] |
| 253 |
bar = 'baz' |
| 254 |
-- layouts/home.html -- |
| 255 |
GA ID: {{ site.Config.Services.GoogleAnalytics.ID }}. |
| 256 |
|
| 257 |
` |
| 258 |
|
| 259 |
b := hugolib.Test(t, files) |
| 260 |
|
| 261 |
conf := b.H.Configs |
| 262 |
base := conf.Base |
| 263 |
|
| 264 |
b.Assert(base.Environment, qt.Equals, hugo.EnvironmentProduction) |
| 265 |
b.Assert(base.BaseURL, qt.Equals, "https://example.com") |
| 266 |
b.Assert(base.Sitemap.Filename, qt.Equals, "mysitemap.xml") |
| 267 |
b.Assert(base.Taxonomies, qt.DeepEquals, map[string]string{"bar": "baz", "foo": "bars"}) |
| 268 |
|
| 269 |
b.AssertFileContent("public/index.html", "GA ID: foo bar.") |
| 270 |
} |
| 271 |
|
| 272 |
func TestMergeDeepBuildStats(t *testing.T) { |
| 273 |
t.Parallel() |
| 274 |
|
| 275 |
files := ` |
| 276 |
-- hugo.toml -- |
| 277 |
baseURL = "https://example.com" |
| 278 |
title = "Theme 1" |
| 279 |
_merge = "deep" |
| 280 |
[module] |
| 281 |
[module.hugoVersion] |
| 282 |
[[module.imports]] |
| 283 |
path = "theme1" |
| 284 |
-- themes/theme1/hugo.toml -- |
| 285 |
[build] |
| 286 |
[build.buildStats] |
| 287 |
disableIDs = true |
| 288 |
enable = true |
| 289 |
-- layouts/home.html -- |
| 290 |
Home. |
| 291 |
|
| 292 |
` |
| 293 |
|
| 294 |
b := hugolib.Test(t, files, hugolib.TestOptOsFs()) |
| 295 |
|
| 296 |
conf := b.H.Configs |
| 297 |
base := conf.Base |
| 298 |
|
| 299 |
b.Assert(base.Title, qt.Equals, "Theme 1") |
| 300 |
b.Assert(len(base.Module.Imports), qt.Equals, 1) |
| 301 |
b.Assert(base.Build.BuildStats.Enable, qt.Equals, true) |
| 302 |
b.AssertFileExists("/hugo_stats.json", true) |
| 303 |
} |
| 304 |
|
| 305 |
func TestMergeDeepBuildStatsTheme(t *testing.T) { |
| 306 |
t.Parallel() |
| 307 |
|
| 308 |
files := ` |
| 309 |
-- hugo.toml -- |
| 310 |
baseURL = "https://example.com" |
| 311 |
_merge = "deep" |
| 312 |
theme = ["theme1"] |
| 313 |
-- themes/theme1/hugo.toml -- |
| 314 |
title = "Theme 1" |
| 315 |
[build] |
| 316 |
[build.buildStats] |
| 317 |
disableIDs = true |
| 318 |
enable = true |
| 319 |
-- layouts/home.html -- |
| 320 |
Home. |
| 321 |
|
| 322 |
` |
| 323 |
|
| 324 |
b := hugolib.Test(t, files, hugolib.TestOptOsFs()) |
| 325 |
|
| 326 |
conf := b.H.Configs |
| 327 |
base := conf.Base |
| 328 |
|
| 329 |
b.Assert(base.Title, qt.Equals, "Theme 1") |
| 330 |
b.Assert(len(base.Module.Imports), qt.Equals, 1) |
| 331 |
b.Assert(base.Build.BuildStats.Enable, qt.Equals, true) |
| 332 |
b.AssertFileExists("/hugo_stats.json", true) |
| 333 |
} |
| 334 |
|
| 335 |
func TestDefaultConfigLanguageBlankWhenNoEnglishExists(t *testing.T) { |
| 336 |
t.Parallel() |
| 337 |
|
| 338 |
files := ` |
| 339 |
-- hugo.toml -- |
| 340 |
baseURL = "https://example.com" |
| 341 |
[languages] |
| 342 |
[languages.nn] |
| 343 |
weight = 20 |
| 344 |
[languages.sv] |
| 345 |
weight = 10 |
| 346 |
[languages.sv.taxonomies] |
| 347 |
tag = "taggar" |
| 348 |
-- layouts/all.html -- |
| 349 |
All. |
| 350 |
` |
| 351 |
|
| 352 |
b := hugolib.Test(t, files) |
| 353 |
|
| 354 |
b.Assert(b.H.Conf.DefaultContentLanguage(), qt.Equals, "sv") |
| 355 |
} |
| 356 |
|
| 357 |
func TestDefaultConfigEnvDisableLanguagesIssue13707(t *testing.T) { |
| 358 |
t.Parallel() |
| 359 |
|
| 360 |
files := ` |
| 361 |
-- hugo.toml -- |
| 362 |
disableLanguages = [] |
| 363 |
[languages] |
| 364 |
[languages.en] |
| 365 |
weight = 1 |
| 366 |
[languages.nn] |
| 367 |
weight = 2 |
| 368 |
[languages.sv] |
| 369 |
weight = 3 |
| 370 |
` |
| 371 |
|
| 372 |
b := hugolib.Test(t, files, hugolib.TestOptWithConfig(func(conf *hugolib.IntegrationTestConfig) { |
| 373 |
conf.Environ = []string{`HUGO_DISABLELANGUAGES=sv nn`} |
| 374 |
})) |
| 375 |
|
| 376 |
b.Assert(len(b.H.Sites), qt.Equals, 1) |
| 377 |
} |
| 378 |
|
| 379 |
// Issue 13535 |
| 380 |
// We changed enablement of the embedded link and image render hooks from |
| 381 |
// booleans to enums in v0.148.0. This should throw error with v0.163.0 and later. |
| 382 |
func TestLegacyEmbeddedRenderHookEnablement(t *testing.T) { |
| 383 |
files := ` |
| 384 |
-- hugo.toml -- |
| 385 |
[markup.goldmark.renderHooks.image] |
| 386 |
#KEY_VALUE_IMAGE |
| 387 |
|
| 388 |
[markup.goldmark.renderHooks.link] |
| 389 |
#KEY_VALUE_LINK |
| 390 |
` |
| 391 |
|
| 392 |
replacer := strings.NewReplacer( |
| 393 |
"#KEY_VALUE_IMAGE", "enableDefault = false", |
| 394 |
"#KEY_VALUE_LINK", "", |
| 395 |
) |
| 396 |
f := replacer.Replace(files) |
| 397 |
b, _ := hugolib.TestE(t, f) |
| 398 |
b.AssertLogContains("ERROR deprecated") |
| 399 |
|
| 400 |
replacer = strings.NewReplacer( |
| 401 |
"#KEY_VALUE_IMAGE", "enableDefault = true", |
| 402 |
"#KEY_VALUE_LINK", "", |
| 403 |
) |
| 404 |
f = replacer.Replace(files) |
| 405 |
b, _ = hugolib.TestE(t, f) |
| 406 |
b.AssertLogContains("ERROR deprecated") |
| 407 |
|
| 408 |
replacer = strings.NewReplacer( |
| 409 |
"#KEY_VALUE_IMAGE", "", |
| 410 |
"#KEY_VALUE_LINK", "enableDefault = false", |
| 411 |
) |
| 412 |
f = replacer.Replace(files) |
| 413 |
b, _ = hugolib.TestE(t, f) |
| 414 |
b.AssertLogContains("ERROR deprecated") |
| 415 |
|
| 416 |
replacer = strings.NewReplacer( |
| 417 |
"#KEY_VALUE_IMAGE", "", |
| 418 |
"#KEY_VALUE_LINK", "enableDefault = true", |
| 419 |
) |
| 420 |
f = replacer.Replace(files) |
| 421 |
b, _ = hugolib.TestE(t, f) |
| 422 |
b.AssertLogContains("ERROR deprecated") |
| 423 |
} |