예제 #1
0
        private void CheckJarGeneratedForPackage(string rootDir, DiagnosticAnalyzer analyzer, IPackage package)
        {
            string jarFilePath = GetGeneratedJars(rootDir).SingleOrDefault(r => r.Contains(package.Id.Replace(".", "").ToLower()));

            jarFilePath.Should().NotBeNull();

            // Check the content of the files embedded in the jar
            ZipFileChecker jarChecker = new ZipFileChecker(TestContext, jarFilePath);

            // Check the contents of the embedded config file
            string embeddedConfigFile     = jarChecker.AssertFileExists("org\\sonar\\plugins\\roslynsdk\\configuration.xml");
            RoslynSdkConfiguration config = RoslynSdkConfiguration.Load(embeddedConfigFile);

            // Check the config settings
            package.Should().NotBeNull("Unexpected repository differentiator");

            string pluginId = package.Id.ToLower();

            config.RepositoryKey.Should().Be("roslyn." + pluginId + ".cs", "Unexpected repository key");
            config.RepositoryLanguage.Should().Be("cs", "Unexpected language");
            config.RepositoryName.Should().Be("dummy title", "Unexpected repository name");

            // Check for the expected property values required by the C# plugin
            // Property name prefixes should be lower case; the case of the value should be the same as the package id
            AssertExpectedPropertyDefinitionValue(pluginId + ".cs.analyzerId", package.Id, config);
            AssertExpectedPropertyDefinitionValue(pluginId + ".cs.ruleNamespace", package.Id, config);
            AssertExpectedPropertyDefinitionValue(pluginId + ".cs.nuget.packageId", package.Id, config);
            AssertExpectedPropertyDefinitionValue(pluginId + ".cs.nuget.packageVersion", package.Version.ToString(), config);
            AssertExpectedPropertyDefinitionValue(pluginId + ".cs.staticResourceName", package.Id + "." + package.Version + ".zip", config);
            AssertExpectedPropertyDefinitionValue(pluginId + ".cs.pluginKey", pluginId.Replace(".", ""), config);
            AssertExpectedPropertyDefinitionValue(pluginId + ".cs.pluginVersion", package.Version.ToString(), config);

            // Check the contents of the manifest
            string actualManifestFilePath = jarChecker.AssertFileExists("META-INF\\MANIFEST.MF");

            var manifestReader = new JarManifestReader(File.ReadAllText(actualManifestFilePath));

            manifestReader.FindValue(WellKnownPluginProperties.Key).Should().Be(pluginId.Replace(".", ""));

            AssertPackagePropertiesInManifest(package, manifestReader);
            AssertFixedValuesInManifest(manifestReader);

            // Check the rules
            string actualRuleFilePath = jarChecker.AssertFileExists("." + config.RulesXmlResourcePath);

            AssertExpectedRulesExist(analyzer, actualRuleFilePath);

            // Now create another checker to check the contents of the zip file (strict check this time)
            CheckEmbeddedAnalyzerPayload(jarChecker, "static\\" + pluginId + "." + package.Version + ".zip",
                                         /* zip file contents */
                                         "analyzers\\RoslynAnalyzer11.dll");
        }
