private void SetSolutionBinding(SonarLintMode mode)
 {
     this.configProvider.ModeToReturn    = mode;
     this.configProvider.ProjectToReturn = mode == SonarLintMode.Standalone ? null :  new BoundSonarQubeProject {
         ProjectKey = "ProjectKey"
     };
 }
        internal SolutionBindingOperation(IServiceProvider serviceProvider,
                                          ConnectionInformation connection,
                                          string projectKey,
                                          string projectName,
                                          SonarLintMode bindingMode,
                                          ILogger logger,
                                          IFileSystem fileSystem)
        {
            if (string.IsNullOrWhiteSpace(projectKey))
            {
                throw new ArgumentNullException(nameof(projectKey));
            }

            bindingMode.ThrowIfNotConnected();

            this.serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider));
            this.connection      = connection ?? throw new ArgumentNullException(nameof(connection));
            this.logger          = logger ?? throw new ArgumentNullException(nameof(logger));
            this.fileSystem      = fileSystem ?? throw new ArgumentNullException(nameof(fileSystem));

            this.projectKey  = projectKey;
            this.projectName = projectName;
            this.bindingMode = bindingMode;

            this.projectSystem = this.serviceProvider.GetService <IProjectSystemHelper>();
            this.projectSystem.AssertLocalServiceIsNotNull();

            this.sourceControlledFileSystem = this.serviceProvider.GetService <ISourceControlledFileSystem>();
            this.sourceControlledFileSystem.AssertLocalServiceIsNotNull();
        }
 private void SetValidSolutionBinding(SonarLintMode bindingMode)
 {
     this.configProvider.ModeToReturn    = bindingMode;
     this.configProvider.ProjectToReturn = new BoundSonarQubeProject {
         ProjectKey = "projectKey"
     };
 }
        private WriteSettings?GetWriteSettings(SonarLintMode bindingMode)
        {
            var writeSettings = new WriteSettings();

            switch (bindingMode)
            {
            case SonarLintMode.LegacyConnected:
            {
                writeSettings.ConfigPath            = legacyPathProvider.Get();
                writeSettings.OnSuccessfulFileWrite = legacyConfigFolderItemAdder.AddToFolder;
                break;
            }

            case SonarLintMode.Connected:
            {
                writeSettings.ConfigPath = connectedModePathProvider.Get();
                break;
            }

            case SonarLintMode.Standalone:
            {
                throw new InvalidOperationException(Strings.Bind_CannotSaveStandaloneConfiguration);
            }

            default:
            {
                Debug.Fail("Unrecognized write mode " + bindingMode);
                return(null);
            }
            }

            return(writeSettings);
        }
        private void SetValidCSharpSolutionRuleSet(RuleSet ruleSet, SonarLintMode bindingMode)
        {
            string expectedSolutionRuleSet = SetValidSolutionRulesFile("projectKey", Language.CSharp, bindingMode);

            ruleSet.FilePath = expectedSolutionRuleSet;
            this.ruleSetSerializer.RegisterRuleSet(ruleSet);
        }
 public static void ThrowIfNotConnected(this SonarLintMode bindingMode)
 {
     if (!bindingMode.IsInAConnectedMode())
     {
         throw new ArgumentOutOfRangeException(nameof(bindingMode));
     }
 }
Beispiel #7
0
        public BindingProcessImpl(IHost host,
                                  BindCommandArgs bindingArgs,
                                  ISolutionBindingOperation solutionBindingOperation,
                                  INuGetBindingOperation nugetBindingOperation,
                                  IUnboundProjectFinder unboundProjectFinder,
                                  IBindingConfigProvider bindingConfigProvider,
                                  SonarLintMode bindingMode,
                                  bool isFirstBinding = false)
        {
            this.host        = host ?? throw new ArgumentNullException(nameof(host));
            this.bindingArgs = bindingArgs ?? throw new ArgumentNullException(nameof(bindingArgs));
            this.solutionBindingOperation = solutionBindingOperation ?? throw new ArgumentNullException(nameof(solutionBindingOperation));
            this.NuGetBindingOperation    = nugetBindingOperation ?? throw new ArgumentNullException(nameof(nugetBindingOperation));
            this.unboundProjectFinder     = unboundProjectFinder ?? throw new ArgumentNullException(nameof(unboundProjectFinder));
            this.bindingConfigProvider    = bindingConfigProvider ?? throw new ArgumentNullException(nameof(bindingConfigProvider));
            this.bindingMode = bindingMode;

            Debug.Assert(bindingArgs.ProjectKey != null);
            Debug.Assert(bindingArgs.ProjectName != null);
            Debug.Assert(bindingArgs.Connection != null);

            this.projectSystem = this.host.GetService <IProjectSystemHelper>();
            this.projectSystem.AssertLocalServiceIsNotNull();

            this.InternalState = new BindingProcessState(isFirstBinding);
        }
