public void Generate_NoAnalyzersFoundInPackage_GenerateFails()
        {
            // Arrange
            string outputDir = TestUtils.CreateTestDirectory(this.TestContext, ".out");

            TestLogger logger = new TestLogger();

            // Create a fake remote repo containing a package that does not contain analyzers
            RemoteRepoBuilder remoteRepoBuilder = new RemoteRepoBuilder(this.TestContext);
            remoteRepoBuilder.CreatePackage("no.analyzers.id", "0.9", TestUtils.CreateTextFile("dummy.txt", outputDir), License.NotRequired /* no dependencies */ );

            NuGetPackageHandler nuGetHandler = new NuGetPackageHandler(remoteRepoBuilder.FakeRemoteRepo, GetLocalNuGetDownloadDir(), logger);
            AnalyzerPluginGenerator apg = new AnalyzerPluginGenerator(nuGetHandler, logger);
            ProcessedArgs args = CreateArgs("no.analyzers.id", "0.9", "cs", null, false, false, outputDir);

            // Act
            bool result = apg.Generate(args);

            // Assert
            Assert.IsFalse(result, "Expecting generation to fail");
            logger.AssertSingleWarningExists(String.Format(UIResources.APG_NoAnalyzersFound, "no.analyzers.id"));
            logger.AssertSingleWarningExists(UIResources.APG_NoAnalyzersInTargetSuggestRecurse);
            logger.AssertWarningsLogged(2);
            AssertSqaleTemplateDoesNotExist(outputDir);
        }
        public void NuGet_TestDependencyResolutionFailure()
        {
            // Arrange
            string testDir = TestUtils.CreateTestDirectory(this.TestContext);
            TestLogger logger = new TestLogger();
            NuGetPackageHandler handler = new NuGetPackageHandler(logger);

            // Act
            // Fetch a package that should fail due to pre-release dependencies
            IPackage package = handler.FetchPackage(AnalyzerPluginGenerator.NuGetPackageSource, "codeCracker", null, testDir);

            // Assert
            // No files should have been downloaded
            Assert.IsTrue(Directory.GetFiles(testDir).Length == 0);
            Assert.IsNull(package);
        }
        public void NuGet_TestPackageDownload_PreRelease_Release()
        {
            // Arrange
            string targetNuGetRoot = TestUtils.CreateTestDirectory(this.TestContext, ".nuget.target");

            // Create test NuGet payload and packages
            IPackageRepository fakeRemoteRepo = CreateTestPackageWithSingleDependency(PreReleaseVersion, ReleaseVersion);

            TestLogger logger = new TestLogger();
            NuGetPackageHandler handler = new NuGetPackageHandler(fakeRemoteRepo, targetNuGetRoot, logger);

            // Act
            // Attempt to download a package which is not released with a dependency that is released
            IPackage package = handler.FetchPackage(DependentPackageId, null);

            // Assert
            AssertExpectedPackage(package, DependentPackageId, PreReleaseVersion);
            // Packages should have been downloaded
            AssertPackageDownloaded(targetNuGetRoot, DependentPackageId, PreReleaseVersion);
            AssertPackageDownloaded(targetNuGetRoot, TestPackageId, ReleaseVersion);
        }
        public void NuGet_TestPackageDownload_PreRelease_Release()
        {
            // Arrange
            string testDir = TestUtils.CreateTestDirectory(this.TestContext);
            string testDownloadDir = Path.Combine(testDir, "download");

            // Create test NuGet payload and packages
            BuildTestPackages(false, true);

            TestLogger logger = new TestLogger();
            NuGetPackageHandler handler = new NuGetPackageHandler(testDir, logger);

            // Act
            // Attempt to download a package which is not released with a dependency that is released
            IPackage package = handler.FetchPackage(DependentPackageName, null, testDownloadDir);

            // Assert
            AssertExpectedPackage(package, DependentPackageName, PreReleaseVersion);
            // Packages should have been downloaded
            AssertPackageDownloaded(testDownloadDir, DependentPackageName, PreReleaseVersion);
            AssertPackageDownloaded(testDownloadDir, TestPackageName, ReleaseVersion);
        }
        public void RoslynPlugin_GenerateForDependencyAnalyzers_Succeeds()
        {
            // Arrange
            TestLogger logger           = new TestLogger();
            string     outputDir        = TestUtils.CreateTestDirectory(TestContext, ".out");
            string     dummyContentFile = TestUtils.CreateTextFile("dummy.txt", outputDir, "non-analyzer content file");

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

            string          fakeRemoteNuGetDir = TestUtils.CreateTestDirectory(TestContext, ".fakeRemoteNuGet");
            IPackageManager fakeRemotePkgMgr   = CreatePackageManager(fakeRemoteNuGetDir);
            //IPackage child1 = AddPackage(fakeRemotePkgMgr, "Analyzer.Child1", "1.1.0", analyzer.GetType().Assembly.Location);
            //IPackage child2 = AddPackage(fakeRemotePkgMgr, "Analyzer.Child2", "1.2.0", analyzer.GetType().Assembly.Location);
            IPackage childV34  = AddPackage(fakeRemotePkgMgr, "Analyzer.Child34", "3.4.0", analyzer.GetType().Assembly.Location);
            IPackage targetPkg = AddPackage(fakeRemotePkgMgr, "Empty.Parent", "1.0.0", dummyContentFile, /*child1, child2,*/ childV34);

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

            // Act
            NuGetPackageHandler     nuGetHandler = new NuGetPackageHandler(fakeRemotePkgMgr.LocalRepository, localPackageDestination, logger);
            AnalyzerPluginGenerator apg          = new AnalyzerPluginGenerator(nuGetHandler, logger);
            ProcessedArgs           args         = new ProcessedArgs(targetPkg.Id, targetPkg.Version, "cs", null, false,
                                                                     true /* generate plugins for dependencies with analyzers*/, outputDir, null);
            bool result = apg.Generate(args);

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

            // Expecting one plugin per dependency with analyzers

            /*CheckJarGeneratedForPackage(outputDir, analyzer, child1);
             * CheckJarGeneratedForPackage(outputDir, analyzer, child2);
             */
            CheckJarGeneratedForPackage(outputDir, analyzer, childV34);
            AssertJarsGenerated(outputDir, 2);
        }
 private AnalyzerPluginGenerator CreateTestSubjectWithFakeRemoteRepo(RemoteRepoBuilder remoteRepoBuilder, TestLogger logger)
 {
     NuGetPackageHandler nuGetHandler = new NuGetPackageHandler(remoteRepoBuilder.FakeRemoteRepo, GetLocalNuGetDownloadDir(), logger);
     return new AnalyzerPluginGenerator(nuGetHandler, logger);
 }
        public void Generate_InvalidSqaleFileSpecified_GeneratorError()
        {
            // Arrange
            string outputDir = TestUtils.CreateTestDirectory(this.TestContext, ".out");

            TestLogger logger = new TestLogger();

            RemoteRepoBuilder remoteRepoBuilder = new RemoteRepoBuilder(this.TestContext);
            CreatePackageInFakeRemoteRepo(remoteRepoBuilder, "dummy.id", "1.1");

            NuGetPackageHandler nuGetHandler = new NuGetPackageHandler(remoteRepoBuilder.FakeRemoteRepo, GetLocalNuGetDownloadDir(), logger);

            // Create an invalid sqale file
            string dummySqaleFilePath = Path.Combine(outputDir, "invalidSqale.xml");
            File.WriteAllText(dummySqaleFilePath, "not valid xml");

            AnalyzerPluginGenerator apg = new AnalyzerPluginGenerator(nuGetHandler, logger);

            ProcessedArgs args = CreateArgs("dummy.id", "1.1", "cs", dummySqaleFilePath, false, outputDir);

            // Act
            bool result = apg.Generate(args);

            // Assert
            Assert.IsFalse(result, "Expecting generation to have failed");
            AssertSqaleTemplateDoesNotExist(outputDir);
            logger.AssertSingleErrorExists("invalidSqale.xml"); // expecting an error containing the invalid sqale file name
        }
        public void Generate_SqaleFileNotSpecified_TemplateFileCreated()
        {
            // Arrange
            string outputDir = TestUtils.CreateTestDirectory(this.TestContext, ".out");

            TestLogger logger = new TestLogger();

            RemoteRepoBuilder remoteRepoBuilder = new RemoteRepoBuilder(this.TestContext);
            CreatePackageInFakeRemoteRepo(remoteRepoBuilder, "dummy.id", "1.1");

            NuGetPackageHandler nuGetHandler = new NuGetPackageHandler(remoteRepoBuilder.FakeRemoteRepo, GetLocalNuGetDownloadDir(), logger);

            string expectedTemplateSqaleFilePath = Path.Combine(outputDir, "dummy.id.1.1.sqale.template.xml");

            AnalyzerPluginGenerator apg = new AnalyzerPluginGenerator(nuGetHandler, logger);

            ProcessedArgs args = CreateArgs("dummy.id", "1.1", "cs", null, false, outputDir);

            // Act
            bool result = apg.Generate(args);

            // Assert
            Assert.IsTrue(result, "Expecting generation to have succeeded");
            Assert.IsTrue(File.Exists(expectedTemplateSqaleFilePath), "Expecting a template sqale file to have been created");
            this.TestContext.AddResultFile(expectedTemplateSqaleFilePath);
            logger.AssertSingleInfoMessageExists(expectedTemplateSqaleFilePath); // should be a message about the generated file
        }
        private AnalyzerPluginGenerator CreateTestSubjectWithFakeRemoteRepo(RemoteRepoBuilder remoteRepoBuilder, TestLogger logger)
        {
            NuGetPackageHandler nuGetHandler = new NuGetPackageHandler(remoteRepoBuilder.FakeRemoteRepo, GetLocalNuGetDownloadDir(), logger);

            return(new AnalyzerPluginGenerator(nuGetHandler, logger));
        }
        public void Generate_SqaleFileNotSpecified_TemplateFileCreated()
        {
            // Arrange
            string outputDir = TestUtils.CreateTestDirectory(this.TestContext, ".out");

            TestLogger logger = new TestLogger();
            RemoteRepoBuilder remoteRepoBuilder = new RemoteRepoBuilder(this.TestContext);
            IPackage child1 = CreatePackageWithAnalyzer(remoteRepoBuilder, "child1.requiredAccept.id", "2.1", License.NotRequired);
            IPackage child2 = CreatePackageWithAnalyzer(remoteRepoBuilder, "child2.id", "2.2", License.NotRequired);
            IPackage parent = CreatePackageWithAnalyzer(remoteRepoBuilder, "parent.id", "1.0", License.NotRequired, child1, child2);

            NuGetPackageHandler nuGetHandler = new NuGetPackageHandler(remoteRepoBuilder.FakeRemoteRepo, GetLocalNuGetDownloadDir(), logger);
            
            AnalyzerPluginGenerator apg = new AnalyzerPluginGenerator(nuGetHandler, logger);

            // 1. Generate a plugin for the target package only. Expecting a plugin and a template SQALE file.
            ProcessedArgs args = CreateArgs("parent.id", "1.0", "cs", null, false, false, outputDir);
            bool result = apg.Generate(args);

            Assert.IsTrue(result, "Expecting generation to have succeeded");
            AssertSqaleFileExistsForPackage(logger, outputDir, parent);

            // 2. Generate a plugin for target package and all dependencies. Expecting three plugins and associated SQALE files.
            logger.Reset();
            args = CreateArgs("parent.id", "1.0", "cs", null, false, true /* /recurse = true */, outputDir);
            result = apg.Generate(args);

            Assert.IsTrue(result, "Expecting generation to have succeeded");
            logger.AssertSingleWarningExists(UIResources.APG_RecurseEnabled_SQALENotEnabled);
            AssertSqaleFileExistsForPackage(logger, outputDir, parent);
            AssertSqaleFileExistsForPackage(logger, outputDir, child1);
            AssertSqaleFileExistsForPackage(logger, outputDir, child2);

        }
        public void FetchPackage_PackageNotFound_NullReturned()
        {
            // Arrange
            string sourceNuGetRoot = TestUtils.CreateTestDirectory(this.TestContext, ".nuget.source");
            string targetNuGetRoot = TestUtils.CreateTestDirectory(this.TestContext, ".nuget.target");
            IPackageManager mgr = CreatePackageManager(sourceNuGetRoot);

            BuildAndInstallPackage(mgr, "package.id.1", "0.8.0");
            BuildAndInstallPackage(mgr, "package.id.1", "0.9.0");

            NuGetPackageHandler handler = new NuGetPackageHandler(sourceNuGetRoot, new TestLogger());

            // 1. Package id not found
            IPackage actual = handler.FetchPackage("unknown.package.id", new SemanticVersion("0.8.0"), targetNuGetRoot);
            Assert.IsNull(actual, "Not expecting a package to be found");

            // 2. Package id not found
            actual = handler.FetchPackage("package.id.1", new SemanticVersion("0.7.0"), targetNuGetRoot);
            Assert.IsNull(actual, "Not expecting a package to be found");
        }
        public void FetchPackage_VersionNotSpecified_NoReleaseVersions_LastPreReleaseVersionSelected()
        {
            // Arrange
            string sourceNuGetRoot = TestUtils.CreateTestDirectory(this.TestContext, ".nuget.source");
            string targetNuGetRoot = TestUtils.CreateTestDirectory(this.TestContext, ".nuget.target");
            IPackageManager mgr = CreatePackageManager(sourceNuGetRoot);

            BuildAndInstallPackage(mgr, "package.id.1", "0.9.0-rc1");
            BuildAndInstallPackage(mgr, "package.id.1", "1.0.0-rc1");
            BuildAndInstallPackage(mgr, "package.id.1", "1.1.0-rc1");
            BuildAndInstallPackage(mgr, "dummy.package.1", "2.0.0");
            BuildAndInstallPackage(mgr, "dummy.package.1", "2.0.0-rc2");

            NuGetPackageHandler handler = new NuGetPackageHandler(sourceNuGetRoot, new TestLogger());

            // Act
            IPackage actual = handler.FetchPackage("package.id.1", null, targetNuGetRoot);

            // Assert
            AssertExpectedPackage(actual, "package.id.1", "1.1.0-rc1");
        }
        public void FetchPackage_VersionSpecified_CorrectVersionSelected()
        {
            string sourceNuGetRoot = TestUtils.CreateTestDirectory(this.TestContext, ".nuget.source");
            string targetNuGetRoot = TestUtils.CreateTestDirectory(this.TestContext, ".nuget.target");
            IPackageManager mgr = CreatePackageManager(sourceNuGetRoot);

            BuildAndInstallPackage(mgr, "package.id.1", "0.8.0");
            BuildAndInstallPackage(mgr, "package.id.1", "1.0.0-rc1");
            BuildAndInstallPackage(mgr, "package.id.1", "2.0.0");

            BuildAndInstallPackage(mgr, "dummy.package.1", "0.8.0");

            BuildAndInstallPackage(mgr, "package.id.1", "0.9.0");
            BuildAndInstallPackage(mgr, "package.id.1", "1.0.0");

            NuGetPackageHandler handler = new NuGetPackageHandler(sourceNuGetRoot, new TestLogger());

            // Check for specific versions
            IPackage actual = handler.FetchPackage("package.id.1", new SemanticVersion("0.8.0"), targetNuGetRoot);
            AssertExpectedPackage(actual, "package.id.1", "0.8.0");

            actual = handler.FetchPackage("package.id.1", new SemanticVersion("1.0.0-rc1"), targetNuGetRoot);
            AssertExpectedPackage(actual, "package.id.1", "1.0.0-rc1");

            actual = handler.FetchPackage("package.id.1", new SemanticVersion("2.0.0"), targetNuGetRoot);
            AssertExpectedPackage(actual, "package.id.1", "2.0.0");
        }
        public void GetDependencies_DependenciesNotInstalledLocally_Warning()
        {
            // Arrange
            string targetNuGetRoot = TestUtils.CreateTestDirectory(this.TestContext, ".nuget.target");
            RemoteRepoBuilder remoteRepoBuilder = new RemoteRepoBuilder(this.TestContext);
            string dummyContentFile = CreateDummyContentFile();

            IEnumerable<IPackage> actualDependencies;

            // Build a dependency graph: "main" depends on "dependency"
            IPackage dependencyPackage = remoteRepoBuilder.CreatePackage("dependency.package.id", "1.2", dummyContentFile, RemoteRepoBuilder.License.Required /* no dependencies */);
            IPackage mainPackage = remoteRepoBuilder.CreatePackage("a", "2.0", dummyContentFile, RemoteRepoBuilder.License.NotRequired, dependencyPackage);


            // 1. Dependencies have not been installed locally -> warning but no error
            TestLogger logger = new TestLogger();
            NuGetPackageHandler testSubject = new NuGetPackageHandler(remoteRepoBuilder.FakeRemoteRepo, targetNuGetRoot, logger);
            actualDependencies = testSubject.GetInstalledDependencies(mainPackage);
            AssertExpectedPackageIds(actualDependencies /* no dependencies resolved*/);

            logger.AssertErrorsLogged(0);
            logger.AssertWarningsLogged(1);
            logger.AssertSingleWarningExists("dependency.package.id");


            // 2. Now install the package -> dependencies should resolve ok
            logger = new TestLogger();
            testSubject = new NuGetPackageHandler(remoteRepoBuilder.FakeRemoteRepo, targetNuGetRoot, logger);

            testSubject.FetchPackage(mainPackage.Id, mainPackage.Version);
            actualDependencies = testSubject.GetInstalledDependencies(mainPackage);
            AssertExpectedPackageIds(actualDependencies, "dependency.package.id");

            logger.AssertErrorsLogged(0);
            logger.AssertWarningsLogged(0);
        }
        public void GetDependencies_DependenciesInstalledLocally_Succeeds()
        {
            // Arrange
            string targetNuGetRoot = TestUtils.CreateTestDirectory(this.TestContext, ".nuget.target");
            RemoteRepoBuilder remoteRepoBuilder = new RemoteRepoBuilder(this.TestContext);
            string dummyContentFile = CreateDummyContentFile();

            // Build a dependency graph 
            // A depends on B
            // B depends on C, D, E
            // D also depends on E => duplicate dependency
            // E depends on F
            // -> expected [B, C, D, E, F]
            
            // Leaf nodes
            IPackage f = remoteRepoBuilder.CreatePackage("f", "1.0", dummyContentFile, RemoteRepoBuilder.License.NotRequired /* no dependencies */);
            IPackage c = remoteRepoBuilder.CreatePackage("c", "1.0", dummyContentFile, RemoteRepoBuilder.License.NotRequired /* no dependencies */);

            IPackage e = remoteRepoBuilder.CreatePackage("e", "1.0", dummyContentFile, RemoteRepoBuilder.License.Required, f);
            IPackage d = remoteRepoBuilder.CreatePackage("d", "1.1", dummyContentFile, RemoteRepoBuilder.License.Required, e);

            IPackage b = remoteRepoBuilder.CreatePackage("b", "1.2", dummyContentFile, RemoteRepoBuilder.License.Required, c, d, e);

            IPackage a = remoteRepoBuilder.CreatePackage("a", "2.0", dummyContentFile, RemoteRepoBuilder.License.NotRequired, b);

            NuGetPackageHandler testSubject = new NuGetPackageHandler(remoteRepoBuilder.FakeRemoteRepo, targetNuGetRoot, new TestLogger());

            // We assume that all of the packages have been installed locally we attempt to resolve the dependencies
            testSubject.FetchPackage("a", new SemanticVersion("2.0"));

            // 1. Package with no dependencies -> empty list
            IEnumerable<IPackage> actualDependencies = testSubject.GetInstalledDependencies(f);
            AssertExpectedPackageIds(actualDependencies /* none */);

            // 2. Package with only direct dependencies -> non-empty list
            actualDependencies = testSubject.GetInstalledDependencies(e);
            AssertExpectedPackageIds(actualDependencies, "f");

            // 3. Package with indirect dependencies -> non-empty list
            actualDependencies = testSubject.GetInstalledDependencies(a);
            AssertExpectedPackageIds(actualDependencies, "b", "c", "d", "e", "f");
        }
        public void FetchPackage_VersionNotSpecified_ReleaseVersionExists_LastReleaseVersionSelected()
        {
            // Arrange
            string targetNuGetRoot = TestUtils.CreateTestDirectory(this.TestContext, ".nuget.target");
            RemoteRepoBuilder remoteRepoBuilder = new RemoteRepoBuilder(this.TestContext);

            BuildAndInstallPackage(remoteRepoBuilder, "package.id.1", "0.8.0");
            BuildAndInstallPackage(remoteRepoBuilder, "package.id.1", "0.9.0-rc1");
            BuildAndInstallPackage(remoteRepoBuilder, "package.id.1", "1.0.0");
            BuildAndInstallPackage(remoteRepoBuilder, "package.id.1", "1.1.0-rc1");
            BuildAndInstallPackage(remoteRepoBuilder, "dummy.package.1", "2.0.0");

            NuGetPackageHandler handler = new NuGetPackageHandler(remoteRepoBuilder.FakeRemoteRepo, targetNuGetRoot, new TestLogger());

            // Act
            IPackage actual = handler.FetchPackage("package.id.1", null);

            // Assert
            AssertExpectedPackage(actual, "package.id.1", "1.0.0");
        }