github.com/whatlly/hugo@v0.47.1/hugolib/case_insensitive_test.go (about) 1 // Copyright 2016 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 "path/filepath" 19 "strings" 20 "testing" 21 22 "github.com/gohugoio/hugo/hugofs" 23 24 "github.com/gohugoio/hugo/deps" 25 "github.com/spf13/afero" 26 "github.com/stretchr/testify/require" 27 ) 28 29 var ( 30 caseMixingSiteConfigTOML = ` 31 Title = "In an Insensitive Mood" 32 DefaultContentLanguage = "nn" 33 defaultContentLanguageInSubdir = true 34 35 [Blackfriday] 36 AngledQuotes = true 37 HrefTargetBlank = true 38 39 [Params] 40 Search = true 41 Color = "green" 42 mood = "Happy" 43 [Params.Colors] 44 Blue = "blue" 45 Yellow = "yellow" 46 47 [Languages] 48 [Languages.nn] 49 title = "Nynorsk title" 50 languageName = "Nynorsk" 51 weight = 1 52 53 [Languages.en] 54 TITLE = "English title" 55 LanguageName = "English" 56 Mood = "Thoughtful" 57 Weight = 2 58 COLOR = "Pink" 59 [Languages.en.blackfriday] 60 angledQuotes = false 61 hrefTargetBlank = false 62 [Languages.en.Colors] 63 BLUE = "blues" 64 yellow = "golden" 65 ` 66 caseMixingPage1En = ` 67 --- 68 TITLE: Page1 En Translation 69 BlackFriday: 70 AngledQuotes: false 71 Color: "black" 72 Search: true 73 mooD: "sad and lonely" 74 ColorS: 75 Blue: "bluesy" 76 Yellow: "sunny" 77 --- 78 # "Hi" 79 {{< shortcode >}} 80 ` 81 82 caseMixingPage1 = ` 83 --- 84 titLe: Side 1 85 blackFriday: 86 angledQuotes: true 87 color: "red" 88 search: false 89 MooD: "sad" 90 COLORS: 91 blue: "heavenly" 92 yelloW: "Sunny" 93 --- 94 # "Hi" 95 {{< shortcode >}} 96 ` 97 98 caseMixingPage2 = ` 99 --- 100 TITLE: Page2 Title 101 BlackFriday: 102 AngledQuotes: false 103 Color: "black" 104 search: true 105 MooD: "moody" 106 ColorS: 107 Blue: "sky" 108 YELLOW: "flower" 109 --- 110 # Hi 111 {{< shortcode >}} 112 ` 113 ) 114 115 func caseMixingTestsWriteCommonSources(t *testing.T, fs afero.Fs) { 116 writeToFs(t, fs, filepath.Join("content", "sect1", "page1.md"), caseMixingPage1) 117 writeToFs(t, fs, filepath.Join("content", "sect2", "page2.md"), caseMixingPage2) 118 writeToFs(t, fs, filepath.Join("content", "sect1", "page1.en.md"), caseMixingPage1En) 119 120 writeToFs(t, fs, "layouts/shortcodes/shortcode.html", ` 121 Shortcode Page: {{ .Page.Params.COLOR }}|{{ .Page.Params.Colors.Blue }} 122 Shortcode Site: {{ .Page.Site.Params.COLOR }}|{{ .Site.Params.COLORS.YELLOW }} 123 `) 124 125 writeToFs(t, fs, "layouts/partials/partial.html", ` 126 Partial Page: {{ .Params.COLOR }}|{{ .Params.Colors.Blue }} 127 Partial Site: {{ .Site.Params.COLOR }}|{{ .Site.Params.COLORS.YELLOW }} 128 `) 129 130 writeToFs(t, fs, "config.toml", caseMixingSiteConfigTOML) 131 132 } 133 134 func TestCaseInsensitiveConfigurationVariations(t *testing.T) { 135 t.Parallel() 136 137 // See issues 2615, 1129, 2590 and maybe some others 138 // Also see 2598 139 // 140 // Viper is now, at least for the Hugo part, case insensitive 141 // So we need tests for all of it, with needed adjustments on the Hugo side. 142 // Not sure what that will be. Let us see. 143 144 // So all the below with case variations: 145 // config: regular fields, blackfriday config, param with nested map 146 // language: new and overridden values, in regular fields and nested paramsmap 147 // page frontmatter: regular fields, blackfriday config, param with nested map 148 149 mm := afero.NewMemMapFs() 150 151 caseMixingTestsWriteCommonSources(t, mm) 152 153 cfg, _, err := LoadConfig(ConfigSourceDescriptor{Fs: mm, Filename: "config.toml"}) 154 require.NoError(t, err) 155 156 fs := hugofs.NewFrom(mm, cfg) 157 158 th := testHelper{cfg, fs, t} 159 160 writeSource(t, fs, filepath.Join("layouts", "_default", "baseof.html"), ` 161 Block Page Colors: {{ .Params.COLOR }}|{{ .Params.Colors.Blue }} 162 {{ block "main" . }}default{{end}}`) 163 164 writeSource(t, fs, filepath.Join("layouts", "sect2", "single.html"), ` 165 {{ define "main"}} 166 Page Colors: {{ .Params.CoLOR }}|{{ .Params.Colors.Blue }} 167 Site Colors: {{ .Site.Params.COlOR }}|{{ .Site.Params.COLORS.YELLOW }} 168 {{ .Content }} 169 {{ partial "partial.html" . }} 170 {{ end }} 171 `) 172 173 writeSource(t, fs, filepath.Join("layouts", "_default", "single.html"), ` 174 Page Title: {{ .Title }} 175 Site Title: {{ .Site.Title }} 176 Site Lang Mood: {{ .Site.Language.Params.MOoD }} 177 Page Colors: {{ .Params.COLOR }}|{{ .Params.Colors.Blue }} 178 Site Colors: {{ .Site.Params.COLOR }}|{{ .Site.Params.COLORS.YELLOW }} 179 {{ .Content }} 180 {{ partial "partial.html" . }} 181 `) 182 183 sites, err := NewHugoSites(deps.DepsCfg{Fs: fs, Cfg: cfg}) 184 185 if err != nil { 186 t.Fatalf("Failed to create sites: %s", err) 187 } 188 189 err = sites.Build(BuildCfg{}) 190 191 if err != nil { 192 t.Fatalf("Failed to build sites: %s", err) 193 } 194 195 th.assertFileContent(filepath.Join("public", "nn", "sect1", "page1", "index.html"), 196 "Page Colors: red|heavenly", 197 "Site Colors: green|yellow", 198 "Site Lang Mood: Happy", 199 "Shortcode Page: red|heavenly", 200 "Shortcode Site: green|yellow", 201 "Partial Page: red|heavenly", 202 "Partial Site: green|yellow", 203 "Page Title: Side 1", 204 "Site Title: Nynorsk title", 205 "«Hi»", // angled quotes 206 ) 207 208 th.assertFileContent(filepath.Join("public", "en", "sect1", "page1", "index.html"), 209 "Site Colors: Pink|golden", 210 "Page Colors: black|bluesy", 211 "Site Lang Mood: Thoughtful", 212 "Page Title: Page1 En Translation", 213 "Site Title: English title", 214 "“Hi”", 215 ) 216 217 th.assertFileContent(filepath.Join("public", "nn", "sect2", "page2", "index.html"), 218 "Page Colors: black|sky", 219 "Site Colors: green|yellow", 220 "Shortcode Page: black|sky", 221 "Block Page Colors: black|sky", 222 "Partial Page: black|sky", 223 "Partial Site: green|yellow", 224 ) 225 } 226 227 func TestCaseInsensitiveConfigurationForAllTemplateEngines(t *testing.T) { 228 t.Parallel() 229 230 noOp := func(s string) string { 231 return s 232 } 233 234 amberFixer := func(s string) string { 235 fixed := strings.Replace(s, "{{ .Site.Params", "{{ Site.Params", -1) 236 fixed = strings.Replace(fixed, "{{ .Params", "{{ Params", -1) 237 fixed = strings.Replace(fixed, ".Content", "Content", -1) 238 fixed = strings.Replace(fixed, "{{", "#{", -1) 239 fixed = strings.Replace(fixed, "}}", "}", -1) 240 241 return fixed 242 } 243 244 for _, config := range []struct { 245 suffix string 246 templateFixer func(s string) string 247 }{ 248 {"amber", amberFixer}, 249 {"html", noOp}, 250 {"ace", noOp}, 251 } { 252 doTestCaseInsensitiveConfigurationForTemplateEngine(t, config.suffix, config.templateFixer) 253 254 } 255 256 } 257 258 func doTestCaseInsensitiveConfigurationForTemplateEngine(t *testing.T, suffix string, templateFixer func(s string) string) { 259 260 mm := afero.NewMemMapFs() 261 262 caseMixingTestsWriteCommonSources(t, mm) 263 264 cfg, err := LoadConfigDefault(mm) 265 require.NoError(t, err) 266 267 fs := hugofs.NewFrom(mm, cfg) 268 269 th := testHelper{cfg, fs, t} 270 271 t.Log("Testing", suffix) 272 273 templTemplate := ` 274 p 275 | 276 | Page Colors: {{ .Params.CoLOR }}|{{ .Params.Colors.Blue }} 277 | Site Colors: {{ .Site.Params.COlOR }}|{{ .Site.Params.COLORS.YELLOW }} 278 | {{ .Content }} 279 280 ` 281 282 templ := templateFixer(templTemplate) 283 284 t.Log(templ) 285 286 writeSource(t, fs, filepath.Join("layouts", "_default", fmt.Sprintf("single.%s", suffix)), templ) 287 288 sites, err := NewHugoSites(deps.DepsCfg{Fs: fs, Cfg: cfg}) 289 290 if err != nil { 291 t.Fatalf("Failed to create sites: %s", err) 292 } 293 294 err = sites.Build(BuildCfg{}) 295 296 if err != nil { 297 t.Fatalf("Failed to build sites: %s", err) 298 } 299 300 th.assertFileContent(filepath.Join("public", "nn", "sect1", "page1", "index.html"), 301 "Page Colors: red|heavenly", 302 "Site Colors: green|yellow", 303 "Shortcode Page: red|heavenly", 304 "Shortcode Site: green|yellow", 305 ) 306 307 }