public async Task GetConfig_ReturnsCorrectRuleset()
        {
            var builder = new TestEnvironmentBuilder(validQualityProfile, Language.VBNET)
            {
                ActiveRulesResponse           = validRules,
                InactiveRulesResponse         = emptyRules,
                PropertiesResponse            = anyProperties,
                NuGetBindingOperationResponse = true,
                RuleSetGeneratorResponse      = validRuleSet,
            };
            var testSubject = builder.CreateTestSubject();

            var expectedRulesetFilePath = builder.BindingConfiguration.BuildPathUnderConfigDirectory(Language.VBNET.FileSuffixAndExtension);

            var response = await testSubject.GetConfigurationAsync(validQualityProfile, Language.VBNET, builder.BindingConfiguration, CancellationToken.None);

            response.Should().BeAssignableTo <ICSharpVBBindingConfig>();

            var actualRuleset = ((ICSharpVBBindingConfig)response).RuleSet;

            actualRuleset.Path.Should().Be(expectedRulesetFilePath);
            actualRuleset.Content.Description.Should().Be(validRuleSet.Description);
            actualRuleset.Content.Rules.Should().BeEquivalentTo(validRuleSet.Rules);
            actualRuleset.Content.ToolsVersion.Should().Be(validRuleSet.ToolsVersion);
        }
        public async Task GetConfig_NoSupportedActiveRules_ReturnsNull()
        {
            // Arrange
            var builder = new TestEnvironmentBuilder(validQualityProfile, Language.VBNET)
            {
                ActiveRulesResponse = new List <SonarQubeRule> {
                    ActiveRuleWithUnsupportedSeverity
                },
                InactiveRulesResponse = validRules,
                PropertiesResponse    = anyProperties
            };
            var testSubject = builder.CreateTestSubject();

            // Act
            var result = await testSubject.GetConfigurationAsync(validQualityProfile, Language.VBNET, builder.BindingConfiguration, CancellationToken.None)
                         .ConfigureAwait(false);

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

            builder.Logger.AssertOutputStrings(1);
            var expectedOutput = string.Format(Strings.SubTextPaddingFormat,
                                               string.Format(Strings.NoSonarAnalyzerActiveRulesForQualityProfile, validQualityProfile.Name, Language.VBNET.Name));

            builder.Logger.AssertOutputStrings(expectedOutput);

            builder.AssertRuleSetGeneratorNotCalled();
            builder.AssertNuGetGeneratorNotCalled();
            builder.AssertNuGetBindingWasNotCalled();
        }
        public void GetRules_UnsupportedLanguage_Throws()
        {
            var builder     = new TestEnvironmentBuilder(validQualityProfile, Language.Cpp);
            var testSubject = builder.CreateTestSubject();

            // Act
            Action act = () => testSubject.GetConfigurationAsync(validQualityProfile, Language.Cpp, BindingConfiguration.Standalone, CancellationToken.None).Wait();

            // Assert
            act.Should().ThrowExactly <ArgumentOutOfRangeException>().And.ParamName.Should().Be("language");
        }
        public void IsLanguageSupported()
        {
            // Arrange
            var builder     = new TestEnvironmentBuilder(validQualityProfile, Language.Cpp);
            var testSubject = builder.CreateTestSubject();

            // 1. Supported languages
            testSubject.IsLanguageSupported(Language.CSharp).Should().BeTrue();
            testSubject.IsLanguageSupported(Language.VBNET).Should().BeTrue();

            // 2. Not supported
            testSubject.IsLanguageSupported(Language.C).Should().BeFalse();
            testSubject.IsLanguageSupported(Language.Cpp).Should().BeFalse();

            testSubject.IsLanguageSupported(Language.Unknown).Should().BeFalse();
        }
