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

proposal: testing: add function to set output writer #59454

Open
peterebden opened this issue Apr 5, 2023 · 3 comments
Open

proposal: testing: add function to set output writer #59454

peterebden opened this issue Apr 5, 2023 · 3 comments
Labels
Milestone

Comments

@peterebden
Copy link

The framework in the testing package prints its output to os.Stdout. A caller might want to redirect this elsewhere, or alter it - e.g. to write to a file as well which another system might parse. This can be achieved by replacing os.Stdout, but this is overly difficult since it's a File (#13473 suggests this is known to be undesirable but can't change before v2), requiring e.g. os.Pipe and associated management + overhead. However, testing only needs an io.Writer rather than the full os.File type.

I propose that we add a new function to the testing package allowing a caller to set the stream that is output to, along the lines of:

// OutputStream sets the stream to which testing output is written.
// It must be called before any tests, benchmarks or fuzz suites are run, and should not be called while they are running.
// If not called, the default is os.Stdout
func OutputStream(w io.Writer) {
    outputStream = w
}

var outputStream = os.Stdout

I might be being a little defensive about not exposing the variable directly, but it feels like the function is a clearer documentation point and helps future-proof the interface a bit.

@gopherbot gopherbot added this to the Proposal milestone Apr 5, 2023
@seankhliao
Copy link
Member

We might not be able to do this since we promise passing through Stdout in one of the modes, see #34877

@peterebden
Copy link
Author

I think that's a slightly different thing. go test guarantees that the subprocess gets os.Stdout pointing to the original terminal. If that subprocess chooses to do something different for output (say in a TestMain), that's up to it - today it can open a File and reassign os.Stdout to that if it wants to. This would just provide a (much) nicer interface for tests wanting to opt into that behaviour.

@ianlancetaylor ianlancetaylor changed the title proposal: testing: Add function to set output writer proposal: testing: add function to set output writer Apr 5, 2023
@bitfield
Copy link

A caller might want to redirect this elsewhere

>

or alter it - e.g. to write to a file as well

| tee

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Incoming
Development

No branches or pull requests

4 participants