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

Feature: Fail fast for time comparisons #1160

Open
distinctdan opened this issue Feb 22, 2022 · 2 comments · May be fixed by #1240
Open

Feature: Fail fast for time comparisons #1160

distinctdan opened this issue Feb 22, 2022 · 2 comments · May be fixed by #1240

Comments

@distinctdan
Copy link

I'm running into an issue with time comparisons failing when the tests are run in github actions, due to internal timezone differences in the time structs. I understand why they're failing per this issue: #666. However, I'd like a way to catch these kind of things locally so that we can prevent coding any time comparisons in the first place.

Would it be possible to globally fail any assertion that tries to compare 2 times directly? Ideally I'm thinking of a global config setting that can be enabled, something like disableTimeCompare, that makes any test fail that directly compares 2 time.Time's.

Example:

type MyStruct struct {
    Now time.Time
}

var a, b MyStruct

// Bad comparison - should fail because of time compare.
assert.Equal(t, a, b)

Alternatively, is there already a way to do this? Thanks!

@brackendawson
Copy link
Collaborator

I'm struggling to come up with a test which is sensitive to the system TimeZone, can you show a minimum reproducible example? In the one you have given time.Time{} == time.Time{} is true in all zones.

A possible workaround might be you can select one of the more obscure time zones in your tests using an init function:

func init() {
    os.Setenv("TZ", "Australia/Broken_Hill")
}

Also, nice issue given the significance of today's date.

@distinctdan
Copy link
Author

So the actual cases for this that I've come across are in integration tests, either when we convert a struct to json and back, or when we read times from the database.

Here's a playground example where I convert a time to json and back. The time shows as having the exact same time, and using the time package's Equal function returns true. However, directly comparing the structs fails:
https://go.dev/play/p/rMxB3oq4QBy

Also, that's a creative idea of setting an odd timezone to make any comparisons fail, thanks, I'll have to try that.

@annarosenthal annarosenthal linked a pull request Jul 31, 2022 that will close this issue
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 a pull request may close this issue.

2 participants