private void RefreshWorkflow(BindingConfiguration configuration)
        {
            // There might be some race condition here if an analysis is triggered while the delegates are reset and set back
            // to the new workflow behavior. This race condition would lead to some issues being reported using the old mode
            // instead of the new one but everything will be fixed on the next analysis.
            ResetState();

            switch (configuration?.Mode)
            {
            case SonarLintMode.Standalone:
                this.logger.WriteLine(Resources.Strings.AnalyzerManager_InStandaloneMode);
                this.currentWorklow = new SonarAnalyzerStandaloneWorkflow(this.workspace);
                break;

            case SonarLintMode.LegacyConnected:
            case SonarLintMode.Connected:
                this.logger.WriteLine(Resources.Strings.AnalyzerManager_InConnectedMode);
                var sonarQubeIssueProvider = new SonarQubeIssuesProvider(sonarQubeService, configuration.Project.ProjectKey,
                                                                         new TimerFactory(), this.logger);
                this.disposableObjects.Add(sonarQubeIssueProvider);
                var liveIssueFactory   = new LiveIssueFactory(workspace, vsSolution);
                var suppressionHandler = new SuppressionHandler(liveIssueFactory, sonarQubeIssueProvider);

                if (configuration.Mode == SonarLintMode.Connected)
                {
                    this.currentWorklow = new SonarAnalyzerConnectedWorkflow(this.workspace, suppressionHandler);
                }
                else     // Legacy
                {
                    this.currentWorklow = new SonarAnalyzerLegacyConnectedWorkflow(this.workspace, suppressionHandler,
                                                                                   this.logger);
                }
                break;
            }
        }
示例#2
0
        public void Create_WhenSyntaxTreeIsNull_ReturnsNull()
        {
            // Arrange
            var  vsSolutionMock = new Mock <IVsSolution>();
            uint fileCount      = 2; // initialized with a value to actually assign a value to the out

            vsSolutionMock.Setup(x => x.GetProjectFilesInSolution(0, 0, null, out fileCount))
            .Returns(VSConstants.S_OK);
            var fileNames = new string[fileCount];

            vsSolutionMock.Setup(x => x.GetProjectFilesInSolution(0, fileCount, fileNames, out fileCount))
            .OutCallback((uint x, uint y, string[] paths, out uint z) =>
            {
                z        = 0;
                paths[0] = "Project1";
                paths[1] = "Project2";
            });
            vsSolutionMock.As <IVsSolution5>().Setup(x => x.GetGuidOfProjectFile(It.IsAny <string>()))
            .Returns(Guid.Empty);
            var liveIssueFactory = new LiveIssueFactory(new AdhocWorkspace(), vsSolutionMock.Object);

            var diagnostic = Diagnostic.Create(new DiagnosticDescriptor("id", "title", "message", "category",
                                                                        DiagnosticSeverity.Hidden, true), Location.None);

            // Act
            var result = liveIssueFactory.Create(null, diagnostic);

            // Assert
            result.Should().BeNull();
        }
