Skip to content

Commit

Permalink
Baseline states (#1245)
Browse files Browse the repository at this point in the history
* Add 'updated' state to baselineState and rename 'existing' to 'unchanged'

* Update prerelease transformer

* Enable appveyor build + test. Correct version constant.

* Update test. Respond to PR feedback.
  • Loading branch information
michaelcfanning authored Jan 30, 2019
1 parent 0def4c5 commit 07811fd
Show file tree
Hide file tree
Showing 16 changed files with 86 additions and 39 deletions.
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ branches:
only:
- master
- develop
- files-array
- tc-31
- sarif-v1

platform: Any CPU
Expand Down
2 changes: 1 addition & 1 deletion src/Sarif.Converters/FxCopConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ internal Result CreateResult(FxCopLogReader.Context context)
}
else if ("ExcludedInProject".Equals(status))
{
result.BaselineState = BaselineState.Existing;
result.BaselineState = BaselineState.Unchanged;
}

result.RuleId = context.CheckId;
Expand Down
4 changes: 2 additions & 2 deletions src/Sarif.UnitTests/Baseline/DefaultBaselineUnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void DefaultBaseline_SameResults_AllExisting()

Run result = defaultBaseliner.CreateBaselinedRun(baseline, next);

result.Results.Should().OnlyContain(r => r.BaselineState == BaselineState.Existing);
result.Results.Should().OnlyContain(r => r.BaselineState == BaselineState.Unchanged);
result.Results.Should().HaveCount(baseline.Results.Count());
}

Expand Down Expand Up @@ -89,7 +89,7 @@ public void DefaultBaseline_ChangedResultOnNonTrackedField_Existing()

Run result = defaultBaseliner.CreateBaselinedRun(baseline, next);

result.Results.Should().OnlyContain(r => r.BaselineState == BaselineState.Existing);
result.Results.Should().OnlyContain(r => r.BaselineState == BaselineState.Unchanged);
result.Results.Should().HaveCount(baseline.Results.Count());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public void IdenticalResultMatcher_MatchesResults_DifferingOnIdOrStatus_Single()

Result changedResultA = resultA.Result.DeepClone();
changedResultA.CorrelationGuid = Guid.NewGuid().ToString();
changedResultA.BaselineState = BaselineState.Existing;
changedResultA.BaselineState = BaselineState.Unchanged;

ExtractedResult resultB = new ExtractedResult()
{
Expand All @@ -146,7 +146,7 @@ public void IdenticalResultMatcher_MatchesResults_DifferingOnIdOrStatus_Multiple

Result changedResultA = resultAA.Result.DeepClone();
changedResultA.CorrelationGuid = Guid.NewGuid().ToString();
changedResultA.BaselineState = BaselineState.Existing;
changedResultA.BaselineState = BaselineState.Unchanged;

ExtractedResult resultBA = new ExtractedResult()
{
Expand Down Expand Up @@ -185,7 +185,7 @@ public void IdenticalResultMatcher_MatchesResults_DifferingOnResultMatchingPrope

Result changedResultA = resultAA.Result.DeepClone();
changedResultA.CorrelationGuid = Guid.NewGuid().ToString();
changedResultA.BaselineState = BaselineState.Existing;
changedResultA.BaselineState = BaselineState.Unchanged;
changedResultA.SetProperty(SarifLogResultMatcher.ResultMatchingResultPropertyName, new Dictionary<string, string> { { "property", "value" } });

ExtractedResult resultBA = new ExtractedResult()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ public void ResultMatchingBaseliner_BaselinesTwoSimpleSarifLogs()


int existingCount = currentLog.Runs[0].Results.Count - 1;
calculatedNextBaseline.Runs[0].Results.Where(r => r.BaselineState == BaselineState.Existing).Count().Should().Be(existingCount);
calculatedNextBaseline.Runs[0].Results.Where(r => r.BaselineState == BaselineState.Unchanged).Count().Should().Be(existingCount);

if (existingCount > 0)
{
// In the event that we generated a SARIF run of only a single result, we will not have an 'existing' match
// since we adjusted the sole result value by adding a property to it.
calculatedNextBaseline.Runs[0].Results.Where(r => r.BaselineState == BaselineState.Existing).First().TryGetProperty(SarifLogResultMatcher.ResultMatchingResultPropertyName, out Dictionary<string, string> CurrentResultProperties).Should().BeTrue();
calculatedNextBaseline.Runs[0].Results.Where(r => r.BaselineState == BaselineState.Unchanged).First().TryGetProperty(SarifLogResultMatcher.ResultMatchingResultPropertyName, out Dictionary<string, string> CurrentResultProperties).Should().BeTrue();
CurrentResultProperties.Should().ContainKey("Run");
CurrentResultProperties["Run"].Should().BeEquivalentTo(currentLog.Runs[0].Id.InstanceGuid);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ public void SarifLogResultMatcher_BaselinesTwoSimpleSarifLogs()

if (currentLog.Runs[0].Results.Count > 1)
{
calculatedNextBaseline.Runs[0].Results.Where(r => r.BaselineState == BaselineState.Existing).Should().HaveCount(currentLog.Runs[0].Results.Count - 1);
calculatedNextBaseline.Runs[0].Results.Where(r => r.BaselineState == BaselineState.Unchanged).Should().HaveCount(currentLog.Runs[0].Results.Count - 1);

calculatedNextBaseline.Runs[0].Results.Where(r => r.BaselineState == BaselineState.Existing).First().TryGetProperty(SarifLogResultMatcher.ResultMatchingResultPropertyName, out Dictionary<string, string> CurrentResultProperties).Should().BeTrue();
calculatedNextBaseline.Runs[0].Results.Where(r => r.BaselineState == BaselineState.Unchanged).First().TryGetProperty(SarifLogResultMatcher.ResultMatchingResultPropertyName, out Dictionary<string, string> CurrentResultProperties).Should().BeTrue();
CurrentResultProperties.Should().ContainKey("Run");
CurrentResultProperties["Run"].Should().BeEquivalentTo(currentLog.Runs[0].Id.InstanceGuid);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Sarif.UnitTests/Baseline/StrictBaselineUnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void StrictBaseline_SameResults_AllExisting()

Run result = strictBaseliner.CreateBaselinedRun(baseline, next);

result.Results.Should().OnlyContain(r => r.BaselineState == BaselineState.Existing);
result.Results.Should().OnlyContain(r => r.BaselineState == BaselineState.Unchanged);
result.Results.Should().HaveCount(baseline.Results.Count());
}

Expand Down
22 changes: 17 additions & 5 deletions src/Sarif.UnitTests/Readers/IsSuppressedInSourceConverterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void BaselineState_None()
}

[Fact]
public void BaselineState_Existing()
public void BaselineState_UnchangedAndUpdated()
{
string expected =
@"{
Expand All @@ -114,7 +114,13 @@ public void BaselineState_Existing()
""message"": {
""text"": ""Some testing occurred.""
},
""baselineState"": ""existing""
""baselineState"": ""unchanged""
},
{
""message"": {
""text"": ""Some testing occurred.""
},
""baselineState"": ""updated""
}
]
}
Expand All @@ -125,18 +131,24 @@ public void BaselineState_Existing()
var run = new Run() { Tool = DefaultTool };
uut.Initialize(run);
uut.WriteResults(new[] { new Result
uut.WriteResults(new[] {
new Result
{
Message = new Message { Text = "Some testing occurred."},
BaselineState = BaselineState.Existing
BaselineState = BaselineState.Unchanged
},
new Result {
Message = new Message { Text = "Some testing occurred."},
BaselineState = BaselineState.Updated
}
});
});
actual.Should().BeCrossPlatformEquivalent<SarifLog>(expected);