示例#5
0
        public void Get_ConnectedMode_MissingSettings_StandaloneModeSettingsUsed(SonarLintMode mode)
        {
            // Arrange
            var builder = new TestEnvironmentBuilder(mode)
            {
                ConnectedSettingsFileExists = false,

                StandaloneModeSettings = new UserSettings
                {
                    Rules = new Dictionary <string, RuleConfig>
                    {
                        { "cpp:rule1", new RuleConfig {
                              Level = RuleLevel.On
                          } },
                        { "cpp:rule2", new RuleConfig {
                              Level = RuleLevel.Off
                          } },
                        { "cpp:rule4", new RuleConfig {
                              Level = RuleLevel.On
                          } },
                        { "XXX:rule3", new RuleConfig {
                              Level = RuleLevel.On
                          } }
                    }
                },

                SonarWayConfig = new DummyCFamilyRulesConfig("cpp")
                                 .AddRule("rule1", IssueSeverity.Info, isActive: false)
                                 .AddRule("rule2", IssueSeverity.Major, isActive: false)
                                 .AddRule("rule3", IssueSeverity.Minor, isActive: true)
                                 .AddRule("rule4", IssueSeverity.Blocker, isActive: false)
            };

            var testSubject = builder.CreateTestSubject();

            // Act
            var result = testSubject.GetRulesConfiguration("cpp");

            // Assert
            result.ActivePartialRuleKeys.Should().BeEquivalentTo("rule1", "rule3", "rule4");
            result.AllPartialRuleKeys.Should().BeEquivalentTo("rule1", "rule2", "rule3", "rule4");

            builder.Logger.AssertOutputStringExists(Resources.Strings.CFamily_UnableToLoadConnectedModeSettings);
        }
        public async Task GetConfig_NuGetBindingOperationFails_ReturnsNull()
        {
            var builder = new TestEnvironmentBuilder(validQualityProfile, Language.VBNET)
            {
                ActiveRulesResponse           = validRules,
                InactiveRulesResponse         = validRules,
                PropertiesResponse            = anyProperties,
                NuGetGeneratorResponse        = validNugetPackages,
                NuGetBindingOperationResponse = false
            };

            var testSubject = builder.CreateTestSubject();

            var result = await testSubject.GetConfigurationAsync(validQualityProfile, Language.VBNET, builder.BindingConfiguration, CancellationToken.None)
                         .ConfigureAwait(false);

            result.Should().BeNull();

            builder.AssertNuGetGeneratorWasCalled();
            builder.AssertNuGetBindingOpWasCalled();

            builder.AssertRuleSetGeneratorNotCalled();
        }
        public async Task GetConfig_ReturnsCorrectAdditionalFile()
        {
            var expectedConfiguration = new SonarLintConfiguration
            {
                Rules = new List <SonarLintRule>
                {
                    new SonarLintRule
                    {
                        Key        = "test",
                        Parameters = new List <SonarLintKeyValuePair> {
                            new SonarLintKeyValuePair {
                                Key = "ruleid", Value = "value"
                            }
                        }
                    }
                }
            };

            var builder = new TestEnvironmentBuilder(validQualityProfile, Language.VBNET)
            {
                ActiveRulesResponse            = validRules,
                InactiveRulesResponse          = emptyRules,
                PropertiesResponse             = anyProperties,
                NuGetBindingOperationResponse  = true,
                RuleSetGeneratorResponse       = validRuleSet,
                SonarLintConfigurationResponse = expectedConfiguration
            };
            var testSubject = builder.CreateTestSubject();

            var expectedAdditionalFilePath = builder.BindingConfiguration.BuildPathUnderConfigDirectory() + "\\VB\\SonarLint.xml";

            var response = await testSubject.GetConfigurationAsync(validQualityProfile, Language.VBNET, builder.BindingConfiguration, CancellationToken.None);

            (response as ICSharpVBBindingConfig).AdditionalFile.Path.Should().Be(expectedAdditionalFilePath);
            (response as ICSharpVBBindingConfig).AdditionalFile.Content.Should().Be(expectedConfiguration);
        }
