コード例 #1
0
        public void RoslynConfig_NoAssemblies()
        {
            // Arrange
            var rootFolder = CreateTestFolders();
            var logger     = new TestLogger();
            var rules      = createRules();
            var language   = RoslynAnalyzerProvider.CSharpLanguage;

            // missing properties to get plugin related properties
            var sonarProperties = new ListPropertiesProvider(new Dictionary <string, string>
            {
                { "wintellect.analyzerId", "Wintellect.Analyzers" },
                { "wintellect.ruleNamespace", "Wintellect.Analyzers" },
                { "sonaranalyzer-cs.analyzerId", "SonarAnalyzer.CSharp" },
                { "sonaranalyzer-cs.ruleNamespace", "SonarAnalyzer.CSharp" }
            });

            var mockInstaller = new MockAnalyzerInstaller
            {
                AnalyzerPluginsToReturn = new List <AnalyzerPlugin> {
                    CreateAnalyzerPlugin("c:\\assembly1.dll", "d:\\foo\\assembly2.dll")
                }
            };
            var settings = CreateSettings(rootFolder);

            var testSubject = new RoslynAnalyzerProvider(mockInstaller, logger);

            // Act
            var actualSettings = testSubject.SetupAnalyzer(settings, sonarProperties, rules, language);

            // Assert
            CheckSettingsInvariants(actualSettings);
            logger.AssertWarningsLogged(0);
            logger.AssertErrorsLogged(0);

            CheckRuleset(actualSettings.RulesetPath, rootFolder, language);
            CheckTestRuleset(actualSettings.DeactivatedRulesetPath, rootFolder, language);

            actualSettings.AnalyzerPlugins.Should().BeEmpty();
            var plugins = new List <string>();

            mockInstaller.AssertExpectedPluginsRequested(plugins);
        }
コード例 #2
0
        public void RoslynConfig_ValidProfile()
        {
            // Arrange
            var rootFolder    = CreateTestFolders();
            var logger        = new TestLogger();
            var rules         = createRules();
            var language      = RoslynAnalyzerProvider.CSharpLanguage;
            var mockInstaller = new MockAnalyzerInstaller
            {
                AnalyzerPluginsToReturn = new List <AnalyzerPlugin>
                {
                    CreateAnalyzerPlugin("c:\\assembly1.dll"),
                    CreateAnalyzerPlugin("d:\\foo\\assembly2.dll")
                }
            };
            var settings = CreateSettings(rootFolder);

            var sonarProperties = new ListPropertiesProvider(new Dictionary <string, string>
            {
                // for ruleset
                { "wintellect.analyzerId", "Wintellect.Analyzers" },
                { "wintellect.ruleNamespace", "Wintellect.Analyzers" },

                // to fetch assemblies
                { "wintellect.pluginKey", "wintellect" },
                { "wintellect.pluginVersion", "1.13.0" },
                { "wintellect.staticResourceName", "SonarAnalyzer.zip" },

                { "sonaranalyzer-cs.analyzerId", "SonarAnalyzer.CSharp" },
                { "sonaranalyzer-cs.ruleNamespace", "SonarAnalyzer.CSharp" },
                { "sonaranalyzer-cs.pluginKey", "csharp" },
                { "sonaranalyzer-cs.staticResourceName", "SonarAnalyzer.zip" },
                { "sonaranalyzer-cs.nuget.packageId", "SonarAnalyzer.CSharp" },
                { "sonaranalyzer-cs.pluginVersion", "1.13.0" },
                { "sonaranalyzer-cs.nuget.packageVersion", "1.13.0" },

                // Extra properties - those started sonar.cs should be included, the others ignored
                { "sonar.vb.testPropertyPattern", "foo" },
                { "sonar.cs.testPropertyPattern", "foo" },
                { "sonar.sources", "**/*.*" },
                { "sonar.cs.foo", "bar" }
            });
            var expectedSonarLintXml = @"<?xml version=""1.0"" encoding=""UTF-8""?>
<AnalysisInput>
  <Settings>
    <Setting>
      <Key>sonar.cs.testPropertyPattern</Key>
      <Value>foo</Value>
    </Setting>
    <Setting>
      <Key>sonar.cs.foo</Key>
      <Value>bar</Value>
    </Setting>
  </Settings>
  <Rules>
    <Rule>
      <Key>S1116</Key>
      <Parameters>
        <Parameter>
          <Key>key</Key>
          <Value>value</Value>
        </Parameter>
      </Parameters>
    </Rule>
    <Rule>
      <Key>S1125</Key>
    </Rule>
  </Rules>
  <Files>
  </Files>
</AnalysisInput>
";
            var testSubject          = new RoslynAnalyzerProvider(mockInstaller, logger);

            // Act
            var actualSettings = testSubject.SetupAnalyzer(settings, sonarProperties, rules, language);

            // Assert
            CheckSettingsInvariants(actualSettings);
            logger.AssertWarningsLogged(0);
            logger.AssertErrorsLogged(0);

            CheckRuleset(actualSettings.RulesetPath, rootFolder, language);
            CheckTestRuleset(actualSettings.DeactivatedRulesetPath, rootFolder, language);

            // Currently, only SonarLint.xml is written
            var filePaths = actualSettings.AdditionalFilePaths;

            filePaths.Should().ContainSingle();
            CheckExpectedAdditionalFileExists("SonarLint.xml", expectedSonarLintXml, actualSettings);

            CheckExpectedAssemblies(actualSettings, "c:\\assembly1.dll", "d:\\foo\\assembly2.dll");
            var plugins = new List <string>
            {
                "wintellect",
                "csharp"
            };

            mockInstaller.AssertExpectedPluginsRequested(plugins);
        }