From 23a3adf8d27d3fe36aa531159ca9edd77eeca8fa Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Wed, 21 Jun 2023 08:12:24 +0200 Subject: [PATCH] use cmp.Diff instead of reflect.DeepEqual Signed-off-by: Kristoffer Dalby --- hscontrol/policy/acls_test.go | 26 ++++++++++++++------------ hscontrol/util/addr_test.go | 6 +++--- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/hscontrol/policy/acls_test.go b/hscontrol/policy/acls_test.go index a71ef20e..ab7cfb47 100644 --- a/hscontrol/policy/acls_test.go +++ b/hscontrol/policy/acls_test.go @@ -3,7 +3,6 @@ package policy import ( "errors" "net/netip" - "reflect" "testing" "github.com/google/go-cmp/cmp" @@ -17,6 +16,10 @@ import ( "tailscale.com/tailcfg" ) +var ipComparer = cmp.Comparer(func(x, y netip.Addr) bool { + return x.Compare(y) == 0 +}) + func Test(t *testing.T) { check.TestingT(t) } @@ -788,8 +791,8 @@ func Test_expandTagOwners(t *testing.T) { return } - if !reflect.DeepEqual(got, test.want) { - t.Errorf("expandTagOwners() = %v, want %v", got, test.want) + if diff := cmp.Diff(test.want, got); diff != "" { + t.Errorf("expandTagOwners() = (-want +got):\n%s", diff) } }) } @@ -885,8 +888,8 @@ func Test_expandPorts(t *testing.T) { return } - if !reflect.DeepEqual(got, test.want) { - t.Errorf("expandPorts() = %v, want %v", got, test.want) + if diff := cmp.Diff(test.want, got); diff != "" { + t.Errorf("expandPorts() = (-want +got):\n%s", diff) } }) } @@ -946,11 +949,10 @@ func Test_listMachinesInUser(t *testing.T) { } for _, test := range tests { t.Run(test.name, func(t *testing.T) { - if got := filterMachinesByUser(test.args.machines, test.args.user); !reflect.DeepEqual( - got, - test.want, - ) { - t.Errorf("listMachinesInUser() = %v, want %v", got, test.want) + got := filterMachinesByUser(test.args.machines, test.args.user) + + if diff := cmp.Diff(test.want, got); diff != "" { + t.Errorf("listMachinesInUser() = (-want +got):\n%s", diff) } }) } @@ -1700,8 +1702,8 @@ func Test_excludeCorrectlyTaggedNodes(t *testing.T) { test.args.nodes, test.args.user, ) - if !reflect.DeepEqual(got, test.want) { - t.Errorf("excludeCorrectlyTaggedNodes() = %v, want %v", got, test.want) + if diff := cmp.Diff(test.want, got, ipComparer); diff != "" { + t.Errorf("excludeCorrectlyTaggedNodes() (-want +got):\n%s", diff) } }) } diff --git a/hscontrol/util/addr_test.go b/hscontrol/util/addr_test.go index 45b2b92f..0e08d707 100644 --- a/hscontrol/util/addr_test.go +++ b/hscontrol/util/addr_test.go @@ -2,9 +2,9 @@ package util import ( "net/netip" - "reflect" "testing" + "github.com/google/go-cmp/cmp" "go4.org/netipx" ) @@ -111,8 +111,8 @@ func Test_parseIPSet(t *testing.T) { return } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("parseIPSet() = %v, want %v", got, tt.want) + if diff := cmp.Diff(tt.want, got); diff != "" { + t.Errorf("parseIPSet() = (-want +got):\n%s", diff) } }) }