Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7390914

Browse files
authored
Merge pull request #359 from anthonylangsworth/HelpWriter_Disposal
Do not dispose ParserSettings.HelpWriter in ParserSettings.Dispose (fix for #355)
2 parents 4fa8ddf + 922fe40 commit 7390914

File tree

3 files changed

+49
-5
lines changed

3 files changed

+49
-5
lines changed

‎src/CommandLine/ParserSettings.cs‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ public CultureInfo ParsingCulture
9292
/// Gets or sets the <see cref="System.IO.TextWriter"/> used for help method output.
9393
/// Setting this property to null, will disable help screen.
9494
/// </summary>
95+
/// <remarks>
96+
/// It is the caller's responsibility to dispose or close the <see cref="TextWriter"/>.
97+
/// </remarks>
9598
public TextWriter HelpWriter
9699
{
97100
get { return helpWriter; }
@@ -165,11 +168,7 @@ private void Dispose(bool disposing)
165168

166169
if (disposing)
167170
{
168-
if (HelpWriter != null)
169-
{
170-
helpWriter.Dispose();
171-
helpWriter = null;
172-
}
171+
// Do not dispose HelpWriter. It is the caller's responsibility.
173172

174173
disposed = true;
175174
}

‎tests/CommandLine.Tests/CommandLine.Tests.csproj‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@
116116
<Compile Include="Unit\Infrastructure\FSharpOptionHelperTests.cs" />
117117
<Compile Include="Unit\Core\ReflectionExtensions.cs" />
118118
<Compile Include="Unit\ParserResultExtensionsTests.cs" />
119+
<Compile Include="Unit\ParserSettingsTests.cs" />
119120
<Compile Include="Unit\ParserTests.cs" />
120121
<Compile Include="Unit\Text\HelpTextTests.cs" />
121122
<Compile Include="Unit\UnParserExtensionsTests.cs" />
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using FluentAssertions;
8+
using Xunit;
9+
10+
namespace CommandLine.Tests.Unit
11+
{
12+
public class ParserSettingsTests
13+
{
14+
public class DisposeTrackingStringWriter : StringWriter
15+
{
16+
public DisposeTrackingStringWriter()
17+
{
18+
Disposed = false;
19+
}
20+
21+
public bool Disposed { get; private set; }
22+
23+
protected override void Dispose(bool disposing)
24+
{
25+
Disposed = true;
26+
base.Dispose(disposing);
27+
}
28+
}
29+
30+
[Fact]
31+
public void Disposal_does_not_dispose_HelpWriter()
32+
{
33+
using (DisposeTrackingStringWriter textWriter = new DisposeTrackingStringWriter())
34+
{
35+
using (ParserSettings parserSettings = new ParserSettings())
36+
{
37+
parserSettings.HelpWriter = textWriter;
38+
}
39+
40+
textWriter.Disposed.Should().BeFalse("not disposed");
41+
}
42+
}
43+
}
44+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /