Example #1
0
        public void Generate_ValidRules_OnlyRulesWithParametersReturned()
        {
            // Arrange
            var testSubject = new SonarLintConfigGenerator();

            var rule1Params = new Dictionary <string, string> {
                { "param1", "value1" }, { "param2", "value2" }
            };
            var rule3Params = new Dictionary <string, string> {
                { "param3", "value4" }
            };

            var rules = new List <SonarQubeRule>()
            {
                CreateRule("s111", "csharpsquid", rule1Params),
                CreateRule("s222", "csharpsquid" /* no params */),
                CreateRule("s333", "csharpsquid", rule3Params)
            };

            // Act
            var actual = testSubject.Generate(rules, EmptyProperties, Language.CSharp);

            // Assert
            actual.Rules.Count.Should().Be(2);

            actual.Rules[0].Key.Should().Be("s111");
            actual.Rules[0].Parameters.Should().BeEquivalentTo(rule1Params);
            actual.Rules[1].Key.Should().Be("s333");
            actual.Rules[1].Parameters.Should().BeEquivalentTo(rule3Params);
        }
Example #2
0
        public void Generate_ValidSettings_OnlyLanguageSpecificSettingsReturned()
        {
            // Arrange
            var properties = new Dictionary <string, string>
            {
                { "sonar.cs.property1", "valid setting 1" },
                { "sonar.cs.property2", "valid setting 2" },
                { "sonar.vbnet.property1", "wrong language - not returned" },
                { "sonar.CS.property2", "wrong case - not returned" },
                { "sonar.cs.", "incorrect prefix - not returned" },
                { "xxx.cs.property1", "key does not match - not returned" },
                { ".does.not.match", "not returned" }
            };

            var testSubject = new SonarLintConfigGenerator();

            // Act
            var actual = testSubject.Generate(EmptyRules, properties, Language.CSharp);

            // Assert
            actual.Settings.Should().BeEquivalentTo(new Dictionary <string, string>
            {
                { "sonar.cs.property1", "valid setting 1" },
                { "sonar.cs.property2", "valid setting 2" }
            });
        }
Example #3
0
        public void Generate_NoActiveRulesOrSettings_ValidLanguage_ReturnsValidConfig(string languageKey)
        {
            var testSubject = new SonarLintConfigGenerator();
            var actual      = testSubject.Generate(EmptyRules, EmptyProperties, ToLanguage(languageKey));

            actual.Should().NotBeNull();
            actual.Rules.Should().BeEmpty();
            actual.Settings.Should().BeEmpty();
        }
Example #4
0
        public void Generate_SonarSecurityRules_AreNotReturned(string languageKey)
        {
            // Arrange
            var testSubject = new SonarLintConfigGenerator();
            var rules       = new List <SonarQubeRule>()
            {
                CreateRuleWithValidParams("valid1", $"roslyn.sonaranalyzer.security.{languageKey}"),
                CreateRuleWithValidParams("valid2", $"roslyn.sonaranalyzer.security.{languageKey}")
            };

            // Act
            var actual = testSubject.Generate(rules, EmptyProperties, ToLanguage(languageKey));

            // Assert
            actual.Rules.Should().BeEmpty();
        }
Example #5
0
        public void Generate_ValidRules_OnlyRulesFromKnownRepositoryReturned(string knownLanguageKey, string knownRepoKey)
        {
            // Arrange
            var testSubject = new SonarLintConfigGenerator();
            var rules       = new List <SonarQubeRule>()
            {
                CreateRuleWithValidParams("valid1", knownRepoKey),
                CreateRuleWithValidParams("unknown1", "unknown.repo.key"),
                CreateRuleWithValidParams("valid2", knownRepoKey),
                CreateRuleWithValidParams("invalid2", "another.unknown.repo.key"),
                CreateRuleWithValidParams("valid3", knownRepoKey)
            };

            // Act
            var actual = testSubject.Generate(rules, EmptyProperties, ToLanguage(knownLanguageKey));

            // Assert
            actual.Rules.Select(r => r.Key).Should().BeEquivalentTo(new string[] { "valid1", "valid2", "valid3" });
        }
Example #6
0
        public void Generate_ValidSettings_SecuredSettingsAreNotReturned()
        {
            // Arrange
            var testSubject = new SonarLintConfigGenerator();
            var properties  = new Dictionary <string, string>
            {
                { "sonar.cs.property1.secured", "secure - should not be returned" },
                { "sonar.cs.property2", "valid setting" },
                { "sonar.cs.property3.SECURED", "secure - should not be returned2" },
            };

            // Act
            var actual = testSubject.Generate(EmptyRules, properties, Language.CSharp);

            // Assert
            actual.Settings.Should().BeEquivalentTo(new Dictionary <string, string>
            {
                { "sonar.cs.property2", "valid setting" }
            });
        }
Example #7
0
        public void Generate_ValidRules_AreSorted()
        {
            // Arrange
            var testSubject = new SonarLintConfigGenerator();

            var rules = new List <SonarQubeRule>()
            {
                CreateRule("s222", "csharpsquid",
                           new Dictionary <string, string> {
                    { "any", "any" }
                }),
                CreateRule("s111", "csharpsquid",
                           new Dictionary <string, string> {
                    { "CCC", "value 1" }, { "BBB", "value 2" }, { "AAA", "value 3" }
                }),
                CreateRule("s333", "csharpsquid",
                           new Dictionary <string, string> {
                    { "any", "any" }
                })
            };

            // Act
            var actual = testSubject.Generate(rules, EmptyProperties, Language.CSharp);

            // Assert
            actual.Rules.Count.Should().Be(3);

            actual.Rules[0].Key.Should().Be("s111");
            actual.Rules[1].Key.Should().Be("s222");
            actual.Rules[2].Key.Should().Be("s333");

            actual.Rules[0].Parameters[0].Key.Should().Be("AAA");
            actual.Rules[0].Parameters[0].Value.Should().Be("value 3");

            actual.Rules[0].Parameters[1].Key.Should().Be("BBB");
            actual.Rules[0].Parameters[1].Value.Should().Be("value 2");

            actual.Rules[0].Parameters[2].Key.Should().Be("CCC");
            actual.Rules[0].Parameters[2].Value.Should().Be("value 1");
        }
Example #8
0
        public void Generate_ValidSettings_AreSorted()
        {
            // Arrange
            var testSubject = new SonarLintConfigGenerator();
            var properties  = new Dictionary <string, string>
            {
                { "sonar.cs.property3", "aaa" },
                { "sonar.cs.property1", "bbb" },
                { "sonar.cs.property2", "ccc" },
            };

            // Act
            var actual = testSubject.Generate(EmptyRules, properties, Language.CSharp);

            // Assert
            actual.Settings[0].Key.Should().Be("sonar.cs.property1");
            actual.Settings[0].Value.Should().Be("bbb");

            actual.Settings[1].Key.Should().Be("sonar.cs.property2");
            actual.Settings[1].Value.Should().Be("ccc");

            actual.Settings[2].Key.Should().Be("sonar.cs.property3");
            actual.Settings[2].Value.Should().Be("aaa");
        }
Example #9
0
        public void Generate_Serialized_ReturnsExpectedXml()
        {
            // Arrange
            var testSubject = new SonarLintConfigGenerator();

            var properties = new Dictionary <string, string>()
            {
                { "sonar.cs.prop1", "value 1" },
                { "sonar.cs.prop2", "value 2" }
            };

            var rules = new List <SonarQubeRule>()
            {
                CreateRule("s555", "csharpsquid",
                           new Dictionary <string, string> {
                    { "x", "y y" }
                }),
                CreateRule("s444", "csharpsquid"),
                CreateRule("s333", "csharpsquid"),
                CreateRule("s222", "csharpsquid",
                           new Dictionary <string, string> {
                    { "ZZZ", "param value1" }, { "AAA", "param value2" }
                }),
                CreateRule("s111", "csharpsquid"),
            };

            // Act
            var actual    = testSubject.Generate(rules, properties, Language.CSharp);
            var actualXml = Serializer.ToString(actual);

            // Assert
            actualXml.Should().Be(@"<?xml version=""1.0"" encoding=""utf-8""?>
<AnalysisInput xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
  <Settings>
    <Setting>
      <Key>sonar.cs.prop1</Key>
      <Value>value 1</Value>
    </Setting>
    <Setting>
      <Key>sonar.cs.prop2</Key>
      <Value>value 2</Value>
    </Setting>
  </Settings>
  <Rules>
    <Rule>
      <Key>s222</Key>
      <Parameters>
        <Parameter>
          <Key>AAA</Key>
          <Value>param value2</Value>
        </Parameter>
        <Parameter>
          <Key>ZZZ</Key>
          <Value>param value1</Value>
        </Parameter>
      </Parameters>
    </Rule>
    <Rule>
      <Key>s555</Key>
      <Parameters>
        <Parameter>
          <Key>x</Key>
          <Value>y y</Value>
        </Parameter>
      </Parameters>
    </Rule>
  </Rules>
</AnalysisInput>");
        }