Skip to content

Commit

Permalink
Fix infinite loop in HelpText.Addline; add unit test + validation
Browse files Browse the repository at this point in the history
  • Loading branch information
qmfrederik committed Dec 29, 2016
1 parent caf0ca3 commit 95ded2d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
25 changes: 21 additions & 4 deletions src/CommandLine/Text/HelpText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public SentenceBuilder SentenceBuilder
public static HelpText AutoBuild<T>(
ParserResult<T> parserResult,
Func<HelpText, HelpText> onError,
Func<Example, Example> onExample,
Func<Example, Example> onExample,
bool verbsIndex = false,
int maxDisplayWidth = DefaultMaximumLength)
{
Expand Down Expand Up @@ -254,7 +254,7 @@ public static HelpText AutoBuild<T>(

usageAttr.Do(
usage => usage.AddToHelpText(auto, true));

usageLines.Do(
lines => auto.AddPreOptionsLines(lines));

Expand Down Expand Up @@ -519,7 +519,7 @@ public static IEnumerable<string> RenderParsingErrorsTextAsLines<T>(
yield return line.ToString();
}

var mutuallyErrs =
var mutuallyErrs =
formatMutuallyExclusiveSetErrors(
meaningfulErrors.OfType<MutuallyExclusiveSetError>());
if (mutuallyErrs.Length > 0)
Expand Down Expand Up @@ -619,8 +619,25 @@ public override string ToString()
.ToString();
}

private static void AddLine(StringBuilder builder, string value, int maximumLength)
internal static void AddLine(StringBuilder builder, string value, int maximumLength)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}

if (value == null)
{
throw new ArgumentNullException(nameof(value));
}

if (maximumLength < 1)
{
throw new ArgumentOutOfRangeException(nameof(value));
}

value = value.Trim();

builder.AppendWhen(builder.Length > 0, Environment.NewLine);
do
{
Expand Down
12 changes: 12 additions & 0 deletions tests/CommandLine.Tests/Unit/Text/HelpTextTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using CommandLine.Text;
using FluentAssertions;
using Xunit;
using System.Text;

namespace CommandLine.Tests.Unit.Text
{
Expand Down Expand Up @@ -657,5 +658,16 @@ public void AutoBuild_with_assembly_company_attribute_only()
ReflectionHelper.SetAttributeOverride(null);
}
}

[Fact]
public void Add_line_with_two_empty_spaces_at_the_end()
{
StringBuilder b = new StringBuilder();
HelpText.AddLine(b,
"Test ",
1);

Assert.Equal("T" + Environment.NewLine + "e" + Environment.NewLine + "s" + Environment.NewLine + "t", b.ToString());
}
}
}

0 comments on commit 95ded2d

Please sign in to comment.