Beispiel #1
0
 public static RulesSettings AddRule(this RulesSettings settings, string ruleKey, RuleLevel level)
 {
     settings.Rules.Add(ruleKey, new RuleConfig {
         Level = level
     });
     return(settings);
 }
        public void GetConfig_RulesInCustomSettings_MergedConfigReturned()
        {
            // Arrange
            var defaultRulesConfig = CreateWellKnownRulesConfig("key");
            var sourcesSettings    = new RulesSettings
            {
                Rules = new Dictionary <string, RuleConfig>
                {
                    // Turn an active rule off...
                    { "key:" + WellKnownPartialRuleKey1_Active, new RuleConfig {
                          Level = RuleLevel.Off
                      } },
                    // ... and an inactive rule on
                    { "key:" + WellKnownPartialRuleKey3_Inactive, new RuleConfig {
                          Level = RuleLevel.On
                      } }
                }
            };

            var result = testSubject.GetEffectiveRulesConfig("key", defaultRulesConfig, sourcesSettings);

            result.LanguageKey.Should().Be("key");
            result.AllPartialRuleKeys.Should().BeEquivalentTo(defaultRulesConfig.AllPartialRuleKeys);
            result.ActivePartialRuleKeys.Should().BeEquivalentTo(WellKnownPartialRuleKey2_Active, WellKnownPartialRuleKey3_Inactive);
        }
Beispiel #3
0
        public GetLookupsourceItemsRuleContext(GetLookupSourceItemsArgs args)
        {
            Settings = new RulesSettings();

            Args = args;
            Item = args.Item;
        }
        public void Ctor_ValidArgs()
        {
            var settings    = new RulesSettings();
            var testSubject = new CFamilyBindingConfigFile(settings);

            testSubject.RuleSettings.Equals(settings);
        }
        public void GetConfig_CachedResultsReturnedIfAvailable()
        {
            // Arrange
            var defaultRulesConfig = CreateWellKnownRulesConfig("key");
            var sourcesSettings    = new RulesSettings
            {
                Rules = new Dictionary <string, RuleConfig>
                {
                    { "rule1", new RuleConfig() }
                }
            };

            // 1. First call -> new config returned
            var result1 = testSubject.GetEffectiveRulesConfig("language1", defaultRulesConfig, sourcesSettings);

            result1.Should().NotBeNull();
            result1.Should().NotBeSameAs(defaultRulesConfig);
            testLogger.AssertOutputStringExists(CoreStrings.EffectiveRules_CacheMiss);

            // 2. Second call with same settings -> cache hit
            testLogger.Reset();
            var result2 = testSubject.GetEffectiveRulesConfig("language1", defaultRulesConfig, sourcesSettings);

            result2.Should().BeSameAs(result1);
            testLogger.AssertOutputStringExists(CoreStrings.EffectiveRules_CacheHit);

            // 3. Call with different key -> cache miss
            testLogger.Reset();
            var result3 = testSubject.GetEffectiveRulesConfig("another language", defaultRulesConfig, sourcesSettings);

            result3.Should().NotBeSameAs(result2);
            testLogger.AssertOutputStringExists(CoreStrings.EffectiveRules_CacheMiss);
        }
        public ReplaceLookupSourceQueryTokens()
        {
            var rulesSettings = new RulesSettings();

            _tokenPrefix = rulesSettings.QueryTokenPrefix;
            _tokenSuffix = rulesSettings.QueryTokenSuffix;
        }
        public void NullOrEmptyRulesSettings_DefaultsUsed()
        {
            // Arrange
            var defaultConfig = new DummyCFamilyRulesConfig("123")
                                .AddRule("rule1", IssueSeverity.Blocker, isActive: true,
                                         parameters: new Dictionary <string, string> {
                { "p1", "v1" }
            })
                                .AddRule("rule2", IssueSeverity.Major, isActive: true,
                                         parameters: new Dictionary <string, string> {
                { "p2", "v2" }
            })
                                .AddRule("rule3", IssueSeverity.Minor, isActive: false,
                                         parameters: new Dictionary <string, string> {
                { "p3", "v3" }
            });

            var settings = new RulesSettings();

            // Act
            var testSubject = new DynamicCFamilyRulesConfig(defaultConfig, settings, new TestLogger(), NoExcludedRules);

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

            testSubject.LanguageKey.Should().Be("123");

            // Other properties should be pass-throughs
            testSubject.AllPartialRuleKeys.Should().BeEquivalentTo(defaultConfig.AllPartialRuleKeys);
            testSubject.RulesParameters.Should().BeEquivalentTo(defaultConfig.RulesParameters);
            testSubject.RulesMetadata.Should().BeEquivalentTo(defaultConfig.RulesMetadata);
        }
