diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/BasicTests.cs b/test/Microsoft.AspNet.Mvc.FunctionalTests/BasicTests.cs index 27d539cfb5..8ef0fb64cb 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/BasicTests.cs +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/BasicTests.cs @@ -20,14 +20,15 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests public class BasicTests { private const string SiteName = nameof(BasicWebSite); - private readonly Action _app = new Startup().Configure; - private readonly Action _configureServices = new Startup().ConfigureServices; // Some tests require comparing the actual response body against an expected response baseline // so they require a reference to the assembly on which the resources are located, in order to // make the tests less verbose, we get a reference to the assembly with the resources and we // use it on all the rest of the tests. - private readonly Assembly _resourcesAssembly = typeof(BasicTests).GetTypeInfo().Assembly; + private static readonly Assembly _resourcesAssembly = typeof(BasicTests).GetTypeInfo().Assembly; + + private readonly Action _app = new Startup().Configure; + private readonly Action _configureServices = new Startup().ConfigureServices; [Theory] [InlineData("http://localhost/")] @@ -39,13 +40,11 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests var server = TestHelper.CreateServer(_app, SiteName, _configureServices); var client = server.CreateClient(); var expectedMediaType = MediaTypeHeaderValue.Parse("text/html; charset=utf-8"); - - // The K runtime compiles every file under compiler/resources as a resource at runtime with the same name - // as the file name, in order to update a baseline you just need to change the file in that folder. - var expectedContent = await _resourcesAssembly.ReadResourceAsStringAsync("compiler/resources/BasicWebSite.Home.Index.html"); + var outputFile = "compiler/resources/BasicWebSite.Home.Index.html"; + var expectedContent = + await ResourceFile.ReadResourceAsync(_resourcesAssembly, outputFile, sourceFile: false); // Act - // The host is not important as everything runs in memory and tests are isolated from each other. var response = await client.GetAsync(url); var responseContent = await response.Content.ReadAsStringAsync(); @@ -53,7 +52,12 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests // Assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.Equal(expectedMediaType, response.Content.Headers.ContentType); + +#if GENERATE_BASELINES + ResourceFile.UpdateFile(_resourcesAssembly, outputFile, expectedContent, responseContent); +#else Assert.Equal(expectedContent, responseContent); +#endif } [Fact] @@ -62,9 +66,10 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests // Arrange var server = TestHelper.CreateServer(_app, SiteName, _configureServices); var client = server.CreateClient(); - var expectedContent = await _resourcesAssembly.ReadResourceAsStringAsync("compiler/resources/BasicWebSite.Home.PlainView.html"); var expectedMediaType = MediaTypeHeaderValue.Parse("text/html; charset=utf-8"); - + var outputFile = "compiler/resources/BasicWebSite.Home.PlainView.html"; + var expectedContent = + await ResourceFile.ReadResourceAsync(_resourcesAssembly, outputFile, sourceFile: false); // Act var response = await client.GetAsync("http://localhost/Home/PlainView"); @@ -73,7 +78,12 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests // Assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.Equal(expectedMediaType, response.Content.Headers.ContentType); + +#if GENERATE_BASELINES + ResourceFile.UpdateFile(_resourcesAssembly, outputFile, expectedContent, responseContent); +#else Assert.Equal(expectedContent, responseContent); +#endif } [Fact] @@ -82,8 +92,9 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests // Arrange var server = TestHelper.CreateServer(_app, SiteName, _configureServices); var client = server.CreateClient(); - var expectedContent = await _resourcesAssembly.ReadResourceAsStringAsync( - "compiler/resources/BasicWebSite.Home.ViewWithPrefixedAttributeValue.html"); + var outputFile = "compiler/resources/BasicWebSite.Home.ViewWithPrefixedAttributeValue.html"; + var expectedContent = + await ResourceFile.ReadResourceAsync(_resourcesAssembly, outputFile, sourceFile: false); // Act var response = await client.GetAsync("http://localhost/Home/ViewWithPrefixedAttributeValue"); @@ -91,7 +102,12 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests // Assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); + +#if GENERATE_BASELINES + ResourceFile.UpdateFile(_resourcesAssembly, outputFile, expectedContent, responseContent); +#else Assert.Equal(expectedContent, responseContent); +#endif } [Fact] diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/ErrorPageTests.cs b/test/Microsoft.AspNet.Mvc.FunctionalTests/ErrorPageTests.cs index 579ad3af74..81350ddaf8 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/ErrorPageTests.cs +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/ErrorPageTests.cs @@ -4,7 +4,6 @@ using System; using System.Net; using System.Net.Http.Headers; -using System.Reflection; using System.Threading.Tasks; using ErrorPageMiddlewareWebSite; using Microsoft.AspNet.Builder; @@ -22,8 +21,6 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests private readonly Action _app = new Startup().Configure; private readonly Action _configureServices = new Startup().ConfigureServices; - private readonly Assembly _resourcesAssembly = typeof(ErrorPageTests).GetTypeInfo().Assembly; - [Theory] [InlineData("CompilationFailure", "Cannot implicitly convert type 'int' to 'string'")] [InlineData("ParserError", "The code block is missing a closing "}" character. Make sure you " + diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/LinkGenerationTests.cs b/test/Microsoft.AspNet.Mvc.FunctionalTests/LinkGenerationTests.cs index 4582c4a497..87f65507a8 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/LinkGenerationTests.cs +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/LinkGenerationTests.cs @@ -15,15 +15,15 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests public class LinkGenerationTests { private const string SiteName = nameof(BasicWebSite); - private readonly Action _app = new BasicWebSite.Startup().Configure; - private readonly Action _configureServices = new BasicWebSite.Startup().ConfigureServices; - // Some tests require comparing the actual response body against an expected response baseline // so they require a reference to the assembly on which the resources are located, in order to // make the tests less verbose, we get a reference to the assembly with the resources and we // use it on all the rest of the tests. - private readonly Assembly _resourcesAssembly = typeof(LinkGenerationTests).GetTypeInfo().Assembly; + private static readonly Assembly _resourcesAssembly = typeof(LinkGenerationTests).GetTypeInfo().Assembly; + + private readonly Action _app = new BasicWebSite.Startup().Configure; + private readonly Action _configureServices = new BasicWebSite.Startup().ConfigureServices; [Theory] [InlineData("http://pingüino/Home/RedirectToActionReturningTaskAction", "/Home/ActionReturningTask")] @@ -55,13 +55,12 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests // Arrange var server = TestHelper.CreateServer(_app, SiteName, _configureServices); var client = server.CreateClient(); - var expectedMediaType = MediaTypeHeaderValue.Parse("text/html; charset=utf-8"); - var expectedContent = await _resourcesAssembly - .ReadResourceAsStringAsync("compiler/resources/BasicWebSite.Home.ActionLinkView.html"); + var outputFile = "compiler/resources/BasicWebSite.Home.ActionLinkView.html"; + var expectedContent = + await ResourceFile.ReadResourceAsync(_resourcesAssembly, outputFile, sourceFile: false); // Act - // The host is not important as everything runs in memory and tests are isolated from each other. var response = await client.GetAsync("http://localhost/Home/ActionLinkView"); var responseContent = await response.Content.ReadAsStringAsync(); @@ -69,7 +68,12 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests // Assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.Equal(expectedMediaType, response.Content.Headers.ContentType); + +#if GENERATE_BASELINES + ResourceFile.UpdateFile(_resourcesAssembly, outputFile, expectedContent, responseContent); +#else Assert.Equal(expectedContent, responseContent); +#endif } } } \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/ModelBindingTest.cs b/test/Microsoft.AspNet.Mvc.FunctionalTests/ModelBindingTest.cs index 71fbd3ef1f..267a8e1d3f 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/ModelBindingTest.cs +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/ModelBindingTest.cs @@ -13,7 +13,6 @@ using System.Threading.Tasks; using Microsoft.AspNet.Builder; using Microsoft.AspNet.Mvc.ModelBinding; using Microsoft.Framework.DependencyInjection; -using ModelBindingWebSite.Controllers; using ModelBindingWebSite.Models; using ModelBindingWebSite.ViewModels; using Newtonsoft.Json; @@ -24,6 +23,8 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests public class ModelBindingTest { private const string SiteName = nameof(ModelBindingWebSite); + private static readonly Assembly _assembly = typeof(ModelBindingTest).GetTypeInfo().Assembly; + private readonly Action _app = new ModelBindingWebSite.Startup().Configure; private readonly Action _configureServices = new ModelBindingWebSite.Startup().ConfigureServices; @@ -1383,10 +1384,10 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests public async Task UpdateDealerVehicle_PopulatesPropertyErrorsInViews() { // Arrange - var expectedContent = await GetType().GetTypeInfo().Assembly.ReadResourceAsStringAsync( - "compiler/resources/UpdateDealerVehicle_PopulatesPropertyErrorsInViews.txt"); var server = TestHelper.CreateServer(_app, SiteName, _configureServices); var client = server.CreateClient(); + var outputFile = "compiler/resources/UpdateDealerVehicle_PopulatesPropertyErrorsInViews.txt"; + var expectedContent = await ResourceFile.ReadResourceAsync(_assembly, outputFile, sourceFile: false); var postedContent = new { Year = 9001, @@ -1406,18 +1407,23 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests // Assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); - var body = await response.Content.ReadAsStringAsync(); - Assert.Equal(expectedContent, body); + + var responseContent = await response.Content.ReadAsStringAsync(); +#if GENERATE_BASELINES + ResourceFile.UpdateFile(_assembly, outputFile, expectedContent, responseContent); +#else + Assert.Equal(expectedContent, responseContent); +#endif } [Fact] public async Task UpdateDealerVehicle_PopulatesValidationSummary() { // Arrange - var expectedContent = await GetType().GetTypeInfo().Assembly.ReadResourceAsStringAsync( - "compiler/resources/UpdateDealerVehicle_PopulatesValidationSummary.txt"); var server = TestHelper.CreateServer(_app, SiteName, _configureServices); var client = server.CreateClient(); + var outputFile = "compiler/resources/UpdateDealerVehicle_PopulatesValidationSummary.txt"; + var expectedContent = await ResourceFile.ReadResourceAsync(_assembly, outputFile, sourceFile: false); var postedContent = new { Year = 2013, @@ -1437,18 +1443,23 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests // Assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); - var body = await response.Content.ReadAsStringAsync(); - Assert.Equal(expectedContent, body); + + var responseContent = await response.Content.ReadAsStringAsync(); +#if GENERATE_BASELINES + ResourceFile.UpdateFile(_assembly, outputFile, expectedContent, responseContent); +#else + Assert.Equal(expectedContent, responseContent); +#endif } [Fact] public async Task UpdateDealerVehicle_UsesDefaultValuesForOptionalProperties() { // Arrange - var expectedContent = await GetType().GetTypeInfo().Assembly.ReadResourceAsStringAsync( - "compiler/resources/UpdateDealerVehicle_UpdateSuccessful.txt"); var server = TestHelper.CreateServer(_app, SiteName, _configureServices); var client = server.CreateClient(); + var outputFile = "compiler/resources/UpdateDealerVehicle_UpdateSuccessful.txt"; + var expectedContent = await ResourceFile.ReadResourceAsync(_assembly, outputFile, sourceFile: false); var postedContent = new { Year = 2013, @@ -1468,8 +1479,13 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests // Assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); - var body = await response.Content.ReadAsStringAsync(); - Assert.Equal(expectedContent, body); + + var responseContent = await response.Content.ReadAsStringAsync(); +#if GENERATE_BASELINES + ResourceFile.UpdateFile(_assembly, outputFile, expectedContent, responseContent); +#else + Assert.Equal(expectedContent, responseContent); +#endif } [Fact] @@ -1603,10 +1619,10 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests public async Task HtmlHelper_DisplayFor_ShowsPropertiesInModelMetadataOrder() { // Arrange - var expectedContent = await GetType().GetTypeInfo().Assembly.ReadResourceAsStringAsync( - "compiler/resources/ModelBindingWebSite.Vehicle.Details.html"); var server = TestHelper.CreateServer(_app, SiteName, _configureServices); var client = server.CreateClient(); + var outputFile = "compiler/resources/ModelBindingWebSite.Vehicle.Details.html"; + var expectedContent = await ResourceFile.ReadResourceAsync(_assembly, outputFile, sourceFile: false); var url = "http://localhost/vehicles/42"; // Act @@ -1615,18 +1631,22 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests // Assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); - var body = await response.Content.ReadAsStringAsync(); - Assert.Equal(expectedContent, body); + var responseContent = await response.Content.ReadAsStringAsync(); +#if GENERATE_BASELINES + ResourceFile.UpdateFile(_assembly, outputFile, expectedContent, responseContent); +#else + Assert.Equal(expectedContent, responseContent); +#endif } [Fact] public async Task HtmlHelper_EditorFor_ShowsPropertiesInModelMetadataOrder() { // Arrange - var expectedContent = await GetType().GetTypeInfo().Assembly.ReadResourceAsStringAsync( - "compiler/resources/ModelBindingWebSite.Vehicle.Edit.html"); var server = TestHelper.CreateServer(_app, SiteName, _configureServices); var client = server.CreateClient(); + var outputFile = "compiler/resources/ModelBindingWebSite.Vehicle.Edit.html"; + var expectedContent = await ResourceFile.ReadResourceAsync(_assembly, outputFile, sourceFile: false); var url = "http://localhost/vehicles/42/edit"; // Act @@ -1635,18 +1655,22 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests // Assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); - var body = await response.Content.ReadAsStringAsync(); - Assert.Equal(expectedContent, body); + var responseContent = await response.Content.ReadAsStringAsync(); +#if GENERATE_BASELINES + ResourceFile.UpdateFile(_assembly, outputFile, expectedContent, responseContent); +#else + Assert.Equal(expectedContent, responseContent); +#endif } [Fact] public async Task HtmlHelper_EditorFor_ShowsPropertiesAndErrorsInModelMetadataOrder() { // Arrange - var expectedContent = await GetType().GetTypeInfo().Assembly.ReadResourceAsStringAsync( - "compiler/resources/ModelBindingWebSite.Vehicle.Edit.Invalid.html"); var server = TestHelper.CreateServer(_app, SiteName, _configureServices); var client = server.CreateClient(); + var outputFile = "compiler/resources/ModelBindingWebSite.Vehicle.Edit.Invalid.html"; + var expectedContent = await ResourceFile.ReadResourceAsync(_assembly, outputFile, sourceFile: false); var url = "http://localhost/vehicles/42/edit"; var contentDictionary = new Dictionary { @@ -1674,8 +1698,12 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests // Assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); - var body = await response.Content.ReadAsStringAsync(); - Assert.Equal(expectedContent, body); + var responseContent = await response.Content.ReadAsStringAsync(); +#if GENERATE_BASELINES + ResourceFile.UpdateFile(_assembly, outputFile, expectedContent, responseContent); +#else + Assert.Equal(expectedContent, responseContent); +#endif } [Fact] diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/MvcTagHelpersTest.cs b/test/Microsoft.AspNet.Mvc.FunctionalTests/MvcTagHelpersTest.cs index d07f243643..dc3f4468c8 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/MvcTagHelpersTest.cs +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/MvcTagHelpersTest.cs @@ -21,9 +21,10 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests public class MvcTagHelpersTest { private const string SiteName = nameof(MvcTagHelpersWebSite); + private static readonly Assembly _resourcesAssembly = typeof(MvcTagHelpersTest).GetTypeInfo().Assembly; + private readonly Action _app = new Startup().Configure; private readonly Action _configureServices = new Startup().ConfigureServices; - private static readonly Assembly _resourcesAssembly = typeof(MvcTagHelpersTest).GetTypeInfo().Assembly; [Theory] [InlineData("Index", null)] @@ -57,11 +58,9 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests var server = TestHelper.CreateServer(_app, SiteName, _configureServices); var client = server.CreateClient(); var expectedMediaType = MediaTypeHeaderValue.Parse("text/html; charset=utf-8"); - - // The K runtime compiles every file under compiler/resources as a resource at runtime with the same name - // as the file name, in order to update a baseline you just need to change the file in that folder. - var expectedContent = await _resourcesAssembly.ReadResourceAsStringAsync( - "compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home." + action + ".html"); + var outputFile = "compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home." + action + ".html"; + var expectedContent = + await ResourceFile.ReadResourceAsync(_resourcesAssembly, outputFile, sourceFile: false); // Act // The host is not important as everything runs in memory and tests are isolated from each other. @@ -72,13 +71,27 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.Equal(expectedMediaType, response.Content.Headers.ContentType); - if (antiForgeryPath != null) + responseContent = responseContent.Trim(); + if (antiForgeryPath == null) + { +#if GENERATE_BASELINES + ResourceFile.UpdateFile(_resourcesAssembly, outputFile, expectedContent, responseContent); +#else + Assert.Equal(expectedContent.Trim(), responseContent); +#endif + } + else { var forgeryToken = AntiForgeryTestHelper.RetrieveAntiForgeryToken(responseContent, antiForgeryPath); +#if GENERATE_BASELINES + // Reverse usual substitution and insert a format item into the new file content. + responseContent = responseContent.Replace(forgeryToken, "{0}"); + ResourceFile.UpdateFile(_resourcesAssembly, outputFile, expectedContent, responseContent); +#else expectedContent = string.Format(expectedContent, forgeryToken); + Assert.Equal(expectedContent.Trim(), responseContent); +#endif } - - Assert.Equal(expectedContent.Trim(), responseContent.Trim()); } [Theory] @@ -101,11 +114,9 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests }); var client = server.CreateClient(); var expectedMediaType = MediaTypeHeaderValue.Parse("text/html; charset=utf-8"); - - // The K runtime compiles every file under compiler/resources as a resource at runtime with the same name - // as the file name, in order to update a baseline you just need to change the file in that folder. - var expectedContent = await _resourcesAssembly.ReadResourceAsStringAsync( - "compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home." + action + ".Encoded.html"); + var outputFile = "compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home." + action + ".Encoded.html"; + var expectedContent = + await ResourceFile.ReadResourceAsync(_resourcesAssembly, outputFile, sourceFile: false); // Act // The host is not important as everything runs in memory and tests are isolated from each other. @@ -116,13 +127,27 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.Equal(expectedMediaType, response.Content.Headers.ContentType); - if (antiForgeryPath != null) + responseContent = responseContent.Trim(); + if (antiForgeryPath == null) + { +#if GENERATE_BASELINES + ResourceFile.UpdateFile(_resourcesAssembly, outputFile, expectedContent, responseContent); +#else + Assert.Equal(expectedContent.Trim(), responseContent); +#endif + } + else { var forgeryToken = AntiForgeryTestHelper.RetrieveAntiForgeryToken(responseContent, antiForgeryPath); +#if GENERATE_BASELINES + // Reverse usual substitution and insert a format item into the new file content. + responseContent = responseContent.Replace(forgeryToken, "{0}"); + ResourceFile.UpdateFile(_resourcesAssembly, outputFile, expectedContent, responseContent); +#else expectedContent = string.Format(expectedContent, forgeryToken); + Assert.Equal(expectedContent.Trim(), responseContent); +#endif } - - Assert.Equal(expectedContent.Trim(), responseContent.Trim()); } [Fact] @@ -131,8 +156,9 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests // Arrange var server = TestHelper.CreateServer(_app, SiteName, _configureServices); var client = server.CreateClient(); - var expectedContent = await _resourcesAssembly.ReadResourceAsStringAsync( - "compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Customer.Index.html"); + var outputFile = "compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Customer.Index.html"; + var expectedContent = + await ResourceFile.ReadResourceAsync(_resourcesAssembly, outputFile, sourceFile: false); var request = new HttpRequestMessage(HttpMethod.Post, "http://localhost/Customer/MvcTagHelper_Customer"); var nameValueCollection = new List> @@ -152,9 +178,18 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests // Assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); - var forgeryToken = AntiForgeryTestHelper.RetrieveAntiForgeryToken(responseContent, "Customer/MvcTagHelper_Customer"); + responseContent = responseContent.Trim(); + var forgeryToken = + AntiForgeryTestHelper.RetrieveAntiForgeryToken(responseContent, "Customer/MvcTagHelper_Customer"); + +#if GENERATE_BASELINES + // Reverse usual substitution and insert a format item into the new file content. + responseContent = responseContent.Replace(forgeryToken, "{0}"); + ResourceFile.UpdateFile(_resourcesAssembly, outputFile, expectedContent, responseContent); +#else expectedContent = string.Format(expectedContent, forgeryToken); - Assert.Equal(expectedContent.Trim(), responseContent.Trim()); + Assert.Equal(expectedContent.Trim(), responseContent); +#endif } [Fact] @@ -168,6 +203,16 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests client.BaseAddress = new Uri("http://localhost"); client.DefaultRequestHeaders.Add("Locale", "North"); + var outputFile1 = assertFile + "1.txt"; + var expected1 = + await ResourceFile.ReadResourceAsync(_resourcesAssembly, outputFile1, sourceFile: false); + var outputFile2 = assertFile + "2.txt"; + var expected2 = + await ResourceFile.ReadResourceAsync(_resourcesAssembly, outputFile2, sourceFile: false); + var outputFile3 = assertFile + "3.txt"; + var expected3 = + await ResourceFile.ReadResourceAsync(_resourcesAssembly, outputFile3, sourceFile: false); + // Act - 1 // Verify that content gets cached based on vary-by-params var targetUrl = "/catalog?categoryId=1&correlationid=1"; @@ -175,10 +220,12 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests var response2 = await client.GetStringAsync(targetUrl); // Assert - 1 - var expected1 = await _resourcesAssembly.ReadResourceAsStringAsync(assertFile + "1.txt"); - +#if GENERATE_BASELINES + ResourceFile.UpdateFile(_resourcesAssembly, outputFile1, expected1, response1.Trim()); +#else Assert.Equal(expected1, response1.Trim()); Assert.Equal(expected1, response2.Trim()); +#endif // Act - 2 // Verify content gets changed in partials when one of the vary by parameters is changed @@ -187,10 +234,12 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests var response4 = await client.GetStringAsync(targetUrl); // Assert - 2 - var expected2 = await _resourcesAssembly.ReadResourceAsStringAsync(assertFile + "2.txt"); - +#if GENERATE_BASELINES + ResourceFile.UpdateFile(_resourcesAssembly, outputFile2, expected2, response3.Trim()); +#else Assert.Equal(expected2, response3.Trim()); Assert.Equal(expected2, response4.Trim()); +#endif // Act - 3 // Verify content gets changed in a View Component when the Vary-by-header parameters is changed @@ -202,10 +251,12 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests var response6 = await client.GetStringAsync(targetUrl); // Assert - 3 - var expected3 = await _resourcesAssembly.ReadResourceAsStringAsync(assertFile + "3.txt"); - +#if GENERATE_BASELINES + ResourceFile.UpdateFile(_resourcesAssembly, outputFile3, expected3, response5.Trim()); +#else Assert.Equal(expected3, response5.Trim()); Assert.Equal(expected3, response6.Trim()); +#endif } [Fact] @@ -424,26 +475,36 @@ Products: Laptops (3)"; var client = server.CreateClient(); var expectedMediaType = MediaTypeHeaderValue.Parse("text/html; charset=utf-8"); - // The K runtime compiles every file under compiler/resources as a resource at runtime with the same name - // as the file name, in order to update a baseline you just need to change the file in that folder. - var resourceName = string.Format( + var outputFile = string.Format( "compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Form.Options.AntiForgery.{0}.html", - optionsAntiForgery?.ToString() ?? "null" - ); - var expectedContent = await _resourcesAssembly.ReadResourceAsStringAsync(resourceName); + optionsAntiForgery?.ToString() ?? "null"); + var expectedContent = + await ResourceFile.ReadResourceAsync(_resourcesAssembly, outputFile, sourceFile: false); // Act // The host is not important as everything runs in memory and tests are isolated from each other. var response = await client.GetAsync("http://localhost/MvcTagHelper_Home/Form"); var responseContent = await response.Content.ReadAsStringAsync(); - var forgeryTokens = AntiForgeryTestHelper.RetrieveAntiForgeryTokens(responseContent); - expectedContent = string.Format(expectedContent, forgeryTokens.ToArray()); - // Assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.Equal(expectedMediaType, response.Content.Headers.ContentType); - Assert.Equal(expectedContent.Trim(), responseContent.Trim()); + + responseContent = responseContent.Trim(); + var forgeryTokens = AntiForgeryTestHelper.RetrieveAntiForgeryTokens(responseContent).ToArray(); + +#if GENERATE_BASELINES + // Reverse usual substitutions and insert format items into the new file content. + for (var index = 0; index < forgeryTokens.Length; index++) + { + responseContent = responseContent.Replace(forgeryTokens[index], $"{{{ index }}}"); + } + + ResourceFile.UpdateFile(_resourcesAssembly, outputFile, expectedContent, responseContent); +#else + expectedContent = string.Format(expectedContent, forgeryTokens); + Assert.Equal(expectedContent.Trim(), responseContent); +#endif } } } \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/RemoteAttributeValidationTest.cs b/test/Microsoft.AspNet.Mvc.FunctionalTests/RemoteAttributeValidationTest.cs index 4db241b634..85aa21ef19 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/RemoteAttributeValidationTest.cs +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/RemoteAttributeValidationTest.cs @@ -18,6 +18,7 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests private const string SiteName = nameof(ValidationWebSite); private static readonly Assembly _resourcesAssembly = typeof(RemoteAttributeValidationTest).GetTypeInfo().Assembly; + private readonly Action _app = new ValidationWebSite.Startup().Configure; private readonly Action _configureServices = new ValidationWebSite.Startup().ConfigureServices; @@ -29,8 +30,9 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests // Arrange var server = TestHelper.CreateServer(_app, SiteName, _configureServices); var client = server.CreateClient(); - var expectedContent = await _resourcesAssembly.ReadResourceAsStringAsync( - "compiler/resources/ValidationWebSite." + areaName + ".RemoteAttribute_Home.Create.html"); + var outputFile = "compiler/resources/ValidationWebSite." + areaName + ".RemoteAttribute_Home.Create.html"; + var expectedContent = + await ResourceFile.ReadResourceAsync(_resourcesAssembly, outputFile, sourceFile: false); var url = "http://localhost" + pathSegment + "/RemoteAttribute_Home/Create"; // Act @@ -40,8 +42,13 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.Equal("text/html", response.Content.Headers.ContentType.MediaType); Assert.Equal("utf-8", response.Content.Headers.ContentType.CharSet); + var responseContent = await response.Content.ReadAsStringAsync(); +#if GENERATE_BASELINES + ResourceFile.UpdateFile(_resourcesAssembly, outputFile, expectedContent, responseContent); +#else Assert.Equal(expectedContent, responseContent); +#endif } [Theory] diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/ResourceHelpers.cs b/test/Microsoft.AspNet.Mvc.FunctionalTests/ResourceHelpers.cs deleted file mode 100644 index cf3488250b..0000000000 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/ResourceHelpers.cs +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System.IO; -using System.Reflection; -using System.Threading.Tasks; - -namespace Microsoft.AspNet.Mvc.FunctionalTests -{ - // This class contains helper methods for reading resources from a given assembly in order - // to make tests that require comparing against baseline files embedded as resources less - // verbose. - public static class ResourceHelpers - { - public static async Task ReadResourceAsStringAsync(this Assembly assembly, string resourceName) - { - resourceName = assembly.GetName().Name + "." + resourceName.Replace('/', '.'); - - using (var resourceStream = assembly.GetManifestResourceStream(resourceName)) - { - using (var streamReader = new StreamReader(resourceStream)) - { - return await streamReader.ReadToEndAsync(); - } - } - } - } -} \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/TagHelpersTest.cs b/test/Microsoft.AspNet.Mvc.FunctionalTests/TagHelpersTest.cs index c1c829e3b2..ccf64a299a 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/TagHelpersTest.cs +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/TagHelpersTest.cs @@ -18,14 +18,15 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests public class TagHelpersTests { private const string SiteName = nameof(TagHelpersWebSite); - private readonly Action _app = new Startup().Configure; - private readonly Action _configureServices = new Startup().ConfigureServices; // Some tests require comparing the actual response body against an expected response baseline // so they require a reference to the assembly on which the resources are located, in order to // make the tests less verbose, we get a reference to the assembly with the resources and we // use it on all the rest of the tests. - private readonly Assembly _resourcesAssembly = typeof(TagHelpersTests).GetTypeInfo().Assembly; + private static readonly Assembly _resourcesAssembly = typeof(TagHelpersTests).GetTypeInfo().Assembly; + + private readonly Action _app = new Startup().Configure; + private readonly Action _configureServices = new Startup().ConfigureServices; [Theory] [InlineData("Index")] @@ -37,22 +38,24 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests var server = TestHelper.CreateServer(_app, SiteName, _configureServices); var client = server.CreateClient(); var expectedMediaType = MediaTypeHeaderValue.Parse("text/html; charset=utf-8"); - - // The K runtime compiles every file under compiler/resources as a resource at runtime with the same name - // as the file name, in order to update a baseline you just need to change the file in that folder. - var expectedContent = await _resourcesAssembly.ReadResourceAsStringAsync( - "compiler/resources/TagHelpersWebSite.Home." + action + ".html"); + var outputFile = "compiler/resources/TagHelpersWebSite.Home." + action + ".html"; + var expectedContent = + await ResourceFile.ReadResourceAsync(_resourcesAssembly, outputFile, sourceFile: false); // Act - // The host is not important as everything runs in memory and tests are isolated from each other. var response = await client.GetAsync("http://localhost/Home/" + action); - var responseContent = await response.Content.ReadAsStringAsync(); // Assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.Equal(expectedMediaType, response.Content.Headers.ContentType); + + var responseContent = await response.Content.ReadAsStringAsync(); +#if GENERATE_BASELINES + ResourceFile.UpdateFile(_resourcesAssembly, outputFile, expectedContent, responseContent); +#else Assert.Equal(expectedContent, responseContent); +#endif } public static TheoryData TagHelpersAreInheritedFromViewImportsPagesData @@ -129,16 +132,22 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests // Arrange var server = TestHelper.CreateServer(_app, SiteName, _configureServices); var client = server.CreateClient(); - var expectedContent = await _resourcesAssembly.ReadResourceAsStringAsync( - "compiler/resources/TagHelpersWebSite.Employee.Create.html"); + var outputFile = "compiler/resources/TagHelpersWebSite.Employee.Create.html"; + var expectedContent = + await ResourceFile.ReadResourceAsync(_resourcesAssembly, outputFile, sourceFile: false); // Act var response = await client.GetAsync("http://localhost/Employee/Create"); // Assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); + var responseContent = await response.Content.ReadAsStringAsync(); +#if GENERATE_BASELINES + ResourceFile.UpdateFile(_resourcesAssembly, outputFile, expectedContent, responseContent); +#else Assert.Equal(expectedContent, responseContent); +#endif } [Fact] @@ -147,8 +156,9 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests // Arrange var server = TestHelper.CreateServer(_app, SiteName, _configureServices); var client = server.CreateClient(); - var expectedContent = await _resourcesAssembly.ReadResourceAsStringAsync( - "compiler/resources/TagHelpersWebSite.Employee.Details.AfterCreate.html"); + var outputFile = "compiler/resources/TagHelpersWebSite.Employee.Details.AfterCreate.html"; + var expectedContent = + await ResourceFile.ReadResourceAsync(_resourcesAssembly, outputFile, sourceFile: false); var validPostValues = new Dictionary { { "FullName", "Boo" }, @@ -165,8 +175,13 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests // Assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); + var responseContent = await response.Content.ReadAsStringAsync(); +#if GENERATE_BASELINES + ResourceFile.UpdateFile(_resourcesAssembly, outputFile, expectedContent, responseContent); +#else Assert.Equal(expectedContent, responseContent); +#endif } [Fact] @@ -175,8 +190,9 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests // Arrange var server = TestHelper.CreateServer(_app, SiteName, _configureServices); var client = server.CreateClient(); - var expectedContent = await _resourcesAssembly.ReadResourceAsStringAsync( - "compiler/resources/TagHelpersWebSite.Employee.Create.Invalid.html"); + var outputFile = "compiler/resources/TagHelpersWebSite.Employee.Create.Invalid.html"; + var expectedContent = + await ResourceFile.ReadResourceAsync(_resourcesAssembly, outputFile, sourceFile: false); var validPostValues = new Dictionary { { "FullName", "Boo" }, @@ -193,8 +209,13 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests // Assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); + var responseContent = await response.Content.ReadAsStringAsync(); +#if GENERATE_BASELINES + ResourceFile.UpdateFile(_resourcesAssembly, outputFile, expectedContent, responseContent); +#else Assert.Equal(expectedContent, responseContent); +#endif } } } \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/ViewEngineTests.cs b/test/Microsoft.AspNet.Mvc.FunctionalTests/ViewEngineTests.cs index a76e1a2303..f92ef73bbb 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/ViewEngineTests.cs +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/ViewEngineTests.cs @@ -15,6 +15,8 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests public class ViewEngineTests { private const string SiteName = nameof(RazorWebSite); + private static readonly Assembly _assembly = typeof(ViewEngineTests).GetTypeInfo().Assembly; + private readonly Action _app = new Startup().Configure; private readonly Action _configureServices = new Startup().ConfigureServices; @@ -318,7 +320,7 @@ View With Layout public async Task ViewStartsCanUseDirectivesInjectedFromParentGlobals() { // Arrange - var expected = + var expected = @"Hello Controller-Person Hello Controller-Person"; var server = TestHelper.CreateServer(_app, SiteName, _configureServices); @@ -426,16 +428,21 @@ Partial that does not specify Layout public async Task RazorView_SetsViewPathAndExecutingPagePath() { // Arrange - var expected = await GetType().GetTypeInfo().Assembly - .ReadResourceAsStringAsync("compiler/resources/ViewEngineController.ViewWithPaths.txt"); var server = TestHelper.CreateServer(_app, SiteName, _configureServices); var client = server.CreateClient(); + var outputFile = "compiler/resources/ViewEngineController.ViewWithPaths.txt"; + var expectedContent = await ResourceFile.ReadResourceAsync(_assembly, outputFile, sourceFile: false); // Act - var body = await client.GetStringAsync("http://localhost/ViewWithPaths"); + var responseContent = await client.GetStringAsync("http://localhost/ViewWithPaths"); // Assert - Assert.Equal(expected, body.Trim()); + responseContent = responseContent.Trim(); +#if GENERATE_BASELINES + ResourceFile.UpdateFile(_assembly, outputFile, expectedContent, responseContent); +#else + Assert.Equal(expectedContent, responseContent); +#endif } } } diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/BasicWebSite.Home.ActionLinkView.html b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/BasicWebSite.Home.ActionLinkView.html index 11d9d6525b..3750dfeabf 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/BasicWebSite.Home.ActionLinkView.html +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/BasicWebSite.Home.ActionLinkView.html @@ -1,4 +1,4 @@ - + Action Link with non unicode host diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/BasicWebSite.Home.PlainView.html b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/BasicWebSite.Home.PlainView.html index b282c5d98f..0ad61a0b3f 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/BasicWebSite.Home.PlainView.html +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/BasicWebSite.Home.PlainView.html @@ -1 +1 @@ -Plain View \ No newline at end of file +Plain View \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/CacheTagHelper_CanCachePortionsOfViewsPartialViewsAndViewComponents.Assert1.txt b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/CacheTagHelper_CanCachePortionsOfViewsPartialViewsAndViewComponents.Assert1.txt index a0a21b6c33..02138e2a5d 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/CacheTagHelper_CanCachePortionsOfViewsPartialViewsAndViewComponents.Assert1.txt +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/CacheTagHelper_CanCachePortionsOfViewsPartialViewsAndViewComponents.Assert1.txt @@ -1,4 +1,4 @@ -

Category: Laptops

+

Category: Laptops

Region: North

Cached content

diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/CacheTagHelper_CanCachePortionsOfViewsPartialViewsAndViewComponents.Assert2.txt b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/CacheTagHelper_CanCachePortionsOfViewsPartialViewsAndViewComponents.Assert2.txt index afd10d8186..ac40c7f759 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/CacheTagHelper_CanCachePortionsOfViewsPartialViewsAndViewComponents.Assert2.txt +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/CacheTagHelper_CanCachePortionsOfViewsPartialViewsAndViewComponents.Assert2.txt @@ -1,4 +1,4 @@ -

Category: Phones

+

Category: Phones

Region: North

Cached content

diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/CacheTagHelper_CanCachePortionsOfViewsPartialViewsAndViewComponents.Assert3.txt b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/CacheTagHelper_CanCachePortionsOfViewsPartialViewsAndViewComponents.Assert3.txt index 2b07849a8b..eb4317226b 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/CacheTagHelper_CanCachePortionsOfViewsPartialViewsAndViewComponents.Assert3.txt +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/CacheTagHelper_CanCachePortionsOfViewsPartialViewsAndViewComponents.Assert3.txt @@ -1,4 +1,4 @@ -

Category: Phones

+

Category: Phones

Region: East

Cached content

diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Customer.Index.html b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Customer.Index.html index 5596228a80..a3340f55ba 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Customer.Index.html +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Customer.Index.html @@ -1,6 +1,3 @@ - - -
diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.CreateWarehouse.html b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.CreateWarehouse.html index b7d6e1c4c5..68ab2d052d 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.CreateWarehouse.html +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.CreateWarehouse.html @@ -1,6 +1,3 @@ - - -
diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Customer.html b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Customer.html index e9cde95b04..4f43116083 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Customer.html +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Customer.html @@ -1,6 +1,3 @@ - - - diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.EditWarehouse.Encoded.html b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.EditWarehouse.Encoded.html index 218ce72280..3cbb7c7161 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.EditWarehouse.Encoded.html +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.EditWarehouse.Encoded.html @@ -1,6 +1,3 @@ - - - @@ -44,4 +41,4 @@ HtmlEncode[[Address_1]]; - + \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.EditWarehouse.html b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.EditWarehouse.html index 59957a8ea2..e1a4e3c25a 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.EditWarehouse.html +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.EditWarehouse.html @@ -1,6 +1,3 @@ - - -
@@ -44,4 +41,4 @@ Address_1;
- + \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.EmployeeList.html b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.EmployeeList.html index 8f377c6caf..f950802a05 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.EmployeeList.html +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.EmployeeList.html @@ -1,6 +1,3 @@ - - -
diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Environment.html b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Environment.html index 26a24d1408..09a8fe15c4 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Environment.html +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Environment.html @@ -1,4 +1,3 @@ - diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Form.Options.AntiForgery.False.html b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Form.Options.AntiForgery.False.html index 5c49603534..18041fe48b 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Form.Options.AntiForgery.False.html +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Form.Options.AntiForgery.False.html @@ -1,4 +1,3 @@ - diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Form.Options.AntiForgery.True.html b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Form.Options.AntiForgery.True.html index 2e2f8fc3d3..b1e706bfed 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Form.Options.AntiForgery.True.html +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Form.Options.AntiForgery.True.html @@ -1,4 +1,3 @@ - diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Form.Options.AntiForgery.null.html b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Form.Options.AntiForgery.null.html index 2e2f8fc3d3..b1e706bfed 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Form.Options.AntiForgery.null.html +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Form.Options.AntiForgery.null.html @@ -1,4 +1,3 @@ - diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Image.html b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Image.html index 1eb2c29e9c..148485f395 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Image.html +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Image.html @@ -1,4 +1,3 @@ - diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Index.Encoded.html b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Index.Encoded.html index 1b8bc54638..3796a25177 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Index.Encoded.html +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Index.Encoded.html @@ -1,6 +1,3 @@ - - -
diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Index.html b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Index.html index 77726bd69b..d55f8d8cff 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Index.html +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Index.html @@ -1,6 +1,3 @@ - - -
diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Link.Encoded.html b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Link.Encoded.html index b2c0357ffe..b3cca4acda 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Link.Encoded.html +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Link.Encoded.html @@ -1,4 +1,3 @@ - diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Link.html b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Link.html index 95d54317d0..9e419b629d 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Link.html +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Link.html @@ -1,5 +1,3 @@ - - diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Order.Encoded.html b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Order.Encoded.html index 2cd434020d..24831dc118 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Order.Encoded.html +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Order.Encoded.html @@ -1,6 +1,3 @@ - - - diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Order.html b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Order.html index 15fcb9678b..11f1713b3f 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Order.html +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Order.html @@ -1,6 +1,3 @@ - - - diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.OrderUsingHtmlHelpers.Encoded.html b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.OrderUsingHtmlHelpers.Encoded.html index 4e7b25450b..38c5d0daa9 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.OrderUsingHtmlHelpers.Encoded.html +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.OrderUsingHtmlHelpers.Encoded.html @@ -1,6 +1,3 @@ - - - diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.OrderUsingHtmlHelpers.html b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.OrderUsingHtmlHelpers.html index 8386a1d79f..5cc7b459f2 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.OrderUsingHtmlHelpers.html +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.OrderUsingHtmlHelpers.html @@ -1,6 +1,3 @@ - - - diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Product.Encoded.html b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Product.Encoded.html index 5bd80b6e84..4cad81d700 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Product.Encoded.html +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Product.Encoded.html @@ -1,6 +1,3 @@ - - - diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Product.html b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Product.html index 805c8d10ed..a4769331ce 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Product.html +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Product.html @@ -1,6 +1,3 @@ - - - diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.ProductList.html b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.ProductList.html index ecc8b1c13c..fdd9d419c6 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.ProductList.html +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.ProductList.html @@ -1,4 +1,3 @@ - @@ -57,4 +56,4 @@ Product_2 description
- + \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Script.Encoded.html b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Script.Encoded.html index 90239290b1..0aba62fd84 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Script.Encoded.html +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Script.Encoded.html @@ -1,4 +1,3 @@ - diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Script.html b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Script.html index dfa9051e3c..83e376e90e 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Script.html +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Script.html @@ -1,4 +1,3 @@ - diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/TagHelpersWebSite.Home.About.html b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/TagHelpersWebSite.Home.About.html index 1585eb09fc..5098dd62f2 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/TagHelpersWebSite.Home.About.html +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/TagHelpersWebSite.Home.About.html @@ -1,4 +1,4 @@ - + diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/TagHelpersWebSite.Home.Help.html b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/TagHelpersWebSite.Home.Help.html index 4f4ab05797..5acbcf815e 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/TagHelpersWebSite.Home.Help.html +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/TagHelpersWebSite.Home.Help.html @@ -1,4 +1,4 @@ - + diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/TagHelpersWebSite.Home.Index.html b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/TagHelpersWebSite.Home.Index.html index fa6c2c7e46..0b51dc9e33 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/TagHelpersWebSite.Home.Index.html +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/TagHelpersWebSite.Home.Index.html @@ -1,4 +1,4 @@ - + diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/UpdateDealerVehicle_PopulatesPropertyErrorsInViews.txt b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/UpdateDealerVehicle_PopulatesPropertyErrorsInViews.txt index be69a486e9..0e644ed8d4 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/UpdateDealerVehicle_PopulatesPropertyErrorsInViews.txt +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/UpdateDealerVehicle_PopulatesPropertyErrorsInViews.txt @@ -1,4 +1,4 @@ -
+
TestCarDealer SE diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/UpdateDealerVehicle_PopulatesValidationSummary.txt b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/UpdateDealerVehicle_PopulatesValidationSummary.txt index baf61e3f81..5ede8f708e 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/UpdateDealerVehicle_PopulatesValidationSummary.txt +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/UpdateDealerVehicle_PopulatesValidationSummary.txt @@ -1,4 +1,4 @@ -
+
TestCarDealer SE diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/UpdateDealerVehicle_UpdateSuccessful.txt b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/UpdateDealerVehicle_UpdateSuccessful.txt index d322993b8f..0e6753165b 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/UpdateDealerVehicle_UpdateSuccessful.txt +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/UpdateDealerVehicle_UpdateSuccessful.txt @@ -1,4 +1,4 @@ -
+
  • Vin: 8chars diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/ValidationWebSite.Aria.RemoteAttribute_Home.Create.html b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/ValidationWebSite.Aria.RemoteAttribute_Home.Create.html index aa7de18bba..24670ea225 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/ValidationWebSite.Aria.RemoteAttribute_Home.Create.html +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/ValidationWebSite.Aria.RemoteAttribute_Home.Create.html @@ -1,4 +1,4 @@ - + diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/ValidationWebSite.Root.RemoteAttribute_Home.Create.html b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/ValidationWebSite.Root.RemoteAttribute_Home.Create.html index 12f2bdab67..809e0f3c41 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/ValidationWebSite.Root.RemoteAttribute_Home.Create.html +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/ValidationWebSite.Root.RemoteAttribute_Home.Create.html @@ -1,4 +1,4 @@ - + diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/ViewEngineController.ViewWithPaths.txt b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/ViewEngineController.ViewWithPaths.txt index 5c2e3458e3..12e73433d8 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/ViewEngineController.ViewWithPaths.txt +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/ViewEngineController.ViewWithPaths.txt @@ -1,4 +1,4 @@ - + /Views/ViewWithPaths/_Layout.cshtml /Views/ViewWithPaths/Index.cshtml diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/project.json b/test/Microsoft.AspNet.Mvc.FunctionalTests/project.json index e1e203142d..1821706094 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/project.json +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/project.json @@ -3,6 +3,7 @@ "../Microsoft.AspNet.Mvc.Xml.Test/XmlAssert.cs" ], "compilationOptions": { + "define": [ "__RemoveThisBitTo__GENERATE_BASELINES" ], "warningsAsErrors": "true" }, "dependencies": { diff --git a/test/Microsoft.AspNet.Mvc.Razor.Host.Test/MvcRazorHostTest.cs b/test/Microsoft.AspNet.Mvc.Razor.Host.Test/MvcRazorHostTest.cs index 0fbac79109..ca075738d8 100644 --- a/test/Microsoft.AspNet.Mvc.Razor.Host.Test/MvcRazorHostTest.cs +++ b/test/Microsoft.AspNet.Mvc.Razor.Host.Test/MvcRazorHostTest.cs @@ -4,6 +4,9 @@ using System; using System.Collections.Generic; using System.IO; +using System.Linq; +using System.Reflection; +using System.Text; using Microsoft.AspNet.Mvc.Razor.Directives; using Microsoft.AspNet.Mvc.Razor.Internal; using Microsoft.AspNet.Razor; @@ -19,6 +22,8 @@ namespace Microsoft.AspNet.Mvc.Razor { public class MvcRazorHostTest { + private static Assembly _assembly = typeof(MvcRazorHostTest).Assembly; + [Theory] [InlineData("//")] [InlineData("C:/")] @@ -159,6 +164,40 @@ namespace Microsoft.AspNet.Mvc.Razor RunRuntimeTest(host, scenarioName); } + [Fact] + public void BasicVisitor_GeneratesCorrectLineMappings() + { + // Arrange + var fileProvider = new TestFileProvider(); + var host = new MvcRazorHost(new DefaultChunkTreeCache(fileProvider)) + { + DesignTimeMode = true + }; + host.NamespaceImports.Clear(); + var expectedLineMappings = new[] + { + BuildLineMapping( + documentAbsoluteIndex: 13, + documentLineIndex: 0, + documentCharacterIndex: 13, + generatedAbsoluteIndex: 1269, + generatedLineIndex: 32, + generatedCharacterIndex: 13, + contentLength: 4), + BuildLineMapping( + documentAbsoluteIndex: 43, + documentLineIndex: 2, + documentCharacterIndex: 5, + generatedAbsoluteIndex: 1353, + generatedLineIndex: 37, + generatedCharacterIndex: 6, + contentLength: 21), + }; + + // Act and Assert + RunDesignTimeTest(host, "Basic", expectedLineMappings); + } + [Fact] public void InjectVisitor_GeneratesCorrectLineMappings() { @@ -246,19 +285,25 @@ namespace Microsoft.AspNet.Mvc.Razor string testName) { var inputFile = "TestFiles/Input/" + testName + ".cshtml"; - var expectedCode = ReadResource("TestFiles/Output/Runtime/" + testName + ".cs"); + var outputFile = "TestFiles/Output/Runtime/" + testName + ".cs"; + var expectedCode = ResourceFile.ReadResource(_assembly, outputFile, sourceFile: false); // Act GeneratorResults results; - using (var stream = GetResourceStream(inputFile)) + using (var stream = ResourceFile.GetResourceStream(_assembly, inputFile, sourceFile: true)) { results = host.GenerateCode(inputFile, stream); } // Assert Assert.True(results.Success); - Assert.Equal(expectedCode, results.GeneratedCode); Assert.Empty(results.ParserErrors); + +#if GENERATE_BASELINES + ResourceFile.UpdateFile(_assembly, outputFile, expectedCode, results.GeneratedCode); +#else + Assert.Equal(expectedCode, results.GeneratedCode); +#endif } private static void RunDesignTimeTest(MvcRazorHost host, @@ -266,39 +311,60 @@ namespace Microsoft.AspNet.Mvc.Razor IEnumerable expectedLineMappings) { var inputFile = "TestFiles/Input/" + testName + ".cshtml"; - var expectedCode = ReadResource("TestFiles/Output/DesignTime/" + testName + ".cs"); + var outputFile = "TestFiles/Output/DesignTime/" + testName + ".cs"; + var expectedCode = ResourceFile.ReadResource(_assembly, outputFile, sourceFile: false); // Act GeneratorResults results; - using (var stream = GetResourceStream(inputFile)) + using (var stream = ResourceFile.GetResourceStream(_assembly, inputFile, sourceFile: true)) { results = host.GenerateCode(inputFile, stream); } // Assert Assert.True(results.Success); - Assert.Equal(expectedCode, results.GeneratedCode); Assert.Empty(results.ParserErrors); - Assert.Equal(expectedLineMappings, results.DesignTimeLineMappings); - } - private static string ReadResource(string resourceName) - { - using (var stream = GetResourceStream(resourceName)) +#if GENERATE_BASELINES + ResourceFile.UpdateFile(_assembly, outputFile, expectedCode, results.GeneratedCode); + + Assert.NotNull(results.DesignTimeLineMappings); // Guard + if (expectedLineMappings == null || + !Enumerable.SequenceEqual(expectedLineMappings, results.DesignTimeLineMappings)) { - using (var streamReader = new StreamReader(stream)) + var lineMappings = new StringBuilder(); + lineMappings.AppendLine($"// !!! Do not check in. Instead paste content into test method. !!!"); + lineMappings.AppendLine(); + + var indent = " "; + lineMappings.AppendLine($"{ indent }var expectedLineMappings = new[]"); + lineMappings.AppendLine($"{ indent }{{"); + foreach (var lineMapping in results.DesignTimeLineMappings) { - return streamReader.ReadToEnd(); + var innerIndent = indent + " "; + var documentLocation = lineMapping.DocumentLocation; + var generatedLocation = lineMapping.GeneratedLocation; + lineMappings.AppendLine($"{ innerIndent }{ nameof(BuildLineMapping) }("); + + innerIndent += " "; + lineMappings.AppendLine($"{ innerIndent }documentAbsoluteIndex: { documentLocation.AbsoluteIndex },"); + lineMappings.AppendLine($"{ innerIndent }documentLineIndex: { documentLocation.LineIndex },"); + lineMappings.AppendLine($"{ innerIndent }documentCharacterIndex: { documentLocation.CharacterIndex },"); + lineMappings.AppendLine($"{ innerIndent }generatedAbsoluteIndex: { generatedLocation.AbsoluteIndex },"); + lineMappings.AppendLine($"{ innerIndent }generatedLineIndex: { generatedLocation.LineIndex },"); + lineMappings.AppendLine($"{ innerIndent }generatedCharacterIndex: { generatedLocation.CharacterIndex },"); + lineMappings.AppendLine($"{ innerIndent }contentLength: { generatedLocation.ContentLength }),"); } + + lineMappings.AppendLine($"{ indent }}};"); + + var lineMappingFile = Path.ChangeExtension(outputFile, "lineMappings.cs"); + ResourceFile.UpdateFile(_assembly, lineMappingFile, previousContent: null, content: lineMappings.ToString()); } - } - - private static Stream GetResourceStream(string resourceName) - { - resourceName = "Microsoft.AspNet.Mvc.Razor.Host.Test." + resourceName.Replace('/', '.'); - - var assembly = typeof(MvcRazorHostTest).Assembly; - return assembly.GetManifestResourceStream(resourceName); +#else + Assert.Equal(expectedCode, results.GeneratedCode); + Assert.Equal(expectedLineMappings, results.DesignTimeLineMappings); +#endif } private static LineMapping BuildLineMapping(int documentAbsoluteIndex, @@ -345,36 +411,43 @@ namespace Microsoft.AspNet.Mvc.Razor { public TestMvcRazorHost(IChunkTreeCache ChunkTreeCache) : base(ChunkTreeCache) - { } + { + } - public override CodeGenerator DecorateCodeGenerator(CodeGenerator incomingBuilder, CodeGeneratorContext context) + public override CodeGenerator DecorateCodeGenerator( + CodeGenerator incomingBuilder, + CodeGeneratorContext context) { base.DecorateCodeGenerator(incomingBuilder, context); - return new TestCSharpCodeGenerator(context, - DefaultModel, - "Microsoft.AspNet.Mvc.Razor.Internal.RazorInjectAttribute", - new GeneratedTagHelperAttributeContext - { - ModelExpressionTypeName = ModelExpressionType, - CreateModelExpressionMethodName = CreateModelExpressionMethod - }); + return new TestCSharpCodeGenerator( + context, + DefaultModel, + "Microsoft.AspNet.Mvc.Razor.Internal.RazorInjectAttribute", + new GeneratedTagHelperAttributeContext + { + ModelExpressionTypeName = ModelExpressionType, + CreateModelExpressionMethodName = CreateModelExpressionMethod + }); } protected class TestCSharpCodeGenerator : MvcCSharpCodeGenerator { private readonly GeneratedTagHelperAttributeContext _tagHelperAttributeContext; - public TestCSharpCodeGenerator(CodeGeneratorContext context, - string defaultModel, - string activateAttribute, - GeneratedTagHelperAttributeContext tagHelperAttributeContext) + public TestCSharpCodeGenerator( + CodeGeneratorContext context, + string defaultModel, + string activateAttribute, + GeneratedTagHelperAttributeContext tagHelperAttributeContext) : base(context, defaultModel, activateAttribute, tagHelperAttributeContext) { _tagHelperAttributeContext = tagHelperAttributeContext; } - protected override CSharpCodeVisitor CreateCSharpCodeVisitor(CSharpCodeWriter writer, CodeGeneratorContext context) + protected override CSharpCodeVisitor CreateCSharpCodeVisitor( + CSharpCodeWriter writer, + CodeGeneratorContext context) { var visitor = base.CreateCSharpCodeVisitor(writer, context); visitor.TagHelperRenderer = new NoUniqueIdsTagHelperCodeRenderer(visitor, writer, context) @@ -387,11 +460,13 @@ namespace Microsoft.AspNet.Mvc.Razor private class NoUniqueIdsTagHelperCodeRenderer : CSharpTagHelperCodeRenderer { - public NoUniqueIdsTagHelperCodeRenderer(IChunkVisitor bodyVisitor, - CSharpCodeWriter writer, - CodeGeneratorContext context) + public NoUniqueIdsTagHelperCodeRenderer( + IChunkVisitor bodyVisitor, + CSharpCodeWriter writer, + CodeGeneratorContext context) : base(bodyVisitor, writer, context) - { } + { + } protected override string GenerateUniqueId() { diff --git a/test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/Output/DesignTime/Basic.cs b/test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/Output/DesignTime/Basic.cs index 6edb207f43..70e14bcb89 100644 --- a/test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/Output/DesignTime/Basic.cs +++ b/test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/Output/DesignTime/Basic.cs @@ -1,14 +1,15 @@ -namespace Asp +namespace Asp { - using System; - using System.Linq; - using System.Collections.Generic; - using Microsoft.AspNet.Mvc; - using Microsoft.AspNet.Mvc.Rendering; using System.Threading.Tasks; public class ASPV_TestFiles_Input_Basic_cshtml : Microsoft.AspNet.Mvc.Razor.RazorPage { + private static object @__o; + private void @__RazorDesignTimeHelpers__() + { + #pragma warning disable 219 + #pragma warning restore 219 + } #line hidden public ASPV_TestFiles_Input_Basic_cshtml() { @@ -28,33 +29,16 @@ #pragma warning disable 1998 public override async Task ExecuteAsync() { - PageExecutionContext.BeginContext(0, 4, true); - WriteLiteral("( #line 1 "TestFiles/Input/Basic.cshtml" - logo + __o = logo; #line default #line hidden - , 12), false)); - PageExecutionContext.BeginContext(18, 24, true); - WriteLiteral(">\r\n Hello world\r\n "); - PageExecutionContext.EndContext(); - PageExecutionContext.BeginContext(43, 21, false); - Write( #line 3 "TestFiles/Input/Basic.cshtml" - Html.Input("SomeKey") +__o = Html.Input("SomeKey"); #line default #line hidden - ); - - PageExecutionContext.EndContext(); - PageExecutionContext.BeginContext(64, 8, true); - WriteLiteral("\r\n
"); - PageExecutionContext.EndContext(); } #pragma warning restore 1998 } diff --git a/test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/Output/DesignTime/Inject.cs b/test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/Output/DesignTime/Inject.cs index e5d1696dfc..a7faa99fa2 100644 --- a/test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/Output/DesignTime/Inject.cs +++ b/test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/Output/DesignTime/Inject.cs @@ -1,4 +1,4 @@ -namespace Asp +namespace Asp { #line 1 "TestFiles/Input/Inject.cshtml" using MyNamespace diff --git a/test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/Output/DesignTime/InjectWithModel.cs b/test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/Output/DesignTime/InjectWithModel.cs index 394d7e258a..037c920c0d 100644 --- a/test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/Output/DesignTime/InjectWithModel.cs +++ b/test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/Output/DesignTime/InjectWithModel.cs @@ -1,4 +1,4 @@ -namespace Asp +namespace Asp { using System.Threading.Tasks; diff --git a/test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/Output/DesignTime/InjectWithSemicolon.cs b/test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/Output/DesignTime/InjectWithSemicolon.cs index 60a4238f38..c1ad592cac 100644 --- a/test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/Output/DesignTime/InjectWithSemicolon.cs +++ b/test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/Output/DesignTime/InjectWithSemicolon.cs @@ -1,4 +1,4 @@ -namespace Asp +namespace Asp { using System.Threading.Tasks; diff --git a/test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/Output/DesignTime/Model.cs b/test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/Output/DesignTime/Model.cs index 77aa9dfb93..d2ca7a6afa 100644 --- a/test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/Output/DesignTime/Model.cs +++ b/test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/Output/DesignTime/Model.cs @@ -1,4 +1,4 @@ -namespace Asp +namespace Asp { using System.Threading.Tasks; diff --git a/test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/Output/DesignTime/ModelExpressionTagHelper.cs b/test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/Output/DesignTime/ModelExpressionTagHelper.cs index ea138cc2d7..295b1a8313 100644 --- a/test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/Output/DesignTime/ModelExpressionTagHelper.cs +++ b/test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/Output/DesignTime/ModelExpressionTagHelper.cs @@ -1,4 +1,4 @@ -namespace Asp +namespace Asp { using Microsoft.AspNet.Razor.Runtime.TagHelpers; using System; diff --git a/test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/Output/Runtime/Basic.cs b/test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/Output/Runtime/Basic.cs index c110ff84e2..798465d637 100644 --- a/test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/Output/Runtime/Basic.cs +++ b/test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/Output/Runtime/Basic.cs @@ -1,4 +1,4 @@ -#pragma checksum "TestFiles/Input/Basic.cshtml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "63d2634be31f68aa89a0c1561d67c73cc446f3d4" +#pragma checksum "TestFiles/Input/Basic.cshtml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "63d2634be31f68aa89a0c1561d67c73cc446f3d4" namespace Asp { using System; diff --git a/test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/Output/Runtime/Inject.cs b/test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/Output/Runtime/Inject.cs index 75267ad35a..dd8b2f351f 100644 --- a/test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/Output/Runtime/Inject.cs +++ b/test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/Output/Runtime/Inject.cs @@ -1,4 +1,4 @@ -#pragma checksum "TestFiles/Input/Inject.cshtml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "424b7fe9f12352c59210b5fa8c74ca8c9c67de81" +#pragma checksum "TestFiles/Input/Inject.cshtml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "424b7fe9f12352c59210b5fa8c74ca8c9c67de81" namespace Asp { #line 1 "TestFiles/Input/Inject.cshtml" diff --git a/test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/Output/Runtime/InjectWithModel.cs b/test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/Output/Runtime/InjectWithModel.cs index 7bc5ff5571..59efea9808 100644 --- a/test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/Output/Runtime/InjectWithModel.cs +++ b/test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/Output/Runtime/InjectWithModel.cs @@ -1,4 +1,4 @@ -#pragma checksum "TestFiles/Input/InjectWithModel.cshtml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "0c0e10d3fd8f5bf30eabc22ca0ee91355a13426d" +#pragma checksum "TestFiles/Input/InjectWithModel.cshtml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "0c0e10d3fd8f5bf30eabc22ca0ee91355a13426d" namespace Asp { using System; diff --git a/test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/Output/Runtime/InjectWithSemicolon.cs b/test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/Output/Runtime/InjectWithSemicolon.cs index 55d8d70e1a..a34f1a970c 100644 --- a/test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/Output/Runtime/InjectWithSemicolon.cs +++ b/test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/Output/Runtime/InjectWithSemicolon.cs @@ -1,4 +1,4 @@ -#pragma checksum "TestFiles/Input/InjectWithSemicolon.cshtml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "b753615982659a9805e6213ceced76ba06782038" +#pragma checksum "TestFiles/Input/InjectWithSemicolon.cshtml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "b753615982659a9805e6213ceced76ba06782038" namespace Asp { using System; diff --git a/test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/Output/Runtime/Model.cs b/test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/Output/Runtime/Model.cs index e6f0f525ce..c63c1223a8 100644 --- a/test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/Output/Runtime/Model.cs +++ b/test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/Output/Runtime/Model.cs @@ -1,4 +1,4 @@ -#pragma checksum "TestFiles/Input/Model.cshtml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "2c1e88396568d309c236020e59bf2abacfadd612" +#pragma checksum "TestFiles/Input/Model.cshtml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "2c1e88396568d309c236020e59bf2abacfadd612" namespace Asp { using System; diff --git a/test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/Output/Runtime/ModelExpressionTagHelper.cs b/test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/Output/Runtime/ModelExpressionTagHelper.cs index b08bd7d25c..1d4a044e51 100644 --- a/test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/Output/Runtime/ModelExpressionTagHelper.cs +++ b/test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/Output/Runtime/ModelExpressionTagHelper.cs @@ -1,4 +1,4 @@ -#pragma checksum "TestFiles/Input/ModelExpressionTagHelper.cshtml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "3b1d4af116a70f83c556ece1980f2e9364e6baa7" +#pragma checksum "TestFiles/Input/ModelExpressionTagHelper.cshtml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "3b1d4af116a70f83c556ece1980f2e9364e6baa7" namespace Asp { using Microsoft.AspNet.Razor.Runtime.TagHelpers; diff --git a/test/Microsoft.AspNet.Mvc.Razor.Host.Test/project.json b/test/Microsoft.AspNet.Mvc.Razor.Host.Test/project.json index 73ab1e681a..bc0a6e284c 100644 --- a/test/Microsoft.AspNet.Mvc.Razor.Host.Test/project.json +++ b/test/Microsoft.AspNet.Mvc.Razor.Host.Test/project.json @@ -10,6 +10,9 @@ "commands": { "test": "xunit.runner.aspnet" }, + "compilationOptions": { + "define": [ "__RemoveThisBitTo__GENERATE_BASELINES" ] + }, "frameworks": { "dnx451": { "dependencies": { diff --git a/test/Microsoft.AspNet.Mvc.TestCommon/ResourceFile.cs b/test/Microsoft.AspNet.Mvc.TestCommon/ResourceFile.cs new file mode 100644 index 0000000000..ba79302d83 --- /dev/null +++ b/test/Microsoft.AspNet.Mvc.TestCommon/ResourceFile.cs @@ -0,0 +1,245 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.Diagnostics; +using System.IO; +using System.Reflection; +using System.Threading.Tasks; +using Xunit; + +namespace Microsoft.AspNet.Mvc +{ + /// + /// Reader and, if GENERATE_BASELINES is defined, writer for files compiled into an assembly as resources. + /// + /// Inspired by Razor's BaselineWriter and TestFile test classes. + public static class ResourceFile + { + private static object writeLock = new object(); + + /// + /// Return for from 's + /// manifest. + /// + /// The containing . + /// + /// Name of the manifest resource in . A path relative to the test project + /// directory. + /// + /// + /// If true is used as a source file and must exist. Otherwise + /// is an output file and, if GENERATE_BASELINES is defined, it will + /// soon be generated if missing. + /// + /// + /// for from 's + /// manifest. null if GENERATE_BASELINES is defined, is + /// false, and is not found in . + /// + /// + /// Thrown if GENERATE_BASELINES is not defined or is true and + /// is not found in . + /// + public static Stream GetResourceStream(Assembly assembly, string resourceName, bool sourceFile) + { + // The DNX runtime compiles every file under the resources folder as a resource available at runtime with + // the same name as the file name. + var fullName = $"{ assembly.GetName().Name }.{ resourceName.Replace('/', '.') }"; + if (!Exists(assembly, fullName)) + { +#if GENERATE_BASELINES + if (sourceFile) + { + // Even when generating baselines, a missing source file is a serious problem. + Assert.True(false, $"Manifest resource: { fullName } not found."); + } +#else + // When not generating baselines, a missing source or output file is always an error. + Assert.True(false, $"Manifest resource '{ fullName }' not found."); +#endif + + return null; + } + + return assembly.GetManifestResourceStream(fullName); + } + + /// + /// Return content of from 's + /// manifest. + /// + /// The containing . + /// + /// Name of the manifest resource in . A path relative to the test project + /// directory. + /// + /// + /// If true is used as a source file and must exist. Otherwise + /// is an output file and, if GENERATE_BASELINES is defined, it will + /// soon be generated if missing. + /// + /// + /// A which on completion returns the content of + /// from 's manifest. null if + /// GENERATE_BASELINES is defined, is false, and + /// is not found in . + /// + /// + /// Thrown if GENERATE_BASELINES is not defined or is true and + /// is not found in . + /// + /// Normalizes line endings to . + public static async Task ReadResourceAsync(Assembly assembly, string resourceName, bool sourceFile) + { + using (var stream = GetResourceStream(assembly, resourceName, sourceFile)) + { + if (stream == null) + { + return null; + } + + using (var streamReader = new StreamReader(stream)) + { + var content = await streamReader.ReadToEndAsync(); + + // Normalize line endings to Environment.NewLine. This removes core.autocrlf, core.eol, + // core.safecrlf, and .gitattributes from the equation and matches what MVC returns. + return content + .Replace("\r", string.Empty) + .Replace("\n", Environment.NewLine); + } + } + } + + /// + /// Return content of from 's + /// manifest. + /// + /// The containing . + /// + /// Name of the manifest resource in . A path relative to the test project + /// directory. + /// + /// + /// If true is used as a source file and must exist. Otherwise + /// is an output file and, if GENERATE_BASELINES is defined, it will + /// soon be generated if missing. + /// + /// + /// The content of from 's + /// manifest. null if GENERATE_BASELINES is defined, is + /// false, and is not found in . + /// + /// + /// Thrown if GENERATE_BASELINES is not defined or is true and + /// is not found in . + /// + /// Normalizes line endings to . + public static string ReadResource(Assembly assembly, string resourceName, bool sourceFile) + { + using (var stream = GetResourceStream(assembly, resourceName, sourceFile)) + { + if (stream == null) + { + return null; + } + + using (var streamReader = new StreamReader(stream)) + { + var content = streamReader.ReadToEnd(); + + // Normalize line endings to Environment.NewLine. This removes core.autocrlf, core.eol, + // core.safecrlf, and .gitattributes from the equation and matches what MVC returns. + return content + .Replace("\r", string.Empty) + .Replace("\n", Environment.NewLine); + } + } + } + + /// + /// Write to file that will become in + /// the next time the project is built. Does nothing if + /// and already match. + /// + /// The containing . + /// + /// Name of the manifest resource in . A path relative to the test project + /// directory. + /// + /// + /// Current content of . null if does + /// not currently exist in . + /// + /// + /// New content of in . + /// + [Conditional("GENERATE_BASELINES")] + public static void UpdateFile(Assembly assembly, string resourceName, string previousContent, string content) + { + if (!string.Equals(previousContent, content, StringComparison.Ordinal)) + { + // The DNX runtime compiles every file under the resources folder as a resource available at runtime with + // the same name as the file name. Need to update this file on disc. + var projectName = assembly.GetName().Name; + var projectPath = GetProjectPath(projectName); + var fullPath = Path.Combine(projectPath, resourceName); + WriteFile(fullPath, content); + } + } + + private static bool Exists(Assembly assembly, string fullName) + { + var resourceNames = assembly.GetManifestResourceNames(); + foreach (var resourceName in resourceNames) + { + // Resource names are case-sensitive. + if (string.Equals(fullName, resourceName, StringComparison.Ordinal)) + { + return true; + } + } + + return false; + } + + private static string GetProjectPath(string projectName) + { + // Initial guess: Already in the project directory. + var projectPath = Path.GetFullPath("."); + + var currentDirectoryName = new DirectoryInfo(projectPath).Name; + if (!string.Equals(projectName, currentDirectoryName, StringComparison.Ordinal)) + { + // Not running from test project directory. Should be in "test" or solution directory. + if (string.Equals("test", currentDirectoryName, StringComparison.Ordinal)) + { + projectPath = Path.Combine(projectPath, projectName); + } + else + { + projectPath = Path.Combine(projectPath, "test", projectName); + } + } + + return projectPath; + } + + private static void WriteFile(string fullPath, string content) + { + // Serialize writes to minimize contention for file handles and directory access. + lock (writeLock) + { + // Write content to the file, creating it if necessary. + using (var stream = File.Open(fullPath, FileMode.Create, FileAccess.Write)) + { + using (var writer = new StreamWriter(stream)) + { + writer.Write(content); + } + } + } + } + } +}