Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing index out of range in baseliner #2102

Merged
merged 3 commits into from
Oct 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public Result CalculateBasedlinedResult(DictionaryMergeBehavior propertyBagMerge
}

Run = PreviousResult.OriginalRun;
result.Run = Run;

return result;
}
Expand Down Expand Up @@ -117,6 +118,7 @@ public Result CalculateBasedlinedResult(DictionaryMergeBehavior propertyBagMerge
}

Run = CurrentResult.OriginalRun;
result.Run = Run;

return result;
}
Expand Down Expand Up @@ -144,6 +146,7 @@ public Result CalculateBasedlinedResult(DictionaryMergeBehavior propertyBagMerge
}

Run = CurrentResult.OriginalRun;
result.Run = Run;

return result;
}
Expand Down
7 changes: 3 additions & 4 deletions src/Sarif/Baseline/ResultMatching/SarifLogResultMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,13 +231,12 @@ private List<ExtractedResult> ExtractResultsFromRuns(IEnumerable<Run> sarifRuns)
}

var visitor = new RunMergingVisitor();

foreach (MatchedResults resultPair in results)
{
Result result = resultPair.CalculateBasedlinedResult(PropertyBagMergeBehavior);
Run contextRun = (result.BaselineState == BaselineState.Unchanged || result.BaselineState == BaselineState.Updated) ? resultPair.PreviousResult.OriginalRun : resultPair.Run;

visitor.CurrentRun = contextRun;
visitor.CurrentRun = result.Run;
visitor.VisitResult(result);
}

Expand All @@ -255,7 +254,7 @@ private List<ExtractedResult> ExtractResultsFromRuns(IEnumerable<Run> sarifRuns)
// Find the 'oldest' log file and initialize properties from that log property bag.
properties = currentRuns.Last().Properties;
}

properties ??= new Dictionary<string, SerializedPropertyInfo>();

var graphs = new List<Graph>();
Expand Down
40 changes: 39 additions & 1 deletion src/Test.UnitTests.Sarif/Baseline2/OverallBaseliningTests.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Collections.Generic;
using System.Linq;

using FluentAssertions;

using Microsoft.CodeAnalysis.Sarif;
using Microsoft.CodeAnalysis.Sarif.Baseline.ResultMatching;

using Microsoft.CodeAnalysis.Test.Utilities.Sarif;
using Newtonsoft.Json;

using Xunit;
Expand Down Expand Up @@ -74,5 +76,41 @@ public void Overall_AbsentResultsInNewRunKept()
// All other results are unchanged.
output.Runs[0].Results.Where(result => result.BaselineState == BaselineState.Unchanged).Count().Should().Be(SampleLog.Runs[0].Results.Count - 1);
}

[Fact]
public void Overall_TestingSameSarif()
{
SarifLog baselineSarif = TestData.CreateBaseline();

ISarifLogMatcher matcher = ResultMatchingBaselinerFactory.GetDefaultResultMatchingBaseliner();
SarifLog output = matcher.Match(new SarifLog[] { baselineSarif }, new SarifLog[] { baselineSarif }).First();

output.Runs.First().Results.First().BaselineState.Should().Be(BaselineState.Unchanged);
}

[Fact]
public void Overall_CheckingUnchangedDifferentRulesOrder()
{
SarifLog baselineSarif = TestData.CreateBaseline();
SarifLog currentSarif = TestData.CreateBaselineUnchanged();

ISarifLogMatcher matcher = ResultMatchingBaselinerFactory.GetDefaultResultMatchingBaseliner();
SarifLog output = matcher.Match(new SarifLog[] { baselineSarif }, new SarifLog[] { currentSarif }).First();

output.Runs[0].Results[0].BaselineState.Should().Be(BaselineState.Unchanged);
}

