Skip to content

fix: time.Time rendering in the diff returned by error message#1829

Open
ccoVeille wants to merge 1 commit intostretchr:masterfrom
ccoveille-forks:gospew-time-stringer
Open

fix: time.Time rendering in the diff returned by error message#1829
ccoVeille wants to merge 1 commit intostretchr:masterfrom
ccoveille-forks:gospew-time-stringer

Conversation

@ccoVeille
Copy link
Collaborator

@ccoVeille ccoVeille commented Nov 18, 2025

Summary

Improve formatting when dealing with time.Time now we vendored gospew

Changes

Now, go-spew is vendored, we can fix old issues we had.

We can fix the representation of time.Time in gospew

And remove the imperfect fix we had in place.

Motivation

#895 was created by @luan to solve some legitimate issues. It was released with v1.6.0 on May 29, 2020

But unfortunately, the changes lead to an issue with the way time.Time are rendered. So #989 was opened on Aug 1, 2020 by @AlekSi

The issue was somehow fixed with #1072 by @HaraldNordgren

#1072 was merged in Apr 27, 2021 (but only released in Mar 1, 2024 with v1.7.1 )

Unfortunately, the fix was incomplete, as it was working only for first level, so #1078 was opened on May 22, 2021 by @shawc71

#1079 was opened by @shawc71 on May 22, 2021 to try to fix the issue

But it was never merged, as the solution was also imperfect, and because they were no solution inside testify to fix this

So #1078 was left open until now.

Related issues

@ccoVeille ccoVeille changed the title chore: import go-spew from https://github.com/davecgh/go-spew fix: time.Time rendering in the diff returned by error message Nov 18, 2025
@ccoVeille ccoVeille force-pushed the gospew-time-stringer branch from 3f71fbe to 7b28236 Compare November 28, 2025 11:38
@ccoVeille
Copy link
Collaborator Author

@brackendawson please review, it's ready.

I'm facing this issue with testify for ages, I cannot wait to see it fixed 😬

fredbi added a commit to fredbi/testify that referenced this pull request Jan 4, 2026
This is inspired by stretchr#1829,
but we proceed differently, not checking for a string type but for
type convertibility to a time instead.

Added more tests to check how embedded types, pointers etc actually render and don't cause
panic.

From the original issues:

* fixes stretchr#1078
* fixes stretchr#1079

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
@fredbi
Copy link

fredbi commented Jan 4, 2026

@ccoVeille I found your PR definitely interesting but perhaps lacking a few things.
You may find an alternate implementation there go-openapi#27
The main differences are:

  • does not use type string representation, but type convertibility to time
  • does not sort using the string representation (time.Time does NOT implement sort.Interface), but uses time.Time.Compare instead
  • more tests with embedded types, private fields, pointers, nil values etc

Could be interesting to exchange our views

@ccoVeille
Copy link
Collaborator Author

Thanks @fredbi. I will take a look.

I saw the PRs you made in the last days.

I agree with you about the string sort that can lead to problem when comparing for example 999-12-31 and 1000-01-01

I'll review the PR in your fork also

fredbi added a commit to go-openapi/testify that referenced this pull request Jan 4, 2026
* fix: rendering time values
* added sortability for time.Time

This is inspired by stretchr#1829,
but we proceed differently, not checking for a string type but for
type convertibility to a time instead.

Added more tests to check how embedded types, pointers etc actually render and don't cause
panic.

From the original issues:

* fixes stretchr#1078
* fixes stretchr#1079

* doc: fixed a few hiccups in README

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>

---------

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
@dolmen dolmen added enhancement: output format Enhancement related to formatting of messages pkg-assert Change related to package testify/assert pkg-require Change related to package testify/require enhancement labels Feb 17, 2026
Copilot AI review requested due to automatic review settings February 23, 2026 19:04
@ccoVeille ccoVeille force-pushed the gospew-time-stringer branch from 7b28236 to 59a70c3 Compare February 23, 2026 19:04
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR improves time.Time formatting in assertion diffs by leveraging the newly vendored internal spew package, removing the prior special-casing in assert.diff and enabling readable time.Time output while keeping method calls disabled for other types.

Changes:

  • Added EnableTimeStringer to internal/spew.ConfigState and used it to selectively call String() for time.Time.
  • Updated assert diffing to rely on the new spew behavior instead of a dedicated time.Time branch/config.
  • Added/expanded tests covering time.Time diff output at top-level and within structs.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
internal/spew/config.go Adds EnableTimeStringer config knob to selectively allow time.Time.String() even when methods are otherwise disabled.
internal/spew/format.go Uses EnableTimeStringer to allow method handling for time.Time during formatting.
internal/spew/dump.go Uses EnableTimeStringer to allow method handling for time.Time during dumping.
internal/spew/common.go Attempts to adjust sorting behavior (currently incorrectly) for time.Time.
internal/spew/spew_test.go Adds test cases validating time.Time formatting with/without the new config flag.
assert/assertions.go Removes the old time.Time-specific diff special-case and enables EnableTimeStringer on the shared spew config.
assert/assertions_test.go Adds a regression test for diffing structs containing time.Time.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@ccoVeille ccoVeille marked this pull request as draft February 23, 2026 19:27
@ccoVeille
Copy link
Collaborator Author

Copilot provided the same feedback as you @fredbi

This time I got it, haha

@fredbi
Copy link

fredbi commented Feb 23, 2026

Copilot provided the same feedback as you @fredbi

This time I got it, haha

Should I feel flattered? Mmh not so sure :)
Anyway you may always take a look at the implementation at github.com/go-openapi/testify (v2). This part has been merged long ago. All further improvements there are of course welcome.

@ccoVeille
Copy link
Collaborator Author

Copilot provided the same feedback as you @fredbi

This time I got it, haha

Should I feel flattered? Mmh not so sure :)

Haha. It's simply that:

  • I haven't check last time
  • Copilot spotted the exact lines that are bad. It was easier to figure out what was the problem.

Anyway you may always take a look at the implementation at github.com/go-openapi/testify (v2). This part has been merged long ago. All further improvements there are of course welcome.

I'm already looking at it.

@ccoVeille ccoVeille force-pushed the gospew-time-stringer branch from 59a70c3 to d071955 Compare February 23, 2026 20:55
Now we vendored go-spew we can modify how time.Time values are handled.

We can remove the imperfect fix we had in place before.
@ccoVeille ccoVeille force-pushed the gospew-time-stringer branch from d071955 to 1485969 Compare February 23, 2026 21:34
@ccoVeille ccoVeille marked this pull request as ready for review February 23, 2026 21:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement: output format Enhancement related to formatting of messages enhancement pkg-assert Change related to package testify/assert pkg-require Change related to package testify/require

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Diffs on structs with nested time.Time objects produce hard to read diffs

4 participants