From 5f69a01cc5cf8766d4d9148561e305d5f054fcdb Mon Sep 17 00:00:00 2001 From: Pranav K Date: Wed, 14 Mar 2018 23:09:28 -0700 Subject: [PATCH] RazorSdk - Attribute code generation fixes * Allow the type name for ProvideApplicationPartFactoryAttribute to be specified. * Generate ReleatedAssemblyAttribute in class library projects --- ...soft.AspNetCore.Mvc.Razor.Extensions.props | 8 ++- ...ft.AspNetCore.Mvc.Razor.Extensions.targets | 14 +++- ...NET.Sdk.Razor.GenerateAssemblyInfo.targets | 3 + .../Sdk.Razor.CurrentVersion.targets | 4 +- .../IntegrationTests/BuildIntegrationTest.cs | 70 +++++++++++++++++++ 5 files changed, 93 insertions(+), 6 deletions(-) diff --git a/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/build/netstandard2.0/Microsoft.AspNetCore.Mvc.Razor.Extensions.props b/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/build/netstandard2.0/Microsoft.AspNetCore.Mvc.Razor.Extensions.props index ef8766cdf2..bf2a6f9c88 100644 --- a/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/build/netstandard2.0/Microsoft.AspNetCore.Mvc.Razor.Extensions.props +++ b/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/build/netstandard2.0/Microsoft.AspNetCore.Mvc.Razor.Extensions.props @@ -11,6 +11,12 @@ --> MVC-2.1 + + true + <_MvcExtensionAssemblyPath Condition="'$(_MvcExtensionAssemblyPath)'==''">$(MSBuildThisFileDirectory)..\..\lib\netstandard2.0\Microsoft.AspNetCore.Mvc.Razor.Extensions.dll @@ -32,4 +38,4 @@ $(_MvcExtensionAssemblyPath) - \ No newline at end of file + diff --git a/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/build/netstandard2.0/Microsoft.AspNetCore.Mvc.Razor.Extensions.targets b/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/build/netstandard2.0/Microsoft.AspNetCore.Mvc.Razor.Extensions.targets index 5fb7dab0e3..a1fd205e62 100644 --- a/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/build/netstandard2.0/Microsoft.AspNetCore.Mvc.Razor.Extensions.targets +++ b/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/build/netstandard2.0/Microsoft.AspNetCore.Mvc.Razor.Extensions.targets @@ -4,7 +4,9 @@ - true + true + + $(GenerateRazorAssemblyInfo) diff --git a/test/Microsoft.AspNetCore.Razor.Design.Test/IntegrationTests/BuildIntegrationTest.cs b/test/Microsoft.AspNetCore.Razor.Design.Test/IntegrationTests/BuildIntegrationTest.cs index 4dbd856543..cce650efc1 100644 --- a/test/Microsoft.AspNetCore.Razor.Design.Test/IntegrationTests/BuildIntegrationTest.cs +++ b/test/Microsoft.AspNetCore.Razor.Design.Test/IntegrationTests/BuildIntegrationTest.cs @@ -367,6 +367,32 @@ namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests Assert.FileContains(result, razorAssemblyInfoPath, "[assembly: Microsoft.AspNetCore.Mvc.ApplicationParts.ProvideApplicationPartFactoryAttribute(\"Microsoft.AspNetCore.Mvc.ApplicationParts.CompiledRazorAssemblyApplicationPartFac\""); } + [Fact] + [InitializeTestProject("ClassLibrary")] + public async Task Build_UsesSpecifiedApplicationPartFactoryTypeName() + { + var razorAssemblyInfo = Path.Combine(IntermediateOutputPath, "ClassLibrary.RazorAssemblyInfo.cs"); + var result = await DotnetMSBuild("Build", "/p:ProvideApplicationPartFactoryAttributeTypeName=CustomFactory"); + + Assert.BuildPassed(result); + + Assert.FileExists(result, razorAssemblyInfo); + Assert.FileContains(result, razorAssemblyInfo, "[assembly: Microsoft.AspNetCore.Mvc.ApplicationParts.ProvideApplicationPartFactoryAttribute(\"CustomFactory\""); + } + + [Fact] + [InitializeTestProject("ClassLibrary")] + public async Task Build_UsesNullFactory_IfEnableDefaultCompiledViewAssemblyLoadBehaviorIsSetToFalse() + { + var razorAssemblyInfo = Path.Combine(IntermediateOutputPath, "ClassLibrary.RazorAssemblyInfo.cs"); + var result = await DotnetMSBuild("Build", "/p:EnableDefaultCompiledViewAssemblyLoadBehavior=false"); + + Assert.BuildPassed(result); + + Assert.FileExists(result, razorAssemblyInfo); + Assert.FileContains(result, razorAssemblyInfo, "[assembly: Microsoft.AspNetCore.Mvc.ApplicationParts.ProvideApplicationPartFactoryAttribute(\"Microsoft.AspNetCore.Mvc.ApplicationParts.NullApplicationPartFactory"); + } + [Fact] [InitializeTestProject("SimpleMvc")] public async Task Build_DoesNotAddRelatedAssemblyPart_IfToolSetIsNotRazorSdk() @@ -412,6 +438,50 @@ namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests Assert.FileDoesNotExist(result, IntermediateOutputPath, "SimpleMvc.RazorAssemblyInfo.cs"); } + [Fact] + [InitializeTestProject("SimpleMvc")] + public async Task Build_WithGenerateRazorHostingAssemblyInfoFalse_DoesNotGenerateHostingAttributes() + { + var assemblyInfo = Path.Combine(IntermediateOutputPath, "SimpleMvc.AssemblyInfo.cs"); + var result = await DotnetMSBuild("Build", "/p:GenerateRazorHostingAssemblyInfo=false"); + + Assert.BuildPassed(result); + + Assert.FileExists(result, assemblyInfo); + Assert.FileDoesNotContain(result, assemblyInfo, "Microsoft.AspNetCore.Razor.Hosting.RazorLanguageVersionAttribute"); + Assert.FileDoesNotContain(result, assemblyInfo, "Microsoft.AspNetCore.Razor.Hosting.RazorConfigurationNameAttribute"); + Assert.FileDoesNotContain(result, assemblyInfo, "Microsoft.AspNetCore.Razor.Hosting.RazorExtensionAssemblyNameAttribute"); + } + + [Fact] + [InitializeTestProject("ClassLibrary")] + public async Task Build_DoesNotGenerateHostingAttributes_InClassLibraryProjects() + { + var assemblyInfo = Path.Combine(IntermediateOutputPath, "ClassLibrary.AssemblyInfo.cs"); + var result = await DotnetMSBuild("Build"); + + Assert.BuildPassed(result); + + Assert.FileExists(result, assemblyInfo); + Assert.FileDoesNotContain(result, assemblyInfo, "Microsoft.AspNetCore.Razor.Hosting.RazorLanguageVersionAttribute"); + Assert.FileDoesNotContain(result, assemblyInfo, "Microsoft.AspNetCore.Razor.Hosting.RazorConfigurationNameAttribute"); + Assert.FileDoesNotContain(result, assemblyInfo, "Microsoft.AspNetCore.Razor.Hosting.RazorExtensionAssemblyNameAttribute"); + } + + [Fact] + [InitializeTestProject("SimpleMvc")] + public async Task Build_GeneratesHostingAttributesByDefault() + { + var assemblyInfo = Path.Combine(IntermediateOutputPath, "SimpleMvc.AssemblyInfo.cs"); + var result = await DotnetMSBuild("Build", "/p:GenerateRazorHostingAssemblyInfo=false"); + + Assert.BuildPassed(result); + + Assert.FileExists(result, assemblyInfo); + Assert.FileDoesNotContain(result, assemblyInfo, "Microsoft.AspNetCore.Razor.Hosting.RazorLanguageVersionAttribute(\"2.1\")"); + Assert.FileDoesNotContain(result, assemblyInfo, "Microsoft.AspNetCore.Razor.Hosting.RazorConfigurationNameAttribute(\"MVC-2-1\")"); + } + private static DependencyContext ReadDependencyContext(string depsFilePath) { var reader = new DependencyContextJsonReader();