| 1 |
// Copyright 2024 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 "testing" |
| 17 |
|
| 18 |
func TestConfigDir(t *testing.T) { |
| 19 |
t.Parallel() |
| 20 |
|
| 21 |
files := ` |
| 22 |
-- config/_default/params.toml -- |
| 23 |
a = "acp1" |
| 24 |
d = "dcp1" |
| 25 |
-- config/_default/config.toml -- |
| 26 |
[params] |
| 27 |
a = "ac1" |
| 28 |
b = "bc1" |
| 29 |
|
| 30 |
-- hugo.toml -- |
| 31 |
baseURL = "https://example.com" |
| 32 |
disableKinds = ["taxonomy", "term", "RSS", "sitemap", "robotsTXT", "page", "section"] |
| 33 |
ignoreErrors = ["error-missing-instagram-accesstoken"] |
| 34 |
[params] |
| 35 |
a = "a1" |
| 36 |
b = "b1" |
| 37 |
c = "c1" |
| 38 |
-- layouts/home.html -- |
| 39 |
Params: {{ site.Params}} |
| 40 |
` |
| 41 |
b := Test(t, files) |
| 42 |
|
| 43 |
b.AssertFileContent("public/index.html", ` |
| 44 |
Params: map[a:acp1 b:bc1 c:c1 d:dcp1] |
| 45 |
|
| 46 |
|
| 47 |
`) |
| 48 |
} |
| 49 |
|
| 50 |
// TOML/YAML can't represent a top-level array, so a basename-matched wrapper |
| 51 |
// key in the config file unwraps to the slice-typed root key. |
| 52 |
func TestConfigDirCascadeSliceIssue12899(t *testing.T) { |
| 53 |
t.Parallel() |
| 54 |
|
| 55 |
files := ` |
| 56 |
-- hugo.toml -- |
| 57 |
baseURL = "https://example.com" |
| 58 |
disableKinds = ["taxonomy", "term", "RSS", "sitemap", "robotsTXT", "home", "section"] |
| 59 |
-- config/_default/cascade.yaml -- |
| 60 |
cascade: |
| 61 |
- target: |
| 62 |
path: /books/** |
| 63 |
params: |
| 64 |
color: red |
| 65 |
- target: |
| 66 |
path: /films/** |
| 67 |
params: |
| 68 |
color: blue |
| 69 |
-- content/books/b1.md -- |
| 70 |
--- |
| 71 |
title: B1 |
| 72 |
--- |
| 73 |
-- content/films/f1.md -- |
| 74 |
--- |
| 75 |
title: F1 |
| 76 |
--- |
| 77 |
-- layouts/page.html -- |
| 78 |
{{ .Title }}|color:{{ .Params.color }}| |
| 79 |
` |
| 80 |
b := Test(t, files) |
| 81 |
|
| 82 |
b.AssertFileContent("public/books/b1/index.html", "B1|color:red|") |
| 83 |
b.AssertFileContent("public/films/f1/index.html", "F1|color:blue|") |
| 84 |
} |
| 85 |
|
| 86 |
func TestConfigDirPermalinksSliceIssue12899(t *testing.T) { |
| 87 |
t.Parallel() |
| 88 |
|
| 89 |
files := ` |
| 90 |
-- hugo.toml -- |
| 91 |
baseURL = "https://example.com" |
| 92 |
disableKinds = ["taxonomy", "term", "RSS", "sitemap", "robotsTXT", "home", "section"] |
| 93 |
-- config/_default/permalinks.yaml -- |
| 94 |
permalinks: |
| 95 |
- target: |
| 96 |
path: /books/** |
| 97 |
pattern: /shelf/:slug/ |
| 98 |
-- content/books/b1.md -- |
| 99 |
--- |
| 100 |
title: B1 |
| 101 |
slug: novel |
| 102 |
--- |
| 103 |
-- layouts/page.html -- |
| 104 |
{{ .Title }}|{{ .RelPermalink }}| |
| 105 |
` |
| 106 |
b := Test(t, files) |
| 107 |
|
| 108 |
b.AssertFileContent("public/shelf/novel/index.html", "B1|/shelf/novel/|") |
| 109 |
} |
| 110 |
|
| 111 |
func TestConfigDirCascadeEnvironmentOverrideIssue12899(t *testing.T) { |
| 112 |
t.Parallel() |
| 113 |
|
| 114 |
files := ` |
| 115 |
-- hugo.toml -- |
| 116 |
baseURL = "https://example.com" |
| 117 |
disableKinds = ["taxonomy", "term", "RSS", "sitemap", "robotsTXT", "home", "section"] |
| 118 |
-- config/_default/cascade.yaml -- |
| 119 |
cascade: |
| 120 |
- target: |
| 121 |
path: /** |
| 122 |
params: |
| 123 |
color: default |
| 124 |
-- config/production/cascade.yaml -- |
| 125 |
cascade: |
| 126 |
- target: |
| 127 |
path: /** |
| 128 |
params: |
| 129 |
color: production |
| 130 |
-- content/p1.md -- |
| 131 |
--- |
| 132 |
title: P1 |
| 133 |
--- |
| 134 |
-- layouts/page.html -- |
| 135 |
{{ .Title }}|color:{{ .Params.color }}| |
| 136 |
` |
| 137 |
b := Test(t, files) |
| 138 |
|
| 139 |
b.AssertFileContent("public/p1/index.html", "P1|color:production|") |
| 140 |
} |
| 141 |
|
| 142 |
// The basename-match unwrap is type-agnostic — also unwraps maps. |
| 143 |
func TestConfigDirRepeatRootMapIssue14882(t *testing.T) { |
| 144 |
t.Parallel() |
| 145 |
|
| 146 |
files := ` |
| 147 |
-- hugo.toml -- |
| 148 |
baseURL = "https://example.com" |
| 149 |
disableKinds = ["taxonomy", "term", "RSS", "sitemap", "robotsTXT", "section"] |
| 150 |
-- config/_default/params.yaml -- |
| 151 |
params: |
| 152 |
a: aval |
| 153 |
b: bval |
| 154 |
-- layouts/home.html -- |
| 155 |
a:{{ site.Params.a }}|b:{{ site.Params.b }}| |
| 156 |
` |
| 157 |
b := Test(t, files) |
| 158 |
|
| 159 |
b.AssertFileContent("public/index.html", "a:aval|b:bval|") |
| 160 |
} |
| 161 |
|
| 162 |
// The unwrap fires only when the basename-matched key is the sole top-level |
| 163 |
// key in the file. A mixed map must be left alone. |
| 164 |
func TestConfigDirUnwrapOnlySoleKeyIssue12899(t *testing.T) { |
| 165 |
t.Parallel() |
| 166 |
|
| 167 |
files := ` |
| 168 |
-- hugo.toml -- |
| 169 |
baseURL = "https://example.com" |
| 170 |
disableKinds = ["taxonomy", "term", "RSS", "sitemap", "robotsTXT", "home", "section"] |
| 171 |
-- config/_default/params.yaml -- |
| 172 |
params: |
| 173 |
nested: yes |
| 174 |
other: top |
| 175 |
-- content/p1.md -- |
| 176 |
--- |
| 177 |
title: P1 |
| 178 |
--- |
| 179 |
-- layouts/page.html -- |
| 180 |
{{ .Title }}|params.params.nested:{{ site.Params.params.nested }}|params.other:{{ site.Params.other }}| |
| 181 |
` |
| 182 |
b := Test(t, files) |
| 183 |
|
| 184 |
b.AssertFileContent("public/p1/index.html", "P1|params.params.nested:yes|params.other:top|") |
| 185 |
} |