From e0780764087eb49156b365ca51f6c5ba4a9c25b7 Mon Sep 17 00:00:00 2001 From: Pranav K Date: Mon, 24 Nov 2014 14:01:16 -0800 Subject: [PATCH] Adding functional test for RazorViewEngineOptions.FileSystem --- Mvc.sln | 15 ++++++ .../Compilation/RoslynCompilationService.cs | 8 ++- .../RazorViewEngineOptionsTest.cs | 49 +++++++++++++++++++ .../project.json | 1 + .../RazorViewEngineOptions_AdminController.cs | 16 ++++++ .../RazorViewEngineOptions_HomeController.cs | 15 ++++++ .../RazorViewEngineOptions_Admin/Login.cshtml | 1 + .../RazorViewEngineOptions_Home/Index.cshtml | 1 + .../RazorViewEngineOptionsWebsite.kproj | 20 ++++++++ .../RazorViewEngineOptionsWebsite/Startup.cs | 39 +++++++++++++++ .../project.json | 20 ++++++++ .../wwwroot/readme.md | 1 + 12 files changed, 184 insertions(+), 2 deletions(-) create mode 100644 test/Microsoft.AspNet.Mvc.FunctionalTests/RazorViewEngineOptionsTest.cs create mode 100644 test/WebSites/RazorViewEngineOptionsWebsite/Controllers/RazorViewEngineOptions_AdminController.cs create mode 100644 test/WebSites/RazorViewEngineOptionsWebsite/Controllers/RazorViewEngineOptions_HomeController.cs create mode 100644 test/WebSites/RazorViewEngineOptionsWebsite/EmbeddedResources/Areas/Restricted/Views/RazorViewEngineOptions_Admin/Login.cshtml create mode 100644 test/WebSites/RazorViewEngineOptionsWebsite/EmbeddedResources/Views/RazorViewEngineOptions_Home/Index.cshtml create mode 100644 test/WebSites/RazorViewEngineOptionsWebsite/RazorViewEngineOptionsWebsite.kproj create mode 100644 test/WebSites/RazorViewEngineOptionsWebsite/Startup.cs create mode 100644 test/WebSites/RazorViewEngineOptionsWebsite/project.json create mode 100644 test/WebSites/RazorViewEngineOptionsWebsite/wwwroot/readme.md diff --git a/Mvc.sln b/Mvc.sln index 82092028de..f1bc715fb6 100644 --- a/Mvc.sln +++ b/Mvc.sln @@ -106,6 +106,8 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "PrecompilationWebSite", "te EndProject Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "RequestServicesWebSite", "test\WebSites\RequestServicesWebSite\RequestServicesWebSite.kproj", "{F12E9CF0-4958-40C6-A6CD-759185157F84}" EndProject +Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "RazorViewEngineOptionsWebsite", "test\WebSites\RazorViewEngineOptionsWebsite\RazorViewEngineOptionsWebsite.kproj", "{B18ADE62-35DE-4A06-8E1D-EDD6245F7F4D}" +EndProject Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "CompositeViewEngineWebSite", "test\WebSites\CompositeViewEngineWebSite\CompositeViewEngineWebSite.kproj", "{A853B2BA-4449-4908-A416-5A3C027FC22B}" EndProject Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "ValueProvidersWebSite", "test\WebSites\ValueProvidersWebSite\ValueProvidersWebSite.kproj", "{14F79E79-AE79-48FA-95DE-D794EF4EABB3}" @@ -570,6 +572,18 @@ Global {F12E9CF0-4958-40C6-A6CD-759185157F84}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU {F12E9CF0-4958-40C6-A6CD-759185157F84}.Release|Mixed Platforms.Build.0 = Release|Any CPU {F12E9CF0-4958-40C6-A6CD-759185157F84}.Release|x86.ActiveCfg = Release|Any CPU + {B18ADE62-35DE-4A06-8E1D-EDD6245F7F4D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B18ADE62-35DE-4A06-8E1D-EDD6245F7F4D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B18ADE62-35DE-4A06-8E1D-EDD6245F7F4D}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU + {B18ADE62-35DE-4A06-8E1D-EDD6245F7F4D}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU + {B18ADE62-35DE-4A06-8E1D-EDD6245F7F4D}.Debug|x86.ActiveCfg = Debug|Any CPU + {B18ADE62-35DE-4A06-8E1D-EDD6245F7F4D}.Debug|x86.Build.0 = Debug|Any CPU + {B18ADE62-35DE-4A06-8E1D-EDD6245F7F4D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B18ADE62-35DE-4A06-8E1D-EDD6245F7F4D}.Release|Any CPU.Build.0 = Release|Any CPU + {B18ADE62-35DE-4A06-8E1D-EDD6245F7F4D}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU + {B18ADE62-35DE-4A06-8E1D-EDD6245F7F4D}.Release|Mixed Platforms.Build.0 = Release|Any CPU + {B18ADE62-35DE-4A06-8E1D-EDD6245F7F4D}.Release|x86.ActiveCfg = Release|Any CPU + {B18ADE62-35DE-4A06-8E1D-EDD6245F7F4D}.Release|x86.Build.0 = Release|Any CPU {A853B2BA-4449-4908-A416-5A3C027FC22B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {A853B2BA-4449-4908-A416-5A3C027FC22B}.Debug|Any CPU.Build.0 = Debug|Any CPU {A853B2BA-4449-4908-A416-5A3C027FC22B}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU @@ -645,6 +659,7 @@ Global {860119ED-3DB1-424D-8D0A-30132A8A7D96} = {3BA657BF-28B1-42DA-B5B0-1C4601FCF7B1} {59E1BE90-92C1-4D35-ADCC-B69F49077C81} = {16703B76-C9F7-4C75-AE6C-53D92E308E3C} {F12E9CF0-4958-40C6-A6CD-759185157F84} = {16703B76-C9F7-4C75-AE6C-53D92E308E3C} + {B18ADE62-35DE-4A06-8E1D-EDD6245F7F4D} = {16703B76-C9F7-4C75-AE6C-53D92E308E3C} {A853B2BA-4449-4908-A416-5A3C027FC22B} = {16703B76-C9F7-4C75-AE6C-53D92E308E3C} {14F79E79-AE79-48FA-95DE-D794EF4EABB3} = {16703B76-C9F7-4C75-AE6C-53D92E308E3C} EndGlobalSection diff --git a/src/Microsoft.AspNet.Mvc.Razor/Compilation/RoslynCompilationService.cs b/src/Microsoft.AspNet.Mvc.Razor/Compilation/RoslynCompilationService.cs index 9d0df4a6ad..a0a977e037 100644 --- a/src/Microsoft.AspNet.Mvc.Razor/Compilation/RoslynCompilationService.cs +++ b/src/Microsoft.AspNet.Mvc.Razor/Compilation/RoslynCompilationService.cs @@ -54,9 +54,13 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation } /// - public CompilationResult Compile(IFileInfo fileInfo, string compilationContent) + public CompilationResult Compile([NotNull] IFileInfo fileInfo, [NotNull] string compilationContent) { - var syntaxTrees = new[] { SyntaxTreeGenerator.Generate(compilationContent, fileInfo.PhysicalPath) }; + // The path passed to SyntaxTreeGenerator.Generate is used by the compiler to generate symbols (pdb) that + // map to the source file. If a file does not exist on a physical file system, PhysicalPath will be null. + // This prevents files that exist in a non-physical file system from being debugged. + var path = fileInfo.PhysicalPath ?? fileInfo.Name; + var syntaxTrees = new[] { SyntaxTreeGenerator.Generate(compilationContent, path) }; var references = _applicationReferences.Value; diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/RazorViewEngineOptionsTest.cs b/test/Microsoft.AspNet.Mvc.FunctionalTests/RazorViewEngineOptionsTest.cs new file mode 100644 index 0000000000..8358e3ce2a --- /dev/null +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/RazorViewEngineOptionsTest.cs @@ -0,0 +1,49 @@ +// 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.Threading.Tasks; +using Microsoft.AspNet.Builder; +using Microsoft.AspNet.TestHost; +using RazorViewEngineOptionsWebsite; +using Xunit; + +namespace Microsoft.AspNet.Mvc.FunctionalTests +{ + public class RazorViewEngineOptionsTest + { + private readonly IServiceProvider _services = TestHelper.CreateServices(nameof(RazorViewEngineOptionsWebsite)); + private readonly Action _app = new Startup().Configure; + + [Fact] + public async Task RazorViewEngine_UsesFileSystemOnViewEngineOptionsToLocateViews() + { + // Arrange + var expectedMessage = "Hello test-user, this is /RazorViewEngineOptions_Home"; + var server = TestServer.Create(_services, _app); + var client = server.CreateClient(); + + // Act + var response = await client.GetStringAsync("http://localhost/RazorViewEngineOptions_Home?User=test-user"); + + // Assert + Assert.Equal(expectedMessage, response); + } + + [Fact] + public async Task RazorViewEngine_UsesFileSystemOnViewEngineOptionsToLocateAreaViews() + { + // Arrange + var expectedMessage = "Hello admin-user, this is /Restricted/RazorViewEngineOptions_Admin/Login"; + var server = TestServer.Create(_services, _app); + var client = server.CreateClient(); + var target = "http://localhost/Restricted/RazorViewEngineOptions_Admin/Login?AdminUser=admin-user"; + + // Act + var response = await client.GetStringAsync(target); + + // Assert + Assert.Equal(expectedMessage, response); + } + } +} \ 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 6f3b83e729..b5bc4b1b20 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/project.json +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/project.json @@ -21,6 +21,7 @@ "RoutingWebSite": "1.0.0", "RazorWebSite": "1.0.0", "RazorInstrumentationWebsite": "1.0.0", + "RazorViewEngineOptionsWebsite": "1.0.0", "RequestServicesWebSite": "1.0.0", "TagHelperSample.Web": "1.0.0", "TagHelpersWebSite": "1.0.0", diff --git a/test/WebSites/RazorViewEngineOptionsWebsite/Controllers/RazorViewEngineOptions_AdminController.cs b/test/WebSites/RazorViewEngineOptionsWebsite/Controllers/RazorViewEngineOptions_AdminController.cs new file mode 100644 index 0000000000..9d302ed2b2 --- /dev/null +++ b/test/WebSites/RazorViewEngineOptionsWebsite/Controllers/RazorViewEngineOptions_AdminController.cs @@ -0,0 +1,16 @@ +// 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 RazorViewEngineOptionsWebsite.Controllers +{ + [Area("Restricted")] + public class RazorViewEngineOptions_AdminController : Controller + { + public IActionResult Login() + { + return View(); + } + } +} \ No newline at end of file diff --git a/test/WebSites/RazorViewEngineOptionsWebsite/Controllers/RazorViewEngineOptions_HomeController.cs b/test/WebSites/RazorViewEngineOptionsWebsite/Controllers/RazorViewEngineOptions_HomeController.cs new file mode 100644 index 0000000000..3c7d7d6cd5 --- /dev/null +++ b/test/WebSites/RazorViewEngineOptionsWebsite/Controllers/RazorViewEngineOptions_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 RazorViewEngineOptionsWebsite +{ + public class RazorViewEngineOptions_HomeController : Controller + { + public IActionResult Index() + { + return View(); + } + } +} \ No newline at end of file diff --git a/test/WebSites/RazorViewEngineOptionsWebsite/EmbeddedResources/Areas/Restricted/Views/RazorViewEngineOptions_Admin/Login.cshtml b/test/WebSites/RazorViewEngineOptionsWebsite/EmbeddedResources/Areas/Restricted/Views/RazorViewEngineOptions_Admin/Login.cshtml new file mode 100644 index 0000000000..c6ea238bbf --- /dev/null +++ b/test/WebSites/RazorViewEngineOptionsWebsite/EmbeddedResources/Areas/Restricted/Views/RazorViewEngineOptions_Admin/Login.cshtml @@ -0,0 +1 @@ +Hello @Context.Request.Query["AdminUser"], this is @Url.Action() \ No newline at end of file diff --git a/test/WebSites/RazorViewEngineOptionsWebsite/EmbeddedResources/Views/RazorViewEngineOptions_Home/Index.cshtml b/test/WebSites/RazorViewEngineOptionsWebsite/EmbeddedResources/Views/RazorViewEngineOptions_Home/Index.cshtml new file mode 100644 index 0000000000..b673b5ff88 --- /dev/null +++ b/test/WebSites/RazorViewEngineOptionsWebsite/EmbeddedResources/Views/RazorViewEngineOptions_Home/Index.cshtml @@ -0,0 +1 @@ +Hello @Context.Request.Query["User"], this is @Url.Action() \ No newline at end of file diff --git a/test/WebSites/RazorViewEngineOptionsWebsite/RazorViewEngineOptionsWebsite.kproj b/test/WebSites/RazorViewEngineOptionsWebsite/RazorViewEngineOptionsWebsite.kproj new file mode 100644 index 0000000000..359e82f004 --- /dev/null +++ b/test/WebSites/RazorViewEngineOptionsWebsite/RazorViewEngineOptionsWebsite.kproj @@ -0,0 +1,20 @@ + + + + 14.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) + + + + b18ade62-35de-4a06-8e1d-edd6245f7f4d + + + + + + + 2.0 + 53986 + + + \ No newline at end of file diff --git a/test/WebSites/RazorViewEngineOptionsWebsite/Startup.cs b/test/WebSites/RazorViewEngineOptionsWebsite/Startup.cs new file mode 100644 index 0000000000..550d6b3894 --- /dev/null +++ b/test/WebSites/RazorViewEngineOptionsWebsite/Startup.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.IO; +using System.Reflection; +using Microsoft.AspNet.Builder; +using Microsoft.AspNet.FileSystems; +using Microsoft.AspNet.Mvc.Razor; +using Microsoft.Framework.DependencyInjection; +using Microsoft.AspNet.Routing; + +namespace RazorViewEngineOptionsWebsite +{ + public class Startup + { + public void Configure(IApplicationBuilder app) + { + var configuration = app.GetTestConfiguration(); + + app.UseServices(services => + { + services.AddMvc(configuration); + + services.Configure(options => + { + options.FileSystem = new EmbeddedResourceFileSystem(GetType().GetTypeInfo().Assembly, "EmbeddedResources"); + }); + }); + + app.UseMvc(routes => + { + routes.MapRoute("areaRoute", "{area:exists}/{controller}/{action}"); + routes.MapRoute("default", + "{controller}/{action}/{id?}", + new { controller = "Home", action = "Index" }); + }); + } + } +} \ No newline at end of file diff --git a/test/WebSites/RazorViewEngineOptionsWebsite/project.json b/test/WebSites/RazorViewEngineOptionsWebsite/project.json new file mode 100644 index 0000000000..441a3fba60 --- /dev/null +++ b/test/WebSites/RazorViewEngineOptionsWebsite/project.json @@ -0,0 +1,20 @@ +{ + "commands": { + "web": "Microsoft.AspNet.Hosting server=Microsoft.AspNet.Server.WebListener server.urls=http://localhost:5001", + "kestrel": "Microsoft.AspNet.Hosting --server Kestrel --server.urls http://localhost:5000" + }, + "resources": "EmbeddedResources/**", + "dependencies": { + "Kestrel": "1.0.0-*", + "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-*", + "Microsoft.AspNet.StaticFiles": "1.0.0-*" + }, + "frameworks": { + "aspnet50": { }, + "aspnetcore50": { } + }, + "webroot": "wwwroot" +} diff --git a/test/WebSites/RazorViewEngineOptionsWebsite/wwwroot/readme.md b/test/WebSites/RazorViewEngineOptionsWebsite/wwwroot/readme.md new file mode 100644 index 0000000000..edb322b45b --- /dev/null +++ b/test/WebSites/RazorViewEngineOptionsWebsite/wwwroot/readme.md @@ -0,0 +1 @@ +Functional test for usage of RazorViewEngineOptions.FileSystem \ No newline at end of file