Beispiel #8
0
        public void DisableExcludedRules_CustomRulesAndExcludedRulesExist_CustomRulesAreUnchangedExcludedRulesAreDisabled()
        {
            // Arrange
            var testLogger = new TestLogger();

            var excluded = new string[] { "excluded1", "excluded2", "excluded3", "excluded4" };
            var custom   = new RulesSettings()
                           .AddRule("xxx", RuleLevel.On)
                           .AddRule("excluded1", RuleLevel.On)
                           .AddRule("yyy", RuleLevel.Off)
                           .AddRule("excluded2", RuleLevel.Off)
                           .AddRule("excluded4", RuleLevel.On);

            // Act
            var result = DynamicCFamilyRulesConfig.DisableExcludedRules(custom, excluded, testLogger);

            // Assert
            result.Should().NotBeSameAs(custom);
            custom.Rules.Count.Should().BeLessThan(result.Rules.Count);

            result.Rules.Keys.Should().BeEquivalentTo("xxx", "excluded1", "yyy", "excluded2", "excluded3", "excluded4");
            result.Rules["xxx"].Level.Should().Be(RuleLevel.On);
            result.Rules["excluded1"].Level.Should().Be(RuleLevel.Off); // changed from On to Off
            result.Rules["yyy"].Level.Should().Be(RuleLevel.Off);
            result.Rules["excluded2"].Level.Should().Be(RuleLevel.Off); // unchanged - still Off
            result.Rules["excluded4"].Level.Should().Be(RuleLevel.Off); // changed from On to Off

            testLogger.AssertPartialOutputStringExists("excluded1", "excluded2", "excluded3", "excluded4");
        }
Beispiel #9
0
        public CFamilyBindingConfig(RulesSettings rulesSettings, string filePath, IFileSystem fileSystem)
        {
            RuleSettings    = rulesSettings ?? throw new ArgumentNullException(nameof(rulesSettings));
            this.fileSystem = fileSystem ?? throw new ArgumentNullException(nameof(fileSystem));

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

            FilePath = filePath;
        }
Beispiel #10
0
 public AnalyticModelSettingsElectricSystem(IList <AnalyticRuleSettings> settings)
 {
     foreach (AnalyticRuleType ruleType in GetRequiredRuleTypes())
     {
         RegisterRequiredType(ruleType);
     }
     foreach (AnalyticRuleSettings ruleSettings in settings)
     {
         RulesSettings.Add(ruleSettings);
     }
     CheckAndThrow();
 }
Beispiel #11
0
 public AnalyticModelSettingsPetrolEngineIgnition(
     IList <AnalyticRuleSettings> settings)
 {
     foreach (AnalyticRuleType ruleType in GetRequiredRuleTypes())
     {
         RegisterRequiredType(ruleType);
     }
     foreach (AnalyticRuleSettings ruleSettings in settings)
     {
         RulesSettings.Add(ruleSettings);
     }
     CheckAndThrow();
 }
        public void GetConfig_NoCustomSettings_DefaultsReturned()
        {
            // Arrange
            var defaultRulesConfig = CreateWellKnownRulesConfig("language1");
            var sourcesSettings    = new RulesSettings();

            // Act
            var result = testSubject.GetEffectiveRulesConfig("language1", defaultRulesConfig, sourcesSettings);

            // Assert
            result.LanguageKey.Should().Be("language1");
            result.AllPartialRuleKeys.Should().BeEquivalentTo(defaultRulesConfig.AllPartialRuleKeys);

            testLogger.AssertOutputStringExists(CoreStrings.CFamily_NoCustomRulesSettings);
        }
Beispiel #13
0
        public void Cache_DifferentSourceSettings_NotFound_AndEntryCleared()
        {
            var sourceConfig1    = new Mock <ICFamilyRulesConfig>().Object;
            var sourceSettings1  = new RulesSettings();
            var effectiveConfig1 = new Mock <ICFamilyRulesConfig>().Object;

            testSubject.Add("key1", sourceConfig1, sourceSettings1, effectiveConfig1);
            testSubject.CacheCount.Should().Be(1);

            // 1. Search for added item -> found
            testSubject.FindConfig("key1", sourceConfig1, sourceSettings1).Should().BeSameAs(effectiveConfig1);
            testSubject.CacheCount.Should().Be(1);

            // 2. Different source settings -> not found
            testSubject.FindConfig("key1", sourceConfig1, new RulesSettings()).Should().BeNull();
            testSubject.CacheCount.Should().Be(0);
        }
