| 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 |
"path/filepath" |
| 21 |
"testing" |
| 22 |
|
| 23 |
qt "github.com/frankban/quicktest" |
| 24 |
"github.com/gohugoio/hugo/config" |
| 25 |
"github.com/gohugoio/hugo/config/testconfig" |
| 26 |
"github.com/gohugoio/hugo/resources/images" |
| 27 |
"github.com/spf13/afero" |
| 28 |
"github.com/spf13/cast" |
| 29 |
) |
| 30 |
|
| 31 |
type tstNoStringer struct{} |
| 32 |
|
| 33 |
type widthHeight struct { |
| 34 |
Width int |
| 35 |
Height int |
| 36 |
} |
| 37 |
|
| 38 |
var configTests = []struct { |
| 39 |
path any |
| 40 |
input widthHeight |
| 41 |
expect any |
| 42 |
}{ |
| 43 |
{ |
| 44 |
path: "a.png", |
| 45 |
input: widthHeight{10, 10}, |
| 46 |
expect: image.Config{ |
| 47 |
Width: 10, |
| 48 |
Height: 10, |
| 49 |
ColorModel: color.NRGBAModel, |
| 50 |
}, |
| 51 |
}, |
| 52 |
{ |
| 53 |
path: "a.png", |
| 54 |
input: widthHeight{10, 10}, |
| 55 |
expect: image.Config{ |
| 56 |
Width: 10, |
| 57 |
Height: 10, |
| 58 |
ColorModel: color.NRGBAModel, |
| 59 |
}, |
| 60 |
}, |
| 61 |
{ |
| 62 |
path: "b.png", |
| 63 |
input: widthHeight{20, 15}, |
| 64 |
expect: image.Config{ |
| 65 |
Width: 20, |
| 66 |
Height: 15, |
| 67 |
ColorModel: color.NRGBAModel, |
| 68 |
}, |
| 69 |
}, |
| 70 |
{ |
| 71 |
path: "a.png", |
| 72 |
input: widthHeight{20, 15}, |
| 73 |
expect: image.Config{ |
| 74 |
Width: 10, |
| 75 |
Height: 10, |
| 76 |
ColorModel: color.NRGBAModel, |
| 77 |
}, |
| 78 |
}, |
| 79 |
// errors |
| 80 |
{path: tstNoStringer{}, expect: false}, |
| 81 |
{path: "non-existent.png", expect: false}, |
| 82 |
{path: "", expect: false}, |
| 83 |
} |
| 84 |
|
| 85 |
func TestNSConfig(t *testing.T) { |
| 86 |
t.Parallel() |
| 87 |
c := qt.New(t) |
| 88 |
|
| 89 |
afs := afero.NewMemMapFs() |
| 90 |
v := config.New() |
| 91 |
v.Set("workingDir", "/a/b") |
| 92 |
d := testconfig.GetTestDeps(afs, v) |
| 93 |
bcfg := d.Conf |
| 94 |
|
| 95 |
ns := New(d) |
| 96 |
|
| 97 |
for _, test := range configTests { |
| 98 |
|
| 99 |
// check for expected errors early to avoid writing files |
| 100 |
if b, ok := test.expect.(bool); ok && !b { |
| 101 |
_, err := ns.Config(test.path) |
| 102 |
c.Assert(err, qt.Not(qt.IsNil)) |
| 103 |
continue |
| 104 |
} |
| 105 |
|
| 106 |
// cast path to string for afero.WriteFile |
| 107 |
sp, err := cast.ToStringE(test.path) |
| 108 |
c.Assert(err, qt.IsNil) |
| 109 |
img := blankImage(d.ResourceSpec.Imaging.Codec, test.input.Width, test.input.Height) |
| 110 |
afero.WriteFile(ns.deps.Fs.Source, filepath.Join(bcfg.WorkingDir(), sp), img, 0o755) |
| 111 |
|
| 112 |
result, err := ns.Config(test.path) |
| 113 |
|
| 114 |
c.Assert(err, qt.IsNil) |
| 115 |
c.Assert(result, qt.Equals, test.expect) |
| 116 |
_, cached := ns.cache.Lookup(sp) |
| 117 |
c.Assert(cached, qt.IsTrue) |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
func blankImage(codec *images.Codec, width, height int) []byte { |
| 122 |
var buf bytes.Buffer |
| 123 |
img := image.NewRGBA(image.Rect(0, 0, width, height)) |
| 124 |
cfg := images.ImageConfig{ |
| 125 |
TargetFormat: images.PNG, |
| 126 |
} |
| 127 |
if err := codec.EncodeTo(cfg, &buf, img); err != nil { |
| 128 |
panic(err) |
| 129 |
} |
| 130 |
return buf.Bytes() |
| 131 |
} |