diff --git a/test/Microsoft.TemplateEngine.Core.UnitTests/ConditionalTests.CStyleEvaluator.cs b/test/Microsoft.TemplateEngine.Core.UnitTests/ConditionalTests.CStyleEvaluator.cs index 5bceb15c582..08106b60d01 100644 --- a/test/Microsoft.TemplateEngine.Core.UnitTests/ConditionalTests.CStyleEvaluator.cs +++ b/test/Microsoft.TemplateEngine.Core.UnitTests/ConditionalTests.CStyleEvaluator.cs @@ -1532,7 +1532,32 @@ public void VerifyConditionAtEnd() IProcessor processor = SetupCStyleNoCommentsProcessor(vc); //Changes should be made - bool changed = processor.Run(input, output, 28); + bool changed = processor.Run(input, output, 50); + Verify(Encoding.UTF8, output, changed, value, expected); + } + +#pragma warning disable xUnit1004 // Test methods should not be skipped + [Fact(Skip = "https://github.com/dotnet/templating/issues/4988")] +#pragma warning restore xUnit1004 // Test methods should not be skipped + public void VerifyXMLConditionAtEnd() + { + string value = @"Hello + +"; + string expected = @"Hello +"; + + byte[] valueBytes = Encoding.UTF8.GetBytes(value); + MemoryStream input = new MemoryStream(valueBytes); + MemoryStream output = new MemoryStream(); + + VariableCollection vc = new VariableCollection(); + IProcessor processor = SetupXmlStyleProcessor(vc); + + //Changes should be made + bool changed = processor.Run(input, output, 50); Verify(Encoding.UTF8, output, changed, value, expected); } diff --git a/test/Microsoft.TemplateEngine.Orchestrator.RunnableProjects.UnitTests/RunnableProjectGeneratorTests.cs b/test/Microsoft.TemplateEngine.Orchestrator.RunnableProjects.UnitTests/RunnableProjectGeneratorTests.cs index d18b1e842f3..ca535b706b1 100644 --- a/test/Microsoft.TemplateEngine.Orchestrator.RunnableProjects.UnitTests/RunnableProjectGeneratorTests.cs +++ b/test/Microsoft.TemplateEngine.Orchestrator.RunnableProjects.UnitTests/RunnableProjectGeneratorTests.cs @@ -10,6 +10,7 @@ using System.Threading; using System.Threading.Tasks; using FakeItEasy; +using Microsoft.Extensions.Logging; using Microsoft.TemplateEngine.Abstractions; using Microsoft.TemplateEngine.Abstractions.Components; using Microsoft.TemplateEngine.Abstractions.Mount; @@ -20,6 +21,7 @@ using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Xunit; +using Xunit.Abstractions; using static Microsoft.TemplateEngine.Orchestrator.RunnableProjects.RunnableProjectGenerator; namespace Microsoft.TemplateEngine.Orchestrator.RunnableProjects.UnitTests @@ -579,5 +581,171 @@ public async void CreateAsyncTest_MultiChoiceParamJoining() string resultContent = environment.Host.FileSystem.ReadAllText(Path.Combine(targetDir, "sourcFile")); Assert.Equal(expectedSnippet, resultContent); } + +#pragma warning disable xUnit1004 // Test methods should not be skipped + [Fact(Skip = "https://github.com/dotnet/templating/issues/4988")] +#pragma warning restore xUnit1004 // Test methods should not be skipped + public async void XMLConditionFailure() + { + // + // Template content preparation + // + + var templateConfig = new + { + identity = "test.template", + symbols = new + { + A = new + { + type = "parameter", + dataType = "bool", + defaultValue = "false" + }, + B = new + { + type = "parameter", + dataType = "bool", + defaultValue = "false" + }, + } + }; + + string sourceSnippet = @" + + +foo + + +This text should not be generated, just to make file content longer to prove the bug. +If the buffer is advanced when evaluating condition, the bug won't be reproduced. +This text ensures that buffer is long enough even considering very-very-long env variable names available on CI machine. + +"; + + string expectedSnippet = @" + +foo +"; + + IDictionary templateSourceFiles = new Dictionary(); + // template.json + templateSourceFiles.Add(TestFileSystemHelper.DefaultConfigRelativePath, JsonConvert.SerializeObject(templateConfig, Formatting.Indented)); + + //content + templateSourceFiles.Add("sourceFile.md", sourceSnippet); + + // + // Dependencies preparation and mounting + // + IEngineEnvironmentSettings settings = _environmentSettingsHelper.CreateEnvironment(virtualize: true); + string sourceBasePath = FileSystemHelpers.GetNewVirtualizedPath(settings); + string targetDir = FileSystemHelpers.GetNewVirtualizedPath(settings); + + TestFileSystemHelper.WriteTemplateSource(settings, sourceBasePath, templateSourceFiles); + IMountPoint? sourceMountPoint = TestFileSystemHelper.CreateMountPoint(settings, sourceBasePath); + RunnableProjectGenerator rpg = new RunnableProjectGenerator(); + SimpleConfigModel configModel = SimpleConfigModel.FromJObject(JObject.FromObject(templateConfig)); + IRunnableProjectConfig runnableConfig = new RunnableProjectConfig(settings, rpg, configModel, sourceMountPoint.FileInfo(TestFileSystemHelper.DefaultConfigRelativePath)); + IParameterSet parameters = new ParameterSet(runnableConfig); + parameters.TryGetParameterDefinition("A", out ITemplateParameter aParam); + parameters.ResolvedValues[aParam] = true; + IDirectory sourceDir = sourceMountPoint!.DirectoryInfo("/")!; + + // + // Running the actual scenario: template files processing and generating output (including macros processing) + // + await rpg.CreateAsync(settings, runnableConfig, sourceDir, parameters, targetDir, CancellationToken.None); + + // + // Veryfying the outputs + // + + string resultContent = settings.Host.FileSystem.ReadAllText(Path.Combine(targetDir, "sourceFile.md")); + Assert.Equal(expectedSnippet, resultContent); + } + +#pragma warning disable xUnit1004 // Test methods should not be skipped + [Fact(Skip = "https://github.com/dotnet/templating/issues/4988")] +#pragma warning restore xUnit1004 // Test methods should not be skipped + public async void HashConditionFailure() + { + // + // Template content preparation + // + + var templateConfig = new + { + identity = "test.template", + symbols = new + { + A = new + { + type = "parameter", + dataType = "bool", + defaultValue = "false" + }, + B = new + { + type = "parameter", + dataType = "bool", + defaultValue = "false" + }, + } + }; + + string sourceSnippet = @" +#if (A) +# comment foo +foo +#endif +##if (B) +This text should not be generated, just to make file content longer to prove the bug. +If the buffer is advanced when evaluating condition, the bug won't be reproduced. +This text ensures that buffer is long enough even considering very-very-long env variable names available on CI machine. +#endif +"; + + string expectedSnippet = @" +# comment foo +foo +"; + + IDictionary templateSourceFiles = new Dictionary(); + // template.json + templateSourceFiles.Add(TestFileSystemHelper.DefaultConfigRelativePath, JsonConvert.SerializeObject(templateConfig, Formatting.Indented)); + + //content + templateSourceFiles.Add("sourceFile.yaml", sourceSnippet); + + // + // Dependencies preparation and mounting + // + IEngineEnvironmentSettings settings = _environmentSettingsHelper.CreateEnvironment(virtualize: true); + string sourceBasePath = FileSystemHelpers.GetNewVirtualizedPath(settings); + string targetDir = FileSystemHelpers.GetNewVirtualizedPath(settings); + + TestFileSystemHelper.WriteTemplateSource(settings, sourceBasePath, templateSourceFiles); + IMountPoint? sourceMountPoint = TestFileSystemHelper.CreateMountPoint(settings, sourceBasePath); + RunnableProjectGenerator rpg = new RunnableProjectGenerator(); + SimpleConfigModel configModel = SimpleConfigModel.FromJObject(JObject.FromObject(templateConfig)); + IRunnableProjectConfig runnableConfig = new RunnableProjectConfig(settings, rpg, configModel, sourceMountPoint.FileInfo(TestFileSystemHelper.DefaultConfigRelativePath)); + IParameterSet parameters = new ParameterSet(runnableConfig); + parameters.TryGetParameterDefinition("A", out ITemplateParameter aParam); + parameters.ResolvedValues[aParam] = true; + IDirectory sourceDir = sourceMountPoint!.DirectoryInfo("/")!; + + // + // Running the actual scenario: template files processing and generating output (including macros processing) + // + await rpg.CreateAsync(settings, runnableConfig, sourceDir, parameters, targetDir, CancellationToken.None); + + // + // Veryfying the outputs + // + + string resultContent = settings.Host.FileSystem.ReadAllText(Path.Combine(targetDir, "sourceFile.yaml")); + Assert.Equal(expectedSnippet, resultContent); + } } } diff --git a/test/Microsoft.TemplateEngine.TestHelper/TestHost.cs b/test/Microsoft.TemplateEngine.TestHelper/TestHost.cs index 63fd732888b..9faf8e50f73 100644 --- a/test/Microsoft.TemplateEngine.TestHelper/TestHost.cs +++ b/test/Microsoft.TemplateEngine.TestHelper/TestHost.cs @@ -53,7 +53,7 @@ internal TestHost( _loggerFactory = new TestLoggerFactory(); addLoggerProviders?.ToList().ForEach(_loggerFactory.AddProvider); - _logger = _loggerFactory.CreateLogger("Test Host"); + _logger = _loggerFactory.CreateLogger(hostIdentifier); _fallbackNames = fallbackNames ?? new[] { "dotnetcli" }; }