Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Commit

Permalink
Get tets working with core.autocrlf=false
Browse files Browse the repository at this point in the history
- #1514
- refactor `RazorCompilationService` to allow a test subclass that normalizes Razor file line endings
 - add `TestRazorCompilationService` to `RazorPageExecutionInstrumentationWebSite`
 - adjust line endings to match in `RazorPageExecutionInstrumentationTest`
- add `ignoreLineEndingDifferences: true` to `Assert.Equal()` calls
 - responses on Windows can have a mix of line endings
  - `git config` setting affects line endings in .cshtml (and baseline) files
  - however MVC and Razor mix `Environment.NewLine`s into HTTP responses
- update `PrecompilationTest` to split response regardless of line endings
- update `ResourceFile` to normalize all source file streams to LF only
 - ensures consistent checksums and line mappings
 - change `MvcRazorHostTest` to expect new line mappings
 - recreate baseline files to expect new checksums and literal line endings
- use verbatim strings in affected tests
 - careful use of `Environment.NewLine` in expectations is now just noise

nits:
- add doc comments in `RazorCompilationService`
- correct `TagHelpersTest` name to match containing file
- avoid incorrect `using` removal when `GENERATE_BASELINES` is not defined
- remove unnecessary `ResourceFile` normalization of output files
  • Loading branch information
dougbu committed Jun 22, 2015
1 parent c4fa402 commit cc0cec8
Show file tree
Hide file tree
Showing 29 changed files with 484 additions and 294 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,18 @@ public class RazorCompilationService : IRazorCompilationService
private readonly IMvcRazorHost _razorHost;
private readonly IFileProvider _fileProvider;

public RazorCompilationService(ICompilationService compilationService,
IMvcRazorHost razorHost,
IOptions<RazorViewEngineOptions> viewEngineOptions)
/// <summary>
/// Instantiates a new instance of the <see cref="RazorCompilationService"/> class.
/// </summary>
/// <param name="compilationService">The <see cref="ICompilationService"/> to compile generated code.</param>
/// <param name="razorHost">The <see cref="IMvcRazorHost"/> to generate code from Razor files.</param>
/// <param name="viewEngineOptions">
/// The <see cref="IFileProvider"/> to read Razor files referenced in error messages.
/// </param>
public RazorCompilationService(
ICompilationService compilationService,
IMvcRazorHost razorHost,
IOptions<RazorViewEngineOptions> viewEngineOptions)
{
_compilationService = compilationService;
_razorHost = razorHost;
Expand All @@ -37,7 +46,7 @@ public CompilationResult Compile([NotNull] RelativeFileInfo file)
GeneratorResults results;
using (var inputStream = file.FileInfo.CreateReadStream())
{
results = _razorHost.GenerateCode(file.RelativePath, inputStream);
results = GenerateCode(file.RelativePath, inputStream);
}

if (!results.Success)
Expand All @@ -48,6 +57,21 @@ public CompilationResult Compile([NotNull] RelativeFileInfo file)
return _compilationService.Compile(file, results.GeneratedCode);
}

/// <summary>
/// Generate code for the Razor file at <paramref name="relativePath"/> with content
/// <paramref name="inputStream"/>.
/// </summary>
/// <param name="relativePath">
/// The path of the Razor file relative to the root of the application. Used to generate line pragmas and
/// calculate the class name of the generated type.
/// </param>
/// <param name="inputStream">A <see cref="Stream"/> that contains the Razor content.</param>
/// <returns>A <see cref="GeneratorResults"/> instance containing results of code generation.</returns>
protected virtual GeneratorResults GenerateCode(string relativePath, Stream inputStream)
{
return _razorHost.GenerateCode(relativePath, inputStream);
}

// Internal for unit testing
internal CompilationResult GetCompilationFailedResult(RelativeFileInfo file, IEnumerable<RazorError> errors)
{
Expand Down
4 changes: 2 additions & 2 deletions test/Microsoft.AspNet.Mvc.FunctionalTests/ActivatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public async Task TagHelperActivation_ConstructorInjection_RendersProperly()
var expected = "<body><h2>Activation Test</h2>" +
Environment.NewLine +
"<div>FakeFakeFake</div>" +
Environment.NewLine +
Environment.NewLine +
"<span>" +
"<input id=\"foo\" name=\"foo\" type=\"hidden\" value=\"test content\" />" +
"</span>" +
Expand All @@ -168,7 +168,7 @@ public async Task TagHelperActivation_ConstructorInjection_RendersProperly()
var body = await client.GetStringAsync("http://localhost/View/UseTagHelper");

// Assert
Assert.Equal(expected, body.Trim());
Assert.Equal(expected, body.Trim(), ignoreLineEndingDifferences: true);
}
}
}
26 changes: 15 additions & 11 deletions test/Microsoft.AspNet.Mvc.FunctionalTests/BasicTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public async Task CanRender_ViewsWithLayout(string url)
#if GENERATE_BASELINES
ResourceFile.UpdateFile(_resourcesAssembly, outputFile, expectedContent, responseContent);
#else
Assert.Equal(expectedContent, responseContent);
Assert.Equal(expectedContent, responseContent, ignoreLineEndingDifferences: true);
#endif
}

