github.com/graemephi/kahugo@v0.62.3-0.20211121071557-d78c0423784d/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  	"path/filepath"
    18  	"testing"
    19  
    20  	"github.com/gohugoio/hugo/hugofs"
    21  
    22  	qt "github.com/frankban/quicktest"
    23  	"github.com/gohugoio/hugo/deps"
    24  	"github.com/spf13/afero"
    25  )
    26  
    27  var (
    28  	caseMixingSiteConfigTOML = `
    29  Title = "In an Insensitive Mood"
    30  DefaultContentLanguage = "nn"
    31  defaultContentLanguageInSubdir = true
    32  
    33  [Blackfriday]
    34  AngledQuotes = true
    35  HrefTargetBlank = true
    36  
    37  [Params]
    38  Search = true
    39  Color = "green"
    40  mood = "Happy"
    41  [Params.Colors]
    42  Blue = "blue"
    43  Yellow = "yellow"
    44  
    45  [Languages]
    46  [Languages.nn]
    47  title = "Nynorsk title"
    48  languageName = "Nynorsk"
    49  weight = 1
    50  
    51  [Languages.en]
    52  TITLE = "English title"
    53  LanguageName = "English"
    54  Mood = "Thoughtful"
    55  Weight = 2
    56  COLOR = "Pink"
    57  [Languages.en.blackfriday]
    58  angledQuotes = false
    59  hrefTargetBlank = false
    60  [Languages.en.Colors]
    61  BLUE = "blues"
    62  Yellow = "golden"
    63  `
    64  	caseMixingPage1En = `
    65  ---
    66  TITLE: Page1 En Translation
    67  BlackFriday:
    68    AngledQuotes: false
    69  Color: "black"
    70  Search: true
    71  mooD: "sad and lonely"
    72  ColorS: 
    73    Blue: "bluesy"
    74    Yellow: "sunny"  
    75  ---
    76  # "Hi"
    77  {{< shortcode >}}
    78  `
    79  
    80  	caseMixingPage1 = `
    81  ---
    82  titLe: Side 1
    83  blackFriday:
    84    angledQuotes: true
    85  color: "red"
    86  search: false
    87  MooD: "sad"
    88  COLORS: 
    89    blue: "heavenly"
    90    yelloW: "Sunny"  
    91  ---
    92  # "Hi"
    93  {{< shortcode >}}
    94  `
    95  
    96  	caseMixingPage2 = `
    97  ---
    98  TITLE: Page2 Title
    99  BlackFriday:
   100    AngledQuotes: false
   101  Color: "black"
   102  search: true
   103  MooD: "moody"
   104  ColorS: 
   105    Blue: "sky"
   106    YELLOW: "flower"  
   107  ---
   108  # Hi
   109  {{< shortcode >}}
   110  `
   111  )
   112  
   113  func caseMixingTestsWriteCommonSources(t *testing.T, fs afero.Fs) {
   114  	writeToFs(t, fs, filepath.Join("content", "sect1", "page1.md"), caseMixingPage1)
   115  	writeToFs(t, fs, filepath.Join("content", "sect2", "page2.md"), caseMixingPage2)
   116  	writeToFs(t, fs, filepath.Join("content", "sect1", "page1.en.md"), caseMixingPage1En)
   117  
   118  	writeToFs(t, fs, "layouts/shortcodes/shortcode.html", `
   119  Shortcode Page: {{ .Page.Params.COLOR }}|{{ .Page.Params.Colors.Blue  }}
   120  Shortcode Site: {{ .Page.Site.Params.COLOR }}|{{ .Site.Params.COLORS.YELLOW  }}
   121  `)
   122  
   123  	writeToFs(t, fs, "layouts/partials/partial.html", `
   124  Partial Page: {{ .Params.COLOR }}|{{ .Params.Colors.Blue }}
   125  Partial Site: {{ .Site.Params.COLOR }}|{{ .Site.Params.COLORS.YELLOW }}
   126  Partial Site Global: {{ site.Params.COLOR }}|{{ site.Params.COLORS.YELLOW }}
   127  `)
   128  
   129  	writeToFs(t, fs, "config.toml", caseMixingSiteConfigTOML)
   130  }
   131  
   132  func TestCaseInsensitiveConfigurationVariations(t *testing.T) {
   133  	t.Parallel()
   134  
   135  	c := qt.New(t)
   136  
   137  	mm := afero.NewMemMapFs()
   138  
   139  	caseMixingTestsWriteCommonSources(t, mm)
   140  
   141  	cfg, _, err := LoadConfig(ConfigSourceDescriptor{Fs: mm, Filename: "config.toml"})
   142  	c.Assert(err, qt.IsNil)
   143  
   144  	fs := hugofs.NewFrom(mm, cfg)
   145  
   146  	th := newTestHelper(cfg, fs, t)
   147  
   148  	writeSource(t, fs, filepath.Join("layouts", "_default", "baseof.html"), `
   149  Block Page Colors: {{ .Params.COLOR }}|{{ .Params.Colors.Blue }}	
   150  {{ block "main" . }}default{{end}}`)
   151  
   152  	writeSource(t, fs, filepath.Join("layouts", "sect2", "single.html"), `
   153  {{ define "main"}}
   154  Page Colors: {{ .Params.CoLOR }}|{{ .Params.Colors.Blue }}
   155  Site Colors: {{ .Site.Params.COlOR }}|{{ .Site.Params.COLORS.YELLOW }}
   156  {{ template "index-color" (dict "name" "Page" "params" .Params) }}
   157  {{ template "index-color" (dict "name" "Site" "params" .Site.Params) }}
   158  
   159  {{ .Content }}
   160  {{ partial "partial.html" . }}
   161  {{ end }}
   162  {{ define "index-color" }}
   163  {{ $yellow := index .params "COLoRS" "yELLOW" }}
   164  {{ $colors := index .params "COLoRS" }}
   165  {{ $yellow2 := index $colors "yEllow" }}
   166  index1|{{ .name }}: {{ $yellow }}|
   167  index2|{{ .name }}: {{ $yellow2 }}|
   168  {{ end }}
   169  `)
   170  
   171  	writeSource(t, fs, filepath.Join("layouts", "_default", "single.html"), `
   172  Page Title: {{ .Title }}
   173  Site Title: {{ .Site.Title }}
   174  Site Lang Mood: {{ .Site.Language.Params.MOoD }}
   175  Page Colors: {{ .Params.COLOR }}|{{ .Params.Colors.Blue }}|{{ index .Params "ColOR" }}
   176  Site Colors: {{ .Site.Params.COLOR }}|{{ .Site.Params.COLORS.YELLOW }}|{{ index .Site.Params "ColOR" }}
   177  {{ $page2 := .Site.GetPage "/sect2/page2" }}
   178  {{ if $page2 }}
   179  Page2: {{ $page2.Params.ColoR }} 
   180  {{ end }}
   181  {{ .Content }}
   182  {{ partial "partial.html" . }}
   183  `)
   184  
   185  	sites, err := NewHugoSites(deps.DepsCfg{Fs: fs, Cfg: cfg})
   186  	if err != nil {
   187  		t.Fatalf("Failed to create sites: %s", err)
   188  	}
   189  
   190  	err = sites.Build(BuildCfg{})
   191  
   192  	if err != nil {
   193  		t.Fatalf("Failed to build sites: %s", err)
   194  	}
   195  
   196  	th.assertFileContent(filepath.Join("public", "nn", "sect1", "page1", "index.html"),
   197  		"Page Colors: red|heavenly|red",
   198  		"Site Colors: green|yellow|green",
   199  		"Site Lang Mood: Happy",
   200  		"Shortcode Page: red|heavenly",
   201  		"Shortcode Site: green|yellow",
   202  		"Partial Page: red|heavenly",
   203  		"Partial Site: green|yellow",
   204  		"Partial Site Global: green|yellow",
   205  		"Page Title: Side 1",
   206  		"Site Title: Nynorsk title",
   207  		"Page2: black ",
   208  	)
   209  
   210  	th.assertFileContent(filepath.Join("public", "en", "sect1", "page1", "index.html"),
   211  		"Site Colors: Pink|golden",
   212  		"Page Colors: black|bluesy",
   213  		"Site Lang Mood: Thoughtful",
   214  		"Page Title: Page1 En Translation",
   215  		"Site Title: English title",
   216  		"&ldquo;Hi&rdquo;",
   217  	)
   218  
   219  	th.assertFileContent(filepath.Join("public", "nn", "sect2", "page2", "index.html"),
   220  		"Page Colors: black|sky",
   221  		"Site Colors: green|yellow",
   222  		"Shortcode Page: black|sky",
   223  		"Block Page Colors: black|sky",
   224  		"Partial Page: black|sky",
   225  		"Partial Site: green|yellow",
   226  		"index1|Page: flower|",
   227  		"index1|Site: yellow|",
   228  		"index2|Page: flower|",
   229  		"index2|Site: yellow|",
   230  	)
   231  }