public async Task GenerateReadmesCommand_MissingTemplate()
        {
            using TempFolderContext tempFolderContext = TestHelper.UseTempFolder();
            GenerateReadmesCommand command = InitializeCommand(tempFolderContext, null, allowOptionalTemplates: false);

            await Assert.ThrowsAsync <InvalidOperationException>(command.ExecuteAsync);
        }
        public void GenerateReadmesCommand_SupportedSymbols(string symbol, string expectedValue, bool isManifest = false)
        {
            using TempFolderContext tempFolderContext = TestHelper.UseTempFolder();
            GenerateReadmesCommand command = InitializeCommand(tempFolderContext);

            IReadOnlyDictionary <Value, Value> symbols;

            if (isManifest)
            {
                symbols = command.GetSymbols(command.Manifest);
            }
            else
            {
                symbols = command.GetSymbols(command.Manifest.GetRepoByModelName("dotnet/repo"));
            }

            Value actualSymbolValue = symbols[symbol];
            Value expectedSymbolValue;

            if (actualSymbolValue.Type == ValueContent.Boolean)
            {
                expectedSymbolValue = Value.FromBoolean(bool.Parse(expectedValue));
            }
            else
            {
                expectedSymbolValue = Value.FromString(expectedValue);
            }

            Assert.Equal(expectedSymbolValue, actualSymbolValue);
        }
        public async Task GenerateReadmesCommand_Validate_UpToDate()
        {
            using TempFolderContext tempFolderContext = TestHelper.UseTempFolder();
            GenerateReadmesCommand command = InitializeCommand(
                tempFolderContext, productFamilyReadme: ExpectedProductFamilyReadme, repoReadme: ExpectedRepoReadme, validate: true);

            await command.ExecuteAsync();

            _environmentServiceMock.Verify(o => o.Exit(It.IsAny <int>()), Times.Never);
        }
        public async Task GenerateReadmesCommand_InvalidTemplate()
        {
            string template = "about{{if:}}";

            using TempFolderContext tempFolderContext = TestHelper.UseTempFolder();
            GenerateReadmesCommand command = InitializeCommand(tempFolderContext, template);

            Exception actualException = await Assert.ThrowsAsync <Exception>(command.ExecuteAsync);

            Assert.Same(_exitException, actualException);
            _environmentServiceMock.Verify(o => o.Exit(It.IsAny <int>()), Times.Once);
        }
        private GenerateReadmesCommand InitializeCommand(
            TempFolderContext tempFolderContext,
            string readmeTemplate       = ReadmeTemplate,
            string productFamilyReadme  = DefaultReadme,
            string repoReadme           = DefaultReadme,
            bool allowOptionalTemplates = true,
            bool validate = false)
        {
            DockerfileHelper.CreateFile(ProductFamilyReadmePath, tempFolderContext, productFamilyReadme);
            DockerfileHelper.CreateFile(RepoReadmePath, tempFolderContext, repoReadme);

            DockerfileHelper.CreateFile(AboutRepoTemplatePath, tempFolderContext, AboutRepoTemplate);

            string templatePath = null;

            if (readmeTemplate != null)
            {
                DockerfileHelper.CreateFile(ReadmeTemplatePath, tempFolderContext, readmeTemplate);
                templatePath = ReadmeTemplatePath;
            }

            Repo repo = CreateRepo("dotnet/repo");

            repo.Readmes = new[]
            {
                new Readme(RepoReadmePath, templatePath)
            };
            Manifest manifest = CreateManifest(repo);

            manifest.Registry = "mcr.microsoft.com";
            manifest.Readme   = new(ProductFamilyReadmePath, templatePath);

            string manifestPath = Path.Combine(tempFolderContext.Path, "manifest.json");

            File.WriteAllText(manifestPath, JsonConvert.SerializeObject(manifest));

            Mock <IGitService> gitServiceMock = new Mock <IGitService>();

            _environmentServiceMock = new Mock <IEnvironmentService>();
            _environmentServiceMock
            .Setup(o => o.Exit(1))
            .Throws(_exitException);

            GenerateReadmesCommand command = new GenerateReadmesCommand(_environmentServiceMock.Object, gitServiceMock.Object);

            command.Options.Manifest = manifestPath;
            command.Options.AllowOptionalTemplates = allowOptionalTemplates;
            command.Options.Validate = validate;
            command.LoadManifest();

            return(command);
        }
        public async Task GenerateReadmesCommand_Validate_OutOfSync()
        {
            using TempFolderContext tempFolderContext = TestHelper.UseTempFolder();
            GenerateReadmesCommand command = InitializeCommand(tempFolderContext, validate: true);

            Exception actualException = await Assert.ThrowsAsync <Exception>(command.ExecuteAsync);

            Assert.Same(_exitException, actualException);
            _environmentServiceMock.Verify(o => o.Exit(It.IsAny <int>()), Times.Once);
            // Validate readmes remain unmodified
            Assert.Equal(DefaultReadme, File.ReadAllText(Path.Combine(tempFolderContext.Path, ProductFamilyReadmePath)));
            Assert.Equal(DefaultReadme, File.ReadAllText(Path.Combine(tempFolderContext.Path, RepoReadmePath)));
        }
        public async Task GenerateReadmesCommand_Canonical()
        {
            using TempFolderContext tempFolderContext = TestHelper.UseTempFolder();
            GenerateReadmesCommand command = InitializeCommand(tempFolderContext);

            await command.ExecuteAsync();

            string generatedReadme = File.ReadAllText(Path.Combine(tempFolderContext.Path, ProductFamilyReadmePath));

            Assert.Equal(ExpectedProductFamilyReadme.NormalizeLineEndings(generatedReadme), generatedReadme);

            generatedReadme = File.ReadAllText(Path.Combine(tempFolderContext.Path, RepoReadmePath));
            Assert.Equal(ExpectedRepoReadme.NormalizeLineEndings(generatedReadme), generatedReadme);
        }
        public async Task GenerateReadmesCommand_TemplateArgs()
        {
            const string readmeTemplate =
                @"Hello World
{{InsertTemplate(""template-with-args.md"", [ ""my-arg"": 123 ])}}";

            const string templateWithArgs =
                @"ABC-{{ARGS[""my-arg""]}}";

            using TempFolderContext tempFolderContext = TestHelper.UseTempFolder();
            DockerfileHelper.CreateFile("template-with-args.md", tempFolderContext, templateWithArgs);
            GenerateReadmesCommand command = InitializeCommand(tempFolderContext, readmeTemplate);

            await command.ExecuteAsync();

            string generatedReadme = File.ReadAllText(Path.Combine(tempFolderContext.Path, ProductFamilyReadmePath));
            string expectedReadme  =
                @"Hello World
ABC-123";

            Assert.Equal(expectedReadme, generatedReadme);
        }