CreateTestRuleSetWithRuleIds() public static method

public static CreateTestRuleSetWithRuleIds ( IEnumerable ids, string analyzerId = "TestId", string ruleNamespace = "TestNamespace", RuleSet existingRuleSet = null ) : RuleSet
ids IEnumerable
analyzerId string
ruleNamespace string
existingRuleSet RuleSet
return RuleSet
コード例 #1
0
        public async Task BindingWorkflow_DownloadQualityProfile_WithNoRules_Fails()
        {
            // Arrange
            const string    QualityProfileName        = "SQQualityProfileName";
            const string    ProjectName               = "SQProjectName";
            BindingWorkflow testSubject               = this.CreateTestSubject("key", ProjectName);
            ConfigurableProgressController controller = new ConfigurableProgressController();
            var notifications = new ConfigurableProgressStepExecutionEvents();

            RuleSet ruleSet         = TestRuleSetHelper.CreateTestRuleSetWithRuleIds(Enumerable.Empty <string>());
            var     nugetPackages   = new[] { new PackageName("myPackageId", new SemanticVersion("1.0.0")) };
            var     additionalFiles = new[] { new AdditionalFileResponse {
                                                  FileName = "abc.xml", Content = new byte[] { 1, 2, 3 }
                                              } };
            RoslynExportProfileResponse export = RoslynExportProfileHelper.CreateExport(ruleSet, nugetPackages, additionalFiles);

            var language = Language.VBNET;
            SonarQubeQualityProfile profile = this.ConfigureProfileExport(export, language, QualityProfileName);

            // Act
            await testSubject.DownloadQualityProfileAsync(controller, notifications, new[] { language }, CancellationToken.None);

            // Assert
            testSubject.Rulesets.Should().NotContainKey(Language.VBNET, "Not expecting any rules for this language");
            testSubject.Rulesets.Should().NotContainKey(language, "Not expecting any rules");
            controller.NumberOfAbortRequests.Should().Be(1);

            notifications.AssertProgressMessages(Strings.DownloadingQualityProfileProgressMessage);

            this.outputWindowPane.AssertOutputStrings(1);
            var expectedOutput = string.Format(Strings.SubTextPaddingFormat,
                                               string.Format(Strings.NoSonarAnalyzerActiveRulesForQualityProfile, QualityProfileName, language.Name));

            this.outputWindowPane.AssertOutputStrings(expectedOutput);
        }
        public async Task GetConfig_LegacyMode_WithNoNugetPackage_Fails()
        {
            // Arrange
            const string QualityProfileName = "SQQualityProfileName";
            const string ProjectName        = "SQProjectName";
            var          legacyNuGetBinding = new NuGetBindingOperation(new ConfigurableHost(), this.logger);
            var          testSubject        = this.CreateTestSubject("key", ProjectName, legacyNuGetBinding);

            RuleSet ruleSet         = TestRuleSetHelper.CreateTestRuleSetWithRuleIds(new[] { "Key1", "Key2" });
            var     additionalFiles = new[] { new AdditionalFileResponse {
                                                  FileName = "abc.xml", Content = new byte[] { 1, 2, 3 }
                                              } };
            RoslynExportProfileResponse export = RoslynExportProfileHelper.CreateExport(ruleSet, Enumerable.Empty <PackageName>(), additionalFiles);

            var language = Language.VBNET;
            var profile  = this.ConfigureProfileExport(export, language, QualityProfileName);

            // Act
            var result = await testSubject.GetConfigurationAsync(profile, null, language, CancellationToken.None)
                         .ConfigureAwait(false);

            // Assert
            result.Should().BeNull();

            this.logger.AssertOutputStrings(1);
            var expectedOutput = string.Format(Strings.SubTextPaddingFormat,
                                               string.Format(Strings.NoNuGetPackageForQualityProfile, language.Name));

            this.logger.AssertOutputStrings(expectedOutput);
        }
        public async Task GetConfig_WithNoActiveRules_Fails()
        {
            // Arrange
            const string QualityProfileName = "SQQualityProfileName";
            const string ProjectName        = "SQProjectName";
            var          testSubject        = this.CreateTestSubject(ProjectName, "http://connected");

            RuleSet ruleSet = TestRuleSetHelper.CreateTestRuleSetWithRuleIds(new[] { "Key1", "Key2" });

            foreach (var rule in ruleSet.Rules)
            {
                rule.Action = RuleAction.None;
            }
            var nugetPackages   = new[] { new PackageName("myPackageId", new SemanticVersion("1.0.0")) };
            var additionalFiles = new[] { new AdditionalFileResponse {
                                              FileName = "abc.xml", Content = new byte[] { 1, 2, 3 }
                                          } };
            RoslynExportProfileResponse export = RoslynExportProfileHelper.CreateExport(ruleSet, nugetPackages, additionalFiles);

            var language = Language.VBNET;
            var profile  = this.ConfigureProfileExport(export, language, QualityProfileName);

            // Act
            var result = await testSubject.GetConfigurationAsync(profile, null, language, CancellationToken.None)
                         .ConfigureAwait(false);

            // Assert
            result.Should().BeNull();

            this.logger.AssertOutputStrings(1);
            var expectedOutput = string.Format(Strings.SubTextPaddingFormat,
                                               string.Format(Strings.NoSonarAnalyzerActiveRulesForQualityProfile, QualityProfileName, language.Name));

            this.logger.AssertOutputStrings(expectedOutput);
        }
        public static ProjectRuleSetConflict CreateConflict(string projectFilePath = "project.csproj", string baselineRuleSet = "baseline.ruleset", string projectRuleSet = "project.csproj", int numberOfConflictingRules = 1)
        {
            IEnumerable <string> ids = Enumerable.Range(0, numberOfConflictingRules).Select(i => "id" + i);
            var ruleSet = TestRuleSetHelper.CreateTestRuleSetWithRuleIds(ids);

            var conflict = new ProjectRuleSetConflict(
                new RuleConflictInfo(ruleSet.Rules, new Dictionary <RuleReference, RuleAction>()),
                new RuleSetInformation(projectFilePath, baselineRuleSet, projectRuleSet, null));

            return(conflict);
        }