Beispiel #14
0
        public void Ctor_NullArguments_Throws()
        {
            var settings = new RulesSettings();

            // 1. Default rules config
            Action act = () => new DynamicCFamilyRulesConfig(null, settings, new TestLogger());

            act.Should().ThrowExactly <ArgumentNullException>().And.ParamName.Should().Be("defaultRulesConfig");

            // 2. Custom settings
            act = () => new DynamicCFamilyRulesConfig(new DummyCFamilyRulesConfig("anyLanguage"), null, new TestLogger());
            act.Should().ThrowExactly <ArgumentNullException>().And.ParamName.Should().Be("customRulesSettings");

            // 3. Logger
            act = () => new DynamicCFamilyRulesConfig(new DummyCFamilyRulesConfig("anyLanguage"), settings, null);
            act.Should().ThrowExactly <ArgumentNullException>().And.ParamName.Should().Be("logger");
        }
Beispiel #15
0
        public void ActiveRules_CustomSettingsOverrideDefaults()
        {
            // Arrange
            var defaultConfig = new DummyCFamilyRulesConfig("c")
                                .AddRule("rule1", isActive: false)
                                .AddRule("rule2", isActive: true)
                                .AddRule("rule3", isActive: true);

            var settings = new RulesSettings
            {
                Rules = new Dictionary <string, RuleConfig>
                {
                    // Unknown rules should be ignored
                    { "x:unknown1", new RuleConfig {
                          Level = RuleLevel.On
                      } },

                    // Turn on a rule that was off (case-insensitive comparison on keys)
                    { "c:rule1", new RuleConfig {
                          Level = RuleLevel.On
                      } },

                    // Turn off a rule that was on
                    { "c:rule2", new RuleConfig {
                          Level = RuleLevel.Off
                      } },

                    // Rule key comparison is case-sensitive
                    { "c:RULE3", new RuleConfig {
                          Level = RuleLevel.Off
                      } },

                    // Settings for other languages should be ignored
                    { "cpp:rule3", new RuleConfig {
                          Level = RuleLevel.Off
                      } }
                }
            };

            // Act
            var testSubject = new DynamicCFamilyRulesConfig(defaultConfig, settings, new TestLogger(), NoExcludedRules);

            // Assert
            testSubject.ActivePartialRuleKeys.Should().BeEquivalentTo("rule1", "rule3");
        }
        public void GetConfig_NullArguments_Throws()
        {
            var defaultRulesConfig = CreateWellKnownRulesConfig("language1");
            var customSettings     = new RulesSettings();

            // 1. Language
            Action act = () => testSubject.GetEffectiveRulesConfig(null, defaultRulesConfig, customSettings);

            act.Should().ThrowExactly <ArgumentNullException>().And.ParamName.Should().Be("languageKey");

            // 2. Default rules config
            act = () => testSubject.GetEffectiveRulesConfig("x", null, customSettings);
            act.Should().ThrowExactly <ArgumentNullException>().And.ParamName.Should().Be("defaultRulesConfig");

            // 3. Custom settings
            act = () => testSubject.GetEffectiveRulesConfig("x", defaultRulesConfig, null);
            act.Should().ThrowExactly <ArgumentNullException>().And.ParamName.Should().Be("customSettings");
        }
        public void RealFile_DisablePreviouslyEnabledRule()
        {
            var dir          = CreateTestSpecificDirectory();
            var settingsFile = Path.Combine(dir, "settings.txt");

            var initialSettings = new RulesSettings
            {
                Rules = new System.Collections.Generic.Dictionary <string, RuleConfig>
                {
                    { "javascript:S111", new RuleConfig {
                          Level = RuleLevel.On
                      } },
                    { "cpp:S111", new RuleConfig {
                          Level = RuleLevel.On
                      } },
                    { "xxx:S222", new RuleConfig {
                          Level = RuleLevel.On
                      } }
                }
            };

            SaveSettings(settingsFile, initialSettings);

            var testLogger  = new TestLogger(logToConsole: true);
            var testSubject = new UserSettingsProvider(testLogger, new FileSystem(), new SingleFileMonitor(settingsFile, testLogger));

            // Sanity check of test setup
            testSubject.UserSettings.RulesSettings.Rules.Count.Should().Be(3);

            // Act - Disable a rule
            testSubject.DisableRule("cpp:S111");

            // Check the data on disc
            File.Exists(settingsFile).Should().BeTrue();

            var reloadedSettings = LoadSettings(settingsFile);

            reloadedSettings.Rules.Count.Should().Be(3);
            reloadedSettings.Rules["javascript:S111"].Level.Should().Be(RuleLevel.On);
            reloadedSettings.Rules["cpp:S111"].Level.Should().Be(RuleLevel.Off);
            reloadedSettings.Rules["xxx:S222"].Level.Should().Be(RuleLevel.On);
        }
