diff --git a/src/Microsoft.AspNetCore.Razor.Evolution/DefaultRazorDesignTimeCSharpLoweringPhase.cs b/src/Microsoft.AspNetCore.Razor.Evolution/DefaultRazorDesignTimeCSharpLoweringPhase.cs index 37ea4e4135..aba0563eb2 100644 --- a/src/Microsoft.AspNetCore.Razor.Evolution/DefaultRazorDesignTimeCSharpLoweringPhase.cs +++ b/src/Microsoft.AspNetCore.Razor.Evolution/DefaultRazorDesignTimeCSharpLoweringPhase.cs @@ -10,6 +10,8 @@ namespace Microsoft.AspNetCore.Razor.Evolution { internal class DefaultRazorDesignTimeCSharpLoweringPhase : RazorCSharpLoweringPhaseBase { + internal static readonly object NewLineString = "NewLineString"; + protected override void ExecuteCore(RazorCodeDocument codeDocument) { var irDocument = codeDocument.GetIRDocument(); @@ -18,9 +20,17 @@ namespace Microsoft.AspNetCore.Razor.Evolution var syntaxTree = codeDocument.GetSyntaxTree(); ThrowForMissingDependency(syntaxTree); + var codeWriter = new CSharpCodeWriter(); + var newLineString = codeDocument.Items[NewLineString]; + if (newLineString != null) + { + // Set new line character to a specific string regardless of platform, for testing purposes. + codeWriter.NewLine = (string)newLineString; + } + var renderingContext = new CSharpRenderingContext() { - Writer = new CSharpCodeWriter(), + Writer = codeWriter, SourceDocument = codeDocument.Source, Options = syntaxTree.Options, }; diff --git a/test/Microsoft.AspNetCore.Razor.Evolution.Test/IntegrationTests/CodeGenerationIntegrationTest.cs b/test/Microsoft.AspNetCore.Razor.Evolution.Test/IntegrationTests/CodeGenerationIntegrationTest.cs index 66bb978915..95bcbb5768 100644 --- a/test/Microsoft.AspNetCore.Razor.Evolution.Test/IntegrationTests/CodeGenerationIntegrationTest.cs +++ b/test/Microsoft.AspNetCore.Razor.Evolution.Test/IntegrationTests/CodeGenerationIntegrationTest.cs @@ -1407,10 +1407,15 @@ namespace Microsoft.AspNetCore.Razor.Evolution.IntegrationTests throw new XunitException($"The resource {sourceFilename} was not found."); } - var codeDocument = RazorCodeDocument.Create(TestRazorSourceDocument.CreateResource(sourceFilename)); + var codeDocument = RazorCodeDocument.Create( + TestRazorSourceDocument.CreateResource(sourceFilename, encoding: null, normalizeNewLines: true)); // This will ensure that we're not putting any randomly generated data in a baseline. codeDocument.Items[DefaultRazorRuntimeCSharpLoweringPhase.SuppressUniqueIds] = "test"; + + // This is to make tests work cross platform. + codeDocument.Items[DefaultRazorDesignTimeCSharpLoweringPhase.NewLineString] = "\r\n"; + return codeDocument; } diff --git a/test/Microsoft.AspNetCore.Razor.Evolution.Test/IntegrationTests/IntegrationTestBase.cs b/test/Microsoft.AspNetCore.Razor.Evolution.Test/IntegrationTests/IntegrationTestBase.cs index cc0549649a..70fe15188d 100644 --- a/test/Microsoft.AspNetCore.Razor.Evolution.Test/IntegrationTests/IntegrationTestBase.cs +++ b/test/Microsoft.AspNetCore.Razor.Evolution.Test/IntegrationTests/IntegrationTestBase.cs @@ -97,10 +97,12 @@ namespace Microsoft.AspNetCore.Razor.Evolution.IntegrationTests break; } - imports.Add(TestRazorSourceDocument.CreateResource(importsFilename)); + imports.Add( + TestRazorSourceDocument.CreateResource(importsFilename, encoding: null, normalizeNewLines: true)); } - var codeDocument = RazorCodeDocument.Create(TestRazorSourceDocument.CreateResource(sourceFilename), imports); + var codeDocument = RazorCodeDocument.Create( + TestRazorSourceDocument.CreateResource(sourceFilename, encoding: null, normalizeNewLines: true), imports); // This will ensure that we're not putting any randomly generated data in a baseline. codeDocument.Items[DefaultRazorRuntimeCSharpLoweringPhase.SuppressUniqueIds] = "test"; @@ -109,11 +111,6 @@ namespace Microsoft.AspNetCore.Razor.Evolution.IntegrationTests protected void AssertIRMatchesBaseline(DocumentIRNode document) { - if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) - { - return; - } - if (Filename == null) { var message = $"{nameof(AssertIRMatchesBaseline)} should only be called from an integration test ({nameof(Filename)} is null)."; @@ -141,11 +138,6 @@ namespace Microsoft.AspNetCore.Razor.Evolution.IntegrationTests protected void AssertCSharpDocumentMatchesBaseline(RazorCSharpDocument document) { - if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) - { - return; - } - if (Filename == null) { var message = $"{nameof(AssertCSharpDocumentMatchesBaseline)} should only be called from an integration test ({nameof(Filename)} is null)."; @@ -177,11 +169,6 @@ namespace Microsoft.AspNetCore.Razor.Evolution.IntegrationTests protected void AssertDesignTimeDocumentMatchBaseline(RazorCodeDocument document) { - if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) - { - return; - } - if (Filename == null) { var message = $"{nameof(AssertDesignTimeDocumentMatchBaseline)} should only be called from an integration test ({nameof(Filename)} is null)."; diff --git a/test/Microsoft.AspNetCore.Razor.Evolution.Test/TestRazorSourceDocument.cs b/test/Microsoft.AspNetCore.Razor.Evolution.Test/TestRazorSourceDocument.cs index 37291e9c91..99cf6529ec 100644 --- a/test/Microsoft.AspNetCore.Razor.Evolution.Test/TestRazorSourceDocument.cs +++ b/test/Microsoft.AspNetCore.Razor.Evolution.Test/TestRazorSourceDocument.cs @@ -3,7 +3,9 @@ using System.IO; using System.Text; -using Microsoft.AspNetCore.Razor.Evolution.Legacy; +using System.Runtime.InteropServices; +using System.Text.RegularExpressions; +using System; namespace Microsoft.AspNetCore.Razor.Evolution { @@ -14,7 +16,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution { } - public static RazorSourceDocument CreateResource(string path, Encoding encoding = null) + public static RazorSourceDocument CreateResource(string path, Encoding encoding = null, bool normalizeNewLines = false) { var file = TestFile.Create(path); @@ -22,17 +24,26 @@ namespace Microsoft.AspNetCore.Razor.Evolution using (var reader = new StreamReader(input)) { var content = reader.ReadToEnd(); + if (normalizeNewLines) + { + content = NormalizeNewLines(content); + } return new TestRazorSourceDocument(content, encoding ?? Encoding.UTF8, path); } } - public static MemoryStream CreateStreamContent(string content = "Hello, World!", Encoding encoding = null) + public static MemoryStream CreateStreamContent(string content = "Hello, World!", Encoding encoding = null, bool normalizeNewLines = false) { var stream = new MemoryStream(); encoding = encoding ?? Encoding.UTF8; using (var writer = new StreamWriter(stream, encoding, bufferSize: 1024, leaveOpen: true)) { + if (normalizeNewLines) + { + content = NormalizeNewLines(content); + } + writer.Write(content); } @@ -41,9 +52,19 @@ namespace Microsoft.AspNetCore.Razor.Evolution return stream; } - public static RazorSourceDocument Create(string content = "Hello, world!", Encoding encoding = null) + public static RazorSourceDocument Create(string content = "Hello, world!", Encoding encoding = null, bool normalizeNewLines = false) { + if (normalizeNewLines) + { + content = NormalizeNewLines(content); + } + return new TestRazorSourceDocument(content, encoding ?? Encoding.UTF8, "test.cshtml"); } + + private static string NormalizeNewLines(string content) + { + return Regex.Replace(content, "(?