serve/http: compare zip test output semantically
Go 1.27 changes compress/flate output, which also changes archive/zip byte output. The HTTP zip download tests currently compare raw zip bytes against golden files, so they fail even though the generated zip archives contain the expected files. Compare zip entries and decompressed contents instead of the exact compressed byte stream. Signed-off-by: Mikel Olasagasti Uranga <mikel@olasagasti.info>
This commit is contained in:
committed by
Nick Craig-Wood
parent
c851d4dec5
commit
169f5b714c
@@ -1,6 +1,8 @@
|
|||||||
package http
|
package http
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"archive/zip"
|
||||||
|
"bytes"
|
||||||
"compress/gzip"
|
"compress/gzip"
|
||||||
"context"
|
"context"
|
||||||
"flag"
|
"flag"
|
||||||
@@ -25,6 +27,11 @@ import (
|
|||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type zipEntry struct {
|
||||||
|
IsDir bool
|
||||||
|
Contents string
|
||||||
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
updateGolden = flag.Bool("updategolden", false, "update golden files for regression test")
|
updateGolden = flag.Bool("updategolden", false, "update golden files for regression test")
|
||||||
)
|
)
|
||||||
@@ -102,12 +109,39 @@ func checkGolden(t *testing.T, fileName string, got []byte) {
|
|||||||
} else {
|
} else {
|
||||||
want, err := os.ReadFile(fileName)
|
want, err := os.ReadFile(fileName)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
if strings.HasSuffix(fileName, ".zip") {
|
||||||
|
assert.Equal(t, readZip(t, want), readZip(t, got), fileName)
|
||||||
|
return
|
||||||
|
}
|
||||||
wants := strings.Split(string(want), "\n")
|
wants := strings.Split(string(want), "\n")
|
||||||
gots := strings.Split(string(got), "\n")
|
gots := strings.Split(string(got), "\n")
|
||||||
assert.Equal(t, wants, gots, fileName)
|
assert.Equal(t, wants, gots, fileName)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func readZip(t *testing.T, data []byte) map[string]zipEntry {
|
||||||
|
t.Helper()
|
||||||
|
zr, err := zip.NewReader(bytes.NewReader(data), int64(len(data)))
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
entries := make(map[string]zipEntry, len(zr.File))
|
||||||
|
for _, f := range zr.File {
|
||||||
|
entry := zipEntry{
|
||||||
|
IsDir: f.FileInfo().IsDir(),
|
||||||
|
}
|
||||||
|
if !entry.IsDir {
|
||||||
|
rc, err := f.Open()
|
||||||
|
require.NoError(t, err)
|
||||||
|
contents, err := io.ReadAll(rc)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.NoError(t, rc.Close())
|
||||||
|
entry.Contents = string(contents)
|
||||||
|
}
|
||||||
|
entries[f.Name] = entry
|
||||||
|
}
|
||||||
|
return entries
|
||||||
|
}
|
||||||
|
|
||||||
func testGET(t *testing.T, useProxy bool) {
|
func testGET(t *testing.T, useProxy bool) {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
// ci := fs.GetConfig(ctx)
|
// ci := fs.GetConfig(ctx)
|
||||||
|
|||||||
Reference in New Issue
Block a user