Beispiel #18
0
        public void DisableExcludedRules_NoExcludedRules_CustomRulesReturned()
        {
            // Arrange
            var testLogger = new TestLogger();

            var excluded = Array.Empty <string>();
            var custom   = new RulesSettings()
                           .AddRule("custom1", RuleLevel.On)
                           .AddRule("custom2", RuleLevel.On);

            // Act
            var result = DynamicCFamilyRulesConfig.DisableExcludedRules(custom, excluded, testLogger);

            // Assert
            result.Should().NotBeSameAs(custom);

            result.Rules.Keys.Should().BeEquivalentTo("custom1", "custom2");
            result.Rules["custom1"].Level.Should().Be(RuleLevel.On);
            result.Rules["custom2"].Level.Should().Be(RuleLevel.On);
        }
Beispiel #19
0
        public void DisableExcludedRules_NoCustomRules_ExcludedRulesAreDisabled()
        {
            // Arrange
            var testLogger = new TestLogger();

            var excluded = new string[] { "excluded" };
            var custom   = new RulesSettings();

            // Act
            var result = DynamicCFamilyRulesConfig.DisableExcludedRules(custom, excluded, testLogger);

            // Assert
            result.Should().NotBeSameAs(custom);
            custom.Rules.Count.Should().Be(0);

            result.Rules.Keys.Should().BeEquivalentTo("excluded");
            result.Rules["excluded"].Level.Should().Be(RuleLevel.Off);

            testLogger.AssertPartialOutputStringExists("excluded");
        }
Beispiel #20
0
        public void ActiveRules_ExcludedRulesOverrideCustomAndDefaults()
        {
            // Arrange
            const string ExcludedRuleKey = "S9876";

            // Enabled the disabled rule in the default confing...
            var defaultConfig = new DummyCFamilyRulesConfig("c")
                                .AddRule(ExcludedRuleKey, isActive: true)
                                .AddRule("rule2", isActive: true);

            // ... and in the custom settings
            var settings = new RulesSettings()
                           .AddRule("c:" + ExcludedRuleKey, RuleLevel.On);

            // Act
            var testSubject = new DynamicCFamilyRulesConfig(defaultConfig, settings, new TestLogger(), new string[] { "c:" + ExcludedRuleKey, "WRONGLANGUAGE:rule2" });

            // Assert
            testSubject.AllPartialRuleKeys.Should().BeEquivalentTo(ExcludedRuleKey, "rule2"); // excluded rule is included
            testSubject.ActivePartialRuleKeys.Should().BeEquivalentTo("rule2");               // ... but not active
        }
Beispiel #21
0
        public void EffectiveSeverity_CustomSettingsOverrideDefaults()
        {
            // Arrange
            var defaultConfig = new DummyCFamilyRulesConfig("c")
                                .AddRule("rule1", IssueSeverity.Major, isActive: false)
                                .AddRule("rule2", IssueSeverity.Minor, isActive: true)
                                .AddRule("rule3", IssueSeverity.Info, isActive: true);

            var settings = new RulesSettings();

            // Rule 1 - severity not specified -> should use default
            settings.Rules["c:rule1"] = new RuleConfig();

            // Rule 2 - should override default severity
            // Rule key comparison should be case-insensitive
            settings.Rules["c:RULE2"] = new RuleConfig {
                Severity = IssueSeverity.Blocker
            };

            // Rule 3 for a different language -> should be ignored and the default config used
            settings.Rules["cpp:rule3"] = new RuleConfig {
                Severity = IssueSeverity.Critical
            };

            // rule in user settings that isn't in the default config should be ignored
            settings.Rules["c:missingRule"] = new RuleConfig {
                Severity = IssueSeverity.Critical
            };

            // Act
            var dynamicConfig = new DynamicCFamilyRulesConfig(defaultConfig, settings, new TestLogger(), NoExcludedRules);

            // Assert
            dynamicConfig.RulesMetadata.Count.Should().Be(3);
            dynamicConfig.RulesMetadata["rule1"].DefaultSeverity.Should().Be(IssueSeverity.Major);
            dynamicConfig.RulesMetadata["rule2"].DefaultSeverity.Should().Be(IssueSeverity.Blocker);
            dynamicConfig.RulesMetadata["rule3"].DefaultSeverity.Should().Be(IssueSeverity.Info);
        }