github.com/shohhei1126/hugo@v0.42.2-0.20180623210752-3d5928889ad7/tpl/images/images_test.go (about) 1 // Copyright 2017 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 images 15 16 import ( 17 "bytes" 18 "fmt" 19 "image" 20 "image/color" 21 "image/png" 22 "path/filepath" 23 "testing" 24 25 "github.com/gohugoio/hugo/deps" 26 "github.com/gohugoio/hugo/hugofs" 27 "github.com/spf13/afero" 28 "github.com/spf13/cast" 29 "github.com/spf13/viper" 30 "github.com/stretchr/testify/assert" 31 "github.com/stretchr/testify/require" 32 ) 33 34 type tstNoStringer struct{} 35 36 var configTests = []struct { 37 path interface{} 38 input []byte 39 expect interface{} 40 }{ 41 { 42 path: "a.png", 43 input: blankImage(10, 10), 44 expect: image.Config{ 45 Width: 10, 46 Height: 10, 47 ColorModel: color.NRGBAModel, 48 }, 49 }, 50 { 51 path: "a.png", 52 input: blankImage(10, 10), 53 expect: image.Config{ 54 Width: 10, 55 Height: 10, 56 ColorModel: color.NRGBAModel, 57 }, 58 }, 59 { 60 path: "b.png", 61 input: blankImage(20, 15), 62 expect: image.Config{ 63 Width: 20, 64 Height: 15, 65 ColorModel: color.NRGBAModel, 66 }, 67 }, 68 { 69 path: "a.png", 70 input: blankImage(20, 15), 71 expect: image.Config{ 72 Width: 10, 73 Height: 10, 74 ColorModel: color.NRGBAModel, 75 }, 76 }, 77 // errors 78 {path: tstNoStringer{}, expect: false}, 79 {path: "non-existent.png", expect: false}, 80 {path: "", expect: false}, 81 } 82 83 func TestNSConfig(t *testing.T) { 84 t.Parallel() 85 86 v := viper.New() 87 v.Set("workingDir", "/a/b") 88 89 ns := New(&deps.Deps{Fs: hugofs.NewMem(v)}) 90 91 for i, test := range configTests { 92 errMsg := fmt.Sprintf("[%d] %s", i, test.path) 93 94 // check for expected errors early to avoid writing files 95 if b, ok := test.expect.(bool); ok && !b { 96 _, err := ns.Config(interface{}(test.path)) 97 require.Error(t, err, errMsg) 98 continue 99 } 100 101 // cast path to string for afero.WriteFile 102 sp, err := cast.ToStringE(test.path) 103 require.NoError(t, err, errMsg) 104 afero.WriteFile(ns.deps.Fs.Source, filepath.Join(v.GetString("workingDir"), sp), test.input, 0755) 105 106 result, err := ns.Config(test.path) 107 108 require.NoError(t, err, errMsg) 109 assert.Equal(t, test.expect, result, errMsg) 110 assert.NotEqual(t, 0, len(ns.cache), errMsg) 111 } 112 } 113 114 func blankImage(width, height int) []byte { 115 var buf bytes.Buffer 116 img := image.NewRGBA(image.Rect(0, 0, width, height)) 117 if err := png.Encode(&buf, img); err != nil { 118 panic(err) 119 } 120 return buf.Bytes() 121 }