Beispiel #8
0
        private static TaintIssuesSynchronizer CreateTestSubject(ITaintStore taintStore = null,
                                                                 ITaintIssueToIssueVisualizationConverter converter = null,
                                                                 ILogger logger                       = null,
                                                                 SonarLintMode mode                   = SonarLintMode.Connected,
                                                                 ISonarQubeService sonarService       = null,
                                                                 IVsMonitorSelection vsMonitor        = null,
                                                                 IToolWindowService toolWindowService = null)
        {
            taintStore ??= Mock.Of <ITaintStore>();
            converter ??= Mock.Of <ITaintIssueToIssueVisualizationConverter>();

            var serviceProvider = CreateServiceProvider(vsMonitor);

            var configurationProvider = new Mock <IConfigurationProvider>();

            configurationProvider
            .Setup(x => x.GetConfiguration())
            .Returns(new BindingConfiguration(new BoundSonarQubeProject {
                ProjectKey = SharedProjectKey
            }, mode, ""));

            sonarService ??= CreateSonarService().Object;
            toolWindowService ??= Mock.Of <IToolWindowService>();

            logger ??= Mock.Of <ILogger>();

            return(new TaintIssuesSynchronizer(taintStore, sonarService, converter, configurationProvider.Object,
                                               toolWindowService, serviceProvider, logger));
        }
Beispiel #9
0
        public SolutionBindingOperation(IServiceProvider serviceProvider, ConnectionInformation connection, string ProjectKey, SonarLintMode bindingMode)
        {
            if (serviceProvider == null)
            {
                throw new ArgumentNullException(nameof(serviceProvider));
            }

            if (connection == null)
            {
                throw new ArgumentNullException(nameof(connection));
            }

            if (string.IsNullOrWhiteSpace(ProjectKey))
            {
                throw new ArgumentNullException(nameof(ProjectKey));
            }

            bindingMode.ThrowIfNotConnected();

            this.serviceProvider = serviceProvider;
            this.connection      = connection;
            this.ProjectKey      = ProjectKey;

            this.projectSystem = this.serviceProvider.GetService <IProjectSystemHelper>();
            this.projectSystem.AssertLocalServiceIsNotNull();

            this.sourceControlledFileSystem = this.serviceProvider.GetService <ISourceControlledFileSystem>();
            this.sourceControlledFileSystem.AssertLocalServiceIsNotNull();

            this.bindingMode = bindingMode;
        }
        public void Ctor_SolutionBindingIsConnected_SonarQubeIssuesProviderCreated(SonarLintMode mode)
        {
            activeSolutionBoundTracker.CurrentConfiguration = new BindingConfiguration(new BoundSonarQubeProject(), mode, null);
            new SuppressedIssuesProvider(activeSolutionBoundTracker, createProviderFunc.Object);

            createProviderFunc.Verify(x => x(activeSolutionBoundTracker.CurrentConfiguration), Times.Once);
        }
        public BindingConfiguration Persist(BoundSonarQubeProject project, SonarLintMode bindingMode)
        {
            project.Should().NotBeNull();
            SavedProject = project;
            SavedMode    = bindingMode;

            return(GetConfiguration());
        }
