Skip to content

testing/protocmp: add IgnoreUnsetFields #1578

Open
@MasterMedo

Description

@MasterMedo

Is your feature request related to a problem? Please describe.
Comparing protobuf types in full with the cmp.Diff or cmp.Equal makes for a change detector test. This is because every time a new field is added to the protobuf all old tests need to be updated to have this field as well. This doesn't improve the confidence that the code is correct and introduces operational load on the engineers. Additionally, the lack of this feature causes many side values of the return type to be mocked. The problem with that is that often the mocked values don't contribute to the validity of the test and when they change in the implementation all tests need to be changed as well.

Describe the solution you'd like
I'd like an option like protocmp.IngoreUnsetFields(m protobufMessage) to exist. It would be used by passing in the want protobuf message as the argument. E.g.

func TestMessageContent(t *testing.T) {
  content := "content"
  want := pb.Message_builder{
    Content: content,
  }
  got := MessageFromContent(content)
  if diff := cmp.Diff(want, got, protocmp.Transform(), protocmp.IgnoreUnsetFields(want)); diff != "" {
    t.Errorf(...)
  }
}

Describe alternatives you've considered
Alternatively, one has to manually implement the function by using the cmp.FilterField function.

Additional context
This started as a document created internally in Google because often when a field was added to a protobuf old tests needed to be changed. The addition of this function only partially solves the issues in that document, but it's a great first step!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions