github.com/linchen2chris/hugo@v0.0.0-20230307053224-cec209389705/hugolib/image_test.go (about)

     1  // Copyright 2019 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  	"testing"
    18  )
    19  
    20  func TestImageResizeMultilingual(t *testing.T) {
    21  	b := newTestSitesBuilder(t).WithConfigFile("toml", `
    22  baseURL="https://example.org"
    23  defaultContentLanguage = "en"
    24  
    25  [languages]
    26  [languages.en]
    27  title = "Title in English"
    28  languageName = "English"
    29  weight = 1
    30  [languages.nn]
    31  languageName = "Nynorsk"
    32  weight = 2
    33  title = "Tittel på nynorsk"
    34  [languages.nb]
    35  languageName = "Bokmål"
    36  weight = 3
    37  title = "Tittel på bokmål"
    38  [languages.fr]
    39  languageName = "French"
    40  weight = 4
    41  title = "French Title"
    42  
    43  `)
    44  
    45  	pageContent := `---
    46  title: "Page"
    47  ---
    48  `
    49  
    50  	b.WithContent("bundle/index.md", pageContent)
    51  	b.WithContent("bundle/index.nn.md", pageContent)
    52  	b.WithContent("bundle/index.fr.md", pageContent)
    53  	b.WithSunset("content/bundle/sunset.jpg")
    54  	b.WithSunset("assets/images/sunset.jpg")
    55  	b.WithTemplates("index.html", `
    56  {{ with (.Site.GetPage "bundle" ) }}
    57  {{ $sunset := .Resources.GetMatch "sunset*" }}
    58  {{ if $sunset }}
    59  {{ $resized := $sunset.Resize "200x200" }}
    60  SUNSET FOR: {{ $.Site.Language.Lang }}: {{ $resized.RelPermalink }}/{{ $resized.Width }}/Lat: {{ $resized.Exif.Lat }}
    61  {{ end }}
    62  {{ else }}
    63  No bundle for {{ $.Site.Language.Lang }}
    64  {{ end }}
    65  
    66  {{ $sunset2 := resources.Get "images/sunset.jpg" }}
    67  {{ $resized2 := $sunset2.Resize "123x234" }}
    68  SUNSET2: {{ $resized2.RelPermalink }}/{{ $resized2.Width }}/Lat: {{ $resized2.Exif.Lat }}
    69  
    70  
    71  `)
    72  
    73  	b.Build(BuildCfg{})
    74  
    75  	b.AssertFileContent("public/index.html", "SUNSET FOR: en: /bundle/sunset_hu59e56ffff1bc1d8d122b1403d34e039f_90587_200x200_resize_q75_box.jpg/200/Lat: 36.59744166666667")
    76  	b.AssertFileContent("public/fr/index.html", "SUNSET FOR: fr: /fr/bundle/sunset_hu59e56ffff1bc1d8d122b1403d34e039f_90587_200x200_resize_q75_box.jpg/200/Lat: 36.59744166666667")
    77  	b.AssertFileContent("public/index.html", " SUNSET2: /images/sunset_hu59e56ffff1bc1d8d122b1403d34e039f_90587_123x234_resize_q75_box.jpg/123/Lat: 36.59744166666667")
    78  	b.AssertFileContent("public/nn/index.html", " SUNSET2: /images/sunset_hu59e56ffff1bc1d8d122b1403d34e039f_90587_123x234_resize_q75_box.jpg/123/Lat: 36.59744166666667")
    79  
    80  	b.AssertImage(200, 200, "public/fr/bundle/sunset_hu59e56ffff1bc1d8d122b1403d34e039f_90587_200x200_resize_q75_box.jpg")
    81  	b.AssertImage(200, 200, "public/bundle/sunset_hu59e56ffff1bc1d8d122b1403d34e039f_90587_200x200_resize_q75_box.jpg")
    82  
    83  	// Check the file cache
    84  	b.AssertImage(200, 200, "resources/_gen/images/bundle/sunset_hu59e56ffff1bc1d8d122b1403d34e039f_90587_200x200_resize_q75_box.jpg")
    85  
    86  	b.AssertFileContent("resources/_gen/images/bundle/sunset_3166614710256882113.json",
    87  		"DateTimeDigitized|time.Time", "PENTAX")
    88  	b.AssertImage(123, 234, "resources/_gen/images/sunset_hu59e56ffff1bc1d8d122b1403d34e039f_90587_123x234_resize_q75_box.jpg")
    89  	b.AssertFileContent("resources/_gen/images/sunset_3166614710256882113.json",
    90  		"DateTimeDigitized|time.Time", "PENTAX")
    91  
    92  	// TODO(bep) add this as a default assertion after Build()?
    93  	b.AssertNoDuplicateWrites()
    94  }