Beispiel #12
0
 private SolutionBindingOperation CreateTestSubject(SonarLintMode bindingMode = SonarLintMode.LegacyConnected)
 {
     return(new SolutionBindingOperation(serviceProvider,
                                         bindingMode,
                                         projectBinderFactoryMock.Object,
                                         new LegacyConfigFolderItemAdder(serviceProvider, fileSystem),
                                         fileSystem));
 }
Beispiel #13
0
        private static IActiveSolutionBoundTracker CreateSolutionTracker(SonarLintMode bindingMode)
        {
            var bindingConfiguration = new BindingConfiguration(new BoundSonarQubeProject(), bindingMode, null);
            var tracker = new Mock <IActiveSolutionBoundTracker>();

            tracker.Setup(x => x.CurrentConfiguration).Returns(bindingConfiguration);
            return(tracker.Object);
        }
 public SolutionBindingOperation(IServiceProvider serviceProvider,
                                 ConnectionInformation connection,
                                 string projectKey,
                                 string projectName,
                                 SonarLintMode bindingMode,
                                 ILogger logger)
     : this(serviceProvider, connection, projectKey, projectName, bindingMode, logger, new FileSystem())
 {
 }
 private SolutionBindingOperation CreateTestSubject(string projectKey,
                                                    ConnectionInformation connection = null,
                                                    SonarLintMode bindingMode        = SonarLintMode.LegacyConnected)
 {
     return(new SolutionBindingOperation(this.serviceProvider,
                                         connection ?? new ConnectionInformation(new Uri("http://host")),
                                         projectKey,
                                         bindingMode));
 }
        private void SetValidSolutionRuleSet(RuleSet ruleSet, SonarLintMode bindingMode)
        {
            string expectedSolutionRuleSet = ((ISolutionRuleSetsInformationProvider)this.ruleSetInfoProvider)
                                             .CalculateSolutionSonarQubeRuleSetFilePath("projectKey", Language.CSharp, bindingMode);

            ruleSet.FilePath = expectedSolutionRuleSet;

            this.ruleSetSerializer.RegisterRuleSet(ruleSet);
        }
        private void ExecuteCommitSolutionBindingTest(SonarLintMode bindingMode)
        {
            // Arrange
            var configProvider = new ConfigurableConfigurationProvider();

            this.serviceProvider.RegisterService(typeof(IConfigurationProvider), configProvider);
            var csProject = this.solutionMock.AddOrGetProject("CS.csproj");

            csProject.SetCSProjectKind();
            var projects = new[] { csProject };

            var connectionInformation            = new ConnectionInformation(new Uri("http://xyz"));
            SolutionBindingOperation testSubject = this.CreateTestSubject("key", connectionInformation, bindingMode);

            var configFileMock    = CreateMockRuleSetConfigFile("cs");
            var languageToFileMap = new Dictionary <Language, IBindingConfigFile>()
            {
                { Language.CSharp, configFileMock.Object }
            };

            testSubject.RegisterKnownConfigFiles(languageToFileMap);
            var profiles = GetQualityProfiles();

            DateTime expectedTimeStamp = DateTime.Now;

            profiles[Language.CSharp] = new SonarQubeQualityProfile("expected profile Key", "", "", false, expectedTimeStamp);
            testSubject.Initialize(projects, profiles);
            testSubject.Binders.Clear(); // Ignore the real binders, not part of this test scope
            bool commitCalledForBinder = false;

            testSubject.Binders.Add(new ConfigurableBindingOperation {
                CommitAction = () => commitCalledForBinder = true
            });
            testSubject.Prepare(CancellationToken.None);

            // Sanity
            configProvider.SavedConfiguration.Should().BeNull();

            // Act
            var commitResult = testSubject.CommitSolutionBinding();

            // Assert
            commitResult.Should().BeTrue();
            commitCalledForBinder.Should().BeTrue();

            configProvider.SavedConfiguration.Should().NotBeNull();
            configProvider.SavedConfiguration.Mode.Should().Be(bindingMode);

            var savedProject = configProvider.SavedConfiguration.Project;

            savedProject.ServerUri.Should().Be(connectionInformation.ServerUri);
            savedProject.Profiles.Should().HaveCount(1);
            savedProject.Profiles[Language.CSharp].ProfileKey.Should().Be("expected profile Key");
            savedProject.Profiles[Language.CSharp].ProfileTimestamp.Should().Be(expectedTimeStamp);
        }