示例#8
0
        public void Get_StandaloneMode()
        {
            // Arrange
            var builder = new TestEnvironmentBuilder(SonarLintMode.Standalone)
            {
                ConnectedModeSettings = new UserSettings
                {
                    Rules = new Dictionary <string, RuleConfig>
                    {
                        { "cpp:rule1", new RuleConfig {
                              Level = RuleLevel.Off
                          } },
                        { "cpp:rule2", new RuleConfig {
                              Level = RuleLevel.On
                          } },
                        { "cpp:rule3", new RuleConfig {
                              Level = RuleLevel.On
                          } },
                        { "XXX:rule3", new RuleConfig {
                              Level = RuleLevel.On
                          } }
                    }
                },

                StandaloneModeSettings = new UserSettings
                {
                    Rules = new Dictionary <string, RuleConfig>
                    {
                        { "cpp:rule1", new RuleConfig {
                              Level = RuleLevel.On
                          } },
                        { "cpp:rule2", new RuleConfig {
                              Level = RuleLevel.Off
                          } },
                        { "cpp:rule4", new RuleConfig {
                              Level = RuleLevel.On
                          } },
                        { "XXX:rule3", new RuleConfig {
                              Level = RuleLevel.On
                          } }
                    }
                },

                SonarWayConfig = new DummyCFamilyRulesConfig("cpp")
                                 .AddRule("rule1", IssueSeverity.Blocker, isActive: false)
                                 .AddRule("rule2", IssueSeverity.Critical, isActive: false)
                                 .AddRule("rule3", IssueSeverity.Major, isActive: true)
                                 .AddRule("rule4", IssueSeverity.Minor, isActive: false)
            };

            var testSubject = builder.CreateTestSubject();

            // Act
            var result = testSubject.GetRulesConfiguration("cpp");

            // Assert
            result.ActivePartialRuleKeys.Should().BeEquivalentTo("rule1", "rule3", "rule4");
            result.AllPartialRuleKeys.Should().BeEquivalentTo("rule1", "rule2", "rule3", "rule4");

            builder.AssertConnectedSettingsNotAccessed();
        }