Expand All @@ -82,7 +82,7 @@ public async Task CanRender_SimpleViews()
#if GENERATE_BASELINES
ResourceFile.UpdateFile(_resourcesAssembly, outputFile, expectedContent, responseContent);
#else
Assert.Equal(expectedContent, responseContent);
Assert.Equal(expectedContent, responseContent, ignoreLineEndingDifferences: true);
#endif
}

Expand All @@ -106,7 +106,7 @@ public async Task ViewWithAttributePrefix_RendersWithoutIgnoringPrefix()
#if GENERATE_BASELINES
ResourceFile.UpdateFile(_resourcesAssembly, outputFile, expectedContent, responseContent);
#else
Assert.Equal(expectedContent, responseContent);
Assert.Equal(expectedContent, responseContent, ignoreLineEndingDifferences: true);
#endif
}

Expand Down Expand Up @@ -258,9 +258,11 @@ public async Task JsonHelper_RendersJson()
Name = "John <b>Smith</b>"
});

var expectedBody = string.Format(@"<script type=""text/javascript"">" + Environment.NewLine +
@" var json = {0};" + Environment.NewLine +
@"</script>", json);
var expectedBody = string.Format(
@"<script type=""text/javascript"">
var json = {0};
</script>",
json);

// Act
var response = await client.GetAsync("https://localhost/Home/JsonHelperInView");
Expand All @@ -270,7 +272,7 @@ public async Task JsonHelper_RendersJson()
Assert.Equal("text/html", response.Content.Headers.ContentType.MediaType);

var actualBody = await response.Content.ReadAsStringAsync();
Assert.Equal(expectedBody, actualBody);
Assert.Equal(expectedBody, actualBody, ignoreLineEndingDifferences: true);
}

[Fact]
Expand All @@ -286,9 +288,11 @@ public async Task JsonHelperWithSettings_RendersJson()
Name = "John <b>Smith</b>"
}, new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() });

var expectedBody = string.Format(@"<script type=""text/javascript"">" + Environment.NewLine +
@" var json = {0};" + Environment.NewLine +
@"</script>", json);
var expectedBody = string.Format(
@"<script type=""text/javascript"">
var json = {0};
</script>",
json);

// Act
var response = await client.GetAsync("https://localhost/Home/JsonHelperWithSettingsInView");
Expand All @@ -298,7 +302,7 @@ public async Task JsonHelperWithSettings_RendersJson()
Assert.Equal("text/html", response.Content.Headers.ContentType.MediaType);

var actualBody = await response.Content.ReadAsStringAsync();
Assert.Equal(expectedBody, actualBody);
Assert.Equal(expectedBody, actualBody, ignoreLineEndingDifferences: true);
}

public static IEnumerable<object[]> HtmlHelperLinkGenerationData
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public async Task GenerateLink_NonExistentAction()

// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal(ExpectedOutput, content);
Assert.Equal(ExpectedOutput, content, ignoreLineEndingDifferences: true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public async Task CompilationOptions_AreUsedByViewsAndPartials()
This method is only defined in DNX451";
#elif DNXCORE50
var expected =
var expected =
@"This method is running from DNXCORE50
This method is only defined in DNXCORE50";
Expand All @@ -40,7 +40,7 @@ public async Task CompilationOptions_AreUsedByViewsAndPartials()
var body = await client.GetStringAsync("http://localhost/ViewsConsumingCompilationOptions/");

// Assert
Assert.Equal(expected, body.Trim());
Assert.Equal(expected, body.Trim(), ignoreLineEndingDifferences: true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -169,17 +169,29 @@ public async Task NoMatchingFormatter_ForTheGivenContentType_Returns406()
}

[Theory]
[InlineData("ContactInfoUsingV3Format", "text/vcard; version=v3.0; charset=utf-8", "BEGIN:VCARD#FN:John Williams#END:VCARD#")]
[InlineData("ContactInfoUsingV4Format", "text/vcard; version=v4.0; charset=utf-8", "BEGIN:VCARD#FN:John Williams#GENDER:M#END:VCARD#")]
[InlineData(
"ContactInfoUsingV3Format",
"text/vcard; version=v3.0; charset=utf-8",
@"BEGIN:VCARD
FN:John Williams
END:VCARD
")]
[InlineData(
"ContactInfoUsingV4Format",
"text/vcard; version=v4.0; charset=utf-8",
@"BEGIN:VCARD
FN:John Williams
GENDER:M
END:VCARD
")]
public async Task ProducesAttribute_WithMediaTypeHavingParameters_IsCaseInsensitiveMatch(
string action,
string expectedMediaType,
string expectedResponseBody)
string action,
string expectedMediaType,
string expectedResponseBody)
{
// Arrange
var server = TestHelper.CreateServer(_app, SiteName, _configureServices);
var client = server.CreateClient();
expectedResponseBody = expectedResponseBody.Replace("#", Environment.NewLine);

// Act
var response = await client.GetAsync("http://localhost/ProducesWithMediaTypeParameters/" + action);
Expand All @@ -192,7 +204,7 @@ public async Task ProducesAttribute_WithMediaTypeHavingParameters_IsCaseInsensit
Assert.Equal(expectedMediaType, contentType.ToString());

var actualResponseBody = await response.Content.ReadAsStringAsync();
Assert.Equal(expectedResponseBody, actualResponseBody);
Assert.Equal(expectedResponseBody, actualResponseBody, ignoreLineEndingDifferences: true);
}

[Fact]
Expand Down
53 changes: 31 additions & 22 deletions test/Microsoft.AspNet.Mvc.FunctionalTests/FlushPointTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,17 @@ public async Task FlushPointsAreExecutedForPagesWithoutLayouts()
waitService.WaitForServer();

// Assert - 4
Assert.Equal(@"After flush inside partial
Assert.Equal(
@"After flush inside partial
<form action=""/FlushPoint/PageWithoutLayout"" method=""post""><input id=""Name1"" name=""Name1"" type=""text"" value="""" />",
GetTrimmedString(stream));
GetTrimmedString(stream),
ignoreLineEndingDifferences: true);
waitService.WaitForServer();

// Assert - 5
Assert.Equal(@"<input id=""Name2"" name=""Name2"" type=""text"" value="""" /></form>",
GetTrimmedString(stream));
Assert.Equal(
@"<input id=""Name2"" name=""Name2"" type=""text"" value="""" /></form>",
GetTrimmedString(stream));
}

[Theory]
Expand All @@ -102,28 +105,32 @@ public async Task FlushPointsAreExecutedForPagesWithComponentsPartialsAndSection
var stream = await client.GetStreamAsync("http://localhost/FlushPoint/" + action);

// Assert - 1
Assert.Equal(string.Join(Environment.NewLine,
"<title>" + title + "</title>",
"",
"RenderBody content"), GetTrimmedString(stream));
Assert.Equal(
$@"<title>{ title }</title>
RenderBody content",
GetTrimmedString(stream),
ignoreLineEndingDifferences: true);
waitService.WaitForServer();

// Assert - 2
Assert.Equal(string.Join(
Environment.NewLine,
"partial-content",
"",
"Value from TaskReturningString",
"<p>section-content</p>"), GetTrimmedString(stream));
Assert.Equal(
@"partial-content
Value from TaskReturningString
<p>section-content</p>",
GetTrimmedString(stream),
ignoreLineEndingDifferences: true);
waitService.WaitForServer();

// Assert - 3
Assert.Equal(string.Join(
Environment.NewLine,
"component-content",
" <span>Content that takes time to produce</span>",
"",
"More content from layout"), GetTrimmedString(stream));
Assert.Equal(
@"component-content
<span>Content that takes time to produce</span>
More content from layout",
GetTrimmedString(stream),
ignoreLineEndingDifferences: true);
}

[Fact]
Expand All @@ -143,10 +150,12 @@ public async Task FlushPointsNestedLayout()
var stream = await client.GetStreamAsync("http://localhost/FlushPoint/PageWithNestedLayout");

// Assert - 1
Assert.Equal(@"Inside Nested Layout
Assert.Equal(
@"Inside Nested Layout
<title>Nested Page With Layout</title>",
GetTrimmedString(stream));
GetTrimmedString(stream),
ignoreLineEndingDifferences: true);
waitService.WaitForServer();

// Assert - 2
Expand Down
Loading

0 comments on commit cc0cec8

Please sign in to comment.