-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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
testing: normalize line endings in example output comparisons #51269
Comments
#41980 could help somewhat here, in that the diff could at least (potentially) show the difference in line endings. |
I tend to agree, but I think this is a subtle enough change to send through the proposal process. Do you want to go that route, or does #41980 suffice? |
I just re-ran this under Go 1.22 and the issue is still there (GitHub Actions failure). It seems very reasonable to me to have code that outputs Can this issue be added to the proposal review? |
This seems completely reasonable to me. Thanks for the good use case description. |
This proposal has been added to the active column of the proposals project |
Based on the discussion above, this proposal seems like a likely accept. The proposal is to convert example output \r\n into \n before comparing against the golden output in the source files, but only when running on Windows. This is necessary since source files have the same conversion applied to them, and some programs aim to print \r\n on Windows. On Unix, using \r\n is a mistake so we won’t do the conversion there. (If we change our mind about Unix we can always change that later, but it seems right not to convert.) |
No change in consensus, so accepted. 🎉 The proposal is to convert example output \r\n into \n before comparing against the golden output in the source files, but only when running on Windows. This is necessary since source files have the same conversion applied to them, and some programs aim to print \r\n on Windows. On Unix, using \r\n is a mistake so we won’t do the conversion there. (If we change our mind about Unix we can always change that later, but it seems right not to convert.) |
Change https://go.dev/cl/627035 mentions this issue: |
Thanks @iwdgo for the fix! |
If you have code which outputs
\r\n
(CR LF) as a newline delimiter on Windows, testable examples using this code will fail. I noticed this on my GoAWK project, which translates\n
to\r\n
on Windows to be a good Windows line-ending citizen on that platform. I'm currently using a!windows
build constraint to skip this on Windows so these testable examples don't fail (source).In addition, it's not obvious from the failure message what's going on, because even though it prints the got/want output, it looks correct (CR LF doesn't look any different from just LF when printed):
Test output and source. My stripped-down example to show what's happening is as follows (though of course in GoAWK it's a bit more complex and hidden):
It seems to me that examples should normalize line endings, and that these tests should succeed on Windows. When you're writing examples you don't want to be bothered by which kind of line ending the code is outputting. (If you care at that level, write a non-example test to ensure that behaviour.)
I'm on Go version 1.17, but this affects all versions going way back.
For reference, here are a few other "testable example whitespace" issues. Most of them are about spaces at the end of lines, so don't directly address this issue:
In #6416, Rob Pike says:
This could be taken as applying to this issue as well. I don't love this -- it is "easiest" to specify, but it seems to me not what you want, and definitely not visually obvious (which is what example tests are about). In any case, if that's the way it is, so be it. Maybe we could at least make the failure message more obvious in that case?
And if there are better ways to approach this problem in GoAWK or wherever else, I'm all ears!
The text was updated successfully, but these errors were encountered: