Skip to content

Commit

Permalink
Fix #1915: Allow result message to be truncated
Browse files Browse the repository at this point in the history
  • Loading branch information
Larry Golding committed Jun 24, 2020
1 parent fb04d12 commit e8b3fe6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Sarif/ExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ public static string GetMessageText(this Result result, ReportingDescriptor rule
return GetMessageText(result, rule, concise: false);
}

public static string GetMessageText(this Result result, ReportingDescriptor rule, bool concise = false)
public static string GetMessageText(this Result result, ReportingDescriptor rule, bool concise = false, int maxLength = 200)
{
if (result == null)
{
Expand Down Expand Up @@ -333,6 +333,10 @@ public static string GetMessageText(this Result result, ReportingDescriptor rule
if (concise)
{
text = GetFirstSentence(text);
if (text.Length > maxLength)
{
text = text.Substring(0, maxLength) + "...";
}
}

return text;
Expand Down
24 changes: 24 additions & 0 deletions src/Test.UnitTests.Sarif/SarifExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,5 +113,29 @@ public void SarifExtensions_Result_GetMessageText_Concise()
string actual = result.GetMessageText(rule, concise: true);
Assert.Equal(expected, actual);
}

[Fact]
public void SarifExtensions_Result_GetMessageText_Concise_Truncated()
{
var result = new Result
{
Message = new Message
{
Id = "ruleStr1"
}
};

var rule = new ReportingDescriptor
{
MessageStrings = new Dictionary<string, MultiformatMessageString>
{
["ruleStr1"] = new MultiformatMessageString { Text = "First sentence is very long. Second sentence." }
}
};

const string Expected = "First sentence is ve...";
string actual = result.GetMessageText(rule, concise: true, maxLength: 20);
Assert.Equal(Expected, actual);
}
}
}

0 comments on commit e8b3fe6

Please sign in to comment.