Skip to content

Commit 23a3e84

Browse files
authored
Add RazorLangVersion 5.0 (#23408)
* Add RazorLangVersion 5.0 * Fixup more tests. Add a test for 3.1
1 parent 0476892 commit 23a3e84

File tree

9 files changed

+81
-17
lines changed

9 files changed

+81
-17
lines changed

src/Razor/Microsoft.AspNetCore.Razor.Language/src/RazorLanguageVersion.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ public sealed class RazorLanguageVersion : IEquatable<RazorLanguageVersion>, ICo
2020

2121
public static readonly RazorLanguageVersion Version_3_0 = new RazorLanguageVersion(3, 0);
2222

23-
public static readonly RazorLanguageVersion Latest = Version_3_0;
23+
public static readonly RazorLanguageVersion Version_5_0 = new RazorLanguageVersion(5, 0);
24+
25+
public static readonly RazorLanguageVersion Latest = Version_5_0;
2426

2527
public static readonly RazorLanguageVersion Experimental = new RazorLanguageVersion(1337, 1337);
2628

@@ -41,6 +43,11 @@ public static bool TryParse(string languageVersion, out RazorLanguageVersion ver
4143
version = Experimental;
4244
return true;
4345
}
46+
else if (languageVersion == "5.0")
47+
{
48+
version = Version_5_0;
49+
return true;
50+
}
4451
else if (languageVersion == "3.0")
4552
{
4653
version = Version_3_0;
@@ -49,7 +56,7 @@ public static bool TryParse(string languageVersion, out RazorLanguageVersion ver
4956
else if (languageVersion == "2.1")
5057
{
5158
version = Version_2_1;
52-
return true;
59+
return true;
5360
}
5461
else if (languageVersion == "2.0")
5562
{
@@ -84,7 +91,7 @@ public static RazorLanguageVersion Parse(string languageVersion)
8491
}
8592

8693
throw new ArgumentException(
87-
Resources.FormatRazorLanguageVersion_InvalidVersion(languageVersion),
94+
Resources.FormatRazorLanguageVersion_InvalidVersion(languageVersion),
8895
nameof(languageVersion));
8996
}
9097

@@ -131,7 +138,7 @@ public override int GetHashCode()
131138
// We don't need to do anything special for our hash code since reference equality is what we're going for.
132139
return base.GetHashCode();
133140
}
134-
141+
135142
public override string ToString() => $"{Major}.{Minor}";
136143

137144
private string DebuggerToString() => $"Razor '{Major}.{Minor}'";

src/Razor/Microsoft.AspNetCore.Razor.Language/test/RazorLanguageVersionTest.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,20 @@ public void TryParse30()
9292
Assert.Same(RazorLanguageVersion.Version_3_0, version);
9393
}
9494

95+
[Fact]
96+
public void TryParse50()
97+
{
98+
// Arrange
99+
var value = "5.0";
100+
101+
// Act
102+
var result = RazorLanguageVersion.TryParse(value, out var version);
103+
104+
// Assert
105+
Assert.True(result);
106+
Assert.Same(RazorLanguageVersion.Version_5_0, version);
107+
}
108+
95109
[Fact]
96110
public void TryParseLatest()
97111
{
@@ -103,7 +117,7 @@ public void TryParseLatest()
103117

104118
// Assert
105119
Assert.True(result);
106-
Assert.Same(RazorLanguageVersion.Version_3_0, version);
120+
Assert.Same(RazorLanguageVersion.Version_5_0, version);
107121
}
108122

109123
[Fact]

src/Razor/Microsoft.NET.Sdk.Razor/integrationtests/BuildIntegrationTest.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -625,28 +625,26 @@ public async Task Build_WithoutServer_ErrorDuringBuild_DisplaysErrorInMsBuildOut
625625
{
626626
var result = await DotnetMSBuild(
627627
"Build",
628-
"/p:UseRazorBuildServer=false /p:RazorLangVersion=5.0",
628+
"/p:UseRazorBuildServer=false /p:RazorLangVersion=99.0",
629629
suppressBuildServer: true);
630630

631631
Assert.BuildFailed(result);
632632
Assert.BuildOutputContainsLine(
633633
result,
634-
$"Invalid option 5.0 for Razor language version --version; must be Latest or a valid version in range 1.0 to 3.0.");
634+
$"Invalid option 99.0 for Razor language version --version; must be Latest or a valid version in range 1.0 to 5.0.");
635635

636636
// Compilation failed without creating the views assembly
637637
Assert.FileExists(result, IntermediateOutputPath, "SimpleMvc.dll");
638638
Assert.FileDoesNotExist(result, IntermediateOutputPath, "SimpleMvc.Views.dll");
639639
}
640640

