| 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 |
"fmt" |
| 18 |
"testing" |
| 19 |
|
| 20 |
qt "github.com/frankban/quicktest" |
| 21 |
) |
| 22 |
|
| 23 |
func TestModulesWithContent(t *testing.T) { |
| 24 |
t.Parallel() |
| 25 |
|
| 26 |
content := func(id string) string { |
| 27 |
return fmt.Sprintf(`--- |
| 28 |
title: Title %s |
| 29 |
--- |
| 30 |
Content %s |
| 31 |
|
| 32 |
`, id, id) |
| 33 |
} |
| 34 |
|
| 35 |
i18nContent := func(id, value string) string { |
| 36 |
return fmt.Sprintf(` |
| 37 |
[%s] |
| 38 |
other = %q |
| 39 |
`, id, value) |
| 40 |
} |
| 41 |
|
| 42 |
files := ` |
| 43 |
-- hugo.toml -- |
| 44 |
baseURL="https://example.org" |
| 45 |
|
| 46 |
defaultContentLanguage = "en" |
| 47 |
|
| 48 |
[module] |
| 49 |
[[module.imports]] |
| 50 |
path="a" |
| 51 |
[[module.imports.mounts]] |
| 52 |
source="myacontent" |
| 53 |
target="content/blog" |
| 54 |
[module.imports.mounts.sites.matrix] |
| 55 |
languages = "en" |
| 56 |
[[module.imports]] |
| 57 |
path="b" |
| 58 |
[[module.imports.mounts]] |
| 59 |
source="mybcontent" |
| 60 |
target="content/blog" |
| 61 |
[module.imports.mounts.sites.matrix] |
| 62 |
languages = "nn" |
| 63 |
[[module.imports]] |
| 64 |
path="c" |
| 65 |
[[module.imports]] |
| 66 |
path="d" |
| 67 |
|
| 68 |
[languages] |
| 69 |
|
| 70 |
[languages.en] |
| 71 |
title = "Title in English" |
| 72 |
label = "English" |
| 73 |
weight = 1 |
| 74 |
[languages.nn] |
| 75 |
label = "Nynorsk" |
| 76 |
weight = 2 |
| 77 |
title = "Tittel på nynorsk" |
| 78 |
[languages.nb] |
| 79 |
label = "Bokmål" |
| 80 |
weight = 3 |
| 81 |
title = "Tittel på bokmål" |
| 82 |
[languages.fr] |
| 83 |
label = "French" |
| 84 |
weight = 4 |
| 85 |
title = "French Title" |
| 86 |
|
| 87 |
-- layouts/home.html -- |
| 88 |
{{ range .Site.RegularPages }} |
| 89 |
|{{ .Title }}|{{ .RelPermalink }}|{{ .Plain }} |
| 90 |
{{ end }} |
| 91 |
{{ $data := hugo.Data }} |
| 92 |
Data Common: {{ $data.common.value }} |
| 93 |
Data C: {{ $data.c.value }} |
| 94 |
Data D: {{ $data.d.value }} |
| 95 |
All Data: {{ $data }} |
| 96 |
|
| 97 |
i18n hello1: {{ i18n "hello1" . }} |
| 98 |
i18n theme: {{ i18n "theme" . }} |
| 99 |
i18n theme2: {{ i18n "theme2" . }} |
| 100 |
-- themes/a/myacontent/page.md -- |
| 101 |
` + content("theme-a-en") + ` |
| 102 |
-- themes/b/mybcontent/page.md -- |
| 103 |
` + content("theme-b-nn") + ` |
| 104 |
-- themes/c/content/blog/c.md -- |
| 105 |
` + content("theme-c-nn") + ` |
| 106 |
-- data/common.toml -- |
| 107 |
value="Project" |
| 108 |
-- themes/c/data/common.toml -- |
| 109 |
value="Theme C" |
| 110 |
-- themes/c/data/c.toml -- |
| 111 |
value="Hugo Rocks!" |
| 112 |
-- themes/d/data/c.toml -- |
| 113 |
value="Hugo Rodcks!" |
| 114 |
-- themes/d/data/d.toml -- |
| 115 |
value="Hugo Rodks!" |
| 116 |
-- i18n/en.toml -- |
| 117 |
` + i18nContent("hello1", "Project") + ` |
| 118 |
-- themes/c/i18n/en.toml -- |
| 119 |
[hello1] |
| 120 |
other="Theme C Hello" |
| 121 |
[theme] |
| 122 |
other="Theme C" |
| 123 |
-- themes/d/i18n/en.toml -- |
| 124 |
` + i18nContent("theme", "Theme D") + ` |
| 125 |
-- themes/d/i18n/en.toml -- |
| 126 |
` + i18nContent("theme2", "Theme2 D") + ` |
| 127 |
-- themes/c/static/hello.txt -- |
| 128 |
Hugo Rocks!" |
| 129 |
` |
| 130 |
b := Test(t, files) |
| 131 |
|
| 132 |
b.AssertFileContent("public/index.html", "|Title theme-a-en|/blog/page/|Content theme-a-en") |
| 133 |
b.AssertFileContent("public/nn/index.html", "|Title theme-b-nn|/nn/blog/page/|Content theme-b-nn") |
| 134 |
|
| 135 |
// Data |
| 136 |
b.AssertFileContent("public/index.html", |
| 137 |
"Data Common: Project", |
| 138 |
"Data C: Hugo Rocks!", |
| 139 |
"Data D: Hugo Rodks!", |
| 140 |
) |
| 141 |
|
| 142 |
// i18n |
| 143 |
b.AssertFileContent("public/index.html", |
| 144 |
"i18n hello1: Project", |
| 145 |
"i18n theme: Theme C", |
| 146 |
"i18n theme2: Theme2 D", |
| 147 |
) |
| 148 |
} |
| 149 |
|
| 150 |
func TestModulesIgnoreConfig(t *testing.T) { |
| 151 |
files := ` |
| 152 |
-- hugo.toml -- |
| 153 |
baseURL="https://example.org" |
| 154 |
|
| 155 |
[module] |
| 156 |
[[module.imports]] |
| 157 |
path="a" |
| 158 |
ignoreConfig=true |
| 159 |
|
| 160 |
-- themes/a/config.toml -- |
| 161 |
[params] |
| 162 |
a = "Should Be Ignored!" |
| 163 |
-- layouts/home.html -- |
| 164 |
Params: {{ .Site.Params }} |
| 165 |
` |
| 166 |
Test(t, files).AssertFileContent("public/index.html", "! Ignored") |
| 167 |
} |
| 168 |
|
| 169 |
func TestModulesDisabled(t *testing.T) { |
| 170 |
files := ` |
| 171 |
-- hugo.toml -- |
| 172 |
baseURL="https://example.org" |
| 173 |
|
| 174 |
[module] |
| 175 |
[[module.imports]] |
| 176 |
path="a" |
| 177 |
[[module.imports]] |
| 178 |
path="b" |
| 179 |
disable=true |
| 180 |
|
| 181 |
-- themes/a/config.toml -- |
| 182 |
[params] |
| 183 |
a = "A param" |
| 184 |
-- themes/b/config.toml -- |
| 185 |
[params] |
| 186 |
b = "B param" |
| 187 |
-- layouts/home.html -- |
| 188 |
Params: {{ .Site.Params }} |
| 189 |
` |
| 190 |
Test(t, files).AssertFileContent("public/index.html", "A param", "! B param") |
| 191 |
} |
| 192 |
|
| 193 |
func TestModulesIncompatible(t *testing.T) { |
| 194 |
t.Parallel() |
| 195 |
|
| 196 |
files := ` |
| 197 |
-- hugo.toml -- |
| 198 |
baseURL="https://example.org" |
| 199 |
|
| 200 |
[module] |
| 201 |
[[module.imports]] |
| 202 |
path="ok" |
| 203 |
[[module.imports]] |
| 204 |
path="incompat1" |
| 205 |
[[module.imports]] |
| 206 |
path="incompat2" |
| 207 |
[[module.imports]] |
| 208 |
path="incompat3" |
| 209 |
|
| 210 |
-- themes/ok/data/ok.toml -- |
| 211 |
title = "OK" |
| 212 |
-- themes/incompat1/config.toml -- |
| 213 |
|
| 214 |
[module] |
| 215 |
[module.hugoVersion] |
| 216 |
min = "0.33.2" |
| 217 |
max = "0.45.0" |
| 218 |
|
| 219 |
-- themes/incompat2/theme.toml -- |
| 220 |
min_version = "5.0.0" |
| 221 |
|
| 222 |
-- themes/incompat3/theme.toml -- |
| 223 |
min_version = 0.55.0 |
| 224 |
|
| 225 |
` |
| 226 |
b := Test(t, files, TestOptWarn()) |
| 227 |
b.AssertLogContains("is not compatible with this Hugo version") |
| 228 |
} |
| 229 |
|
| 230 |
func TestMountsProject(t *testing.T) { |
| 231 |
t.Parallel() |
| 232 |
|
| 233 |
files := ` |
| 234 |
-- hugo.toml -- |
| 235 |
baseURL="https://example.org" |
| 236 |
|
| 237 |
[module] |
| 238 |
[[module.mounts]] |
| 239 |
source="mycontent" |
| 240 |
target="content" |
| 241 |
-- layouts/single.html -- |
| 242 |
Permalink: {{ .Permalink }}| |
| 243 |
-- mycontent/mypage.md -- |
| 244 |
--- |
| 245 |
title: "My Page" |
| 246 |
--- |
| 247 |
` |
| 248 |
b := Test(t, files) |
| 249 |
|
| 250 |
b.AssertFileContent("public/mypage/index.html", "Permalink: https://example.org/mypage/|") |
| 251 |
} |
| 252 |
|
| 253 |
// https://github.com/gohugoio/hugo/issues/6684 |
| 254 |
func TestMountsContentFile(t *testing.T) { |
| 255 |
files := ` |
| 256 |
-- hugo.toml -- |
| 257 |
disableKinds = ["taxonomy", "term", "RSS", "sitemap", "robotsTXT", "page", "section"] |
| 258 |
disableLiveReload = true |
| 259 |
[module] |
| 260 |
[[module.mounts]] |
| 261 |
source = "README.md" |
| 262 |
target = "content/_index.md" |
| 263 |
-- README.md -- |
| 264 |
# Hello World |
| 265 |
-- layouts/home.html -- |
| 266 |
Home: {{ .Title }}|{{ .Content }}| |
| 267 |
` |
| 268 |
b := Test(t, files) |
| 269 |
b.AssertFileContent("public/index.html", "Home: |<h1 id=\"hello-world\">Hello World</h1>\n|") |
| 270 |
} |
| 271 |
|
| 272 |
// https://github.com/gohugoio/hugo/issues/14543 |
| 273 |
func TestModulePrivateRepo(t *testing.T) { |
| •274 |
t.Skip("This tests needs internet access") |
| 275 |
t.Parallel() |
| 276 |
|
| 277 |
files := ` |
| 278 |
-- hugo.toml -- |
| 279 |
baseURL = "https://example.com/" |
| 280 |
[module] |
| 281 |
[[module.imports]] |
| 282 |
path = "github.com/bep/this-is-a-non-existing-repo" |
| 283 |
version = "main" |
| 284 |
[[module.imports.mounts]] |
| 285 |
source = "content" |
| 286 |
target = "content" |
| 287 |
-- layouts/all.html -- |
| 288 |
Title: {{ .Title }}| |
| 289 |
List: {{ .Title }} |
| 290 |
` |
| 291 |
|
| 292 |
b, err := TestE(t, files, TestOptOsFs()) |
| 293 |
b.Assert(err, qt.ErrorMatches, `(?s).*mod download.*invalid version.*repository.*`) |
| 294 |
} |
| 295 |
|
| 296 |
// https://github.com/gohugoio/hugo/issues/6299 |
| 297 |
func TestSiteWithGoModButNoModules(t *testing.T) { |
| 298 |
t.Parallel() |
| 299 |
|
| 300 |
files := ` |
| 301 |
-- hugo.toml -- |
| 302 |
baseURL = "https://example.org" |
| 303 |
-- go.mod -- |
| 304 |
|
| 305 |
` |
| 306 |
|
| 307 |
b, err := TestE(t, files, TestOptOsFs()) |
| 308 |
b.Assert(err, qt.IsNil) |
| 309 |
} |
| 310 |
|
| 311 |
// Issue 9426 |
| 312 |
func TestMountSameSource(t *testing.T) { |
| 313 |
files := ` |
| 314 |
-- hugo.toml -- |
| 315 |
baseURL = 'https://example.org/' |
| 316 |
locale = 'en-us' |
| 317 |
title = 'Hugo GitHub Issue #9426' |
| 318 |
|
| 319 |
disableKinds = ['RSS','sitemap','taxonomy','term'] |
| 320 |
|
| 321 |
[[module.mounts]] |
| 322 |
source = "content" |
| 323 |
target = "content" |
| 324 |
|
| 325 |
[[module.mounts]] |
| 326 |
source = "extra-content" |
| 327 |
target = "content/resources-a" |
| 328 |
|
| 329 |
[[module.mounts]] |
| 330 |
source = "extra-content" |
| 331 |
target = "content/resources-b" |
| 332 |
-- layouts/single.html -- |
| 333 |
Single |
| 334 |
-- content/p1.md -- |
| 335 |
-- extra-content/_index.md -- |
| 336 |
-- extra-content/subdir/_index.md -- |
| 337 |
-- extra-content/subdir/about.md -- |
| 338 |
" |
| 339 |
` |
| 340 |
b := Test(t, files) |
| 341 |
|
| 342 |
b.AssertFileContent("public/resources-a/subdir/about/index.html", "Single") |
| 343 |
b.AssertFileContent("public/resources-b/subdir/about/index.html", "Single") |
| 344 |
} |
| 345 |
|
| 346 |
func TestMountData(t *testing.T) { |
| 347 |
files := ` |
| 348 |
-- hugo.toml -- |
| 349 |
baseURL = 'https://example.org/' |
| 350 |
disableKinds = ["taxonomy", "term", "RSS", "sitemap", "robotsTXT", "page", "section"] |
| 351 |
|
| 352 |
[[module.mounts]] |
| 353 |
source = "data" |
| 354 |
target = "data" |
| 355 |
|
| 356 |
[[module.mounts]] |
| 357 |
source = "extra-data" |
| 358 |
target = "data/extra" |
| 359 |
-- extra-data/test.yaml -- |
| 360 |
message: Hugo Rocks |
| 361 |
-- layouts/home.html -- |
| 362 |
{{ hugo.Data.extra.test.message }} |
| 363 |
` |
| 364 |
|
| 365 |
b := Test(t, files) |
| 366 |
|
| 367 |
b.AssertFileContent("public/index.html", "Hugo Rocks") |
| 368 |
} |