[Fact]
public void Overall_CheckingAbsentAndNew()
{
SarifLog baselineSarif = TestData.CreateBaseline();
SarifLog currentSarif = TestData.CreateBaselineNew();

ISarifLogMatcher matcher = ResultMatchingBaselinerFactory.GetDefaultResultMatchingBaseliner();
SarifLog output = matcher.Match(new SarifLog[] { baselineSarif }, new SarifLog[] { currentSarif }).First();

output.Runs[0].Results[0].BaselineState.Should().Be(BaselineState.Absent);
output.Runs[0].Results[1].BaselineState.Should().Be(BaselineState.New);
}
}
}
183 changes: 181 additions & 2 deletions src/Test.Utilities.Sarif/TestData.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright (c) Microsoft Corporation. All Rights Reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Collections.Generic;
using System.Data;
using Microsoft.CodeAnalysis.Sarif;

namespace Microsoft.CodeAnalysis.Test.Utilities.Sarif
Expand Down Expand Up @@ -109,7 +109,7 @@ public static SarifLog CreateOneIdThreeLocations()
}

public static SarifLog CreateTwoRunThreeResultLog()
{
{
return new SarifLog
{
Runs = new[]
Expand Down Expand Up @@ -257,5 +257,184 @@ public static SarifLog CreateEmptyRun()
}
};
}

public static SarifLog CreateBaseline()
{
return new SarifLog
{
Runs = new Run[] {
new Run
{
Tool = new Tool
{
Driver = new ToolComponent
{
Name = TestToolName,
Rules = new ReportingDescriptor[]
{
new ReportingDescriptor
{
Id = RuleIds.Rule2,
ShortDescription = new MultiformatMessageString
{
Text = TestMessageText
}
}
}
}
},
Results = new Result[]
{
new Result
{
RuleId = RuleIds.Rule2,
RuleIndex = 0,
Message = new Message
{
Text = TestMessageText
},
Locations = new Location[]
{
new Location
{
PhysicalLocation = new PhysicalLocation
{
ArtifactLocation = new ArtifactLocation
{
Uri = new Uri("src/test0001.cs", UriKind.Relative)
},
Region = new Region
{
StartLine = 0,
StartColumn = 0,
}
}
}
},
BaselineState = BaselineState.Unchanged
}
}
}
},
};
}

public static SarifLog CreateBaselineUnchanged()
{
return new SarifLog
{
Runs = new List<Run> {
new Run
{
Tool = new Tool
{
Driver = new ToolComponent
{
Name = TestToolName,
Rules = new List<ReportingDescriptor>
{
new ReportingDescriptor
{
Id = RuleIds.Rule1,
ShortDescription = new MultiformatMessageString
{
Text = TestMessageText
}
},
new ReportingDescriptor
{
Id = RuleIds.Rule2,
ShortDescription = new MultiformatMessageString
{
Text = TestMessageText
}
}
}
}
},
Results = new List<Result>
{
new Result
{
RuleId = RuleIds.Rule2,
RuleIndex = 1,
Message = new Message
{
Text = TestMessageText
},
Locations = new List<Location>
{
new Location
{
PhysicalLocation = new PhysicalLocation
{
ArtifactLocation = new ArtifactLocation
{
Uri = new Uri("src/test0001.cs", UriKind.Relative)
}
}
}
}
}
}
}
},
};
}

public static SarifLog CreateBaselineNew()
{
return new SarifLog
{
Runs = new Run[] {
new Run
{
Tool = new Tool
{
Driver = new ToolComponent
{
Name = TestToolName,
Rules = new ReportingDescriptor[]
{
new ReportingDescriptor
{
Id = RuleIds.Rule1,
ShortDescription = new MultiformatMessageString
{
Text = TestMessageText
}
}
}
}
},
Results = new Result[]
{
new Result
{
RuleId = RuleIds.Rule1,
RuleIndex = 0,
Message = new Message
{
Text = TestMessageText
},
Locations = new Location[]
{
new Location
{
PhysicalLocation = new PhysicalLocation
{
ArtifactLocation = new ArtifactLocation
{
Uri = new Uri("src/test0001.cs", UriKind.Relative)
}
}
}
}
}
}
}
},
};
}
}
}