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

Fixed issue where TextRunner accidentally disposes System.Out #4318

Closed
wants to merge 1 commit into from

Conversation

jeroenvervaeke
Copy link

It is possible to replace System.Out with your own implementation of TextWriter.
One reason why you would want to do that is if you want to both capture all output to a stream and write it to console.

I was using this approach and I stumbled upon an issue when using TextRunner.
When using TextRunner, and not providing a file, TextRunner defaults to System.Out.
When TextRunner is done running it disposes the underlying stream that it was writing to. This is correct behaviour when using files, but not desired behaviour when using System.Out.
See: source code

I modified ConsoleColorWriter so Dispose is a NOP. (The base class disposes the writer provided by ConsoleColorWriter = System.Out).

I've also added a unit test to verify that the fix works. If you want a code example of what crashes, check the unit test.

@jeroenvervaeke
Copy link
Author

@dotnet-policy-service agree

@stevenaw
Copy link
Member

Appreciate your time and effort on this @jeroenvervaeke . I'm going to close this one as a duplicate of Norm's as I suspect the detection of Console.Out being changed in that PR may make it a safer fix with fewer chance of edge cases. Thanks again for having looked into and submitted a fix for this in your spare time.

@stevenaw stevenaw closed this Mar 26, 2023
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

Successfully merging this pull request may close these issues.

None yet

2 participants