public void AbsoluteUri_ReversesRebasedURIs() { Random random = RandomSarifLogGenerator.GenerateRandomAndLog(this.output); int count = random.Next(10) + 1; for (int i = 0; i < count; i++) { SarifLog log = RandomSarifLogGenerator.GenerateSarifLogWithRuns(random, random.Next(10)); string inputSarifText = JsonConvert.SerializeObject(log, Formatting.Indented); log.RebaseUri("SRCROOT", false, new Uri(RandomSarifLogGenerator.GeneratorBaseUri)).MakeUrisAbsolute(); string outputSarifText = JsonConvert.SerializeObject(log, Formatting.Indented); if (log.Runs == null) { continue; } log.Runs.All( run => run.Results == null || run.Results.All( result => result.Locations == null || result.Locations.All( location => location.PhysicalLocation.ArtifactLocation.Uri.IsAbsoluteUri && string.IsNullOrEmpty(location.PhysicalLocation.ArtifactLocation.UriBaseId)))) .Should().BeTrue(); } }
public void AbsoluteUri_ReversesRebasedURIs() { Random random = RandomSarifLogGenerator.GenerateRandomAndLog(this.output); List <SarifLog> logs = new List <SarifLog>(); int count = random.Next(10) + 1; for (int i = 0; i < count; i++) { SarifLog log = RandomSarifLogGenerator.GenerateSarifLogWithRuns(random, random.Next(10)); logs.Add(log); } logs.RebaseUri("SRCROOT", false, new Uri(RandomSarifLogGenerator.GeneratorBaseUri)).MakeUrisAbsolute(); // All file URIs should be absolute. logs.All( log => log.Runs == null || log.Runs.All( run => run.Results == null || run.Results.All( result => result.Locations == null || result.Locations.All( location => !location.PhysicalLocation.FileLocation.Uri.IsAbsoluteUri && !string.IsNullOrEmpty(location.PhysicalLocation.FileLocation.UriBaseId))))) .Should().BeTrue(); }
public void RewriteUri_RewritesAllFiles(int fileCount) { Random random = RandomSarifLogGenerator.GenerateRandomAndLog(this.output); List <SarifLog> logs = new List <SarifLog>(); for (int i = 0; i < fileCount; i++) { logs.Add(RandomSarifLogGenerator.GenerateSarifLogWithRuns(random, random.Next(10))); } bool rebaseRelativeUris = false; IActionWrapper <SarifLog> RewriteUri = SarifLogProcessorFactory.GetActionStage(SarifLogAction.RebaseUri, new string[] { "SRCROOT", rebaseRelativeUris.ToString(), @"C:\src\" }); IEnumerable <SarifLog> rewrittenLogs = RewriteUri.Act(logs.AsEnumerable()); rewrittenLogs.Should().HaveCount(logs.Count); // We just check that the log rewriter hit each run. We'll test the RewriteUriVisitor more comprehensively in its own test class. foreach (SarifLog rewrittenLog in rewrittenLogs) { if (rewrittenLog.Runs != null) { foreach (Run run in rewrittenLog.Runs) { run.OriginalUriBaseIds.Should().ContainKey("SRCROOT"); } } } }
public void SarifLogResultMatcher_PreviousLogEmpty_WorksAsExpected() { Random random = RandomSarifLogGenerator.GenerateRandomAndLog(this.output); SarifLog currentLog = RandomSarifLogGenerator.GenerateSarifLogWithRuns(random, 1); currentLog.Runs[0].InstanceGuid = Guid.NewGuid().ToString(); SarifLog calculatedNextBaseline = baseliner.Match(new SarifLog[0], new SarifLog[] { currentLog }).First(); calculatedNextBaseline.Runs.Should().HaveCount(1); if (currentLog.Runs[0].Results.Any()) { // We should have the same number of results. calculatedNextBaseline.Runs[0].Results.Should().HaveCount(currentLog.Runs[0].Results.Count); // They should all have correllation ids calculatedNextBaseline.Runs[0] .Results.Where(r => string.IsNullOrEmpty(r.CorrelationGuid)).Should().HaveCount(0); // They should all be new. calculatedNextBaseline.Runs[0] .Results.Where(r => r.BaselineState == BaselineState.New).Should().HaveCount(currentLog.Runs[0].Results.Count); // And they should have the correct property set. calculatedNextBaseline.Runs[0] .Results.Where(r => r.BaselineState == BaselineState.New) .First().TryGetProperty( SarifLogResultMatcher.ResultMatchingResultPropertyName, out Dictionary <string, object> NewResultProperties) .Should().BeTrue(); NewResultProperties.Should().ContainKey("Run"); NewResultProperties["Run"].Should().BeEquivalentTo(currentLog.Runs[0].InstanceGuid); } }
public void SarifLogResultMatcher_CurrentLogEmpty_AllAbsent() { Random random = RandomSarifLogGenerator.GenerateRandomAndLog(this.output); SarifLog baselineLog = RandomSarifLogGenerator.GenerateSarifLogWithRuns(random, 1); SarifLog currentLog = new SarifLog(); currentLog.Runs = new Run[] { new Run() }; baselineLog.Runs[0].InstanceGuid = Guid.NewGuid().ToString(); currentLog.Runs[0].InstanceGuid = Guid.NewGuid().ToString(); currentLog.Runs[0].Tool = new Tool() { Name = "Test" }; foreach (Result result in baselineLog.Runs[0].Results) { result.CorrelationGuid = Guid.NewGuid().ToString(); } SarifLog calculatedNextBaseline = baseliner.Match(new SarifLog[] { baselineLog }, new SarifLog[] { currentLog }).First(); calculatedNextBaseline.Runs.Should().HaveCount(1); if (baselineLog.Runs[0].Results.Any()) { calculatedNextBaseline.Runs[0].Results.Should().HaveCount(baselineLog.Runs[0].Results.Count); calculatedNextBaseline.Runs[0].Results.Where(r => string.IsNullOrEmpty(r.CorrelationGuid)).Should().HaveCount(0); calculatedNextBaseline.Runs[0].Results.Where(r => r.BaselineState == BaselineState.Absent).Should().HaveCount(baselineLog.Runs[0].Results.Count); calculatedNextBaseline.Runs[0].Results.Where(r => r.BaselineState == BaselineState.Absent).First().TryGetProperty(SarifLogResultMatcher.ResultMatchingResultPropertyName, out Dictionary <string, string> AbsentResultProperties).Should().BeTrue(); AbsentResultProperties.Should().ContainKey("Run"); AbsentResultProperties["Run"].Should().BeEquivalentTo(baselineLog.Runs[0].InstanceGuid); } }
public void AbsoluteUri_ReversesRebasedURIs() { Random random = RandomSarifLogGenerator.GenerateRandomAndLog(this.output); List <SarifLog> logs = new List <SarifLog>(); int count = random.Next(10) + 1; for (int i = 0; i < count; i++) { SarifLog log = RandomSarifLogGenerator.GenerateSarifLogWithRuns(random, random.Next(10)); logs.Add(log); } logs.RebaseUri("%SRCROOT%", new Uri(RandomSarifLogGenerator.GeneratorBaseUri)).MakeUrisAbsolute(); // All file URIs should be absolute. logs.All( log => log.Runs == null || log.Runs.All( run => run.Files == null || run.Files.Keys.All( key => run.Files[key].FileLocation.Uri.ToString() == key && run.Files[key].FileLocation.Uri.IsAbsoluteUri && string.IsNullOrEmpty(run.Files[key].FileLocation.UriBaseId)))) .Should().BeTrue(); }
public void RebaseUri_WorksAsExpected() { Random random = RandomSarifLogGenerator.GenerateRandomAndLog(this.output); List <SarifLog> logs = new List <SarifLog>(); int count = random.Next(10) + 1; for (int i = 0; i < count; i++) { SarifLog log = RandomSarifLogGenerator.GenerateSarifLogWithRuns(random, random.Next(10)); logs.Add(log); } logs.RebaseUri("SRCROOT", false, new Uri(RandomSarifLogGenerator.GeneratorBaseUri)); // All file URIs should be relative and the files dictionary should be rewritten. logs.All( log => log.Runs == null || log.Runs.All( run => run.Results == null || run.Results.All( result => result.Locations == null || result.Locations.All( location => !location.PhysicalLocation.ArtifactLocation.Uri.IsAbsoluteUri && !string.IsNullOrEmpty(location.PhysicalLocation.ArtifactLocation.UriBaseId))))) .Should().BeTrue(); }
public void RebaseUri_WorksAsExpected() { Random random = RandomSarifLogGenerator.GenerateRandomAndLog(this.output); List <SarifLog> logs = new List <SarifLog>(); int count = random.Next(10) + 1; for (int i = 0; i < count; i++) { SarifLog log = RandomSarifLogGenerator.GenerateSarifLogWithRuns(random, random.Next(10)); logs.Add(log); } logs.RebaseUri("%SRCROOT%", new Uri(RandomSarifLogGenerator.GeneratorBaseUri)); // All file URIs should be relative and the files dictionary should be rewritten. logs.All( log => log.Runs == null || log.Runs.All( run => run.Files == null || run.Files.Keys.All( key => run.Files[key].FileLocation.Uri.ToString() == key && !run.Files[key].FileLocation.Uri.IsAbsoluteUri && !string.IsNullOrEmpty(run.Files[key].FileLocation.UriBaseId)))) .Should().BeTrue(); }
public void SarifLogResultMatcher_PreservesPropertiesProperly() { // Verify that SarifLog matching keeps old or latest property values on matched Results // depending on how it was configured. // This test used to verify that old Artifact properties are also preserved, but this behavior // doesn't make sense to us. If we need Artifacts to have property values from old runs, we // will need to merge them carefully, keeping the latest URI, File Content, and Hashes, and only // taking the older version of the Property Bag properties. (The latest merging code just took the // old Artifact wholesale) Random random = RandomSarifLogGenerator.GenerateRandomAndLog(this.output); SarifLog baselineLog = RandomSarifLogGenerator.GenerateSarifLogWithRuns(random, 1); SarifLog currentLog = baselineLog.DeepClone(); string baselinePropertyValue = Guid.NewGuid().ToString(); string currentPropertyValue = Guid.NewGuid().ToString(); SetPropertyOnAllResultObjects(baselineLog, "Key", baselinePropertyValue); SetPropertyOnAllResultObjects(currentLog, "Key", currentPropertyValue); SarifLog matchedLog = s_preserveOldestPropertyBagMatcher.Match(baselineLog.DeepClone(), currentLog.DeepClone()); matchedLog.Runs[0].Results?.Where((r) => { return(r.GetProperty("Key") == baselinePropertyValue); }).Count().Should().Be(matchedLog.Runs[0].Results.Count); // Retain property bag values from most current run matchedLog = s_preserveMostRecentPropertyBagMatcher.Match(baselineLog.DeepClone(), currentLog.DeepClone()); matchedLog.Runs[0].Results?.Where((r) => { return(r.GetProperty("Key") == currentPropertyValue); }).Count().Should().Be(matchedLog.Runs[0].Results.Count); }
public void ResultMatchingBaseliner_BaselinesTwoSimpleSarifLogs() { Random random = RandomSarifLogGenerator.GenerateRandomAndLog(this.output); SarifLog baselineLog = RandomSarifLogGenerator.GenerateSarifLogWithRuns(random, 1); SarifLog currentLog = baselineLog.DeepClone(); baselineLog.Runs[0].Id = new RunAutomationDetails { InstanceGuid = Guid.NewGuid().ToString() }; currentLog.Runs[0].Id = new RunAutomationDetails { InstanceGuid = Guid.NewGuid().ToString() }; if (currentLog.Runs[0].Results.Any()) { currentLog.Runs[0].Results[0].Tags.Add("New Unused Tag"); } foreach (Result result in baselineLog.Runs[0].Results) { result.CorrelationGuid = Guid.NewGuid().ToString(); } SarifLog calculatedNextBaseline = baseliner.Match(new SarifLog[] { baselineLog }, new SarifLog[] { currentLog }).First(); calculatedNextBaseline.Runs.Should().HaveCount(1); if (currentLog.Runs[0].Results.Any()) { calculatedNextBaseline.Runs[0].Results.Should().HaveCount(currentLog.Runs[0].Results.Count + 1); calculatedNextBaseline.Runs[0].Results.Where(r => string.IsNullOrEmpty(r.CorrelationGuid)).Should().HaveCount(0); calculatedNextBaseline.Runs[0].Results.Where(r => r.BaselineState == BaselineState.Absent).Should().HaveCount(1); calculatedNextBaseline.Runs[0].Results.Where(r => r.BaselineState == BaselineState.Absent).First().TryGetProperty(SarifLogResultMatcher.ResultMatchingResultPropertyName, out Dictionary <string, string> AbsentResultProperties).Should().BeTrue(); AbsentResultProperties.Should().ContainKey("Run"); AbsentResultProperties["Run"].Should().BeEquivalentTo(baselineLog.Runs[0].Id.InstanceGuid); int existingCount = currentLog.Runs[0].Results.Count - 1; 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.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); } calculatedNextBaseline.Runs[0].Results.Where(r => r.BaselineState == BaselineState.New).Should().HaveCount(1); calculatedNextBaseline.Runs[0].Results.Where(r => r.BaselineState == BaselineState.New).First().TryGetProperty(SarifLogResultMatcher.ResultMatchingResultPropertyName, out Dictionary <string, string> NewResultProperties).Should().BeTrue(); NewResultProperties.Should().ContainKey("Run"); NewResultProperties["Run"].Should().BeEquivalentTo(currentLog.Runs[0].Id.InstanceGuid); } }
public void SarifLog_SplitPerRun() { var random = new Random(); SarifLog sarifLog = RandomSarifLogGenerator.GenerateSarifLogWithRuns(random, 1); sarifLog.Split(SplittingStrategy.PerRun).Should().HaveCount(1); sarifLog = RandomSarifLogGenerator.GenerateSarifLogWithRuns(random, 3); sarifLog.Split(SplittingStrategy.PerRun).Should().HaveCount(3); }
public void SarifLogResultMatcher_MatchMultipleCurrentLogsWithNoBaseline() { Random random = RandomSarifLogGenerator.GenerateRandomAndLog(this.output); SarifLog mostRecentLog = RandomSarifLogGenerator.GenerateSarifLogWithRuns(random, 1); SarifLog oldestLog = mostRecentLog.DeepClone(); string sharedPropertyName = nameof(sharedPropertyName); string currentSharedPropertyValue = Guid.NewGuid().ToString(); string uniqueToMostRecentPropertyName = nameof(uniqueToMostRecentPropertyName); string uniqueToMostRecentPropertyValue = Guid.NewGuid().ToString(); string uniqueToOldestPropertyName = nameof(uniqueToOldestPropertyName); string uniqueToOldestPropertyValue = Guid.NewGuid().ToString(); mostRecentLog.Runs[0].SetProperty(sharedPropertyName, currentSharedPropertyValue); oldestLog.Runs[0].SetProperty(sharedPropertyName, currentSharedPropertyValue); mostRecentLog.Runs[0].SetProperty(uniqueToMostRecentPropertyName, uniqueToMostRecentPropertyValue); oldestLog.Runs[0].SetProperty(uniqueToOldestPropertyName, uniqueToOldestPropertyValue); SarifLog calculatedNextBaseline = s_preserveOldestPropertyBagMatcher.Match( previousLogs: null, currentLogs: new SarifLog[] { oldestLog, mostRecentLog }).First(); calculatedNextBaseline.Runs[0].Properties.Should().NotBeNull(); calculatedNextBaseline.Runs[0].Properties.Count.Should().Be(2); string value = null; // The default property bag matching behavior is to retain the oldest property bag available. Since we have no // baseline in this test, we expect the preserved property bag to be associated with the first (i.e., oldest) // run in the currentLogs property calculatedNextBaseline.Runs[0].GetProperty(sharedPropertyName).Should().Be(currentSharedPropertyValue); calculatedNextBaseline.Runs[0].TryGetProperty(uniqueToOldestPropertyName, out value).Should().BeTrue(); calculatedNextBaseline.Runs[0].GetProperty(uniqueToOldestPropertyName).Should().Be(uniqueToOldestPropertyValue); calculatedNextBaseline.Runs[0].TryGetProperty(uniqueToMostRecentPropertyName, out value).Should().BeFalse(); calculatedNextBaseline = s_preserveMostRecentPropertyBagMatcher.Match( previousLogs: null, currentLogs: new SarifLog[] { oldestLog, mostRecentLog }).First(); calculatedNextBaseline.Runs[0].Properties.Should().NotBeNull(); calculatedNextBaseline.Runs[0].Properties.Count.Should().Be(2); calculatedNextBaseline.Runs[0].GetProperty(sharedPropertyName).Should().Be(currentSharedPropertyValue); calculatedNextBaseline.Runs[0].TryGetProperty(uniqueToMostRecentPropertyName, out value).Should().BeTrue(); calculatedNextBaseline.Runs[0].GetProperty(uniqueToMostRecentPropertyName).Should().Be(uniqueToMostRecentPropertyValue); calculatedNextBaseline.Runs[0].TryGetProperty(uniqueToOldestPropertyName, out value).Should().BeFalse(); }
public void MergeStage_SingleFile_ReturnedUnchanged(int runs) { Random random = RandomSarifLogGenerator.GenerateRandomAndLog(this.output); SarifLog log = RandomSarifLogGenerator.GenerateSarifLogWithRuns(random, runs); SarifLog processed = Merge.Fold(new List <SarifLog>() { log }); processed.ShouldBeEquivalentTo(log); }
public void SarifLogResultMatcher_BaselinesTwoSimpleSarifLogs() { Random random = RandomSarifLogGenerator.GenerateRandomAndLog(this.output); SarifLog baselineLog = RandomSarifLogGenerator.GenerateSarifLogWithRuns(random, 1); SarifLog currentLog = baselineLog.DeepClone(); baselineLog.Runs[0].InstanceGuid = Guid.NewGuid().ToString(); currentLog.Runs[0].InstanceGuid = Guid.NewGuid().ToString(); if (currentLog.Runs[0].Results.Any()) { currentLog.Runs[0].Results[0].Tags.Add("New Unused Tag"); } foreach (Result result in baselineLog.Runs[0].Results) { result.CorrelationGuid = Guid.NewGuid().ToString(); } SarifLog calculatedNextBaseline = baseliner.Match(new SarifLog[] { baselineLog }, new SarifLog[] { currentLog }).First(); calculatedNextBaseline.Runs.Should().HaveCount(1); if (currentLog.Runs[0].Results.Any()) { calculatedNextBaseline.Runs[0].Results.Should().HaveCount(currentLog.Runs[0].Results.Count + 1); calculatedNextBaseline.Runs[0].Results.Where(r => string.IsNullOrEmpty(r.CorrelationGuid)).Should().HaveCount(0); calculatedNextBaseline.Runs[0].Results.Where(r => r.BaselineState == BaselineState.Absent).Should().HaveCount(1); calculatedNextBaseline.Runs[0].Results.Where(r => r.BaselineState == BaselineState.Absent).First().TryGetProperty(SarifLogResultMatcher.ResultMatchingResultPropertyName, out Dictionary <string, string> AbsentResultProperties).Should().BeTrue(); AbsentResultProperties.Should().ContainKey("Run"); AbsentResultProperties["Run"].Should().BeEquivalentTo(baselineLog.Runs[0].InstanceGuid); 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.Existing).First().TryGetProperty(SarifLogResultMatcher.ResultMatchingResultPropertyName, out Dictionary <string, string> CurrentResultProperties).Should().BeTrue(); CurrentResultProperties.Should().ContainKey("Run"); CurrentResultProperties["Run"].Should().BeEquivalentTo(currentLog.Runs[0].InstanceGuid); } calculatedNextBaseline.Runs[0].Results.Where(r => r.BaselineState == BaselineState.New).Should().HaveCount(1); calculatedNextBaseline.Runs[0].Results.Where(r => r.BaselineState == BaselineState.New).First().TryGetProperty(SarifLogResultMatcher.ResultMatchingResultPropertyName, out Dictionary <string, string> NewResultProperties).Should().BeTrue(); NewResultProperties.Should().ContainKey("Run"); NewResultProperties["Run"].Should().BeEquivalentTo(currentLog.Runs[0].InstanceGuid); } }
public void TestMerge_WorksAsExpected() { Random random = RandomSarifLogGenerator.GenerateRandomAndLog(this.output); List <SarifLog> logs = new List <SarifLog>(); List <SarifLog> secondLogSet = new List <SarifLog>(); int count = random.Next(10) + 1; for (int i = 0; i < count; i++) { SarifLog log = RandomSarifLogGenerator.GenerateSarifLogWithRuns(random, random.Next(1, 10)); logs.Add(log); secondLogSet.Add(log.DeepClone()); } SarifLog combinedLog = logs.Merge(); combinedLog.Runs.Count.Should().Be(secondLogSet.Select(l => l.Runs == null ? 0 : l.Runs.Count).Sum()); }
public void SarifLogResultMatcher_BaselinesSarifLogsWithProperties() { Random random = RandomSarifLogGenerator.GenerateRandomAndLog(this.output); SarifLog baselineLog = RandomSarifLogGenerator.GenerateSarifLogWithRuns(random, 1); SarifLog currentLog = baselineLog.DeepClone(); string sharedPropertyName = nameof(sharedPropertyName); string currentSharedPropertyValue = Guid.NewGuid().ToString(); string uniqueToBaselinePropertyName = nameof(uniqueToBaselinePropertyName); string uniqueToBaselinePropertyValue = Guid.NewGuid().ToString(); string uniqueToCurrentPropertyName = nameof(uniqueToCurrentPropertyName); string uniqueToCurrentPropertyValue = Guid.NewGuid().ToString(); baselineLog.Runs[0].SetProperty(sharedPropertyName, currentSharedPropertyValue); currentLog.Runs[0].SetProperty(sharedPropertyName, currentSharedPropertyValue); baselineLog.Runs[0].SetProperty(uniqueToBaselinePropertyName, uniqueToBaselinePropertyValue); currentLog.Runs[0].SetProperty(uniqueToCurrentPropertyName, uniqueToCurrentPropertyValue); SarifLog calculatedNextBaseline = s_preserveOldestPropertyBagMatcher.Match(new SarifLog[] { baselineLog }, new SarifLog[] { currentLog }).First(); string value = null; // The default property bag matching behavior is to retain the property bag in its entirety from the baseline calculatedNextBaseline.Runs[0].Properties.Should().NotBeNull(); calculatedNextBaseline.Runs[0].Properties.Count.Should().Be(2); calculatedNextBaseline.Runs[0].GetProperty(sharedPropertyName).Should().Be(currentSharedPropertyValue); calculatedNextBaseline.Runs[0].GetProperty(uniqueToBaselinePropertyName).Should().Be(uniqueToBaselinePropertyValue); calculatedNextBaseline.Runs[0].TryGetProperty(uniqueToCurrentPropertyName, out value).Should().BeFalse(); calculatedNextBaseline = s_preserveMostRecentPropertyBagMatcher.Match(new SarifLog[] { baselineLog }, new SarifLog[] { currentLog }).First(); // The default property bag matching behavior is to retain the property bag in its entirety from the baseline calculatedNextBaseline.Runs[0].Properties.Should().NotBeNull(); calculatedNextBaseline.Runs[0].Properties.Count.Should().Be(2); calculatedNextBaseline.Runs[0].GetProperty(sharedPropertyName).Should().Be(currentSharedPropertyValue); calculatedNextBaseline.Runs[0].GetProperty(uniqueToCurrentPropertyName).Should().Be(uniqueToCurrentPropertyValue); calculatedNextBaseline.Runs[0].TryGetProperty(uniqueToBaselinePropertyName, out value).Should().BeFalse(); }
public void MergeStage_MultipleFiles_MergeCorrectly(int fileCount) { Random random = RandomSarifLogGenerator.GenerateRandomAndLog(this.output); List <SarifLog> logs = new List <SarifLog>(); for (int i = 0; i < fileCount; i++) { logs.Add(RandomSarifLogGenerator.GenerateSarifLogWithRuns(random, random.Next(5))); } SarifLog merged = Merge.Fold(logs); foreach (var log in logs) { if (log.Runs != null) { log.Runs.Should().BeSubsetOf(merged.Runs); } } }
public void SarifLogResultMatcher_PreservesPropertiesProperly() { Random random = RandomSarifLogGenerator.GenerateRandomAndLog(this.output); SarifLog baselineLog = RandomSarifLogGenerator.GenerateSarifLogWithRuns(random, 1); SarifLog currentLog = baselineLog.DeepClone(); string baselinePropertyValue = Guid.NewGuid().ToString(); string currentPropertyValue = Guid.NewGuid().ToString(); SetPropertyOnAllFileAndResultObjects(baselineLog, "Key", baselinePropertyValue); SetPropertyOnAllFileAndResultObjects(currentLog, "Key", currentPropertyValue); SarifLog matchedLog = s_preserveOldestPropertyBagMatcher.Match(baselineLog.DeepClone(), currentLog.DeepClone()); matchedLog.Runs[0].Results?.Where((r) => { return(r.GetProperty("Key") == baselinePropertyValue); }).Count().Should().Be(matchedLog.Runs[0].Results.Count); matchedLog.Runs[0].Artifacts?.Where((r) => { return(r.GetProperty("Key") == baselinePropertyValue); }).Count().Should().Be(matchedLog.Runs[0].Artifacts.Count); // Retain property bag values from most current run matchedLog = s_preserveMostRecentPropertyBagMatcher.Match(baselineLog.DeepClone(), currentLog.DeepClone()); matchedLog.Runs[0].Results?.Where((r) => { return(r.GetProperty("Key") == currentPropertyValue); }).Count().Should().Be(matchedLog.Runs[0].Results.Count); matchedLog.Runs[0].Artifacts?.Where((r) => { return(r.GetProperty("Key") == currentPropertyValue); }).Count().Should().Be(matchedLog.Runs[0].Artifacts.Count); }
public void SarifLogResultMatcher_BaselinesTwoSimpleSarifLogs() { Random random = RandomSarifLogGenerator.GenerateRandomAndLog(this.output); SarifLog baselineLog = RandomSarifLogGenerator.GenerateSarifLogWithRuns(random, 1); SarifLog currentLog = baselineLog.DeepClone(); baselineLog.Runs[0].AutomationDetails = new RunAutomationDetails { Guid = Guid.NewGuid().ToString() }; currentLog.Runs[0].AutomationDetails = new RunAutomationDetails { Guid = Guid.NewGuid().ToString() }; // This code exists to force a result to diverge from the previous run. By modifying this tag, // we ensure that at least one result will be regarded as new (which implies one result // will be regarded as going absent). if (currentLog.Runs[0].Results.Any()) { currentLog.Runs[0].Results[0].Tags.Add("New Unused Tag"); } string propertyName = "WeLikePi"; float propertyValue = 3.14159F; baselineLog.Runs[0].SetProperty(propertyName, propertyValue); foreach (Result result in baselineLog.Runs[0].Results) { result.CorrelationGuid = Guid.NewGuid().ToString(); } SarifLog calculatedNextBaseline = s_preserveOldestPropertyBagMatcher.Match(new SarifLog[] { baselineLog }, new SarifLog[] { currentLog }).First(); calculatedNextBaseline.Runs.Should().HaveCount(1); calculatedNextBaseline.Runs[0].Properties.Should().NotBeNull(); calculatedNextBaseline.Runs[0].GetProperty <float>(propertyName).Should().Be(propertyValue); if (currentLog.Runs[0].Results.Any()) { calculatedNextBaseline.Runs[0].Results.Should().HaveCount(currentLog.Runs[0].Results.Count + 1); calculatedNextBaseline.Runs[0].Results.Where(r => string.IsNullOrEmpty(r.CorrelationGuid)).Should().HaveCount(0); calculatedNextBaseline.Runs[0].Results.Where(r => r.BaselineState == BaselineState.Absent).Should().HaveCount(1); calculatedNextBaseline.Runs[0].Results.Where(r => r.BaselineState == BaselineState.Absent).First().TryGetProperty(SarifLogResultMatcher.ResultMatchingResultPropertyName, out Dictionary <string, string> AbsentResultProperties).Should().BeTrue(); AbsentResultProperties.Should().ContainKey("Run"); AbsentResultProperties["Run"].Should().BeEquivalentTo(baselineLog.Runs[0].AutomationDetails.Guid); if (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.Unchanged).First().TryGetProperty(SarifLogResultMatcher.ResultMatchingResultPropertyName, out Dictionary <string, string> CurrentResultProperties).Should().BeTrue(); CurrentResultProperties.Should().ContainKey("Run"); CurrentResultProperties["Run"].Should().BeEquivalentTo(currentLog.Runs[0].AutomationDetails.Guid); } calculatedNextBaseline.Runs[0].Results.Where(r => r.BaselineState == BaselineState.New).Should().HaveCount(1); calculatedNextBaseline.Runs[0].Results.Where(r => r.BaselineState == BaselineState.New).First().TryGetProperty(SarifLogResultMatcher.ResultMatchingResultPropertyName, out Dictionary <string, string> NewResultProperties).Should().BeTrue(); NewResultProperties.Should().ContainKey("Run"); NewResultProperties["Run"].Should().BeEquivalentTo(currentLog.Runs[0].AutomationDetails.Guid); } }