コード例 #5
0
        public async Task BindingWorkflow_DownloadQualityProfile_Success()
        {
            // Arrange
            const string QualityProfileName = "SQQualityProfileName";
            const string ProjectName        = "SQProjectName";

            // Record all of the calls to NuGetBindingOperation.ProcessExport
            var actualProfiles = new List <Tuple <Language, RoslynExportProfileResponse> >();
            Mock <INuGetBindingOperation> nuGetOpMock = new Mock <INuGetBindingOperation>();

            nuGetOpMock.Setup(x => x.ProcessExport(It.IsAny <Language>(), It.IsAny <RoslynExportProfileResponse>()))
            .Callback <Language, RoslynExportProfileResponse>((l, r) => actualProfiles.Add(new Tuple <Language, RoslynExportProfileResponse>(l, r)))
            .Returns(true);

            BindingWorkflow testSubject = this.CreateTestSubject("key", ProjectName, nuGetOpMock.Object);


            ConfigurableProgressController controller = new ConfigurableProgressController();
            var notifications = new ConfigurableProgressStepExecutionEvents();

            RuleSet ruleSet         = TestRuleSetHelper.CreateTestRuleSetWithRuleIds(new[] { "Key1", "Key2" });
            var     expectedRuleSet = new RuleSet(ruleSet)
            {
                NonLocalizedDisplayName = string.Format(Strings.SonarQubeRuleSetNameFormat, ProjectName, QualityProfileName),
                NonLocalizedDescription = "\r\nhttp://connected/profiles/show?key="
            };
            var nugetPackages   = new[] { new PackageName("myPackageId", new SemanticVersion("1.0.0")) };
            var additionalFiles = new[] { new AdditionalFileResponse {
                                              FileName = "abc.xml", Content = new byte[] { 1, 2, 3 }
                                          } };
            RoslynExportProfileResponse export = RoslynExportProfileHelper.CreateExport(ruleSet, nugetPackages, additionalFiles);

            var language = Language.VBNET;
            SonarQubeQualityProfile profile = this.ConfigureProfileExport(export, language, QualityProfileName);

            // Act
            await testSubject.DownloadQualityProfileAsync(controller, notifications, new[] { language }, CancellationToken.None);

            // Assert
            RuleSetAssert.AreEqual(expectedRuleSet, testSubject.Rulesets[language], "Unexpected rule set");
            testSubject.QualityProfiles[language].Should().Be(profile);
            VerifyNuGetPackgesDownloaded(nugetPackages, language, actualProfiles);

            controller.NumberOfAbortRequests.Should().Be(0);
            notifications.AssertProgress(0.0, 1.0);
            notifications.AssertProgressMessages(Strings.DownloadingQualityProfileProgressMessage, string.Empty);

            this.outputWindowPane.AssertOutputStrings(1);
            var expectedOutput = string.Format(Strings.SubTextPaddingFormat,
                                               string.Format(Strings.QualityProfileDownloadSuccessfulMessageFormat, QualityProfileName, string.Empty, language.Name));

            this.outputWindowPane.AssertOutputStrings(expectedOutput);
        }
