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

            var logger = new TestLogger();

            var remoteRepoBuilder = new RemoteRepoBuilder(this.TestContext);

            CreatePackageInFakeRemoteRepo(remoteRepoBuilder, "dummy.id", "1.1");

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

            // Create an invalid rule file
            var dummyRuleFilePath = Path.Combine(outputDir, "invalidRule.xml");

            File.WriteAllText(dummyRuleFilePath, "not valid xml");

            AnalyzerPluginGenerator apg = new AnalyzerPluginGenerator(nuGetHandler, logger);

            var args = new ProcessedArgsBuilder("dummy.id", outputDir)
                       .SetLanguage("cs")
                       .SetPackageVersion("1.1")
                       .SetRuleFilePath(dummyRuleFilePath)
                       .Build();

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

            // Assert
            result.Should().BeFalse();
            AssertRuleTemplateDoesNotExist(outputDir);
            logger.AssertSingleErrorExists("invalidRule.xml"); // expecting an error containing the invalid rule file name
        }
        public void Generate_ValidRuleFileSpecified_TemplateFileNotCreated()
        {
            // Arrange
            var outputDir = TestUtils.CreateTestDirectory(this.TestContext, ".out");

            var remoteRepoBuilder = new RemoteRepoBuilder(this.TestContext);
            var apg = CreateTestSubjectWithFakeRemoteRepo(remoteRepoBuilder);

            CreatePackageInFakeRemoteRepo(remoteRepoBuilder, "dummy.id", "1.1");

            // Create a dummy rule file
            var dummyRuleFilePath = Path.Combine(outputDir, "inputRule.xml");

            Serializer.SaveModel(new Rules(), dummyRuleFilePath);

            var args = new ProcessedArgsBuilder("dummy.id", outputDir)
                       .SetLanguage("cs")
                       .SetPackageVersion("1.1")
                       .SetRuleFilePath(dummyRuleFilePath)
                       .Build();

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

            // Assert
            result.Should().BeTrue();
            AssertRuleTemplateDoesNotExist(outputDir);
        }
        public void RepoFactory_CreateRepositoryForArguments_CustomNuGetRepo_Overwrites_Default()
        {
            var settings = new ProcessedArgsBuilder("SomePackage", "SomeoutDir")
                           .SetCustomNuGetRepository("file:///customrepo/path")
                           .SetPackageVersion("0.0.1")
                           .SetLanguage("cs")
                           .Build();
            var logger = new TestLogger();

            var repo = NuGetRepositoryFactory.CreateRepositoryForArguments(logger, settings, "c:\\dummy\\config\\");

            repo.Should().BeOfType <LazyLocalPackageRepository>();
            repo.Source.Should().Be("/customrepo/path");
            logger.AssertErrorsLogged(0);
            logger.AssertWarningsLogged(0);
        }
        public void Generate_RulesFileNotSpecified_TemplateFileCreated()
        {
            // Arrange
            string outputDir = TestUtils.CreateTestDirectory(this.TestContext, ".out");

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

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

            var testSubject = new AnalyzerPluginGenerator(nuGetHandler, logger);

            // 1. Generate a plugin for the target package only. Expecting a plugin and a template rule file.
            var args = new ProcessedArgsBuilder("parent.id", outputDir)
                       .SetLanguage("cs")
                       .SetPackageVersion("1.0")
                       .SetRecurseDependencies(true)
                       .Build();
            bool result = testSubject.Generate(args);

            result.Should().BeTrue();

            AssertRuleTemplateFileExistsForPackage(logger, outputDir, parent);

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

            logger.AssertSingleWarningExists(UIResources.APG_RecurseEnabled_RuleCustomizationNotEnabled);
            AssertRuleTemplateFileExistsForPackage(logger, outputDir, parent);
            AssertRuleTemplateFileExistsForPackage(logger, outputDir, child1);
            AssertRuleTemplateFileExistsForPackage(logger, outputDir, child2);
        }
        public void RepoFactory_CreateRepositoryForArguments_NoCustomNuGetRepo_DefaultUsed()
        {
            // Create a valid config settings file that specifies the package sources to use
            var configXml          = @"<?xml version=""1.0"" encoding=""utf-8""?>
<configuration>
  <packageSources>
    <add key=""local1"" value=""d:\cache1"" />
    <add key=""local2"" value=""c:\cache2"" />
  </packageSources>
  <disabledPackageSources>
    <add key=""local1_inactive"" value=""true"" />
  </disabledPackageSources>
</configuration>";
            var fullConfigFilePath = WriteConfigFile(configXml);

            var settings = new ProcessedArgsBuilder("SomePackage", "SomeoutDir")
                           .SetPackageVersion("0.0.1")
                           .SetLanguage("cs")
                           .Build();

            var logger = new TestLogger();

            var actualRepo = NuGetRepositoryFactory.CreateRepositoryForArguments(logger, settings,
                                                                                 Path.GetDirectoryName(fullConfigFilePath));

            actualRepo.Should().BeOfType <AggregateRepository>();
            AggregateRepository actualAggregateRepo = (AggregateRepository)actualRepo;

            // There might be other machine-level nuget.config settings that have been picked,
            // so we'll only check that the known package sources above were found
            AssertExpectedPackageSourcesExist(actualAggregateRepo,
                                              "d:\\cache1",
                                              "c:\\cache2");

            logger.AssertErrorsLogged(0);
            logger.AssertWarningsLogged(0);
        }