public void Get_NoUserOverrides_RulesWithNullEslintKeysAreNotReturned()
        {
            var ruleDefns = new RuleDefinitionsBuilder()
                            .AddRule("aaa")
                            .AddRule(null)
                            .AddRule("bbb");

            var testSubject = CreateTestSubject(ruleDefns, EmptyUserSettingsProvider);

            var result = testSubject.Calculate().ToArray();

            CheckExpectedRuleKeys(result, "aaa", "bbb");
            CheckConfigurationsAreEmpty(result);
        }
        public void Get_NoUserOverrides_RulesWithConfigurations_ExpectedConfigsReturned()
        {
            var config1 = new object();
            var config2 = "a default rule value";

            var ruleDefns = new RuleDefinitionsBuilder()
                            .AddRule("aaa", configurations: new object[] { config1, config2 });

            var testSubject = CreateTestSubject(ruleDefns, EmptyUserSettingsProvider);

            var result = testSubject.Calculate().ToArray();

            CheckExpectedRuleKeys(result, "aaa");
            result[0].Configurations.Should().ContainInOrder(config1, config2);
        }
        public void Get_NoUserOverrides_InactiveRulesAreNotReturned()
        {
            var ruleDefns = new RuleDefinitionsBuilder()
                            .AddRule("active 1", activeByDefault: true)
                            .AddRule("inactive AAA", activeByDefault: false)
                            .AddRule("active 2", activeByDefault: true)
                            .AddRule("inactive BBB", activeByDefault: false);

            var testSubject = CreateTestSubject(ruleDefns, EmptyUserSettingsProvider);

            var result = testSubject.Calculate().ToArray();

            CheckExpectedRuleKeys(result, "active 1", "active 2");
            CheckConfigurationsAreEmpty(result);
        }
        public void Get_NoUserOverrides_HotspotsAreNotReturned()
        {
            // NOTE: there are currently no taint vulnerabilities in the SonarJS jar
            var ruleDefns = new RuleDefinitionsBuilder()
                            .AddRule("bug", ruleType: RuleType.BUG)
                            .AddRule("codesmell", ruleType: RuleType.CODE_SMELL)
                            .AddRule("hotspot", ruleType: RuleType.SECURITY_HOTSPOT)
                            .AddRule("vuln", ruleType: RuleType.VULNERABILITY);

            var testSubject = CreateTestSubject(ruleDefns, EmptyUserSettingsProvider);

            var result = testSubject.Calculate().ToArray();

            CheckExpectedRuleKeys(result, "bug", "codesmell", "vuln");
            CheckConfigurationsAreEmpty(result);
        }
        public void Get_HasUserOverrides_IrregularRules_UserOverridesAreIgnored()
        {
            var ruleDefns = new RuleDefinitionsBuilder()
                            .AddRule(ruleKey: "javascript:hotspot1", activeByDefault: false, eslintKey: "hotspot1", ruleType: RuleType.SECURITY_HOTSPOT)
                            .AddRule(ruleKey: "javascript:S2260", activeByDefault: false, eslintKey: null);

            var userSettings = new UserSettingsBuilder()
                               .Add("javascript:hotspot1", RuleLevel.On)
                               .Add("javascript:S2260", RuleLevel.On);

            var testSubject = CreateTestSubject(ruleDefns, userSettings);

            var result = testSubject.Calculate().ToArray();

            result.Length.Should().Be(0);
        }
        public void Get_HasUserOverrides_NoRulesMatchTheUserOverrides_UserOverridesAreIgnored()
        {
            var ruleDefns = new RuleDefinitionsBuilder()
                            .AddRule(ruleKey: "javascript:activeRule1", activeByDefault: true, eslintKey: "active1");

            var userSettings = new UserSettingsBuilder()
                               .Add("wrongLanguage:activeRule1", RuleLevel.Off) // Correct rule key but wrong repo -> ignored
                               .Add("javascript:unknownRule", RuleLevel.On);    // Right repo, but rule key doesn't match a defined rule -> ignored

            var testSubject = CreateTestSubject(ruleDefns, userSettings);

            var result = testSubject.Calculate().ToArray();

            result.Length.Should().Be(1);
            result[0].Key.Should().Be("active1");
        }
        public void Get_HasUserOverrides_OverridesDefaultActivationLevel(
            bool onByDefault, bool onInUserSettings)
        {
            // The value in the user settings should always override the default
            var expectedRuleCount = onInUserSettings ? 1 : 0;

            // Create definition and rule settings with a single rule
            var ruleDefns = new RuleDefinitionsBuilder()
                            .AddRule(ruleKey: "javascript:rule1", activeByDefault: onByDefault, eslintKey: "esKey");

            var userSettingsLevel = onInUserSettings ? RuleLevel.On : RuleLevel.Off;
            var userSettings      = new UserSettingsBuilder()
                                    .Add("javascript:rule1", userSettingsLevel);

            var testSubject = CreateTestSubject(ruleDefns, userSettings);

            var result = testSubject.Calculate().ToArray();

            result.Length.Should().Be(expectedRuleCount);
        }
 private static ActiveRulesCalculator CreateTestSubject(RuleDefinitionsBuilder ruleDefinitionsBuilder, IUserSettingsProvider userSettingsProvider) =>
 new ActiveRulesCalculator(ruleDefinitionsBuilder.GetDefinitions(), userSettingsProvider);