コード例 #6
0
        public void BindingWorkflow_DownloadQualityProfile_WithNoActiveRules_Fails()
        {
            // Arrange
            const string QualityProfileName   = "SQQualityProfileName";
            const string SonarQubeProjectName = "SQProjectName";
            var          projectInfo          = new ProjectInformation {
                Key = "key", Name = SonarQubeProjectName
            };
            BindingWorkflow testSubject = this.CreateTestSubject(projectInfo);
            ConfigurableProgressController controller = new ConfigurableProgressController();
            var notifications = new ConfigurableProgressStepExecutionEvents();

            RuleSet ruleSet = TestRuleSetHelper.CreateTestRuleSetWithRuleIds(new[] { "Key1", "Key2" });

            foreach (var rule in ruleSet.Rules)
            {
                rule.Action = RuleAction.None;
            }
            var expectedRuleSet = new RuleSet(ruleSet)
            {
                NonLocalizedDisplayName = string.Format(Strings.SonarQubeRuleSetNameFormat, SonarQubeProjectName, QualityProfileName),
                NonLocalizedDescription = "\r\nhttp://connected/profiles/show?key="
            };
            var nugetPackages   = new[] { new PackageName("myPackageId", new SemanticVersion("1.0.0")) };
            var additionalFiles = new[] { new AdditionalFile {
                                              FileName = "abc.xml", Content = new byte[] { 1, 2, 3 }
                                          } };
            RoslynExportProfile export = RoslynExportProfileHelper.CreateExport(ruleSet, nugetPackages, additionalFiles);

            var            language = Language.VBNET;
            QualityProfile profile  = this.ConfigureProfileExport(export, language);

            profile.Name = QualityProfileName;

            // Act
            testSubject.DownloadQualityProfile(controller, CancellationToken.None, notifications, new[] { language });

            // Assert
            testSubject.Rulesets.Should().NotContainKey(Language.VBNET, "Not expecting any rules for this language");
            testSubject.Rulesets.Should().NotContainKey(language, "Not expecting any rules");
            controller.NumberOfAbortRequests.Should().Be(1);

            notifications.AssertProgressMessages(Strings.DownloadingQualityProfileProgressMessage);

            this.outputWindowPane.AssertOutputStrings(1);
            var expectedOutput = string.Format(Strings.SubTextPaddingFormat,
                                               string.Format(Strings.NoSonarAnalyzerActiveRulesForQualityProfile, QualityProfileName, language.Name));

            this.outputWindowPane.AssertOutputStrings(expectedOutput);
        }
