-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Make spew config selection configurable to allow for helpful diffs on time.Time objects nested in structs. #1079
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
base: master
Are you sure you want to change the base?
Make spew config selection configurable to allow for helpful diffs on time.Time objects nested in structs. #1079
Conversation
… time.Time objects nested in structs.
|
@boyan-soubachov would it be possible to take a look at this. I think this is a nice usability improvement. |
|
Hmm. It seems like most of the feedback after #895 seems to be that In other words:
Would it make more sense if the control is something that's injected in the code rather than an env var. I think it would make more sense if it's something explicit and isn't coupled to environment configuration since this is a unit testing framework, after all :) |
Agreed.
Agreed. I don't think this is something one would want to change depending on a particular environment, but rather an option a project could use to control how diffs are printed in a static manner. |
|
Can this be please merged as it is? Wouldn't change the logic as it breaks backwards compatibility. |
brackendawson
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm open to allowing users to configure the diff to use stringers so long as they are aware that it can call methods on the potentially faulty code under test.
I'm not convinced an environment variable is the way to do this, one would need to see the horrible diff with all the time zones listed, know this feature exists and then re-run the tests with an environment variable set. Setting environment variables for Go tests in some contexts (like VSCode) is overly burdensome.
I actually think a package global variable in the assert package would be better. This can be set by the test code calling testify in an init function for all tests in a package, or in a narrower scope such as one synchronous test or even a few lines of one test. This would only affect the package being tested as other packages are tested with newly initialised imports.
| } | ||
|
|
||
| var spewConfig = spew.ConfigState{ | ||
| const stringerEnabledEnvVarName = "TESTIFY_SPEW_STRINGER_ENABLE" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't expose the library name in the environment variable. Something like "TESTIFY_DIFF_STRINGER_ENABLE" would be better, if we use an environment variable at all.
|
I have opened another PR to address this issue, I would like to get feedback from people who were involved in this current PR ☝️ |
Summary
Make time.Time objects nested inside structs produce reasonable diffs by making spewConfig configurable
Changes
#895 had some unintended side-effects on what diffs of
time.Timelook like: see #1078.This attempts to solve the problem by allowing the user to select if they want to use the SpewConfig that has Stringer methods enabled. The default behavior remains the same. But if the user runs their tests after setting the environment variable
TESTIFY_SPEW_STRINGER_ENABLEtoTRUE, testify will pick the stringer config that does not disable the Stringer methods and thus produces nice diffs for time.Time nested inside structs.This contains a minimal repro of this issue, right now those tests produce an almost 1500 line diff on test failure. After this change and when
TESTIFY_SPEW_STRINGER_ENABLEenv var is set toTRUEit will produce a user friendly diff like:Motivation
Please see #1078 which describes the issue in detail
export TESTIFY_SPEW_STRINGER_ENABLE=TRUE && go test ./...Related issues
#1078