示例#3
0
        private LiveIssue SetupAndCreate(Diagnostic diagnostic, string filePath)
        {
            // Arrange
            var vsSolutionMock = SetupSolutionMocks(
                new KeyValuePair <string, string>("C:\\Project1.csproj", "{31D0DAAC-8606-40FE-8DF0-01784706EA3E}"));

            var projectId = ProjectId.CreateNewId();
            var workspace = new AdhocWorkspace();
            var solution  = workspace.CurrentSolution
                            .AddProject(ProjectInfo.Create(projectId, VersionStamp.Default, "Project1", "Assembly1", LanguageNames.CSharp,
                                                           filePath))
                            .AddDocument(DocumentInfo.Create(DocumentId.CreateNewId(projectId), "MySource.cs",
                                                             loader: TextLoader.From(TextAndVersion.Create(SourceText.From(@"namespace {
    class Foo
    {
    }
}"), VersionStamp.Default))));

            workspace.TryApplyChanges(solution);

            var liveIssueFactory = new LiveIssueFactory(workspace, vsSolutionMock.Object);

            var syntaxTree = workspace.CurrentSolution.Projects.First().GetCompilationAsync().Result.SyntaxTrees.First();

            // Act
            using (new AssertIgnoreScope())
            {
                return(liveIssueFactory.Create(syntaxTree, diagnostic));
            }
        }
        private LiveIssue SetupAndCreate(Diagnostic diagnostic, string filePath)
        {
            // Arrange
            var  vsSolutionMock = new Mock <IVsSolution>();
            uint fileCount      = 1; // initialized with a value to actually assign a value to the out

            vsSolutionMock.Setup(x => x.GetProjectFilesInSolution(0, 0, null, out fileCount))
            .Returns(VSConstants.S_OK);
            var fileNames = new string[fileCount];

            vsSolutionMock.Setup(x => x.GetProjectFilesInSolution(0, fileCount, fileNames, out fileCount))
            .OutCallback((uint x, uint y, string[] paths, out uint z) =>
            {
                z        = 0;
                paths[0] = "Project1";
            });

            vsSolutionMock.As <IVsSolution5>().Setup(x => x.GetGuidOfProjectFile(It.IsAny <string>()))
            .Returns(Guid.Parse("{31D0DAAC-8606-40FE-8DF0-01784706EA3E}"));

            var projectId = ProjectId.CreateNewId();
            var workspace = new AdhocWorkspace();
            var solution  = workspace.CurrentSolution
                            .AddProject(ProjectInfo.Create(projectId, VersionStamp.Default, "Project1", "Assembly1", LanguageNames.CSharp,
                                                           filePath))
                            .AddDocument(DocumentInfo.Create(DocumentId.CreateNewId(projectId), "MySource.cs",
                                                             loader: TextLoader.From(TextAndVersion.Create(SourceText.From(@"namespace {
    class Foo
    {
    }
}"), VersionStamp.Default))));

            workspace.TryApplyChanges(solution);

            var liveIssueFactory = new LiveIssueFactory(workspace, vsSolutionMock.Object);

            var syntaxTree = workspace.CurrentSolution.Projects.First().GetCompilationAsync().Result.SyntaxTrees.First();

            // Act
            using (new AssertIgnoreScope())
            {
                return(liveIssueFactory.Create(syntaxTree, diagnostic));
            }
        }
示例#5
0
        public void BuildMap_DuplicateSolutionFolderNamesAreIgnored()
        {
            // See #413: https://github.com/SonarSource/sonarlint-visualstudio/issues/413

            // Arrange
            var vsSolutionMock = SetupSolutionMocks(
                new KeyValuePair <string, string>("SolutionFolder1", "11111111-1111-1111-1111-111111111111"),
                new KeyValuePair <string, string>("Item1", "22222222-2222-2222-2222-222222222222"),
                new KeyValuePair <string, string>("SolutionFolder2", "33333333-3333-3333-3333-333333333333"),
                new KeyValuePair <string, string>("Item1", "44444444-4444-4444-4444-444444444444"),
                new KeyValuePair <string, string>("realProject.csproj", "55555555-5555-5555-5555-555555555555"),
                new KeyValuePair <string, string>("item1", "66666666-6666-6666-6666-666666666666"),
                new KeyValuePair <string, string>("ITEM1", "77777777-7777-7777-7777-777777777777")
                );

            uint fileCountOut;

            // Act
            IDictionary <string, string> map = LiveIssueFactory.BuildProjectPathToIdMap(vsSolutionMock.Object);

            // Assert
            vsSolutionMock.Verify(x => x.GetProjectFilesInSolution(0, It.IsAny <uint>(), It.IsAny <string[]>(), out fileCountOut),
                                  Times.Exactly(2));
            vsSolutionMock.As <IVsSolution5>().Verify(x => x.GetGuidOfProjectFile(It.IsAny <string>()), Times.Exactly(7));

            // Duplicate entries should have been ignored
            map.ContainsKey("SolutionFolder1").Should().BeTrue();
            map.ContainsKey("Item1").Should().BeTrue();
            map.ContainsKey("SolutionFolder2").Should().BeTrue();
            map.ContainsKey("realProject.csproj").Should().BeTrue();
            map.Count.Should().Be(4);

            map["SolutionFolder1"].Should().Be("11111111-1111-1111-1111-111111111111");
            map["SolutionFolder2"].Should().Be("33333333-3333-3333-3333-333333333333");
            map["realProject.csproj"].Should().Be("55555555-5555-5555-5555-555555555555");
            map["item1"].Should().Be("77777777-7777-7777-7777-777777777777");
        }