示例#9
0
        public void Get_ConnectedMode(SonarLintMode mode)
        {
            // Arrange
            var builder = new TestEnvironmentBuilder(mode)
            {
                ConnectedModeSettings = new UserSettings
                {
                    Rules = new Dictionary <string, RuleConfig>
                    {
                        { "cpp:rule1", new RuleConfig {
                              Level = RuleLevel.Off, Severity = null
                          } },
                        { "cpp:rule2", new RuleConfig {
                              Level = RuleLevel.On, Severity = IssueSeverity.Blocker
                          } },
                        { "cpp:rule3", new RuleConfig {
                              Level = RuleLevel.On, Severity = IssueSeverity.Critical
                          } },
                        { "XXX:rule4", new RuleConfig {
                              Level = RuleLevel.On, Severity = IssueSeverity.Info
                          } }
                    }
                },

                StandaloneModeSettings = new UserSettings
                {
                    Rules = new Dictionary <string, RuleConfig>
                    {
                        { "cpp:rule1", new RuleConfig {
                              Level = RuleLevel.On
                          } },
                        { "cpp:rule2", new RuleConfig {
                              Level = RuleLevel.Off
                          } },
                        { "cpp:rule4", new RuleConfig {
                              Level = RuleLevel.On
                          } },
                        { "XXX:rule4", new RuleConfig {
                              Level = RuleLevel.On
                          } }
                    }
                },

                SonarWayConfig = new DummyCFamilyRulesConfig("cpp")
                                 .AddRule("rule1", IssueSeverity.Info, isActive: false)
                                 .AddRule("rule2", IssueSeverity.Major, isActive: false)
                                 .AddRule("rule3", IssueSeverity.Minor, isActive: true)
                                 .AddRule("rule4", IssueSeverity.Blocker, isActive: false)
            };

            var testSubject = builder.CreateTestSubject();

            // Act
            var result = testSubject.GetRulesConfiguration("cpp");

            // Assert
            result.ActivePartialRuleKeys.Should().BeEquivalentTo("rule2", "rule3");
            result.AllPartialRuleKeys.Should().BeEquivalentTo("rule1", "rule2", "rule3", "rule4");

            result.RulesMetadata["rule1"].DefaultSeverity.Should().Be(IssueSeverity.Info);     // not set in ConnectedModeSettings so should use default
            result.RulesMetadata["rule2"].DefaultSeverity.Should().Be(IssueSeverity.Blocker);  // ConnectedModeSetting should override the default
            result.RulesMetadata["rule3"].DefaultSeverity.Should().Be(IssueSeverity.Critical); // ConnectedModeSetting should override the default
            result.RulesMetadata["rule4"].DefaultSeverity.Should().Be(IssueSeverity.Blocker);  // ConnectedModeSetting should override the default

            builder.AssertStandaloneSettingsNotAccessed();
            builder.Logger.AssertOutputStringExists(Resources.Strings.CFamily_UsingConnectedModeSettings);
        }
        public async Task GetConfig_HasActiveInactiveAndUnsupportedRules_ReturnsValidBindingConfig()
        {
            // Arrange
            const string expectedProjectName = "my project";
            const string expectedServerUrl   = "http://myhost:123/";

            var properties = new SonarQubeProperty[]
            {
                new SonarQubeProperty("propertyAAA", "111"), new SonarQubeProperty("propertyBBB", "222")
            };

            var activeRules = new SonarQubeRule[]
            {
                CreateRule("activeRuleKey", "repoKey1", true),
                ActiveTaintAnalysisRule,
                ActiveRuleWithUnsupportedSeverity
            };

            var inactiveRules = new SonarQubeRule[]
            {
                CreateRule("inactiveRuleKey", "repoKey2", false),
                InactiveTaintAnalysisRule,
                InactiveRuleWithUnsupportedSeverity
            };

            var builder = new TestEnvironmentBuilder(validQualityProfile, Language.CSharp, expectedProjectName, expectedServerUrl)
            {
                ActiveRulesResponse           = activeRules,
                InactiveRulesResponse         = inactiveRules,
                PropertiesResponse            = properties,
                NuGetBindingOperationResponse = true,
                RuleSetGeneratorResponse      = validRuleSet
            };

            var testSubject = builder.CreateTestSubject();

            // Act
            var result = await testSubject.GetConfigurationAsync(validQualityProfile, Language.CSharp, builder.BindingConfiguration, CancellationToken.None)
                         .ConfigureAwait(false);

            // Assert
            result.Should().NotBeNull();
            result.Should().BeOfType <CSharpVBBindingConfig>();
            var dotNetResult = (CSharpVBBindingConfig)result;

            dotNetResult.RuleSet.Should().NotBeNull();
            dotNetResult.RuleSet.Content.ToolsVersion.Should().Be(validRuleSet.ToolsVersion);

            var expectedName = string.Format(Strings.SonarQubeRuleSetNameFormat, expectedProjectName, validQualityProfile.Name);

            dotNetResult.RuleSet.Content.Name.Should().Be(expectedName);

            var expectedDescription = $"{OriginalValidRuleSetDescription} {string.Format(Strings.SonarQubeQualityProfilePageUrlFormat, expectedServerUrl, validQualityProfile.Key)}";

            dotNetResult.RuleSet.Content.Description.Should().Be(expectedDescription);

            // Check properties passed to the ruleset generator
            builder.CapturedPropertiesPassedToRuleSetGenerator.Should().NotBeNull();
            var capturedProperties = builder.CapturedPropertiesPassedToRuleSetGenerator.ToList();

            capturedProperties.Count.Should().Be(2);
            capturedProperties[0].Key.Should().Be("propertyAAA");
            capturedProperties[0].Value.Should().Be("111");
            capturedProperties[1].Key.Should().Be("propertyBBB");
            capturedProperties[1].Value.Should().Be("222");

            // Check both active and inactive rules were passed to the ruleset generator.
            // The unsupported rules should have been removed.
            builder.CapturedRulesPassedToRuleSetGenerator.Should().NotBeNull();
            var capturedRules = builder.CapturedRulesPassedToRuleSetGenerator.ToList();

            capturedRules.Count.Should().Be(2);
            capturedRules[0].Key.Should().Be("activeRuleKey");
            capturedRules[0].RepositoryKey.Should().Be("repoKey1");
            capturedRules[1].Key.Should().Be("inactiveRuleKey");
            capturedRules[1].RepositoryKey.Should().Be("repoKey2");

            builder.AssertNuGetBindingOpWasCalled();
            builder.Logger.AssertOutputStrings(0); // not expecting anything in the case of success
        }