641-
[Fact(Skip = "Default C# version is 7.3 for netcoreapp3.1 and later https://github.com/dotnet/aspnetcore/issues/13930")]
641+
[Fact]
642642
[InitializeTestProject("SimpleMvc")]
643643
public async Task Build_ImplicitCSharp8_NullableEnforcement_WarningsDuringBuild_NoBuildServer()
644644
{
645645
var result = await DotnetMSBuild(
646646
"Build",
647-
// Remove /p:LangVersion=Default once we've picked up a compiler that supports .NET 5.0.
648-
// Tracked by https://github.com/dotnet/aspnetcore/issues/13304
649-
"/p:Nullable=enable /p:LangVersion=Default",
647+
"/p:Nullable=enable",
650648
suppressBuildServer: true);
651649
var indexFilePath = Path.Combine(RazorIntermediateOutputPath, "Views", "Home", "Index.cshtml.g.cs");
652650

src/Razor/Microsoft.NET.Sdk.Razor/integrationtests/BuildIntrospectionTest.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public async Task GetCopyToOutputDirectoryItems_WhenNoFileIsPresent_ReturnsEmpty
8888
}
8989

9090
[Fact]
91-
[InitializeTestProject("SimpleMvc")]
91+
[InitializeTestProject("SimpleMvc31")]
9292
public async Task RazorSdk_ResolvesRazorLangVersionTo30ForNetCoreApp30Projects()
9393
{
9494
var result = await DotnetMSBuild("ResolveRazorConfiguration", "/t:_IntrospectResolvedConfiguration");
@@ -98,6 +98,17 @@ public async Task RazorSdk_ResolvesRazorLangVersionTo30ForNetCoreApp30Projects()
9898
Assert.BuildOutputContainsLine(result, "ResolvedRazorConfiguration: MVC-3.0");
9999
}
100100

101+
[Fact]
102+
[InitializeTestProject("SimpleMvc")]
103+
public async Task RazorSdk_ResolvesRazorLangVersionTo50ForNetCoreApp50Projects()
104+
{
105+
var result = await DotnetMSBuild("ResolveRazorConfiguration", "/t:_IntrospectResolvedConfiguration");
106+
107+
Assert.BuildPassed(result);
108+
Assert.BuildOutputContainsLine(result, "RazorLangVersion: 5.0");
109+
Assert.BuildOutputContainsLine(result, "ResolvedRazorConfiguration: MVC-3.0");
110+
}
111+
101112
[Fact]
102113
[InitializeTestProject("ComponentLibrary")]
103114
public async Task RazorSdk_ResolvesRazorLangVersionFromValueSpecified()
@@ -142,7 +153,7 @@ public async Task RazorSdk_ResolvesRazorConfigurationToMvc30()
142153
var result = await DotnetMSBuild("ResolveRazorConfiguration", "/t:_IntrospectResolvedConfiguration");
143154

144155
Assert.BuildPassed(result);
145-
Assert.BuildOutputContainsLine(result, "RazorLangVersion: 3.0");
156+
Assert.BuildOutputContainsLine(result, "RazorLangVersion: 5.0");
146157
Assert.BuildOutputContainsLine(result, "ResolvedRazorConfiguration: MVC-3.0");
147158
}
148159

src/Razor/Microsoft.NET.Sdk.Razor/integrationtests/BuildServerIntegrationTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,12 @@ public async Task Build_ErrorInServer_DisplaysErrorInMsBuildOutput()
100100
{
101101
var result = await DotnetMSBuild(
102102
"Build",
103-
"/p:_RazorForceBuildServer=true /p:RazorLangVersion=5.0");
103+
"/p:_RazorForceBuildServer=true /p:RazorLangVersion=99.0");
104104

105105
Assert.BuildFailed(result);
106106
Assert.BuildOutputContainsLine(
107107
result,
108-
$"Invalid option 5.0 for Razor language version --version; must be Latest or a valid version in range 1.0 to 3.0.");
108+
$"Invalid option 99.0 for Razor language version --version; must be Latest or a valid version in range 1.0 to 5.0.");
109109

110110
// Compilation failed without creating the views assembly
111111
Assert.FileExists(result, IntermediateOutputPath, "SimpleMvc.dll");

