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

Replaced reflect.DeepEqual with cmp.Diff in Go tests #2281

Conversation

tariq-hasan
Copy link
Contributor

What this PR does / why we need it: This PR replaces reflect.DeepEqual with cmp.Diff in all the tests.

Which issue(s) this PR fixes (optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close the issue(s) when PR gets merged):
Fixes #2268

Checklist:

  • Docs included if any changes are user facing

@tariq-hasan tariq-hasan changed the title replaced reflect with cmp Replaced reflect.DeepEqual with cmp.Diff in Go tests Mar 10, 2024
Copy link
Member

@tenzen-y tenzen-y left a comment

Choose a reason for hiding this comment

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

Could you apply the same approach the same as my comment to other places?

} else if !reflect.DeepEqual(expectedRunSpec, actualRunSpec) {
t.Errorf("Case: %v failed. Expected %v\n got %v", tc.testDescription, expectedRunSpec.Object, actualRunSpec.Object)
} else if diff := cmp.Diff(expectedRunSpec.Object, actualRunSpec.Object); diff != "" {
t.Errorf("Case: %v failed. Diff (-expected, +actual):\n%s", tc.testDescription, diff)
Copy link
Member

Choose a reason for hiding this comment

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

We must define errors, and compare expected and wanted errors and values.

Define errors:

var (
ErrKatibConfigNil = fmt.Errorf("failed to parse katib-config.yaml in ConfigMap: %s", consts.KatibConfigMapName)
ErrInvalidGVKFormat = errors.New("invalid GroupVersionKinds")
ErrTrialResourcesAreEmpty = errors.New("trialResources are empty")
)

Compare Errors:
if diff := cmp.Diff(tc.wantError, err, cmpopts.EquateErrors()); len(diff) != 0 {
t.Errorf("Unexpected error from TrialResourcesToGVKs (-want,+got):\n%s", diff)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good. I am creating a separate PR given that file-metricscollector_test.go has already been updated in #2274.

@tariq-hasan tariq-hasan force-pushed the replace-reflect.DeepEqual-with-cmp.Diff branch from 6fb9157 to 6f372f6 Compare March 12, 2024 22:48
@google-oss-prow google-oss-prow bot removed the size/M label Mar 12, 2024
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: tariq-hasan

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Replace reflect.DeepEqual with cmp.Diff in tests
2 participants