Enabled Razor.Evolution tests on linux

This commit is contained in:
Ajay Bhargav Baaskaran 2017-01-25 14:38:33 -08:00
parent 243446d225
commit 7725c20c47
4 changed files with 46 additions and 23 deletions

View File

@ -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,
};

View File

@ -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;
}

View File

@ -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).";

View File

@ -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, "(?<!\r)\n", "\r\n", RegexOptions.None, TimeSpan.FromSeconds(10));
}
}
}