Skip to content

logtest: add WithCmpOpts#6774

Closed
pellared wants to merge 7 commits intoopen-telemetry:mainfrom
pellared:logtest-msg
Closed

logtest: add WithCmpOpts#6774
pellared wants to merge 7 commits intoopen-telemetry:mainfrom
pellared:logtest-msg

Conversation

@pellared
Copy link
Copy Markdown
Member

@pellared pellared commented May 14, 2025

Fixes #6491
Fixes #6489

Towards #6341

Comment thread log/logtest/assert.go
Comment on lines +69 to +75
// WithCmpOpts adds additional [cmp.Option] used when comparing values.
func WithCmpOpts(opts ...cmp.Option) AssertOption {
return fnOption(func(cfg assertConfig) assertConfig {
cfg.cmpOpts = append(cfg.cmpOpts, opts...)
return cfg
})
}
Copy link
Copy Markdown
Member Author

@pellared pellared May 14, 2025

Choose a reason for hiding this comment

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

Initially I though, about adding something like

// Transform applies a transformation f function that
// converts values of a certain type into that of another.
// f must not mutate A in any way.
func Transform[A, B any](f func(A) B) AssertOption {
	return fnOption(func(cfg assertConfig) assertConfig {
		cfg.cmpOpts = append(cfg.cmpOpts, cmp.Transformer("", f))
		return cfg
	})
}

Usage:

logtest.AssertEqual(t, want, got,
	// Ignore Timestamps.
	logtest.Transform(func(time.Time) time.Time {
		return time.Time{}
	}),
)

But after some thoughts I think that allowing to use any cmp.Option gives more flexibility.
There is a cost - tight coupling to cmp. However, I do not think we would want to to use anything different for comparing. This is also a "helper" module for tests and we do not plan making a release anytime soon (at least before cmp has a v1.

I am open to suggestions and different opinions.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm in favor of not tightly coupling to cmp. If a user wants to use that package for testing won't they just use it instead of this package?

I like the transform function provided above.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@dmathieu, do you have any preference? Are you ok with Transform proposal?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't have any preference.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Created #6794

@pellared pellared marked this pull request as ready for review May 14, 2025 15:07
@pellared
Copy link
Copy Markdown
Member Author

pellared commented May 14, 2025

@pellared pellared mentioned this pull request May 19, 2025
@pellared pellared marked this pull request as draft May 19, 2025 11:15
pellared added a commit that referenced this pull request May 20, 2025
@pellared pellared closed this May 20, 2025
@pellared pellared deleted the logtest-msg branch May 20, 2025 09:01
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.

log/logtest: Add option to alter got before comparing log/logtest: Add testable example

3 participants