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

Simplifying tests and adding fix in changelog #2111

Merged
merged 5 commits into from
Oct 16, 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
1 change: 1 addition & 0 deletions src/ReleaseHistory.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

* DEPENDENCY BREAKING: SARIF now requires Newtonsoft.JSON 11.0.2 (rather than 10.0.3)
* DEPENDENCY: SARIF TypeScript package now requires minimist 1.2.3 or later (rather than >=1.2.0)
* BUGFIX: Fix index out of range exception when baselining [#2102](https://github.com/microsoft/sarif-sdk/pull/2102)
* FEATURE: Add a setter to `GitHelper.GitExePath`. [#2110](https://github.com/microsoft/sarif-sdk/pull/2110)
* FEATURE: `GitHelper` will search in %PATH% variable for `git.exe` instead of its default install location. [#2107](https://github.com/microsoft/sarif-sdk/pull/2107)
* FEATURE: Add helper in `SarifLog` and `Run` to `ApplyPolicies`. [#2109](https://github.com/microsoft/sarif-sdk/pull/2109)
Expand Down
29 changes: 11 additions & 18 deletions src/Test.UnitTests.Sarif/Baseline2/OverallBaseliningTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,39 +78,32 @@ public void Overall_AbsentResultsInNewRunKept()
}

[Fact]
public void Overall_TestingSameSarif()
public void Overall_CheckingAbsentUnchangedAndNew()
{
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();
SarifLog baselineSarif = TestData.CreateSimpleLogWithRules(ruleIdStartIndex: 0, resultCount: 2);
SarifLog currentSarif = TestData.CreateSimpleLogWithRules(ruleIdStartIndex: 1, resultCount: 2);

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);
output.Runs[0].Results[0].BaselineState.Should().Be(BaselineState.Absent);
output.Runs[0].Results[1].BaselineState.Should().Be(BaselineState.Unchanged);
output.Runs[0].Results[2].BaselineState.Should().Be(BaselineState.New);
}

[Fact]
public void Overall_CheckingAbsentAndNew()
{
SarifLog baselineSarif = TestData.CreateBaseline();
SarifLog currentSarif = TestData.CreateBaselineNew();
SarifLog baselineSarif = TestData.CreateSimpleLogWithRules(ruleIdStartIndex: 0, resultCount: 2);
SarifLog currentSarif = TestData.CreateSimpleLogWithRules(ruleIdStartIndex: 10, resultCount: 2);

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);
output.Runs[0].Results[1].BaselineState.Should().Be(BaselineState.Absent);
output.Runs[0].Results[2].BaselineState.Should().Be(BaselineState.New);
output.Runs[0].Results[3].BaselineState.Should().Be(BaselineState.New);
}
}
}
4 changes: 1 addition & 3 deletions src/Test.UnitTests.Sarif/GitHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,8 @@ public void GitExePath_WhenPathDoesNotExist_SettingManuallyShouldWork()

var gitHelper = new GitHelper(mockFileSystem.Object);

gitHelper.GitExePath.Should().BeNull();

gitHelper.GitExePath = @"C:\dev";
gitHelper.GitExePath.Should().NotBeNullOrEmpty();
gitHelper.GitExePath.Should().Be(@"C:\dev");
}
}
}
185 changes: 27 additions & 158 deletions src/Test.Utilities.Sarif/TestData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,183 +258,52 @@ public static SarifLog CreateEmptyRun()
};
}

public static SarifLog CreateBaseline()
public static SarifLog CreateSimpleLogWithRules(int ruleIdStartIndex, int resultCount)
{
return new SarifLog
var rules = new ReportingDescriptor[resultCount];
var results = new Result[resultCount];
for (int i = 0; i < resultCount; i++)
{
Runs = new Run[] {
new Run
rules[i] = new ReportingDescriptor
{
Id = $"TEST{i + ruleIdStartIndex}",
ShortDescription = new MultiformatMessageString
{
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
}
}
Text = $"Test description {i + ruleIdStartIndex}"
}
},
};
}
};

public static SarifLog CreateBaselineUnchanged()
{
return new SarifLog
{
Runs = new List<Run> {
new Run
results[i] = new Result
{
RuleId = $"TEST{i + ruleIdStartIndex}",
RuleIndex = 0,
Message = new Message
{
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)
}
}
}
}
}
}
}
},
};
}
Text = $"Error description {i + ruleIdStartIndex}"
},
};
}

public static SarifLog CreateBaselineNew()
{
return new SarifLog
var sarifLog = new SarifLog
{
Runs = new Run[] {
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
}
}
}
Rules = rules
}
},
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)
}
}
}
}
}
}
Results = results
}
},
}
};

return sarifLog;
}
}
}