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

Optionally disable truncating string output in diffs #279

Closed
paddycarver opened this issue Oct 28, 2021 · 8 comments
Closed

Optionally disable truncating string output in diffs #279

paddycarver opened this issue Oct 28, 2021 · 8 comments

Comments

@paddycarver
Copy link

paddycarver commented Oct 28, 2021

Hello,

We're using cmp to check our test output:

if diff := cmp.Diff(wanted, got); diff != "" {
  t.Fatalf("Diff: %s", diff)
}

This works great usually, and has been incredibly helpful.

However, we have certain error output that can contain long strings. To make matters worse, the long string sometimes has the error information we need at the end, not at the beginning.

So our test will fail with an unexpected error (not actually an error interface implementation, it's a long story. It's a struct of strings) and will helpfully tell us there's one error more than it expected. Unhelpfully, though, the information about what the error is is truncated by cmp. So to find out what error actually occurred, we need to go in and add a logging statement to log what the error was and print the whole thing.

This works, but it would be much more convenient if cmp would just print the whole string for us.

Is there a method we can implement or an option we can pass that just says "no, show the whole string"? Is there a way to achieve that outcome?

@dsnet
Copy link
Collaborator

dsnet commented Oct 28, 2021

We've historically avoided any knobs to configure the verbosity of the output and instead relied upon heuristics to determine how much output is helpful to emit. This is especially relevant for recursive data structures where the "full" output may be infinite.

To better tune the heuristics, do you have a reproduction that I can look at?

@paddycarver
Copy link
Author

Sure, here's a reproduction: https://play.golang.org/p/KwJoZWmrtiC

The error message there is actually copy/pasted from our codebase.

Please let me know if I can provide any more information. If we need to implement a certain interface, or something, that's something we can do. Sadly, the type in question is an interface, so I can't promise things like "it will always be a struct" or "it will always have a property named this".

I'm wondering if defining a String() method on the type would solve this issue....

@dsnet
Copy link
Collaborator

dsnet commented Oct 28, 2021

I'm not sure I understand the original problem. The reproduction prints:

  diag.Diagnostics(
- 	nil,
+ 	{
+ 		diag.ErrorDiagnostic{
+ 			detail:  "An unexpected error was encountered trying to convert into a Ter"...,
+ 			summary: "Something went wrong",
+ 		},
+ 	},
  )

Even if the string is truncated, it's clear what's different at a higher level. That is, one Diagnostic is nil, but the other one isn't.

@paddycarver
Copy link
Author

My issue is that the helpful information is the bit that's truncated. I know I've got one more Diagnostic than I need, but to figure out why that's happening, I need the truncated "mismatched types: wanted Foo, got Bar", which points me to why I've got one more diagnostic than I need.

Diagnostics are our version of errors; knowing I've got an error is helpful, but knowing what the error is is more helpful.

@s3rj1k
Copy link

s3rj1k commented Nov 22, 2021

I am also curious how can this be tweaked a bit, as I have nested struct with long URLs inside it, and URLs get truncated

@PatrickCronin
Copy link

I also have long structures which are getting truncated. For my particular case, it's not too relevant for the portions which are identical, but when I see ... // 4 identical, 2 inserted, and 169 modified entries, the missing inserted and modified are very relevant. I'm wondering if I could create a custom Reporter option similar to the defaultReporter but it called FormatDiff on a formatOptions struct containing a typeMode of emitType and a maximum verbosity; perhaps that would work? I haven't tried it yet though.

@s3rj1k
Copy link

s3rj1k commented Nov 30, 2021

@PatrickCronin Please share code example if you do get around truncation with custom Reporter, thanks.

@dsnet
Copy link
Collaborator

dsnet commented Dec 9, 2021

Closing as duplicate of #264. Let's only keep one issue open about adjusting verbosity.

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

No branches or pull requests

4 participants