コード例 #7
0
        public void BindingWorkflow_DownloadQualityProfile_Success()
        {
            // Setup
            const string QualityProfileName   = "SQQualityProfileName";
            const string SonarQubeProjectName = "SQProjectName";
            var          projectInfo          = new ProjectInformation {
                Key = "key", Name = SonarQubeProjectName
            };
            BindingWorkflow testSubject = this.CreateTestSubject(projectInfo);
            ConfigurableProgressController controller = new ConfigurableProgressController();
            var notifications = new ConfigurableProgressStepExecutionEvents();

            RuleSet ruleSet         = TestRuleSetHelper.CreateTestRuleSetWithRuleIds(new[] { "Key1", "Key2" });
            var     expectedRuleSet = new RuleSet(ruleSet)
            {
                NonLocalizedDisplayName = string.Format(Strings.SonarQubeRuleSetNameFormat, SonarQubeProjectName, QualityProfileName),
                NonLocalizedDescription = "\r\nhttp://connected/profiles/show?key="
            };
            var nugetPackages   = new[] { new PackageName("myPackageId", new SemanticVersion("1.0.0")) };
            var additionalFiles = new[] { new AdditionalFile {
                                              FileName = "abc.xml", Content = new byte[] { 1, 2, 3 }
                                          } };
            RoslynExportProfile export = RoslynExportProfileHelper.CreateExport(ruleSet, nugetPackages, additionalFiles);

            var            language = Language.VBNET;
            QualityProfile profile  = this.ConfigureProfileExport(export, language);

            profile.Name = QualityProfileName;

            // Act
            testSubject.DownloadQualityProfile(controller, CancellationToken.None, notifications, new[] { language });

            // Verify
            RuleSetAssert.AreEqual(expectedRuleSet, testSubject.Rulesets[language], "Unexpected rule set");
            Assert.AreSame(profile, testSubject.QualityProfiles[language]);
            VerifyNuGetPackgesDownloaded(nugetPackages, testSubject, language);
            controller.AssertNumberOfAbortRequests(0);
            notifications.AssertProgress(
                0.0,
                1.0);
            notifications.AssertProgressMessages(Strings.DownloadingQualityProfileProgressMessage, string.Empty);

            this.outputWindowPane.AssertOutputStrings(1);
            var expectedOutput = string.Format(Strings.SubTextPaddingFormat,
                                               string.Format(Strings.QualityProfileDownloadSuccessfulMessageFormat, QualityProfileName, string.Empty, language.Name));

            this.outputWindowPane.AssertOutputStrings(expectedOutput);
        }
        public async Task GetConfig_Success()
        {
            // Arrange
            const string QualityProfileName = "SQQualityProfileName";
            const string ProjectName        = "SQProjectName";

            // Record all of the calls to NuGetBindingOperation.ProcessExport
            var actualProfiles = new List <Tuple <Language, RoslynExportProfileResponse> >();
            Mock <INuGetBindingOperation> nuGetOpMock = new Mock <INuGetBindingOperation>();

            nuGetOpMock.Setup(x => x.ProcessExport(It.IsAny <Language>(), It.IsAny <RoslynExportProfileResponse>()))
            .Callback <Language, RoslynExportProfileResponse>((l, r) => actualProfiles.Add(new Tuple <Language, RoslynExportProfileResponse>(l, r)))
            .Returns(true);

            var testSubject = this.CreateTestSubject(ProjectName, "http://connected/", nuGetOpMock.Object);

            RuleSet ruleSet         = TestRuleSetHelper.CreateTestRuleSetWithRuleIds(new[] { "Key1", "Key2" });
            var     expectedRuleSet = new RuleSet(ruleSet)
            {
                NonLocalizedDisplayName = string.Format(Strings.SonarQubeRuleSetNameFormat, ProjectName, QualityProfileName),
                NonLocalizedDescription = "\r\nhttp://connected/profiles/show?key="
            };
            var nugetPackages   = new[] { new PackageName("myPackageId", new SemanticVersion("1.0.0")) };
            var additionalFiles = new[] { new AdditionalFileResponse {
                                              FileName = "abc.xml", Content = new byte[] { 1, 2, 3 }
                                          } };
            RoslynExportProfileResponse export = RoslynExportProfileHelper.CreateExport(ruleSet, nugetPackages, additionalFiles);

            var language = Language.VBNET;
            SonarQubeQualityProfile profile = this.ConfigureProfileExport(export, language, QualityProfileName);

            // Act
            var result = await testSubject.GetConfigurationAsync(profile, "TODO", language, CancellationToken.None)
                         .ConfigureAwait(false);

            // Assert
            result.Should().NotBeNull();
            var dotNetResult = result as DotNetBindingConfigFile;

            dotNetResult.Should().NotBeNull();

            RuleSetAssert.AreEqual(expectedRuleSet, dotNetResult.RuleSet, "Unexpected rule set");
            VerifyNuGetPackgesDownloaded(nugetPackages, language, actualProfiles);

            this.logger.AssertOutputStrings(0); // not expecting anything in the case of success
        }
