Style fixes to test

Signed-off-by: Eli Treuherz <et@arenko.group>
This commit is contained in:
Eli Treuherz 2024-06-18 12:05:49 +01:00
parent b00001d8ac
commit bcd04d5a64
2 changed files with 24 additions and 23 deletions

2
go.mod
View File

@ -21,7 +21,6 @@ require (
github.com/gofrs/flock v0.8.1
github.com/gogo/protobuf v1.3.2
github.com/golang/protobuf v1.5.4
github.com/google/go-cmp v0.6.0
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
github.com/google/uuid v1.6.0
github.com/hashicorp/go-cty-funcs v0.0.0-20230405223818-a090f58aa992
@ -99,6 +98,7 @@ require (
github.com/go-viper/mapstructure/v2 v2.0.0 // indirect
github.com/gogo/googleapis v1.4.1 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect

View File

@ -5,7 +5,6 @@ import (
"slices"
"testing"
gocmp "github.com/google/go-cmp/cmp"
"github.com/moby/buildkit/exporter/containerimage/exptypes"
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/stretchr/testify/assert"
@ -86,33 +85,35 @@ func TestParseAnnotations(t *testing.T) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got, gotErr := ParseAnnotations(test.in)
got, err := ParseAnnotations(test.in)
if test.wantErr != "" {
require.ErrorContains(t, gotErr, test.wantErr)
require.ErrorContains(t, err, test.wantErr)
} else {
assert.NoError(t, gotErr)
require.NoError(t, err)
}
// Can't compare maps with pointer in their keys, need to extract and sort the map entries
type kv struct {
Key exptypes.AnnotationKey
Val string
}
var wantKVs, gotKVs []kv
for k, v := range test.want {
wantKVs = append(wantKVs, kv{k, v})
}
for k, v := range got {
gotKVs = append(gotKVs, kv{k, v})
}
wantKVs := entries(test.want)
gotKVs := entries(got)
sortFunc := func(a, b kv) int { return cmp.Compare(a.Key.String(), b.Key.String()) }
slices.SortFunc(wantKVs, sortFunc)
slices.SortFunc(gotKVs, sortFunc)
if diff := gocmp.Diff(wantKVs, gotKVs); diff != "" {
t.Error(diff)
}
assert.Equal(t, wantKVs, gotKVs)
})
}
}
type kv struct {
Key exptypes.AnnotationKey
Val string
}
func entries(in map[exptypes.AnnotationKey]string) []kv {
var out []kv
for k, v := range in {
out = append(out, kv{k, v})
}
sortFunc := func(a, b kv) int { return cmp.Compare(a.Key.String(), b.Key.String()) }
slices.SortFunc(out, sortFunc)
return out
}