diff --git a/src/CommandLine/ParserSettings.cs b/src/CommandLine/ParserSettings.cs index 5e2eb895..93962566 100644 --- a/src/CommandLine/ParserSettings.cs +++ b/src/CommandLine/ParserSettings.cs @@ -92,6 +92,9 @@ public CultureInfo ParsingCulture /// Gets or sets the used for help method output. /// Setting this property to null, will disable help screen. /// + /// + /// It is the caller's responsibility to dispose or close the . + /// public TextWriter HelpWriter { get { return helpWriter; } @@ -165,11 +168,7 @@ private void Dispose(bool disposing) if (disposing) { - if (HelpWriter != null) - { - helpWriter.Dispose(); - helpWriter = null; - } + // Do not dispose HelpWriter. It is the caller's responsibility. disposed = true; } diff --git a/tests/CommandLine.Tests/CommandLine.Tests.csproj b/tests/CommandLine.Tests/CommandLine.Tests.csproj index dfcc5c1e..9c985ca0 100644 --- a/tests/CommandLine.Tests/CommandLine.Tests.csproj +++ b/tests/CommandLine.Tests/CommandLine.Tests.csproj @@ -116,6 +116,7 @@ + diff --git a/tests/CommandLine.Tests/Unit/ParserSettingsTests.cs b/tests/CommandLine.Tests/Unit/ParserSettingsTests.cs new file mode 100644 index 00000000..86691c8f --- /dev/null +++ b/tests/CommandLine.Tests/Unit/ParserSettingsTests.cs @@ -0,0 +1,44 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using FluentAssertions; +using Xunit; + +namespace CommandLine.Tests.Unit +{ + public class ParserSettingsTests + { + public class DisposeTrackingStringWriter : StringWriter + { + public DisposeTrackingStringWriter() + { + Disposed = false; + } + + public bool Disposed { get; private set; } + + protected override void Dispose(bool disposing) + { + Disposed = true; + base.Dispose(disposing); + } + } + + [Fact] + public void Disposal_does_not_dispose_HelpWriter() + { + using (DisposeTrackingStringWriter textWriter = new DisposeTrackingStringWriter()) + { + using (ParserSettings parserSettings = new ParserSettings()) + { + parserSettings.HelpWriter = textWriter; + } + + textWriter.Disposed.Should().BeFalse("not disposed"); + } + } + } +}