better tests
6
.gitignore
vendored
@@ -1,5 +1,5 @@
|
||||
bin
|
||||
example.*
|
||||
test.*
|
||||
*.exe
|
||||
encoded.*
|
||||
decoded.*
|
||||
ffwebp.exe
|
||||
ffwebp
|
4
go.mod
@@ -21,15 +21,19 @@ require (
|
||||
github.com/spakin/netpbm v1.3.2
|
||||
github.com/srwiley/oksvg v0.0.0-20221011165216-be6e8873101c
|
||||
github.com/srwiley/rasterx v0.0.0-20220730225603-2ab79fcdd4ef
|
||||
github.com/stretchr/testify v1.10.0
|
||||
github.com/xyproto/xbm v1.0.0
|
||||
golang.org/x/image v0.30.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/ebitengine/purego v0.8.4 // indirect
|
||||
github.com/gopherjs/gopherjs v1.17.2 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
github.com/sergeymakinen/go-bmp v1.0.0 // indirect
|
||||
github.com/tetratelabs/wazero v1.9.0 // indirect
|
||||
golang.org/x/net v0.43.0 // indirect
|
||||
golang.org/x/text v0.28.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
2
go.sum
@@ -56,5 +56,7 @@ golang.org/x/net v0.43.0/go.mod h1:vhO1fvI4dGsIjh73sWfUVjj3N7CA9WkKJNQm2svM6Jg=
|
||||
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/text v0.28.0 h1:rhazDwis8INMIwQ4tpjLDzUhx6RlXqZNPEM0huQojng=
|
||||
golang.org/x/text v0.28.0/go.mod h1:U8nCwOR8jO/marOQ0QbDiOngZVEBB7MAiitBuMjXiNU=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
BIN
integration/example.jpeg
Normal file
After Width: | Height: | Size: 22 KiB |
Before Width: | Height: | Size: 214 KiB After Width: | Height: | Size: 214 KiB |
Before Width: | Height: | Size: 7.0 MiB After Width: | Height: | Size: 7.0 MiB |
Before Width: | Height: | Size: 176 KiB After Width: | Height: | Size: 176 KiB |
Before Width: | Height: | Size: 695 KiB After Width: | Height: | Size: 695 KiB |
Before Width: | Height: | Size: 175 KiB After Width: | Height: | Size: 175 KiB |
Before Width: | Height: | Size: 416 KiB After Width: | Height: | Size: 416 KiB |
Before Width: | Height: | Size: 491 KiB After Width: | Height: | Size: 491 KiB |
Before Width: | Height: | Size: 4.9 KiB After Width: | Height: | Size: 4.9 KiB |
Before Width: | Height: | Size: 3.1 MiB After Width: | Height: | Size: 3.1 MiB |
Before Width: | Height: | Size: 30 KiB After Width: | Height: | Size: 30 KiB |
155
integration/integration_test.go
Normal file
@@ -0,0 +1,155 @@
|
||||
package integration
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"image/jpeg"
|
||||
"io/fs"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// skip encoding tests for these extensions
|
||||
var encodeOnly = map[string]bool{
|
||||
"heic": true,
|
||||
"heif": true,
|
||||
"psd": true,
|
||||
"svg": true,
|
||||
}
|
||||
|
||||
func TestFFWebP(t *testing.T) {
|
||||
// build a fresh executable with "full" tags
|
||||
executable, err := buildExecutable()
|
||||
require.NoError(t, err)
|
||||
|
||||
// resolve all test files
|
||||
files, err := listFiles("images")
|
||||
require.NoError(t, err)
|
||||
|
||||
// test all extensions
|
||||
for _, path := range files {
|
||||
ext := strings.TrimLeft(filepath.Ext(path), ".")
|
||||
|
||||
decoded := "decoded.jpeg"
|
||||
encoded := fmt.Sprintf("encoded.%s", ext)
|
||||
|
||||
// test if we can convert from the codec to jpeg
|
||||
t.Run(fmt.Sprintf("decode %s", ext), func(t *testing.T) {
|
||||
defer os.Remove(decoded)
|
||||
|
||||
err = runCommand(executable, "-i", path, "-o", decoded)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = validateJPEG(decoded, 0)
|
||||
require.NoError(t, err)
|
||||
})
|
||||
|
||||
if encodeOnly[ext] {
|
||||
continue
|
||||
}
|
||||
|
||||
// test if we can convert from jpeg to the codec and then back to jpeg
|
||||
t.Run(fmt.Sprintf("encode %s", ext), func(t *testing.T) {
|
||||
defer os.Remove(encoded)
|
||||
defer os.Remove(decoded)
|
||||
|
||||
err = runCommand(executable, "-i", "example.jpeg", "-o", encoded)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = runCommand(executable, "-i", encoded, "-o", decoded)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = validateJPEG(decoded, 256)
|
||||
require.NoError(t, err)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func buildExecutable() (string, error) {
|
||||
if runtime.GOOS == "windows" {
|
||||
err := runCommand("go", "build", "-tags", "full", "-o", "ffwebp.exe", "..\\cmd\\ffwebp")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return "./ffwebp.exe", nil
|
||||
}
|
||||
|
||||
err := runCommand("go", "build", "-tags", "full", "-o", "ffwebp", "../cmd/ffwebp")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
err = runCommand("chmod", "+x", "ffwebp")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return "./ffwebp", nil
|
||||
}
|
||||
|
||||
func listFiles(directory string) ([]string, error) {
|
||||
var files []string
|
||||
|
||||
err := filepath.Walk(directory, func(path string, info fs.FileInfo, err error) error {
|
||||
if err != nil || info.IsDir() {
|
||||
return err
|
||||
}
|
||||
|
||||
files = append(files, path)
|
||||
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return files, nil
|
||||
}
|
||||
|
||||
func runCommand(command string, args ...string) error {
|
||||
cmd := exec.Command(command, args...)
|
||||
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
if len(out) > 0 {
|
||||
return errors.New(string(out))
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateJPEG(path string, requireSize int) error {
|
||||
file, err := os.OpenFile(path, os.O_RDONLY, 0)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer file.Close()
|
||||
|
||||
img, err := jpeg.Decode(file)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
bounds := img.Bounds()
|
||||
|
||||
if bounds.Dx() == 0 || bounds.Dy() == 0 {
|
||||
return fmt.Errorf("invalid dimensions: %dx%d", bounds.Dx(), bounds.Dy())
|
||||
}
|
||||
|
||||
if requireSize != 0 && (bounds.Dx() != requireSize || bounds.Dy() != requireSize) {
|
||||
return fmt.Errorf("mismatched size: %dx%dx", bounds.Dx(), bounds.Dy())
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
@@ -33,7 +33,7 @@ func Sniff(reader io.Reader, input string, ignoreExtension bool) (*Sniffed, io.R
|
||||
if !ignoreExtension {
|
||||
ext := strings.ToLower(strings.TrimPrefix(filepath.Ext(input), "."))
|
||||
if ext != "" {
|
||||
codec, _ := FindCodec(ext)
|
||||
codec, _ := FindCodec(ext, false)
|
||||
if codec != nil {
|
||||
return &Sniffed{
|
||||
Header: []byte("." + ext),
|
||||
@@ -95,7 +95,7 @@ func Detect(output, override string) (Codec, string, error) {
|
||||
}
|
||||
}
|
||||
|
||||
codec, err := FindCodec(ext)
|
||||
codec, err := FindCodec(ext, true)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
@@ -107,7 +107,7 @@ func Detect(output, override string) (Codec, string, error) {
|
||||
return codec, ext, nil
|
||||
}
|
||||
|
||||
func FindCodec(ext string) (Codec, error) {
|
||||
func FindCodec(ext string, requireEncode bool) (Codec, error) {
|
||||
codec, ok := codecs[ext]
|
||||
if ok {
|
||||
return codec, nil
|
||||
@@ -116,7 +116,7 @@ func FindCodec(ext string) (Codec, error) {
|
||||
for _, codec := range codecs {
|
||||
for _, alias := range codec.Extensions() {
|
||||
if ext == alias {
|
||||
if !codec.CanEncode() {
|
||||
if requireEncode && !codec.CanEncode() {
|
||||
return nil, fmt.Errorf("decode-only output codec: %q", ext)
|
||||
}
|
||||
|
||||
|