예제 #2
0
        public void SdkConfig_LoadRealExample_Succeeds()
        {
            // Arrange
            #region File content

            string exampleConfig = @"<RoslynSdkConfiguration>
  <PluginKeyDifferentiator>example</PluginKeyDifferentiator>
  <RepositoryKey>roslyn.example</RepositoryKey>
  <RepositoryLanguage>example</RepositoryLanguage>
  <RepositoryName>example</RepositoryName>
  <RulesXmlResourcePath>/org/sonar/plugins/roslynsdk/rules.xml</RulesXmlResourcePath>
  <SqaleXmlResourcePath>/org/sonar/plugins/roslynsdk/sqale.xml</SqaleXmlResourcePath>
  <PluginProperties>
    <example.pluginKey>example.pluginKey.Value</example.pluginKey>
    <example.pluginVersion>example.pluginVersion.Value</example.pluginVersion>
    <example.staticResourceName>example.staticResourceName.Value</example.staticResourceName>
    <example.nuget.packageId>example.nuget.packageId.Value</example.nuget.packageId>
    <example.nuget.packageVersion>example.nuget.packageVersion.Value</example.nuget.packageVersion>
    <example.analyzerId>example.analyzerId.Value</example.analyzerId>
    <example.ruleNamespace>example.ruleNamespace.Value</example.ruleNamespace>
  </PluginProperties>
</RoslynSdkConfiguration>
";

            #endregion

            string testDir  = TestUtils.CreateTestDirectory(this.TestContext);
            string filePath = TestUtils.CreateTextFile("realPluginProperties.txt", testDir, exampleConfig);
            this.TestContext.AddResultFile(filePath);

            // Act
            RoslynSdkConfiguration loaded = RoslynSdkConfiguration.Load(filePath);
            string resavedFilePath        = Path.Combine(testDir, "resaved.txt");
            loaded.Save(resavedFilePath);
            this.TestContext.AddResultFile(resavedFilePath);

            // Assert
            Assert.AreEqual("example", loaded.PluginKeyDifferentiator);
            Assert.AreEqual("roslyn.example", loaded.RepositoryKey);
            Assert.AreEqual("example", loaded.RepositoryLanguage);
            Assert.AreEqual("example", loaded.RepositoryName);
            Assert.AreEqual("/org/sonar/plugins/roslynsdk/rules.xml", loaded.RulesXmlResourcePath);
            Assert.AreEqual("/org/sonar/plugins/roslynsdk/sqale.xml", loaded.SqaleXmlResourcePath);

            AssertPropertyExists("example.pluginKey", "example.pluginKey.Value", loaded.Properties);
            AssertPropertyExists("example.pluginVersion", "example.pluginVersion.Value", loaded.Properties);
            AssertPropertyExists("example.staticResourceName", "example.staticResourceName.Value", loaded.Properties);
            AssertPropertyExists("example.nuget.packageId", "example.nuget.packageId.Value", loaded.Properties);
            AssertPropertyExists("example.nuget.packageVersion", "example.nuget.packageVersion.Value", loaded.Properties);
            AssertPropertyExists("example.analyzerId", "example.analyzerId.Value", loaded.Properties);
            AssertPropertyExists("example.ruleNamespace", "example.ruleNamespace.Value", loaded.Properties);
        }
예제 #3
0
        public void SdkConfig_SaveAndReload_Succeeds()
        {
            // Arrange
            string testDir  = TestUtils.CreateTestDirectory(this.TestContext);
            string filePath = Path.Combine(testDir, "original.txt");


            RoslynSdkConfiguration config = new RoslynSdkConfiguration();

            config.PluginKeyDifferentiator = "diff";
            config.RepositoryKey           = "key";
            config.RepositoryName          = "repo.name";
            config.RepositoryLanguage      = "language";
            config.RulesXmlResourcePath    = "rulesPath";
            config.SqaleXmlResourcePath    = "sqalePath";

            config.Properties["prop1.Key"] = "value1";
            config.Properties["prop2.Key"] = "value2";

            // Save and check
            config.Save(filePath);
            Assert.AreEqual(filePath, config.FileName);
            this.TestContext.AddResultFile(filePath);

            // Reload and check
            RoslynSdkConfiguration reloaded = RoslynSdkConfiguration.Load(filePath);

            Assert.IsNotNull(reloaded);

            Assert.AreEqual("diff", reloaded.PluginKeyDifferentiator);
            Assert.AreEqual("key", reloaded.RepositoryKey);
            Assert.AreEqual("repo.name", reloaded.RepositoryName);
            Assert.AreEqual("language", reloaded.RepositoryLanguage);
            Assert.AreEqual("rulesPath", reloaded.RulesXmlResourcePath);
            Assert.AreEqual("sqalePath", reloaded.SqaleXmlResourcePath);

            Assert.AreEqual(2, reloaded.Properties.Count);
            AssertPropertyExists("prop1.Key", "value1", reloaded.Properties);
            AssertPropertyExists("prop2.Key", "value2", reloaded.Properties);
        }
        public void SdkConfig_SaveAndReload_Succeeds()
        {
            // Arrange
            string testDir = TestUtils.CreateTestDirectory(this.TestContext);
            string filePath = Path.Combine(testDir, "original.txt");


            RoslynSdkConfiguration config = new RoslynSdkConfiguration();

            config.PluginKeyDifferentiator = "diff";
            config.RepositoryKey = "key";
            config.RepositoryName = "repo.name";
            config.RepositoryLanguage = "language";
            config.RulesXmlResourcePath = "rulesPath";
            config.SqaleXmlResourcePath = "sqalePath";

            config.Properties["prop1.Key"] = "value1";
            config.Properties["prop2.Key"] = "value2";

            // Save and check 
            config.Save(filePath);
            Assert.AreEqual(filePath, config.FileName);
            this.TestContext.AddResultFile(filePath);

            // Reload and check
            RoslynSdkConfiguration reloaded = RoslynSdkConfiguration.Load(filePath);

            Assert.IsNotNull(reloaded);

            Assert.AreEqual("diff", reloaded.PluginKeyDifferentiator);
            Assert.AreEqual("key", reloaded.RepositoryKey);
            Assert.AreEqual("repo.name", reloaded.RepositoryName);
            Assert.AreEqual("language", reloaded.RepositoryLanguage);
            Assert.AreEqual("rulesPath", reloaded.RulesXmlResourcePath);
            Assert.AreEqual("sqalePath", reloaded.SqaleXmlResourcePath);

            Assert.AreEqual(2, reloaded.Properties.Count);
            AssertPropertyExists("prop1.Key", "value1", reloaded.Properties);
            AssertPropertyExists("prop2.Key", "value2", reloaded.Properties);
        }
