github.com/anakojm/hugo-katex@v0.0.0-20231023141351-42d6f5de9c0b/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 "image" 19 "image/color" 20 "image/png" 21 "path/filepath" 22 "testing" 23 24 qt "github.com/frankban/quicktest" 25 "github.com/gohugoio/hugo/config" 26 "github.com/gohugoio/hugo/config/testconfig" 27 "github.com/spf13/afero" 28 "github.com/spf13/cast" 29 ) 30 31 type tstNoStringer struct{} 32 33 var configTests = []struct { 34 path any 35 input []byte 36 expect any 37 }{ 38 { 39 path: "a.png", 40 input: blankImage(10, 10), 41 expect: image.Config{ 42 Width: 10, 43 Height: 10, 44 ColorModel: color.NRGBAModel, 45 }, 46 }, 47 { 48 path: "a.png", 49 input: blankImage(10, 10), 50 expect: image.Config{ 51 Width: 10, 52 Height: 10, 53 ColorModel: color.NRGBAModel, 54 }, 55 }, 56 { 57 path: "b.png", 58 input: blankImage(20, 15), 59 expect: image.Config{ 60 Width: 20, 61 Height: 15, 62 ColorModel: color.NRGBAModel, 63 }, 64 }, 65 { 66 path: "a.png", 67 input: blankImage(20, 15), 68 expect: image.Config{ 69 Width: 10, 70 Height: 10, 71 ColorModel: color.NRGBAModel, 72 }, 73 }, 74 // errors 75 {path: tstNoStringer{}, expect: false}, 76 {path: "non-existent.png", expect: false}, 77 {path: "", expect: false}, 78 } 79 80 func TestNSConfig(t *testing.T) { 81 t.Parallel() 82 c := qt.New(t) 83 84 afs := afero.NewMemMapFs() 85 v := config.New() 86 v.Set("workingDir", "/a/b") 87 d := testconfig.GetTestDeps(afs, v) 88 bcfg := d.Conf 89 90 ns := New(d) 91 92 for _, test := range configTests { 93 94 // check for expected errors early to avoid writing files 95 if b, ok := test.expect.(bool); ok && !b { 96 _, err := ns.Config(test.path) 97 c.Assert(err, qt.Not(qt.IsNil)) 98 continue 99 } 100 101 // cast path to string for afero.WriteFile 102 sp, err := cast.ToStringE(test.path) 103 c.Assert(err, qt.IsNil) 104 afero.WriteFile(ns.deps.Fs.Source, filepath.Join(bcfg.WorkingDir(), sp), test.input, 0755) 105 106 result, err := ns.Config(test.path) 107 108 c.Assert(err, qt.IsNil) 109 c.Assert(result, qt.Equals, test.expect) 110 c.Assert(len(ns.cache), qt.Not(qt.Equals), 0) 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 }