hugo

Fork of github.com/gohugoio/hugo with reverse pagination support

git clone git://git.shimmy1996.com/hugo.git

integration_test.go (2698B)

    1 // Copyright 2022 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 resources_test
   15 
   16 import (
   17 	"strings"
   18 	"testing"
   19 
   20 	qt "github.com/frankban/quicktest"
   21 	"github.com/gohugoio/hugo/hugolib"
   22 )
   23 
   24 // Issue 8931
   25 func TestImageCache(t *testing.T) {
   26 	t.Parallel()
   27 
   28 	files := `
   29 -- config.toml --
   30 baseURL = "https://example.org"
   31 -- content/mybundle/index.md --
   32 ---
   33 title: "My Bundle"
   34 ---
   35 -- content/mybundle/pixel.png --
   36 iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==
   37 -- layouts/foo.html --
   38 -- layouts/index.html --
   39 {{ $p := site.GetPage "mybundle"}}
   40 {{ $img := $p.Resources.Get "pixel.png" }}
   41 {{ $gif := $img.Resize "1x1 gif" }}
   42 {{ $bmp := $img.Resize "1x1 bmp" }}
   43 
   44 gif: {{ $gif.RelPermalink }}|{{ $gif.MediaType }}|
   45 bmp: {{ $bmp.RelPermalink }}|{{ $bmp.MediaType }}|	
   46 `
   47 
   48 	b := hugolib.NewIntegrationTestBuilder(
   49 		hugolib.IntegrationTestConfig{
   50 			T:           t,
   51 			TxtarString: files,
   52 			NeedsOsFS:   true,
   53 			Running:     true,
   54 		}).Build()
   55 
   56 	assertImages := func() {
   57 		b.AssertFileContent("public/index.html", `
   58 		gif: /mybundle/pixel_hu8aa3346827e49d756ff4e630147c42b5_70_1x1_resize_box_3.gif|image/gif|
   59 		bmp: /mybundle/pixel_hu8aa3346827e49d756ff4e630147c42b5_70_1x1_resize_box_3.bmp|image/bmp|
   60 		
   61 		`)
   62 	}
   63 
   64 	assertImages()
   65 
   66 	b.EditFileReplace("content/mybundle/index.md", func(s string) string { return strings.ReplaceAll(s, "Bundle", "BUNDLE") })
   67 	b.Build()
   68 
   69 	assertImages()
   70 
   71 }
   72 
   73 func TestSVGError(t *testing.T) {
   74 	t.Parallel()
   75 
   76 	files := `
   77 -- config.toml --
   78 -- assets/circle.svg --
   79 <svg height="100" width="100"><circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" /></svg> 
   80 -- layouts/index.html --
   81 {{ $svg := resources.Get "circle.svg" }}
   82 Width: {{ $svg.Width }}	
   83 `
   84 
   85 	b, err := hugolib.NewIntegrationTestBuilder(
   86 		hugolib.IntegrationTestConfig{
   87 			T:           t,
   88 			TxtarString: files,
   89 			NeedsOsFS:   true,
   90 			Running:     true,
   91 		}).BuildE()
   92 
   93 	b.Assert(err, qt.IsNotNil)
   94 	b.Assert(err.Error(), qt.Contains, `error calling Width: this method is only available for raster images. To determine if an image is SVG, you can do {{ if eq .MediaType.SubType "svg" }}{{ end }}`)
   95 
   96 }