var sarifLog = JsonConvert.DeserializeObject<SarifLog>(actual);
Assert.Equal(SuppressionStates.None, sarifLog.Runs[0].Results[0].SuppressionStates);
Assert.Equal(BaselineState.Existing, sarifLog.Runs[0].Results[0].BaselineState);
Assert.Equal(BaselineState.Unchanged, sarifLog.Runs[0].Results[0].BaselineState);
Assert.Equal(BaselineState.Updated, sarifLog.Runs[0].Results[1].BaselineState);
}
}
}
3 changes: 2 additions & 1 deletion src/Sarif/Autogenerated/BaselineState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ namespace Microsoft.CodeAnalysis.Sarif
public enum BaselineState
{
None,
Existing,
Unchanged,
Updated,
New,
Absent
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ private Result ConstructExistingResult(
Result result = CurrentResult.Result.DeepClone();
result.CorrelationGuid = PreviousResult.Result.CorrelationGuid;
result.SuppressionStates = PreviousResult.Result.SuppressionStates;
result.BaselineState = BaselineState.Existing;
result.BaselineState = BaselineState.Unchanged;

if (!PreviousResult.Result.TryGetProperty(SarifLogResultMatcher.ResultMatchingResultPropertyName, out OriginalResultMatchingProperties))
{
Expand Down
3 changes: 2 additions & 1 deletion src/Sarif/Baseline/ResultMatching/SarifLogMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,8 @@ private SarifLog ConstructSarifLogFromMatchedResults(
Result result = resultPair.CalculateBasedlinedResult(PropertyBagMergeBehavior);

IList<FileData> files =
(PropertyBagMergeBehavior.HasFlag(DictionaryMergeBehavior.InitializeFromOldest) && result.BaselineState == BaselineState.Existing)
(PropertyBagMergeBehavior.HasFlag(DictionaryMergeBehavior.InitializeFromOldest) &&
(result.BaselineState == BaselineState.Unchanged || result.BaselineState == BaselineState.Updated))
? resultPair.PreviousResult.OriginalRun.Files
: resultPair.Run.Files;

Expand Down
2 changes: 1 addition & 1 deletion src/Sarif/Baseline/SarifLogBaseliner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public Run CreateBaselinedRun(Run baseLine, Run nextLog)
Result newResult = result.DeepClone();

newResult.BaselineState =
baseLine.Results.Contains(result, ResultComparator) ? BaselineState.Existing : BaselineState.New;
baseLine.Results.Contains(result, ResultComparator) ? BaselineState.Unchanged : BaselineState.New;

differencedRun.Results.Add(newResult);
}
Expand Down
3 changes: 2 additions & 1 deletion src/Sarif/CodeGenHints.json
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,8 @@
"description": "The state of a result relative to a baseline of a previous run.",
"zeroValueName": "None",
"memberNames": [
"Existing",
"Unchanged",
"Updated",
"New",
"Absent"
]
Expand Down
3 changes: 2 additions & 1 deletion src/Sarif/Schemata/sarif-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1428,7 +1428,8 @@
"description": "The state of a result relative to a baseline of a previous run.",
"enum": [
"new",
"existing",
"unchanged",
"updated",
"absent"
]
},
Expand Down
5 changes: 3 additions & 2 deletions src/Sarif/Visitors/SarifTransformerUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public static BaselineState CreateBaselineState(BaselineStateVersionOne v1Baseli
case BaselineStateVersionOne.Absent:
return BaselineState.Absent;
case BaselineStateVersionOne.Existing:
return BaselineState.Existing;
return BaselineState.Unchanged;
case BaselineStateVersionOne.New:
return BaselineState.New;
default:
Expand All @@ -200,7 +200,8 @@ public static BaselineStateVersionOne CreateBaselineStateVersionOne(BaselineStat
{
case BaselineState.Absent:
return BaselineStateVersionOne.Absent;
case BaselineState.Existing:
case BaselineState.Unchanged:
case BaselineState.Updated:
return BaselineStateVersionOne.Existing;
case BaselineState.New:
return BaselineStateVersionOne.New;
Expand Down
58 changes: 44 additions & 14 deletions src/Sarif/Writers/PrereleaseCompatibilityTransformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,32 +47,40 @@ public static SarifLog UpdateToCurrentVersion(

switch (version)
{
case "2.0.0-csd.2.beta.2018-11-28":
case "2.0.0-csd.2.beta.2019-01-24":
{
// SARIF TC28. Nothing to do.
// SARIF TC31. Nothing to do.
break;
}

case "2.0.0-csd.2.beta.2019-01-09":
{
modifiedLog |= ApplyChangesFromTC31(sarifLog);
break;
}

case "2.0.0-csd.2.beta.2018-10-10":
{
// 2.0.0-csd.2.beta.2018-10-10 == changes through SARIF TC #25
modifiedLog |= ApplyChangesFromTC25ThroughTC28(
modifiedLog |= ApplyChangesFromTC25ThroughTC30(
sarifLog,
out fullyQualifiedLogicalNameToIndexMap,
out fileLocationKeyToIndexMap,
out ruleKeyToIndexMap);
break;
modifiedLog |= ApplyChangesFromTC31(sarifLog);
break;
}

default:
{
modifiedLog |= ApplyCoreTransformations(sarifLog);
modifiedLog |= ApplyChangesFromTC25ThroughTC28(
modifiedLog |= ApplyChangesFromTC25ThroughTC30(
sarifLog,
out fullyQualifiedLogicalNameToIndexMap,
out fileLocationKeyToIndexMap,
out ruleKeyToIndexMap);
break;
modifiedLog |= ApplyChangesFromTC31(sarifLog);
break;
}
}

Expand Down Expand Up @@ -109,7 +117,33 @@ public static SarifLog UpdateToCurrentVersion(
return transformedSarifLog;
}

private static bool ApplyChangesFromTC25ThroughTC28(
private static bool ApplyChangesFromTC31(JObject sarifLog)
{
bool modifiedLog = UpdateSarifLogVersion(sarifLog); ;

if (sarifLog["runs"] is JArray runs)
{
foreach (JObject run in runs)
{
if (run["results"] is JArray results)
{
foreach (JObject result in results)
{
string baselineState = (string)result["baselineState"];

if ("existing".Equals(baselineState))
{
result["baselineState"] = "unchanged";
modifiedLog = true;
}
}
}
}
}
return modifiedLog;
}

private static bool ApplyChangesFromTC25ThroughTC30(
JObject sarifLog,
out Dictionary<string, int> fullyQualifiedLogicalNameToIndexMap,
out Dictionary<string, int> fileKeyToIndexMap,
Expand All @@ -128,10 +162,8 @@ private static bool ApplyChangesFromTC25ThroughTC28(
// For completness, this update added run.newlineSequences to the schema
// This is a non-breaking (additive) change, so there is no work to do.
//https://github.com/oasis-tcs/sarif-spec/issues/169

var runs = (JArray)sarifLog["runs"];

if (runs != null)

if (sarifLog["runs"] is JArray runs)
{
foreach (JObject run in runs)
{
Expand Down Expand Up @@ -551,9 +583,7 @@ private static bool ApplyCoreTransformations(JObject sarifLog)
{
bool modifiedLog = UpdateSarifLogVersion(sarifLog);

var runs = (JArray)sarifLog["runs"];

if (runs != null)
if (sarifLog["runs"] is JArray runs)
{
foreach (JObject run in runs)
{
Expand Down

0 comments on commit 07811fd

Please sign in to comment.