コード例 #9
0
        public async Task DownloadQualityProfile_Success()
        {
            // Arrange
            const string QualityProfileName = "SQQualityProfileName";
            const string ProjectName        = "SQProjectName";

            Mock <INuGetBindingOperation> nuGetOpMock = new Mock <INuGetBindingOperation>();

            var notifications   = new ConfigurableProgressStepExecutionEvents();
            var progressAdapter = new FixedStepsProgressAdapter(notifications);

            RuleSet expectedRuleSet = TestRuleSetHelper.CreateTestRuleSetWithRuleIds(new[] { "Key1", "Key2" });
            var     configFile      = new Mock <IBindingConfigFile>().Object;

            var language = Language.VBNET;
            SonarQubeQualityProfile profile = this.ConfigureQualityProfile(language, QualityProfileName);

            var configProviderMock = new Mock <IBindingConfigProvider>();

            configProviderMock.Setup(x => x.GetConfigurationAsync(profile, null, language, CancellationToken.None))
            .ReturnsAsync(configFile);

            var testSubject = this.CreateTestSubject("key", ProjectName, nuGetOpMock.Object, configProviderMock.Object);

            ConfigureSupportedBindingProject(testSubject.InternalState, language);

            // Act
            var result = await testSubject.DownloadQualityProfileAsync(progressAdapter, CancellationToken.None);

            // Assert
            result.Should().BeTrue();
            testSubject.InternalState.BindingConfigFiles.Should().ContainKey(language);
            testSubject.InternalState.BindingConfigFiles[language].Should().Be(configFile);
            testSubject.InternalState.BindingConfigFiles.Count().Should().Be(1);

            testSubject.InternalState.QualityProfiles[language].Should().Be(profile);

            notifications.AssertProgress(0.0, 1.0);
            notifications.AssertProgressMessages(Strings.DownloadingQualityProfileProgressMessage, string.Empty);

            this.outputWindowPane.AssertOutputStrings(1);
            var expectedOutput = string.Format(Strings.SubTextPaddingFormat,
                                               string.Format(Strings.QualityProfileDownloadSuccessfulMessageFormat, QualityProfileName, string.Empty, language.Name));

            this.outputWindowPane.AssertOutputStrings(expectedOutput);
        }
コード例 #10
0
        private void CreateProjectAndRuleSet(bool createRuleSet = true, string analyzerId = "SonarAnalyzer.Foo")
        {
            var projectPath = Path.GetTempFileName();

            var rulesetName = $"{Guid.NewGuid()}.ruleset";

            if (createRuleSet)
            {
                var ruleset = TestRuleSetHelper.CreateTestRuleSetWithRuleIds(new[] { "S110" }, analyzerId);
                ruleset.WriteToFile(Path.Combine(Path.GetDirectoryName(projectPath), rulesetName));
            }

            var project = new ProjectMock(projectPath);

            projectSystemHelper.Projects = new[] { project };
            project.SetBuildProperty(Constants.CodeAnalysisRuleSetPropertyKey, rulesetName);

            this.ruleSetProvider.RegisterProjectInfo(project, new RuleSetDeclaration(project, new PropertyMock("bar", null),
                                                                                     rulesetName, "", ""));
        }
コード例 #11
0
        public async Task BindingWorkflow_DownloadQualityProfile_LegacyMode_WithNoNugetPackage_Fails()
        {
            // Arrange
            const string    QualityProfileName        = "SQQualityProfileName";
            const string    ProjectName               = "SQProjectName";
            var             legacyNuGetBinding        = new NuGetBindingOperation(this.host, this.host.Logger);
            BindingWorkflow testSubject               = this.CreateTestSubject("key", ProjectName, legacyNuGetBinding);
            ConfigurableProgressController controller = new ConfigurableProgressController();
            var notifications = new ConfigurableProgressStepExecutionEvents();

            RuleSet ruleSet         = TestRuleSetHelper.CreateTestRuleSetWithRuleIds(new[] { "Key1", "Key2" });
            var     expectedRuleSet = new RuleSet(ruleSet)
            {
                NonLocalizedDisplayName = string.Format(Strings.SonarQubeRuleSetNameFormat, ProjectName, QualityProfileName),
                NonLocalizedDescription = "\r\nhttp://connected/profiles/show?key="
            };
            var additionalFiles = new[] { new AdditionalFileResponse {
                                              FileName = "abc.xml", Content = new byte[] { 1, 2, 3 }
                                          } };
            RoslynExportProfileResponse export = RoslynExportProfileHelper.CreateExport(ruleSet, Enumerable.Empty <PackageName>(), additionalFiles);

            var language = Language.VBNET;
            SonarQubeQualityProfile profile = this.ConfigureProfileExport(export, language, QualityProfileName);

            // Act
            await testSubject.DownloadQualityProfileAsync(controller, notifications, new[] { language }, CancellationToken.None);

            // Assert
            testSubject.Rulesets.Should().NotContainKey(Language.VBNET, "Not expecting any rules for this language");
            testSubject.Rulesets.Should().NotContainKey(language, "Not expecting any rules");
            controller.NumberOfAbortRequests.Should().Be(1);

            notifications.AssertProgressMessages(Strings.DownloadingQualityProfileProgressMessage);

            this.outputWindowPane.AssertOutputStrings(1);
            var expectedOutput = string.Format(Strings.SubTextPaddingFormat,
                                               string.Format(Strings.NoNuGetPackageForQualityProfile, language.Name));

            this.outputWindowPane.AssertOutputStrings(expectedOutput);
        }