예제 #5
0
        public void SdkConfig_SaveAndReload_Succeeds()
        {
            // Arrange
            string testDir  = TestUtils.CreateTestDirectory(TestContext);
            string filePath = Path.Combine(testDir, "original.txt");

            RoslynSdkConfiguration config = new RoslynSdkConfiguration
            {
                PluginKeyDifferentiator = "diff",
                RepositoryKey           = "key",
                RepositoryName          = "repo.name",
                RepositoryLanguage      = "language",
                RulesXmlResourcePath    = "rulesPath",
            };

            config.Properties["prop1.Key"] = "value1";
            config.Properties["prop2.Key"] = "value2";

            // Save and check
            config.Save(filePath);
            filePath.Should().Be(config.FileName);
            TestContext.AddResultFile(filePath);

            // Reload and check
            RoslynSdkConfiguration reloaded = RoslynSdkConfiguration.Load(filePath);

            reloaded.Should().NotBeNull();

            reloaded.PluginKeyDifferentiator.Should().Be("diff");
            reloaded.RepositoryKey.Should().Be("key");
            reloaded.RepositoryName.Should().Be("repo.name");
            reloaded.RepositoryLanguage.Should().Be("language");
            reloaded.RulesXmlResourcePath.Should().Be("rulesPath");

            reloaded.Properties.Count.Should().Be(2);
            AssertPropertyExists("prop1.Key", "value1", reloaded.Properties);
            AssertPropertyExists("prop2.Key", "value2", reloaded.Properties);
        }
예제 #6
0
        private static void AssertExpectedPropertyDefinitionValue(string propertyName, string expectedValue, RoslynSdkConfiguration actualConfig)
        {
            actualConfig.Properties.Should().NotBeNull("Configuration Properties should not be null");

            actualConfig.Properties.ContainsKey(propertyName).Should().BeTrue("Expected property is not set: {0}", propertyName);

            actualConfig.Properties[propertyName].Should().Be(expectedValue, "Property does not have the expected value. Property: {0}", propertyName);
        }
예제 #7
0
        private static void AssertExpectedPropertyDefinitionValue(string propertyName, string expectedValue, RoslynSdkConfiguration actualConfig)
        {
            Assert.IsNotNull(actualConfig.Properties, "Configuration Properties should not be null");

            Assert.IsTrue(actualConfig.Properties.ContainsKey(propertyName), "Expected property is not set: {0}", propertyName);

            Assert.AreEqual(expectedValue, actualConfig.Properties[propertyName], "Property does not have the expected value. Property: {0}", propertyName);
        }