forked from webp-sh/webp_server_go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencoder_test.go
66 lines (57 loc) · 1.49 KB
/
encoder_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
import (
"github.com/stretchr/testify/assert"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
"testing"
)
func walker() []string {
var list []string
_ = filepath.Walk("./pics", func(p string, info os.FileInfo, err error) error {
if !info.IsDir() && !strings.HasPrefix(path.Base(p), ".") {
list = append(list, p)
}
return nil
})
return list
}
func TestWebPEncoder(t *testing.T) {
// Go through every files
var dest = "/tmp/test-result"
var target = walker()
for _, f := range target {
runEncoder(t, f, dest)
}
_ = os.Remove(dest)
}
func TestAvifEncoder(t *testing.T) {
// Only one file: img_over_16383px.jpg might cause memory issues on CI environment
var dest = "/tmp/test-result"
avifEncoder("./pics/big.jpg", dest, 80)
assertType(t, dest, "image/avif")
}
func TestNonExistImage(t *testing.T) {
var dest = "/tmp/test-result"
webpEncoder("./pics/empty.jpg", dest, 80)
avifEncoder("./pics/empty.jpg", dest, 80)
}
func TestConvertFail(t *testing.T) {
var dest = "/tmp/test-result"
webpEncoder("./pics/webp_server.jpg", dest, -1)
avifEncoder("./pics/webp_server.jpg", dest, -1)
}
func runEncoder(t *testing.T, file string, dest string) {
if file == "pics/empty.jpg" {
t.Log("Empty file, that's okay.")
}
webpEncoder(file, dest, 80)
assertType(t, dest, "image/webp")
}
func assertType(t *testing.T, dest, mime string) {
data, _ := ioutil.ReadFile(dest)
types := getFileContentType(data[:512])
assert.Equalf(t, mime, types, "File %s should be %s", dest, mime)
}