Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

status: modify TestStatus_ErrorDetails_Fail to replace protoimpl package #6953

Merged
merged 5 commits into from
Feb 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 28 additions & 19 deletions status/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/protoadapt"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/runtime/protoimpl"
"google.golang.org/protobuf/types/known/anypb"
"google.golang.org/protobuf/types/known/durationpb"

Expand Down Expand Up @@ -378,23 +377,23 @@ func (s) TestStatus_WithDetails_Fail(t *testing.T) {

func (s) TestStatus_ErrorDetails_Fail(t *testing.T) {
tests := []struct {
s *Status
i []any
s *Status
want []any
}{
{
nil,
nil,
s: nil,
want: nil,
},
{
FromProto(nil),
nil,
s: FromProto(nil),
want: nil,
},
{
New(codes.OK, ""),
[]any{},
s: New(codes.OK, ""),
want: []any{},
},
{
FromProto(&spb.Status{
s: FromProto(&spb.Status{
Code: int32(cpb.Code_CANCELLED),
Details: []*anypb.Any{
{
Expand All @@ -408,8 +407,8 @@ func (s) TestStatus_ErrorDetails_Fail(t *testing.T) {
}),
},
}),
[]any{
protoimpl.X.NewError("invalid empty type URL"),
want: []any{
errors.New("invalid empty type URL"),
&epb.ResourceInfo{
ResourceType: "book",
ResourceName: "projects/1234/books/5678",
Expand All @@ -419,17 +418,27 @@ func (s) TestStatus_ErrorDetails_Fail(t *testing.T) {
},
}
for _, tc := range tests {
got := tc.s.Details()
if !cmp.Equal(got, tc.i, cmp.Comparer(proto.Equal), cmp.Comparer(equalError)) {
t.Errorf("(%v).Details() = %+v, want %+v", str(tc.s), got, tc.i)
details := tc.s.Details()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should first ensure len(details) == len(tc.want) here or it could panic.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's right! thanks for catching that

if len(details) != len(tc.want) {
t.Fatalf("len(s.Details()) = %v, want = %v.", len(details), len(tc.want))
}
for i, d := range details {
// s.Details can either contain an error or a proto message. We
// want to do a compare the proto message for an Equal match, and
// for errors only check for presence.
if _, ok := d.(error); ok {
if (d != nil) != (tc.want[i] != nil) {
t.Fatalf("s.Details()[%v] was %v; want %v", i, d, tc.want[i])
}
continue
}
if !cmp.Equal(d, tc.want[i], cmp.Comparer(proto.Equal)) {
t.Fatalf("s.Details()[%v] was %v; want %v", i, d, tc.want[i])
}
}
}
}

func equalError(x, y error) bool {
return x == y || (x != nil && y != nil && x.Error() == y.Error())
}

func str(s *Status) string {
if s == nil {
return "nil"
Expand Down
Loading