From 310feeb826b3a5ea489925ca83628e96f8af5817 Mon Sep 17 00:00:00 2001 From: jacalvar Date: Thu, 23 Oct 2014 11:26:28 -0700 Subject: [PATCH] Added a functional test for view precompilation --- Mvc.sln | 13 +++ .../PreProcess/RazorPreCompilation.cs | 15 ++++ .../MvcSample.Web/Views/Shared/MyView.cshtml | 4 +- .../PrecompilationWebSite.kproj | 17 ++++ .../PrecompilationTest.cs | 39 +++++++++ .../project.json | 1 + .../PreProcess/RazorPreCompilation.cs | 84 +++++++++++++++++++ .../Controllers/HomeController.cs | 15 ++++ .../PrecompilationWebSite.kproj | 17 ++++ .../WebSites/PrecompilationWebSite/Startup.cs | 24 ++++++ .../Views/Home/Index.cshtml | 2 + .../PrecompilationWebSite/project.json | 13 +++ 12 files changed, 242 insertions(+), 2 deletions(-) create mode 100644 samples/MvcSample.Web/Compiler/PreProcess/RazorPreCompilation.cs create mode 100644 src/PrecompilationWebSite/PrecompilationWebSite.kproj create mode 100644 test/Microsoft.AspNet.Mvc.FunctionalTests/PrecompilationTest.cs create mode 100644 test/WebSites/PrecompilationWebSite/Compiler/PreProcess/RazorPreCompilation.cs create mode 100644 test/WebSites/PrecompilationWebSite/Controllers/HomeController.cs create mode 100644 test/WebSites/PrecompilationWebSite/PrecompilationWebSite.kproj create mode 100644 test/WebSites/PrecompilationWebSite/Startup.cs create mode 100644 test/WebSites/PrecompilationWebSite/Views/Home/Index.cshtml create mode 100644 test/WebSites/PrecompilationWebSite/project.json diff --git a/Mvc.sln b/Mvc.sln index e6a4c9cd93..252acd505d 100644 --- a/Mvc.sln +++ b/Mvc.sln @@ -106,6 +106,8 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.AspNet.Mvc.TagHel EndProject Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.AspNet.Mvc.TagHelpers.Test", "test\Microsoft.AspNet.Mvc.TagHelpers.Test\Microsoft.AspNet.Mvc.TagHelpers.Test.kproj", "{860119ED-3DB1-424D-8D0A-30132A8A7D96}" EndProject +Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "PrecompilationWebSite", "test\WebSites\PrecompilationWebSite\PrecompilationWebSite.kproj", "{59E1BE90-92C1-4D35-ADCC-B69F49077C81}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -566,6 +568,16 @@ Global {860119ED-3DB1-424D-8D0A-30132A8A7D96}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU {860119ED-3DB1-424D-8D0A-30132A8A7D96}.Release|Mixed Platforms.Build.0 = Release|Any CPU {860119ED-3DB1-424D-8D0A-30132A8A7D96}.Release|x86.ActiveCfg = Release|Any CPU + {59E1BE90-92C1-4D35-ADCC-B69F49077C81}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {59E1BE90-92C1-4D35-ADCC-B69F49077C81}.Debug|Any CPU.Build.0 = Debug|Any CPU + {59E1BE90-92C1-4D35-ADCC-B69F49077C81}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU + {59E1BE90-92C1-4D35-ADCC-B69F49077C81}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU + {59E1BE90-92C1-4D35-ADCC-B69F49077C81}.Debug|x86.ActiveCfg = Debug|Any CPU + {59E1BE90-92C1-4D35-ADCC-B69F49077C81}.Release|Any CPU.ActiveCfg = Release|Any CPU + {59E1BE90-92C1-4D35-ADCC-B69F49077C81}.Release|Any CPU.Build.0 = Release|Any CPU + {59E1BE90-92C1-4D35-ADCC-B69F49077C81}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU + {59E1BE90-92C1-4D35-ADCC-B69F49077C81}.Release|Mixed Platforms.Build.0 = Release|Any CPU + {59E1BE90-92C1-4D35-ADCC-B69F49077C81}.Release|x86.ActiveCfg = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -617,5 +629,6 @@ Global {2223120F-D675-40DA-8CD8-11DC14A0B2C7} = {DAAE4C74-D06F-4874-A166-33305D2643CE} {B2347320-308E-4D2B-AEC8-005DFA68B0C9} = {32285FA4-6B46-4D6B-A840-2B13E4C8B58E} {860119ED-3DB1-424D-8D0A-30132A8A7D96} = {3BA657BF-28B1-42DA-B5B0-1C4601FCF7B1} + {59E1BE90-92C1-4D35-ADCC-B69F49077C81} = {16703B76-C9F7-4C75-AE6C-53D92E308E3C} EndGlobalSection EndGlobal diff --git a/samples/MvcSample.Web/Compiler/PreProcess/RazorPreCompilation.cs b/samples/MvcSample.Web/Compiler/PreProcess/RazorPreCompilation.cs new file mode 100644 index 0000000000..004f1ed655 --- /dev/null +++ b/samples/MvcSample.Web/Compiler/PreProcess/RazorPreCompilation.cs @@ -0,0 +1,15 @@ +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using Microsoft.AspNet.Mvc; + +namespace MvcSample.Web +{ + public class MyCompilation : RazorPreCompileModule + { + public MyCompilation(IServiceProvider provider) : base(provider) + { + } + } +} \ No newline at end of file diff --git a/samples/MvcSample.Web/Views/Shared/MyView.cshtml b/samples/MvcSample.Web/Views/Shared/MyView.cshtml index d2ed197537..8fbe48d3f6 100644 --- a/samples/MvcSample.Web/Views/Shared/MyView.cshtml +++ b/samples/MvcSample.Web/Views/Shared/MyView.cshtml @@ -35,9 +35,9 @@ } @functions { - public async Task AsyncValueRetrieval() + public Task AsyncValueRetrieval() { - return "Hello World"; + return Task.FromResult("Hello World"); } public Task RenderHelloWorldPartial(User model) diff --git a/src/PrecompilationWebSite/PrecompilationWebSite.kproj b/src/PrecompilationWebSite/PrecompilationWebSite.kproj new file mode 100644 index 0000000000..61d0148291 --- /dev/null +++ b/src/PrecompilationWebSite/PrecompilationWebSite.kproj @@ -0,0 +1,17 @@ + + + + 14.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) + + + + 7af02e0f-a0cd-4a79-8c48-b5decfdd9df6 + PrecompilationWebSite + + + 2.0 + 34201 + + + \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/PrecompilationTest.cs b/test/Microsoft.AspNet.Mvc.FunctionalTests/PrecompilationTest.cs new file mode 100644 index 0000000000..985905a773 --- /dev/null +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/PrecompilationTest.cs @@ -0,0 +1,39 @@ +// Copyright (c) Microsoft Open Technologies, Inc. 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.Net; +using System.Reflection; +using System.Threading.Tasks; +using Microsoft.AspNet.Builder; +using Microsoft.AspNet.TestHost; +using Xunit; + +namespace Microsoft.AspNet.Mvc.FunctionalTests +{ + public class PrecompilationTest + { + private readonly IServiceProvider _services = TestHelper.CreateServices("PrecompilationWebSite"); + private readonly Action _app = new PrecompilationWebSite.Startup().Configure; + + [Fact] + public async Task PrecompiledView_RendersCorrectly() + { + // Arrange + var server = TestServer.Create(_services, _app); + var client = server.CreateClient(); + + // We will render a view that writes the fully qualified name of the Assembly containing the type of + // the view. If the view is precompiled, this assembly will be PrecompilationWebsite. + var expectedContent = typeof(PrecompilationWebSite.Startup).GetTypeInfo().Assembly.GetName().ToString(); + + // Act + var response = await client.GetAsync("http://localhost/Home/Index"); + var responseContent = await response.Content.ReadAsStringAsync(); + + // Assert + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + Assert.Equal(expectedContent, responseContent); + } + } +} \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/project.json b/test/Microsoft.AspNet.Mvc.FunctionalTests/project.json index f38d7d4cdd..b334fcb8a7 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/project.json +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/project.json @@ -17,6 +17,7 @@ "InlineConstraintsWebSite": "1.0.0", "ModelBindingWebSite": "1.0.0", "MvcSample.Web": "1.0.0", + "PrecompilationWebSite": "1.0.0", "RoutingWebSite": "1.0.0", "RazorWebSite": "1.0.0", "RazorInstrumentationWebsite": "1.0.0", diff --git a/test/WebSites/PrecompilationWebSite/Compiler/PreProcess/RazorPreCompilation.cs b/test/WebSites/PrecompilationWebSite/Compiler/PreProcess/RazorPreCompilation.cs new file mode 100644 index 0000000000..52d8a22f9c --- /dev/null +++ b/test/WebSites/PrecompilationWebSite/Compiler/PreProcess/RazorPreCompilation.cs @@ -0,0 +1,84 @@ +// Copyright (c) Microsoft Open Technologies, Inc. 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.IO; +using System.Runtime.Versioning; +using Microsoft.AspNet.Mvc; +using Microsoft.Framework.DependencyInjection; +using Microsoft.Framework.DependencyInjection.Fallback; +using Microsoft.Framework.Runtime; + +namespace PrecompilationWebSite +{ + public class MyCompilation : RazorPreCompileModule + { + public MyCompilation(IServiceProvider provider) : base(ReplaceProvider(provider)) + { + } + + // When running in memory tests the application base path will point to the functional tests + // project folder. + // We need to replace it to point to the actual precompilation website so that the views can + // be found. + public static IServiceProvider ReplaceProvider(IServiceProvider provider) + { + var originalEnvironment = provider.GetService(); + var newPath = Path.GetFullPath( + Path.Combine( + originalEnvironment.ApplicationBasePath, + "..", + "WebSites", + "PrecompilationWebSite")); + + var precompilationApplicationEnvironment = new PrecompilationApplicationEnvironment( + originalEnvironment, + newPath); + + var collection = new ServiceCollection(); + collection.AddInstance(precompilationApplicationEnvironment); + + return collection.BuildServiceProvider(provider); + } + + private class PrecompilationApplicationEnvironment : IApplicationEnvironment + { + private readonly IApplicationEnvironment _originalApplicationEnvironment; + private readonly string _applicationBasePath; + + public PrecompilationApplicationEnvironment(IApplicationEnvironment original, string appBasePath) + { + _originalApplicationEnvironment = original; + _applicationBasePath = appBasePath; + } + + public string ApplicationName + { + get { return _originalApplicationEnvironment.ApplicationName; } + } + + public string Version + { + get { return _originalApplicationEnvironment.Version; } + } + + public string ApplicationBasePath + { + get { return _applicationBasePath; } + } + + public string Configuration + { + get + { + return _originalApplicationEnvironment.Configuration; + } + } + + public FrameworkName RuntimeFramework + { + get { return _originalApplicationEnvironment.RuntimeFramework; } + } + } + } +} \ No newline at end of file diff --git a/test/WebSites/PrecompilationWebSite/Controllers/HomeController.cs b/test/WebSites/PrecompilationWebSite/Controllers/HomeController.cs new file mode 100644 index 0000000000..bdff9e4876 --- /dev/null +++ b/test/WebSites/PrecompilationWebSite/Controllers/HomeController.cs @@ -0,0 +1,15 @@ +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using Microsoft.AspNet.Mvc; + +namespace PrecompilationWebSite.Controllers +{ + public class HomeController : Controller + { + public IActionResult Index() + { + return View(); + } + } +} diff --git a/test/WebSites/PrecompilationWebSite/PrecompilationWebSite.kproj b/test/WebSites/PrecompilationWebSite/PrecompilationWebSite.kproj new file mode 100644 index 0000000000..87949c61f0 --- /dev/null +++ b/test/WebSites/PrecompilationWebSite/PrecompilationWebSite.kproj @@ -0,0 +1,17 @@ + + + + 14.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) + + + + 59e1be90-92c1-4d35-adcc-b69f49077c81 + PrecompilationWebSite + + + 2.0 + 34231 + + + \ No newline at end of file diff --git a/test/WebSites/PrecompilationWebSite/Startup.cs b/test/WebSites/PrecompilationWebSite/Startup.cs new file mode 100644 index 0000000000..0d99e31a5a --- /dev/null +++ b/test/WebSites/PrecompilationWebSite/Startup.cs @@ -0,0 +1,24 @@ +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using Microsoft.AspNet.Builder; +using Microsoft.AspNet.Routing; +using Microsoft.Framework.DependencyInjection; + +namespace PrecompilationWebSite +{ + public class Startup + { + public void Configure(IApplicationBuilder app) + { + var configuration = app.GetTestConfiguration(); + + app.UseServices(services => + { + services.AddMvc(configuration); + }); + + app.UseMvc(); + } + } +} diff --git a/test/WebSites/PrecompilationWebSite/Views/Home/Index.cshtml b/test/WebSites/PrecompilationWebSite/Views/Home/Index.cshtml new file mode 100644 index 0000000000..97580a0513 --- /dev/null +++ b/test/WebSites/PrecompilationWebSite/Views/Home/Index.cshtml @@ -0,0 +1,2 @@ +@using System.Reflection +@(GetType().GetTypeInfo().Assembly.GetName()) \ No newline at end of file diff --git a/test/WebSites/PrecompilationWebSite/project.json b/test/WebSites/PrecompilationWebSite/project.json new file mode 100644 index 0000000000..74663e7fc8 --- /dev/null +++ b/test/WebSites/PrecompilationWebSite/project.json @@ -0,0 +1,13 @@ +{ + "dependencies": { + "Microsoft.AspNet.Mvc": "6.0.0-*", + "Microsoft.AspNet.Mvc.TestConfiguration": "1.0.0", + "Microsoft.AspNet.Server.IIS": "1.0.0-*", + "Microsoft.AspNet.Server.WebListener": "1.0.0-*" + }, + "frameworks": { + "aspnet50": { }, + "aspnetcore50": { } + }, + "webroot": "." +}