Beispiel #18
0
        private BindingConfiguration TryGetBindingConfiguration(string bindingPath, SonarLintMode sonarLintMode)
        {
            if (bindingPath == null)
            {
                return(null);
            }

            var boundProject = solutionBindingSerializer.Read(bindingPath);

            return(boundProject == null
                ? null
                : BindingConfiguration.CreateBoundConfiguration(boundProject, sonarLintMode));
        }
Beispiel #19
0
        public void GetUnboundProjects_SolutionBound_EmptyFilteredProjects(SonarLintMode mode)
        {
            // Arrange - no projects created
            var testConfig = new TestConfigurationBuilder(mode, "sqKey1");

            var testSubject = testConfig.CreateTestSubject();

            // Act
            var result = testSubject.GetUnboundProjects();

            // Assert
            AssertEmptyResult(result);
        }
Beispiel #20
0
        public void WhenDisposed_EventsAreIgnored(SonarLintMode bindingMode)
        {
            var builder = new TestEnvironmentBuilder(bindingMode);

            // Act
            builder.TestSubject.Dispose();

            // Raise events and check they are ignored
            builder.SimulateSuppressionsUpdated();
            builder.SimulateUserSettingsChanged();

            builder.AssertAnalysisIsNotRequested();
        }
        public BindingConfiguration Persist(BoundSonarQubeProject project, SonarLintMode bindingMode)
        {
            if (project == null)
            {
                throw new ArgumentNullException(nameof(project));
            }

            var writeSettings = GetWriteSettings(bindingMode);

            var success = writeSettings.HasValue &&
                          solutionBindingDataWriter.Write(writeSettings.Value.ConfigPath, project, writeSettings?.OnSuccessfulFileWrite);

            return(success ? CreateBindingConfiguration(writeSettings.Value.ConfigPath, project, bindingMode) : null);
        }
        string ISolutionRuleSetsInformationProvider.GetSolutionSonarQubeRulesFolder(SonarLintMode bindingMode)
        {
            switch (bindingMode)
            {
            case SonarLintMode.LegacyConnected:
                return(Path.Combine(this.SolutionRootFolder, DummyLegacyModeFolderName));

            case SonarLintMode.Connected:
                return(Path.Combine(this.SolutionRootFolder, DummyConnectedModeFolderName));

            default:
                throw new System.ArgumentOutOfRangeException($"Invalid bindingMode supplied: {bindingMode}");
            }
        }
        public void GetUnboundProjects_HasNoBoundProjects(SonarLintMode mode)
        {
            // Arrange
            var testSubject = CreateTestSubject();

            this.SetValidSolutionBinding(mode);
            this.SetValidFilteredProjects();

            // Act
            var projects = testSubject.GetUnboundProjects();

            // Assert
            CollectionAssert.AreEquivalent(this.projectSystemHelper.FilteredProjects.ToArray(), projects.ToArray(), "Unexpected unbound projects");
        }
Beispiel #24
0
        private static BindingConfiguration CreateConfiguration(SonarLintMode mode, string serverUri)
        {
            if (mode == SonarLintMode.Standalone)
            {
                if (serverUri != null)
                {
                    Assert.Fail("Test setup error: should pass a null serverUri for standalone mode");
                }
                return(BindingConfiguration.Standalone);
            }

            var project = new BoundSonarQubeProject(new Uri(serverUri), "dummy.project.key", "dummy.projectName");

            return(BindingConfiguration.CreateBoundConfiguration(project, mode, "c:\\test"));
        }
        public void GetUnboundProjects_ValidSolution_ProjectRuleSetIsMissing(SonarLintMode mode)
        {
            // Arrange
            var testSubject = CreateTestSubject();
            var expected    = this.SetValidFilteredProjects();

            this.SetValidSolutionBinding(mode);
            this.SetValidCSharpSolutionRuleSet(new RuleSet("SolutionRuleSet"), mode);

            // Act
            var projects = testSubject.GetUnboundProjects();

            // Assert
            AssertExpectedProjects(expected, projects);
        }
