tplimpl: Refactor imageConfig into a struct

Updates #2701
This commit is contained in:
Bjørn Erik Pedersen
2017-02-17 14:22:40 +01:00
parent c507e2717d
commit 77cbe4d60b
3 changed files with 34 additions and 79 deletions

View File

@@ -667,16 +667,13 @@ func TestImageConfig(t *testing.T) {
f := newTestFuncsterWithViper(v)
for i, this := range []struct {
resetCache bool
path string
input []byte
expected image.Config
path string
input []byte
expected image.Config
}{
// Make sure that the cache is initialized by default.
{
resetCache: false,
path: "a.png",
input: blankImage(10, 10),
path: "a.png",
input: blankImage(10, 10),
expected: image.Config{
Width: 10,
Height: 10,
@@ -684,9 +681,8 @@ func TestImageConfig(t *testing.T) {
},
},
{
resetCache: true,
path: "a.png",
input: blankImage(10, 10),
path: "a.png",
input: blankImage(10, 10),
expected: image.Config{
Width: 10,
Height: 10,
@@ -694,9 +690,8 @@ func TestImageConfig(t *testing.T) {
},
},
{
resetCache: false,
path: "b.png",
input: blankImage(20, 15),
path: "b.png",
input: blankImage(20, 15),
expected: image.Config{
Width: 20,
Height: 15,
@@ -704,33 +699,18 @@ func TestImageConfig(t *testing.T) {
},
},
{
resetCache: false,
path: "a.png",
input: blankImage(20, 15),
path: "a.png",
input: blankImage(20, 15),
expected: image.Config{
Width: 10,
Height: 10,
ColorModel: color.NRGBAModel,
},
},
{
resetCache: true,
path: "a.png",
input: blankImage(20, 15),
expected: image.Config{
Width: 20,
Height: 15,
ColorModel: color.NRGBAModel,
},
},
} {
afero.WriteFile(f.Fs.Source, filepath.Join(workingDir, this.path), this.input, 0755)
if this.resetCache {
resetImageConfigCache()
}
result, err := f.imageConfig(this.path)
result, err := f.image.config(this.path)
if err != nil {
t.Errorf("imageConfig returned error: %s", err)
}
@@ -739,29 +719,23 @@ func TestImageConfig(t *testing.T) {
t.Errorf("[%d] imageConfig: expected '%v', got '%v'", i, this.expected, result)
}
if len(defaultImageConfigCache.config) == 0 {
if len(f.image.imageConfigCache) == 0 {
t.Error("defaultImageConfigCache should have at least 1 item")
}
}
if _, err := f.imageConfig(t); err == nil {
if _, err := f.image.config(t); err == nil {
t.Error("Expected error from imageConfig when passed invalid path")
}
if _, err := f.imageConfig("non-existent.png"); err == nil {
if _, err := f.image.config("non-existent.png"); err == nil {
t.Error("Expected error from imageConfig when passed non-existent file")
}
if _, err := f.imageConfig(""); err == nil {
if _, err := f.image.config(""); err == nil {
t.Error("Expected error from imageConfig when passed empty path")
}
// test cache clearing
ResetCaches()
if len(defaultImageConfigCache.config) != 0 {
t.Error("ResetCaches should have cleared defaultImageConfigCache")
}
}
func TestIn(t *testing.T) {