src/Razor/Microsoft.NET.Sdk.Razor/integrationtests/ConfigurationMetadataIntegrationTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public async Task Build_WithGenerateRazorHostingAssemblyInfo_AddsConfigurationMe
6161
Assert.FileContainsLine(
6262
result,
6363
razorAssemblyInfo,
64-
"[assembly: Microsoft.AspNetCore.Razor.Hosting.RazorLanguageVersionAttribute(\"3.0\")]");
64+
"[assembly: Microsoft.AspNetCore.Razor.Hosting.RazorLanguageVersionAttribute(\"5.0\")]");
6565
Assert.FileContainsLine(
6666
result,
6767
razorAssemblyInfo,

src/Razor/Microsoft.NET.Sdk.Razor/integrationtests/MvcBuildIntegrationTest31.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System.IO;
5+
using System.Threading.Tasks;
6+
using Xunit;
7+
48
namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests
59
{
610
public class MvcBuildIntegrationTest31 : MvcBuildIntegrationTestLegacy
@@ -12,5 +16,33 @@ public MvcBuildIntegrationTest31(LegacyBuildServerTestFixture buildServer)
1216

1317
public override string TestProjectName => "SimpleMvc31";
1418
public override string TargetFramework => "netcoreapp3.1";
19+
20+
[Fact]
21+
public async Task Build_WithGenerateRazorHostingAssemblyInfo_AddsConfigurationMetadata()
22+
{
23+
using var project = CreateTestProject();
24+
25+
var razorAssemblyInfo = Path.Combine(IntermediateOutputPath, "SimpleMvc31.RazorAssemblyInfo.cs");
26+
var result = await DotnetMSBuild("Build", "/p:GenerateRazorHostingAssemblyInfo=true");
27+
28+
Assert.BuildPassed(result);
29+
30+
Assert.FileExists(result, IntermediateOutputPath, "SimpleMvc31.Views.dll");
31+
Assert.FileExists(result, IntermediateOutputPath, "SimpleMvc31.Views.pdb");
32+
33+
Assert.FileExists(result, razorAssemblyInfo);
34+
Assert.FileContainsLine(
35+
result,
36+
razorAssemblyInfo,
37+
"[assembly: Microsoft.AspNetCore.Razor.Hosting.RazorLanguageVersionAttribute(\"3.0\")]");
38+
Assert.FileContainsLine(
39+
result,
40+
razorAssemblyInfo,
41+
"[assembly: Microsoft.AspNetCore.Razor.Hosting.RazorConfigurationNameAttribute(\"MVC-3.0\")]");
42+
Assert.FileContainsLine(
43+
result,
44+
razorAssemblyInfo,
45+
"[assembly: Microsoft.AspNetCore.Razor.Hosting.RazorExtensionAssemblyNameAttribute(\"MVC-3.0\", \"Microsoft.AspNetCore.Mvc.Razor.Extensions\")]");
46+
}
1547
}
1648
}

src/Razor/Microsoft.NET.Sdk.Razor/src/build/netstandard2.0/Microsoft.NET.Sdk.Razor.Configuration.targets

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ Copyright (c) .NET Foundation. All rights reserved.
6969
Determine the default Razor configuration
7070
-->
7171
<PropertyGroup Condition="'$(RazorDefaultConfiguration)'==''">
72+
<!-- For 5.0, we're not introducing any new language features for MVC. We can continue using the 3.0 configuration \ extension for MVC support. -->
7273
<RazorDefaultConfiguration Condition="'$(AddRazorSupportForMvc)'=='true'">MVC-3.0</RazorDefaultConfiguration>
7374
<RazorDefaultConfiguration Condition="'$(RazorDefaultConfiguration)'==''">Default</RazorDefaultConfiguration>
7475
</PropertyGroup>
@@ -80,7 +81,7 @@ Copyright (c) .NET Foundation. All rights reserved.
8081
the project's runtime.
8182
-->
8283
<RazorConfiguration Include="Default" />
83-
<RazorConfiguration Include="MVC-3.0" >
84+
<RazorConfiguration Include="MVC-3.0">
8485
<Extensions>MVC-3.0;$(CustomRazorExtension)</Extensions>
8586
</RazorConfiguration>
8687
</ItemGroup>

src/Razor/Microsoft.NET.Sdk.Razor/src/build/netstandard2.0/Sdk.Razor.CurrentVersion.targets

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ Copyright (c) .NET Foundation. All rights reserved.
4545
<!--
4646
Infer the RazorLangVersion if no value was specified. When adding support for newer target frameworks, list newer language versions first.
4747
-->
48+
<RazorLangVersion Condition="'$(RazorLangVersion)' == '' AND '$(_TargetingNET50OrLater)' == 'true'">5.0</RazorLangVersion>
4849
<RazorLangVersion Condition="'$(RazorLangVersion)' == '' AND '$(_TargetingNETCoreApp30OrLater)' == 'true'">3.0</RazorLangVersion>
4950

5051
<!--

0 commit comments

Comments
 (0)