private int WriteRunToErrorList(Run run, string logFilePath, Solution solution) { RunDataCache dataCache = new RunDataCache(run); CodeAnalysisResultManager.Instance.RunDataCaches.Add(++CodeAnalysisResultManager.Instance.CurrentRunId, dataCache); CodeAnalysisResultManager.Instance.CacheUriBasePaths(run); List <SarifErrorListItem> sarifErrors = new List <SarifErrorListItem>(); var projectNameCache = new ProjectNameCache(solution); StoreFileDetails(run.Artifacts); if (run.Results != null) { foreach (Result result in run.Results) { result.Run = run; var sarifError = new SarifErrorListItem(run, result, logFilePath, projectNameCache); sarifErrors.Add(sarifError); } } if (run.Invocations != null) { foreach (var invocation in run.Invocations) { if (invocation.ToolConfigurationNotifications != null) { foreach (Notification configurationNotification in invocation.ToolConfigurationNotifications) { var sarifError = new SarifErrorListItem(run, configurationNotification, logFilePath, projectNameCache); sarifErrors.Add(sarifError); } } if (invocation.ToolExecutionNotifications != null) { foreach (Notification toolNotification in invocation.ToolExecutionNotifications) { if (toolNotification.Level != FailureLevel.Note) { var sarifError = new SarifErrorListItem(run, toolNotification, logFilePath, projectNameCache); sarifErrors.Add(sarifError); } } } } } (dataCache.SarifErrors as List <SarifErrorListItem>).AddRange(sarifErrors); SarifTableDataSource.Instance.AddErrors(sarifErrors); return(sarifErrors.Count); }
private int WriteRunToErrorList(Run run, string logFilePath, Solution solution) { List <SarifErrorListItem> sarifErrors = new List <SarifErrorListItem>(); var projectNameCache = new ProjectNameCache(solution); StoreFileDetails(run.Files); if (run.Results != null) { foreach (Result result in run.Results) { var sarifError = new SarifErrorListItem(run, result, logFilePath, projectNameCache); sarifErrors.Add(sarifError); } } if (run.Invocations != null) { foreach (var invocation in run.Invocations) { if (invocation.ConfigurationNotifications != null) { foreach (Notification configurationNotification in invocation.ConfigurationNotifications) { var sarifError = new SarifErrorListItem(run, configurationNotification, logFilePath, projectNameCache); sarifErrors.Add(sarifError); } } if (invocation.ToolNotifications != null) { foreach (Notification toolNotification in invocation.ToolNotifications) { if (toolNotification.Level != NotificationLevel.Note) { var sarifError = new SarifErrorListItem(run, toolNotification, logFilePath, projectNameCache); sarifErrors.Add(sarifError); } } } } } foreach (var error in sarifErrors) { CodeAnalysisResultManager.Instance.SarifErrors.Add(error); } SarifTableDataSource.Instance.AddErrors(sarifErrors); return(sarifErrors.Count); }
public void ProjectNameCache_GetName_WhereSolutionDoesNotContainFile_ReturnsEmptyString() { // Arrange. // Create a solution which doesn't contain project items matching any file name. var mockSolution = new Mock <Solution>(); mockSolution .Setup(s => s.FindProjectItem(It.IsAny <string>())) .Returns(default(ProjectItem)); var target = new ProjectNameCache(mockSolution.Object); // Act. string result = target.GetName(FileName); // Assert. result.Should().BeEmpty(); }
public void ProjectNameCache_GetName_LooksUpAGivenFileNameInTheSolutionOnlyOnce() { // Arrange. // Create a project with the specified name. var mockProject = new Mock <Project>(); mockProject .SetupGet(p => p.Name) .Returns(ProjectName); // Create an item within that project. The item's Name property doesn't matter; // all that matters is its ContainingProject. var mockProjectItem = new Mock <ProjectItem>(); mockProjectItem .SetupGet(pi => pi.ContainingProject) .Returns(mockProject.Object); // Create a solution which returns that project item for the specified file name. // Again, the ProjectItem's Name property doesn't have to be set to match the name // by which we look it up. var mockSolution = new Mock <Solution>(MockBehavior.Loose); mockSolution .Setup(s => s.FindProjectItem(FileName)) .Returns(mockProjectItem.Object); var target = new ProjectNameCache(mockSolution.Object); // Act. string result1 = target.GetName(FileName); string result2 = target.GetName(FileName); // Assert. result1.Should().Be(ProjectName); result2.Should().Be(ProjectName); // We should have looked up the file name in the solution only once. mockSolution.Verify(s => s.FindProjectItem(FileName), Times.Once()); }
private void WriteRunToErrorList(Run run, string logFilePath, Solution solution) { List <SarifErrorListItem> sarifErrors = new List <SarifErrorListItem>(); var projectNameCache = new ProjectNameCache(solution); StoreFileDetails(run.Files); if (run.Results != null) { foreach (Result result in run.Results) { var sarifError = new SarifErrorListItem(run, result, logFilePath, projectNameCache); sarifErrors.Add(sarifError); } } foreach (var error in sarifErrors) { CodeAnalysisResultManager.Instance.SarifErrors.Add(error); } SarifTableDataSource.Instance.AddErrors(sarifErrors); }
private void WriteRunToErrorList(Run run, string logFilePath, Solution solution) { List <SarifErrorListItem> sarifErrors = new List <SarifErrorListItem>(); var projectNameCache = new ProjectNameCache(solution); if (run.Files != null) { foreach (var file in run.Files) { var hasSha256Hash = file.Value.Hashes?.Any(x => x.Algorithm == AlgorithmKind.Sha256); var contents = file.Value.Contents; var key = new Uri(file.Key); if ((hasSha256Hash ?? false) && contents != null) { var fileDetails = new FileDetailsModel(file.Value); CodeAnalysisResultManager.Instance.FileDetails.Add( key.IsAbsoluteUri ? key.LocalPath : key.OriginalString, fileDetails); } } } if (run.Results != null) { foreach (Result result in run.Results) { var sarifError = new SarifErrorListItem(run, result, logFilePath, projectNameCache); sarifErrors.Add(sarifError); } } foreach (var error in sarifErrors) { CodeAnalysisResultManager.Instance.SarifErrors.Add(error); } SarifTableDataSource.Instance.AddErrors(sarifErrors); }
public void ProjectNameCache_GetName_WhereSolutionContainsFile_ReturnsNameOfProjectContainingFile() { // Arrange. // Create a project with the specified name. var mockProject = new Mock <Project>(); mockProject .SetupGet(p => p.Name) .Returns(ProjectName); // Create an item within that project. The item's Name property doesn't matter; // all that matters is its ContainingProject. var mockProjectItem = new Mock <ProjectItem>(); mockProjectItem .SetupGet(pi => pi.ContainingProject) .Returns(mockProject.Object); // Create a solution which returns that project item for the specified file name. // Again, the ProjectItem's Name property doesn't have to be set to match the name // by which we look it up. var mockSolution = new Mock <Solution>(MockBehavior.Loose); mockSolution .Setup(s => s.FindProjectItem(FileName)) .Returns(mockProjectItem.Object); var target = new ProjectNameCache(mockSolution.Object); // Act. string result = target.GetName(FileName); // Assert. result.Should().Be(ProjectName); }
private int WriteRunToErrorList(Run run, string logFilePath, SarifLog sarifLog) { if (!SarifViewerPackage.IsUnitTesting) { #pragma warning disable VSTHRD108 // Assert thread affinity unconditionally ThreadHelper.ThrowIfNotOnUIThread(); #pragma warning restore VSTHRD108 } int runIndex = CodeAnalysisResultManager.Instance.GetNextRunIndex(); var dataCache = new RunDataCache(runIndex, logFilePath, sarifLog); CodeAnalysisResultManager.Instance.RunIndexToRunDataCache.Add(runIndex, dataCache); CodeAnalysisResultManager.Instance.CacheUriBasePaths(run); var sarifErrors = new List <SarifErrorListItem>(); var dte = Package.GetGlobalService(typeof(DTE)) as DTE2; var projectNameCache = new ProjectNameCache(dte?.Solution); this.StoreFileDetails(run.Artifacts); if (run.Results != null) { foreach (Result result in run.Results) { result.Run = run; var sarifError = new SarifErrorListItem(run, runIndex, result, logFilePath, projectNameCache); sarifErrors.Add(sarifError); } } if (run.Invocations != null) { foreach (Invocation invocation in run.Invocations) { if (invocation.ToolConfigurationNotifications != null) { foreach (Notification configurationNotification in invocation.ToolConfigurationNotifications) { var sarifError = new SarifErrorListItem(run, runIndex, configurationNotification, logFilePath, projectNameCache); sarifErrors.Add(sarifError); } } if (invocation.ToolExecutionNotifications != null) { foreach (Notification toolNotification in invocation.ToolExecutionNotifications) { if (toolNotification.Level != FailureLevel.Note) { var sarifError = new SarifErrorListItem(run, runIndex, toolNotification, logFilePath, projectNameCache); sarifErrors.Add(sarifError); } } } } } if (run.HasAbsentResults()) { this.ShowFilteredCategoryColumn(); } if (run.HasSuppressedResults()) { this.ShowFilteredSuppressionStateColumn(); } (dataCache.SarifErrors as List <SarifErrorListItem>).AddRange(sarifErrors); SarifTableDataSource.Instance.AddErrors(sarifErrors); // This causes already open "text views" to be tagged when SARIF logs are processed after a view is opened. SarifLocationTagHelpers.RefreshTags(); return(sarifErrors.Count); }