Ejemplo n.º 1
0
        private static JarInfo.RulesDefinition AssertRulesDefinitionExists(JarInfo jarInfo)
        {
            Assert.IsNotNull(jarInfo.Extensions, "Extensions should not be null");

            IEnumerable <JarInfo.RulesDefinition> defns = jarInfo.Extensions.OfType <JarInfo.RulesDefinition>();

            Assert.AreNotEqual(0, defns.Count(), "RulesDefinition extension does not exist");
            Assert.AreEqual(1, defns.Count(), "Multiple rules definitions exist");

            JarInfo.RulesDefinition defn = defns.Single();

            return(defn);
        }
Ejemplo n.º 2
0
        public void RoslynPlugin_GenerateForValidAnalyzer_Succeeds()
        {
            // Arrange
            TestLogger      logger    = new TestLogger();
            string          outputDir = TestUtils.CreateTestDirectory(this.TestContext, ".out");
            PluginInspector inspector = CreatePluginInspector(logger);

            // Create a valid analyzer package
            ExampleAnalyzer1.CSharpAnalyzer analyzer = new ExampleAnalyzer1.CSharpAnalyzer();

            string          packageId       = "analyzer1.id1";
            string          localNuGetDir   = TestUtils.CreateTestDirectory(this.TestContext, ".localNuGet");
            IPackageManager localNuGetStore = CreatePackageManager(localNuGetDir);

            AddPackage(localNuGetStore, packageId, "1.0", analyzer.GetType().Assembly.Location);

            // Act
            NuGetPackageHandler     nuGetHandler = new NuGetPackageHandler(localNuGetDir, logger);
            AnalyzerPluginGenerator apg          = new AnalyzerPluginGenerator(nuGetHandler, logger);
            bool result = apg.Generate(new Roslyn.CommandLine.NuGetReference(packageId, new SemanticVersion("1.0")), "cs", null, outputDir);

            // Assert
            Assert.IsTrue(result);
            string jarFilePath = AssertPluginJarExists(outputDir);

            JarInfo jarInfo = inspector.GetPluginDescription(jarFilePath);

            if (jarInfo != null)
            {
                this.TestContext.AddResultFile(jarInfo.FileName);
            }

            Assert.IsNotNull(jarInfo, "Failed to process the generated jar successfully");

            AssertPropertyDefinitionExists(packageId + "_sarif.AnalyzerId", jarInfo);
            AssertPropertyDefinitionExists(packageId + "_sarif.RuleNamespace", jarInfo);
            AssertPropertyDefinitionExists(packageId + "_sarif.nuget.packageId", jarInfo);
            AssertPropertyDefinitionExists(packageId + "_sarif.nuget.packageVersion", jarInfo);

            JarInfo.RulesDefinition rulesDefn = AssertRulesDefinitionExists(jarInfo);
            AssertRepositoryIsValid(rulesDefn.Repository);

            AssertExpectedRulesExist(analyzer, rulesDefn.Repository);

            Assert.AreEqual(5, jarInfo.Extensions.Count, "Unexpected number of extensions");
        }