Beispiel #26
0
            public TestEnvironmentBuilder(SonarLintMode bindingMode)
            {
                analysisRequesterMock       = new Mock <IAnalysisRequester>();
                userSettingsProviderMock    = new Mock <IUserSettingsProvider>();
                suppressedIssuesMonitorMock = new Mock <ISuppressedIssuesMonitor>();

                var solutionBoundTracker = new ConfigurableActiveSolutionBoundTracker
                {
                    CurrentConfiguration = new BindingConfiguration(new BoundSonarQubeProject(), bindingMode, null)
                };

                Logger = new TestLogger();

                TestSubject = new AnalysisConfigMonitor(analysisRequesterMock.Object,
                                                        userSettingsProviderMock.Object, solutionBoundTracker, suppressedIssuesMonitorMock.Object, Logger);
            }
            public TestEnvironmentBuilder(SonarLintMode bindingMode)
            {
                this.bindingMode = bindingMode;

                analysisRequesterMock    = new Mock <IAnalysisRequester>();
                userSettingsProviderMock = new Mock <IUserSettingsProvider>();
                solutionBoundTracker     = new ConfigurableActiveSolutionBoundTracker()
                {
                    CurrentConfiguration = new BindingConfiguration(new Persistence.BoundSonarQubeProject(), bindingMode)
                };

                Logger = new TestLogger();

                TestSubject = new AnalysisConfigMonitor(analysisRequesterMock.Object,
                                                        userSettingsProviderMock.Object, solutionBoundTracker, Logger);
            }
Beispiel #28
0
            public TestEnvironmentBuilder(SonarLintMode mode)
            {
                standaloneSettingsProviderMock = new Mock <IUserSettingsProvider>();
                fileWrapperMock = new Mock <IFile>();
                Logger          = new TestLogger();

                sonarWayProviderMock       = new Mock <ICFamilyRulesConfigProvider>();
                activeSolutionBoundTracker = new ConfigurableActiveSolutionBoundTracker();
                rulesetInfoProviderMock    = new Mock <ISolutionRuleSetsInformationProvider>();

                // Register the local services
                host = new Mock <IHost>();
                host.Setup(x => x.GetService(typeof(ISolutionRuleSetsInformationProvider))).Returns(rulesetInfoProviderMock.Object);

                bindingMode = mode;
            }
        private BindingProcessImpl CreateTestSubject(BindCommandArgs bindingArgs = null,
                                                     INuGetBindingOperation nuGetBindingOperation = null,
                                                     IBindingConfigProvider configProvider        = null,
                                                     SonarLintMode mode = SonarLintMode.Connected)
        {
            bindingArgs           = bindingArgs ?? new BindCommandArgs("key", "name", new ConnectionInformation(new Uri("http://connected")));
            nuGetBindingOperation = nuGetBindingOperation ?? new NoOpNuGetBindingOperation(this.host.Logger);
            configProvider        = configProvider ?? new Mock <IBindingConfigProvider>().Object;

            this.host.SonarQubeService = this.sonarQubeServiceMock.Object;

            var slnBindOperation = new SolutionBindingOperation(this.host, SonarLintMode.LegacyConnected, this.host.Logger);
            var finder           = new ConfigurableUnboundProjectFinder();

            return(new BindingProcessImpl(this.host, bindingArgs, slnBindOperation, nuGetBindingOperation, finder, configProvider, mode));
        }
Beispiel #30
0
        public void WhenUserSettingsChange(SonarLintMode bindingMode, bool shouldAnalysisBeRequested)
        {
            var builder = new TestEnvironmentBuilder(bindingMode);

            builder.SimulateUserSettingsChanged();

            if (shouldAnalysisBeRequested)
            {
                builder.AssertAnalysisIsRequested();
                builder.Logger.AssertOutputStringExists(AnalysisStrings.ConfigMonitor_UserSettingsChanged);
            }
            else
            {
                builder.AssertAnalysisIsNotRequested();
                builder.Logger.AssertOutputStringExists(AnalysisStrings.ConfigMonitor_IgnoringUserSettingsChanged);
            }
        }