golang/go

testing: Testable examples collapse `Output` newlines, making them impossible to pass

Open

#59,191 opened on Mar 23, 2023

View on GitHub
 (5 comments) (0 reactions) (0 assignees)Go (19,008 forks)batch import
NeedsFixhelp wanted

Repository metrics

Stars
 (133,883 stars)
PR merge metrics
 (No merged PRs in 30d)

Description

What version of Go are you using (go version)?

⚠️ This also appears to affect go1.19, though I believe it worked at some point in the past

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

❌ Not relevant, this happens in multiple environments including Go Playground with Go 1.19 and Go 1.20

What did you do?

  1. Provide a testable example of a function which returns output containing successive newlines
  2. Example cannot pass tests, since the Output annotation is modified by the testing parser

Minimal example

Viewable here: https://go.dev/play/p/LFktKVr75lz

✅ The ExampleFoo function returns data containing successive newlines:

Single newline:

Double newline:


Final text

✅ The ExampleFoo contains an embedded Output: annotation with the same expected output:

	//Output:
	//Single newline:
	//
	//Double newline:
	//
	//
	//Final text

✅ The Output and stdout are compared, with leading and trailing whitespace removed ❌ Multiple newlines are collapsed into single newlines, making the test fail - only solution is to modify the function under test (which is not an option)

What did you expect to see?

Output: should be parsed as-is, not modified (beyond trimming leading and trailing whitespace)

What did you see instead?

The multiple empty lines are collapsed into a single empty line

Additional context

Did a little hunting to see if I could find a root cause, no success yet

Currently suspect it's either hidden somewhere within the testing package, or is an unintended side effect of how chains of single line comments are parsed by ast

Contributor guide