Fresh dep ensure

This commit is contained in:
Mike Cronce
2018-11-26 13:23:56 -05:00
parent 93cb8a04d7
commit 407478ab9a
9016 changed files with 551394 additions and 279685 deletions

View File

@ -1,36 +0,0 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
"go_test",
)
go_test(
name = "go_default_test",
srcs = ["diff_test.go"],
embed = [":go_default_library"],
)
go_library(
name = "go_default_library",
srcs = ["diff.go"],
importpath = "k8s.io/apimachinery/pkg/util/diff",
deps = [
"//vendor/github.com/davecgh/go-spew/spew:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/util/validation/field:go_default_library",
],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
)

View File

@ -89,20 +89,60 @@ func ObjectReflectDiff(a, b interface{}) string {
}
out := []string{""}
for _, d := range diffs {
elidedA, elidedB := limit(d.a, d.b, 80)
out = append(out,
fmt.Sprintf("%s:", d.path),
limit(fmt.Sprintf(" a: %#v", d.a), 80),
limit(fmt.Sprintf(" b: %#v", d.b), 80),
fmt.Sprintf(" a: %s", elidedA),
fmt.Sprintf(" b: %s", elidedB),
)
}
return strings.Join(out, "\n")
}
func limit(s string, max int) string {
if len(s) > max {
return s[:max]
// limit:
// 1. stringifies aObj and bObj
// 2. elides identical prefixes if either is too long
// 3. elides remaining content from the end if either is too long
func limit(aObj, bObj interface{}, max int) (string, string) {
elidedPrefix := ""
elidedASuffix := ""
elidedBSuffix := ""
a, b := fmt.Sprintf("%#v", aObj), fmt.Sprintf("%#v", bObj)
if aObj != nil && bObj != nil {
if aType, bType := fmt.Sprintf("%T", aObj), fmt.Sprintf("%T", bObj); aType != bType {
a = fmt.Sprintf("%s (%s)", a, aType)
b = fmt.Sprintf("%s (%s)", b, bType)
}
}
for {
switch {
case len(a) > max && len(a) > 4 && len(b) > 4 && a[:4] == b[:4]:
// a is too long, b has data, and the first several characters are the same
elidedPrefix = "..."
a = a[2:]
b = b[2:]
case len(b) > max && len(b) > 4 && len(a) > 4 && a[:4] == b[:4]:
// b is too long, a has data, and the first several characters are the same
elidedPrefix = "..."
a = a[2:]
b = b[2:]
case len(a) > max:
a = a[:max]
elidedASuffix = "..."
case len(b) > max:
b = b[:max]
elidedBSuffix = "..."
default:
// both are short enough
return elidedPrefix + a + elidedASuffix, elidedPrefix + b + elidedBSuffix
}
}
return s
}
func public(s string) bool {

View File

@ -75,6 +75,11 @@ object.A:
a: []int(nil)
b: []int{}`,
},
"display type differences": {a: []interface{}{int64(1)}, b: []interface{}{uint64(1)}, out: `
object[0]:
a: 1 (int64)
b: 0x1 (uint64)`,
},
}
for name, test := range testCases {
expect := test.out
@ -94,3 +99,50 @@ func TestStringDiff(t *testing.T) {
t.Errorf("diff returned %v", diff)
}
}
func TestLimit(t *testing.T) {
testcases := []struct {
a interface{}
b interface{}
expectA string
expectB string
}{
{
a: `short a`,
b: `short b`,
expectA: `"short a"`,
expectB: `"short b"`,
},
{
a: `short a`,
b: `long b needs truncating`,
expectA: `"short a"`,
expectB: `"long b ne...`,
},
{
a: `long a needs truncating`,
b: `long b needs truncating`,
expectA: `...g a needs ...`,
expectB: `...g b needs ...`,
},
{
a: `long common prefix with different stuff at the end of a`,
b: `long common prefix with different stuff at the end of b`,
expectA: `...end of a"`,
expectB: `...end of b"`,
},
{
a: `long common prefix with different stuff at the end of a`,
b: `long common prefix with different stuff at the end of b which continues`,
expectA: `...of a"`,
expectB: `...of b which...`,
},
}
for _, tc := range testcases {
a, b := limit(tc.a, tc.b, 10)
if a != tc.expectA || b != tc.expectB {
t.Errorf("limit(%q, %q)\n\texpected: %s, %s\n\tgot: %s, %s", tc.a, tc.b, tc.expectA, tc.expectB, a, b)
}
}
}