Skip to content

Commit

Permalink
(cake-contribGH-681) Set correct ruleIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalberger committed Jul 17, 2024
1 parent b0fb158 commit 34d61a4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,39 @@ public void Should_Set_Rules_If_RuleUrl_Without_RuleName()
result.GetProperty(SarifIssueReportGenerator.RuleUrlPropertyName).ShouldBe(ruleUrl);
}

[Fact]
public void Should_Set_Correct_RuleIndex()
{
// Given
var fixture = new SarifIssueReportFixture();
var firstRuleId = "Rule Foo";
var secondRuleId = "Rule Bar";
var ruleUrl = new Uri("https://www.example.come/rules/rule.html");
var issues =
new List<IIssue>
{
IssueBuilder
.NewIssue("Message Foo.", "ProviderType Foo", "ProviderName Foo")
.OfRule(firstRuleId, ruleUrl)
.Create(),
IssueBuilder
.NewIssue("Message Bar.", "ProviderType Foo", "ProviderName Foo")
.OfRule(secondRuleId, ruleUrl)
.Create(),
};

// When
var logContents = fixture.CreateReport(issues);

// Then
var sarifLog = JsonConvert.DeserializeObject<SarifLog>(logContents);

var results = sarifLog.Results();
results.Count().ShouldBe(2);
results.Single(x => x.RuleId == firstRuleId).RuleIndex.ShouldBe(0);
results.Single(x => x.RuleId == secondRuleId).RuleIndex.ShouldBe(1);
}

[Fact]
public void Should_Set_Text_Message()
{
Expand Down
3 changes: 2 additions & 1 deletion src/Cake.Issues.Reporting.Sarif/SarifIssueReportGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,8 @@ private Result GetResult(SarifIssue sarifIssue)
{
if (!this.ruleIndices.TryGetValue(sarifIssue.Issue.RuleId, out var value))
{
this.ruleIndices.Add(sarifIssue.Issue.RuleId, this.rules.Count);
value = this.rules.Count;
this.ruleIndices.Add(sarifIssue.Issue.RuleId, value);
this.rules.Add(
new ReportingDescriptor
{
Expand Down

0 comments on commit 34d61a4

Please sign in to comment.