Skip to content

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

@keilin-anz

Description

@keilin-anz

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

$ go version
go version go1.20.2 darwin/amd64

⚠️ 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsFixThe path to resolution is known, but the work has not been done.help wanted

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions