Skip to content

Tests: Use DeepEqual replacement using go-cmp, which is more flexible#13452

Merged
bboreham merged 6 commits intoprometheus:mainfrom
bboreham:go-cmp
Feb 12, 2024
Merged

Tests: Use DeepEqual replacement using go-cmp, which is more flexible#13452
bboreham merged 6 commits intoprometheus:mainfrom
bboreham:go-cmp

Conversation

@bboreham
Copy link
Copy Markdown
Member

@bboreham bboreham commented Jan 24, 2024

go-cmp allows control over how unexported fields and implementation details are handled.
Make use of this flexibility to remove convoluted work-arounds for comparing NaNs.

This is excerpted from #12304, where an implementation detail of labels.Labels makes DeepEqual unhelpful.
I though it is valid as a stand-alone change, and makes #12304 easier to review.

I did not change every use of require.Equal to use this new implementation, only those impacted by #12304.

Copy link
Copy Markdown
Member

@cstyan cstyan left a comment

Choose a reason for hiding this comment

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

remote pkg changes lgtm

should we consider a follow up to change all usages of require.Equal, just so we don't end up with inconsistent usage throughout the repo?

@bboreham
Copy link
Copy Markdown
Member Author

should we consider a follow up to change all usages of require.Equal, just so we don't end up with inconsistent usage throughout the repo?

A couple of reasons why I didn't do that already:

  • Package model/labels, which testutils imports, cannot use the new function because that would create an import cycle.
    Possibly I can break that cycle by introducing a third package.
  • Every place you make this change you need to think about unexported fields, since go-cmp will panic if it knows nothing about them. This is an ongoing burden as data structures evolve.

I'm interested to hear what anyone else thinks.

@cstyan
Copy link
Copy Markdown
Member

cstyan commented Jan 30, 2024

should we consider a follow up to change all usages of require.Equal, just so we don't end up with inconsistent usage throughout the repo?

A couple of reasons why I didn't do that already:

  • Package model/labels, which testutils imports, cannot use the new function because that would create an import cycle.
    Possibly I can break that cycle by introducing a third package.
  • Every place you make this change you need to think about unexported fields, since go-cmp will panic if it knows nothing about them. This is an ongoing burden as data structures evolve.

I'm interested to hear what anyone else thinks.

Yep, was just thinking about it as a follow up not in this PR. Your third point is a compelling one for not doing so though. But my question then would be how we can document and make recommendations about when the testutil comparison should be used over deep equal.

go-cmp allows control over how unexported fields and implementation
details are handled.

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
Use DeepEqual replacement using go-cmp, which is more flexible.

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
go-cmp allows more control over unexported fields and implementation
details.

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
Normally, a NaN value is never equal to any other value. Compare sample
values via `Float64bits` so that NaN values which are exactly the same
will compare equal.

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
Also one simpler call checking nil.

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
@bboreham bboreham merged commit ff6c832 into prometheus:main Feb 12, 2024
@bboreham bboreham deleted the go-cmp branch February 12, 2024 14:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants