Example #1
0
        private void CheckCanGenerateForThirdPartyAssembly(string packageId, SemanticVersion version)
        {
            // Arrange
            var logger    = new TestLogger();
            var outputDir = TestUtils.CreateTestDirectory(TestContext, ".out");
            var localPackageDestination    = TestUtils.CreateTestDirectory(TestContext, ".localpackages");
            var localRepoWithRemotePackage = InstallRemotePackageLocally(packageId, version);

            // Act
            var nuGetHandler = new NuGetPackageHandler(localRepoWithRemotePackage, localPackageDestination, logger);

            var apg    = new AnalyzerPluginGenerator(nuGetHandler, logger);
            var args   = new ProcessedArgs(packageId, version, "cs", null, false, false, outputDir);
            var result = apg.Generate(args);

            // Assert
            result.Should().BeTrue();

            // Expecting one plugin per dependency with analyzers
            AssertJarsGenerated(outputDir, 1);

            var jarFilePath            = Directory.GetFiles(outputDir, "*.jar", SearchOption.TopDirectoryOnly).Single();
            var jarChecker             = new ZipFileChecker(TestContext, jarFilePath);
            var actualManifestFilePath = jarChecker.AssertFileExists("META-INF\\MANIFEST.MF");

            JarManifestReader reader = new JarManifestReader(File.ReadAllText(actualManifestFilePath));

            AssertFixedValuesInManifest(reader);
        }
Example #2
0
        private void CheckEmbeddedAnalyzerPayload(ZipFileChecker jarChecker, string staticResourceName,
                                                  params string[] expectedZipContents)
        {
            // Now create another checker to check the contents of the zip file (strict check this time)
            string embeddedZipFilePath = jarChecker.AssertFileExists(staticResourceName);

            ZipFileChecker embeddedFileChecker = new ZipFileChecker(TestContext, embeddedZipFilePath);

            embeddedFileChecker.AssertZipContainsOnlyExpectedFiles(expectedZipContents);
        }
        public void ArchiveUpdater_SimpleUpdateJar_Succeeds()
        {
            // Arrange - create an input archive file
            string rootTestDir     = TestUtils.CreateTestDirectory(this.TestContext);
            string originalZipFile = Path.Combine(rootTestDir, "original.zip");
            string updatedZipFile  = Path.Combine(rootTestDir, "updated.zip");

            string setupDir = TestUtils.CreateTestDirectory(this.TestContext, ".zip.setup");

            TestUtils.CreateTextFile("file1.txt", setupDir, "file 1 content");
            TestUtils.CreateTextFile("sub1\\sub2\\file2.txt", setupDir, "file 2 content");

            ZipFile.CreateFromDirectory(setupDir, originalZipFile);

            // Sanity check that the test archive was built correctly
            ZipFileChecker checker = new ZipFileChecker(this.TestContext, originalZipFile);

            checker.AssertZipContainsOnlyExpectedFiles(
                // Original files
                "file1.txt",
                "sub1\\sub2\\file2.txt");


            // Create some new dummy files to add
            string addFile1 = TestUtils.CreateTextFile("additional1.txt", rootTestDir, "a1");
            string addFile2 = TestUtils.CreateTextFile("additional2.txt", rootTestDir, "a2");

            string updaterRootDir = TestUtils.CreateTestDirectory(this.TestContext, "updater");

            ArchiveUpdater updater = new ArchiveUpdater(updaterRootDir, new TestLogger());

            // Act
            updater.SetInputArchive(originalZipFile)
            .SetOutputArchive(updatedZipFile)
            .AddFile(addFile1, "addFile1.txt")
            .AddFile(addFile2, "sub1\\sub2\\addFile2.txt")
            .AddFile(addFile1, "newSubDir\\addFile3.txt");
            updater.UpdateArchive();

            // Assert
            checker = new ZipFileChecker(this.TestContext, updatedZipFile);

            checker.AssertZipContainsOnlyExpectedFiles(
                // Original files
                "file1.txt",
                "sub1\\sub2\\file2.txt",

                // Added files
                "addFile1.txt",
                "sub1\\sub2\\addFile2.txt",
                "newSubDir\\addFile3.txt"
                );
        }
Example #4
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");
        }
        public void RoslynPlugin_Test()
        {
            // Arrange
            string testDir           = TestUtils.CreateTestDirectory(TestContext);
            string workingDir        = TestUtils.CreateTestDirectory(TestContext, ".working");
            string outputJarFilePath = Path.Combine(testDir, "created.jar");

            string dummyRulesFile = TestUtils.CreateTextFile("rules.txt", testDir, "<rules />");
            string dummyZipFile   = TestUtils.CreateTextFile("payload.txt", testDir, "zip");

            PluginManifest manifest = new PluginManifest()
            {
                Key         = "pluginkey",
                Description = "description",
                Name        = "name"
            };

            // Act
            RoslynPluginJarBuilder builder = new RoslynPluginJarBuilder(new TestLogger());

            builder.SetLanguage("cs")
            .SetRepositoryKey("repo.key")
            .SetRepositoryName("repo.name")
            .SetRulesFilePath(dummyRulesFile)
            .SetPluginManifestProperties(manifest)
            .AddResourceFile(dummyZipFile, "static\\foo.zip")
            .SetJarFilePath(outputJarFilePath);

            builder.BuildJar(workingDir);

            // Assert
            ZipFileChecker checker = new ZipFileChecker(TestContext, outputJarFilePath);

            checker.AssertZipContainsFiles(
                "META-INF\\MANIFEST.MF",
                "static\\foo.zip",
                "org\\sonar\\plugins\\roslynsdk\\configuration.xml",
                "org\\sonar\\plugins\\roslynsdk\\rules.xml"
                );

            checker.AssertZipDoesNotContainFiles(
                "org\\sonar\\plugins\\roslynsdk\\sqale.xml");
        }
Example #6
0
        public void ZipDir_SimpleFilter_2()
        {
            // Arrange
            string testDir = TestUtils.CreateTestDirectory(this.TestContext);

            TestUtils.CreateTextFile("dummy.txt", testDir, "dummy content");
            TestUtils.CreateTextFile("sub1\\foo.txt", testDir, "dummy content");
            TestUtils.CreateTextFile("sub2\\bar.123", testDir, "dummy content");
            TestUtils.CreateTextFile("sub2\\archive1.zip", testDir, "dummy content");
            TestUtils.CreateTextFile("archive2.zip", testDir, "dummy content");

            Func <string, bool> shouldInclude = f => f.Contains("sub");
            string fullzipFileName            = Path.Combine(testDir, "output.zip");

            ZipExtensions.CreateFromDirectory(testDir, fullzipFileName, shouldInclude);

            ZipFileChecker checker = new ZipFileChecker(this.TestContext, fullzipFileName);

            checker.AssertZipContainsOnlyExpectedFiles(
                "sub1\\foo.txt",
                "sub2\\bar.123",
                "sub2\\archive1.zip");
        }