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"
                );
        }
        public void RoslynPlugin_Test()
        {
            // Arrange
            string testDir = TestUtils.CreateTestDirectory(this.TestContext);
            string workingDir = TestUtils.CreateTestDirectory(this.TestContext, ".working");
            string outputJarFilePath = Path.Combine(testDir, "created.jar");

            string dummyRulesFile = TestUtils.CreateTextFile("rules.txt", testDir, "<rules />");
            string dummySqaleFile = TestUtils.CreateTextFile("sqale.txt", testDir, "<sqale />");
            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)
                .SetSqaleFilePath(dummySqaleFile)
                .SetPluginManifestProperties(manifest)
                .AddResourceFile(dummyZipFile, "static\\foo.zip")
                .SetJarFilePath(outputJarFilePath);
            
            builder.BuildJar(workingDir);

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

            checker.AssertZipContainsFiles(
                "META-INF\\MANIFEST.MF",
                "static\\foo.zip",
                "org\\sonar\\plugins\\roslynsdk\\configuration.xml",
                "org\\sonar\\plugins\\roslynsdk\\sqale.xml",
                "org\\sonar\\plugins\\roslynsdk\\rules.xml"
                );
        }
        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");
        }
        private void CheckJarGeneratedForPackage(string rootDir, DiagnosticAnalyzer analyzer, IPackage package)
        {
            string jarFilePath = GetGeneratedJars(rootDir).SingleOrDefault(r => r.Contains(package.Id.Replace(".", "").ToLower()));
            Assert.IsNotNull(jarFilePath);

            // Check the content of the files embedded in the jar
            ZipFileChecker jarChecker = new ZipFileChecker(this.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
            Assert.IsNotNull(package, "Unexpected repository differentiator");

            string pluginId = package.Id.ToLower();
            Assert.AreEqual("roslyn." + pluginId + ".cs", config.RepositoryKey, "Unexpected repository key");
            Assert.AreEqual("cs", config.RepositoryLanguage, "Unexpected language");
            Assert.AreEqual("dummy title", config.RepositoryName, "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");
            string[] actualManifest = File.ReadAllLines(actualManifestFilePath);
            AssertExpectedManifestValue(WellKnownPluginProperties.Key, pluginId.Replace(".", ""), actualManifest);
            AssertExpectedManifestValue("Plugin-Key", pluginId.Replace(".", ""), actualManifest); // plugin-key should be lowercase and alphanumeric
            AssertPackagePropertiesInManifest(package, actualManifest);

            // 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");
        }
        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(this.TestContext, embeddedZipFilePath);
            embeddedFileChecker.AssertZipContainsOnlyExpectedFiles(expectedZipContents);
        }
        public void RoslynPlugin_GenerateForValidAnalyzer_Succeeds()
        {
            // Arrange
            TestLogger logger = new TestLogger();
            string outputDir = TestUtils.CreateTestDirectory(this.TestContext, ".out");

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

            string packageId = "Analyzer1.Pkgid1"; // package id is not all lowercase
            string fakeRemoteNuGetDir = TestUtils.CreateTestDirectory(this.TestContext, ".fakeRemoteNuGet");
            IPackageManager fakeRemotePkgMgr = CreatePackageManager(fakeRemoteNuGetDir);
            IPackage analyzerPkg =  AddPackage(fakeRemotePkgMgr, packageId, "1.0.2", analyzer.GetType().Assembly.Location);

            string localPackageDestination = TestUtils.CreateTestDirectory(this.TestContext, ".localpackages");

            // Act
            NuGetPackageHandler nuGetHandler = new NuGetPackageHandler(fakeRemotePkgMgr.LocalRepository, localPackageDestination, logger);
            AnalyzerPluginGenerator apg = new AnalyzerPluginGenerator(nuGetHandler, logger);
            ProcessedArgs args = new ProcessedArgs(packageId, new SemanticVersion("1.0.2"), "cs", null, false, outputDir);
            bool result = apg.Generate(args);

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

            // Check the content of the files embedded in the jar
            ZipFileChecker jarChecker = new ZipFileChecker(this.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
            Assert.AreEqual("analyzer1pkgid1", config.PluginKeyDifferentiator, "Unexpected repository differentiator");
            Assert.AreEqual("roslyn.analyzer1.pkgid1.cs", config.RepositoryKey, "Unexpected repository key");
            Assert.AreEqual("cs", config.RepositoryLanguage, "Unexpected language");
            Assert.AreEqual("dummy title", config.RepositoryName, "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("analyzer1.pkgid1.cs.analyzerId", "Analyzer1.Pkgid1", config);
            AssertExpectedPropertyDefinitionValue("analyzer1.pkgid1.cs.ruleNamespace", "Analyzer1.Pkgid1", config);
            AssertExpectedPropertyDefinitionValue("analyzer1.pkgid1.cs.nuget.packageId", "Analyzer1.Pkgid1", config);
            AssertExpectedPropertyDefinitionValue("analyzer1.pkgid1.cs.nuget.packageVersion", "1.0.2", config);
            AssertExpectedPropertyDefinitionValue("analyzer1.pkgid1.cs.staticResourceName", "Analyzer1.Pkgid1.1.0.2.zip", config);
            AssertExpectedPropertyDefinitionValue("analyzer1.pkgid1.cs.pluginKey", "analyzer1pkgid1", config);
            AssertExpectedPropertyDefinitionValue("analyzer1.pkgid1.cs.pluginVersion", "1.0.2", config);

            // Check the contents of the manifest
            string actualManifestFilePath = jarChecker.AssertFileExists("META-INF\\MANIFEST.MF");
            string[] actualManifest = File.ReadAllLines(actualManifestFilePath);
            AssertExpectedManifestValue(WellKnownPluginProperties.Key, "analyzer1pkgid1", actualManifest);
            AssertExpectedManifestValue("Plugin-Key", "analyzer1pkgid1", actualManifest); // plugin-key should be lowercase and alphanumeric
            AssertPackagePropertiesInManifest(analyzerPkg, actualManifest);


            // 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\\analyzer1.pkgid1.1.0.2.zip",
                /* zip file contents */
                "analyzers\\RoslynAnalyzer11.dll");
        }