Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -959,10 +959,18 @@ private void EnsureDirectiveIsAtStartOfLine()
}
}

protected void MapDirectives(Action<SyntaxListBuilder<RazorSyntaxNode>, CSharpTransitionSyntax> handler, params string[] directives)
// Internal for unit testing
internal void MapDirectives(Action<SyntaxListBuilder<RazorSyntaxNode>, CSharpTransitionSyntax> handler, params string[] directives)
{
foreach (var directive in directives)
{
if (_directiveParserMap.ContainsKey(directive))
{
// It is possible for the list to contain duplicates in cases when the project is misconfigured.
// In those cases, we shouldn't register multiple handlers per keyword.
continue;
}

_directiveParserMap.Add(directive, (builder, transition) =>
{
handler(builder, transition);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,5 +211,19 @@ public void TagHelperPrefixDirective_DuplicatesCauseError()
var diagnostic = Assert.Single(chunkGenerator.Diagnostics);
Assert.Equal(expectedDiagnostic, diagnostic);
}

[Fact]
public void MapDirectives_HandlesDuplicates()
{
// Arrange
var source = TestRazorSourceDocument.Create();
var options = RazorParserOptions.CreateDefault();
var context = new ParserContext(source, options);
var parser = new CSharpCodeParser(context);

// Act & Assert (Does not throw)
parser.MapDirectives((b, t) => { }, "test");
parser.MapDirectives((b, t) => { }, "test");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public async Task BuildMvcApp()
Assert.BuildPassed(result);
var summary = ParseTaskPerformanceSummary(result.Output);

Assert.Equal(1, summary.First(f => f.Name == "RazorGenerate").Calls);
Assert.Equal(1, summary.First(f => f.Name == "RazorTagHelper").Calls);
Assert.Equal(1, summary.First(f => f.Name == "SdkRazorGenerate").Calls);
Assert.Equal(1, summary.First(f => f.Name == "SdkRazorTagHelper").Calls);

// Incremental builds
for (var i = 0; i < 2; i++)
Expand All @@ -37,8 +37,8 @@ public async Task BuildMvcApp()
Assert.BuildPassed(result);
summary = ParseTaskPerformanceSummary(result.Output);

Assert.DoesNotContain(summary, item => item.Name == "RazorGenerate");
Assert.DoesNotContain(summary, item => item.Name == "RazorTagHelper");
Assert.DoesNotContain(summary, item => item.Name == "SdkRazorGenerate");
Assert.DoesNotContain(summary, item => item.Name == "SdkRazorTagHelper");
}
}

Expand All @@ -52,8 +52,8 @@ public async Task BuildMvcAppWithComponents()
var summary = ParseTaskPerformanceSummary(result.Output);

// One for declaration build, one for the "real" code gen
Assert.Equal(2, summary.First(f => f.Name == "RazorGenerate").Calls);
Assert.Equal(1, summary.First(f => f.Name == "RazorTagHelper").Calls);
Assert.Equal(2, summary.First(f => f.Name == "SdkRazorGenerate").Calls);
Assert.Equal(1, summary.First(f => f.Name == "SdkRazorTagHelper").Calls);

// Incremental builds
for (var i = 0; i < 2; i++)
Expand All @@ -63,8 +63,8 @@ public async Task BuildMvcAppWithComponents()
Assert.BuildPassed(result);
summary = ParseTaskPerformanceSummary(result.Output);

Assert.DoesNotContain(summary, item => item.Name == "RazorGenerate");
Assert.DoesNotContain(summary, item => item.Name == "RazorTagHelper");
Assert.DoesNotContain(summary, item => item.Name == "SdkRazorGenerate");
Assert.DoesNotContain(summary, item => item.Name == "SdkRazorTagHelper");
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// 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.Threading.Tasks;
using Microsoft.AspNetCore.Testing;
using Xunit;

namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests
{
public class MvcBuildIntegrationTest21NetFx :
MSBuildIntegrationTestBase,
IClassFixture<LegacyBuildServerTestFixture>
{
private const string TestProjectName = "SimpleMvc21NetFx";

public MvcBuildIntegrationTest21NetFx(LegacyBuildServerTestFixture buildServer)
: base(buildServer)
{
}

public string OutputFileName => $"{TestProjectName}.exe";

[ConditionalFact]
[OSSkipCondition(OperatingSystems.Linux | OperatingSystems.MacOSX)]
[InitializeTestProject(TestProjectName)]
public async Task BuildingProject_CopyToOutputDirectoryFiles()
{
Project.TargetFramework = "net461";

// Build
var result = await DotnetMSBuild("Build");

Assert.BuildPassed(result);
// No cshtml files should be in the build output directory
Assert.FileCountEquals(result, 0, Path.Combine(OutputPath, "Views"), "*.cshtml");

// refs are required for runtime compilation in desktop targeting projects.
Assert.FileCountEquals(result, 97, Path.Combine(OutputPath, "refs"), "*.dll");
}

[ConditionalFact]
[OSSkipCondition(OperatingSystems.Linux | OperatingSystems.MacOSX)]
[InitializeTestProject(TestProjectName)]
public async Task PublishingProject_CopyToPublishDirectoryItems()
{
Project.TargetFramework = "net461";

// Build
var result = await DotnetMSBuild("Publish");

Assert.BuildPassed(result);

// refs shouldn't be produced by default
Assert.FileCountEquals(result, 0, Path.Combine(PublishOutputPath, "refs"), "*.dll");

// Views shouldn't be produced by default
Assert.FileCountEquals(result, 0, Path.Combine(PublishOutputPath, "Views"), "*.cshtml");
}

[ConditionalFact]
[OSSkipCondition(OperatingSystems.Linux | OperatingSystems.MacOSX)]
[InitializeTestProject(TestProjectName)]
public async Task Publish_IncludesRefAssemblies_WhenCopyRefAssembliesToPublishDirectoryIsSet()
{
Project.TargetFramework = "net461";

// Build
var result = await DotnetMSBuild("Publish", "/p:CopyRefAssembliesToPublishDirectory=true");

Assert.BuildPassed(result);

// refs should be present if CopyRefAssembliesToPublishDirectory is set.
Assert.FileExists(result, PublishOutputPath, "refs", "System.Threading.Tasks.Extensions.dll");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Microsoft.AspNetCore.Razor.Tasks
{
public class RazorGenerate : DotNetToolTask
public class SdkRazorGenerate : DotNetToolTask
{
private static readonly string[] SourceRequiredMetadata = new string[]
{
Expand Down Expand Up @@ -169,18 +169,18 @@ protected override string GenerateResponseFileCommands()
builder.AppendLine(RootNamespace);
}

if (!string.IsNullOrEmpty(CSharpLanguageVersion))
{
builder.AppendLine("--csharp-language-version");
builder.AppendLine(CSharpLanguageVersion);
}

if (GenerateDeclaration)
{
builder.AppendLine("--generate-declaration");
}
}

if (!string.IsNullOrEmpty(CSharpLanguageVersion))
{
builder.AppendLine("--csharp-language-version");
builder.AppendLine(CSharpLanguageVersion);
}

for (var i = 0; i < Extensions.Length; i++)
{
builder.AppendLine("-n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace Microsoft.AspNetCore.Razor.Tasks
{
public class RazorTagHelper : DotNetToolTask
public class SdkRazorTagHelper : DotNetToolTask
{
private const string Identity = "Identity";
private const string AssemblyName = "AssemblyName";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ Copyright (c) .NET Foundation. All rights reserved.
-->

<UsingTask
TaskName="Microsoft.AspNetCore.Razor.Tasks.RazorGenerate"
TaskName="Microsoft.AspNetCore.Razor.Tasks.SdkRazorGenerate"
AssemblyFile="$(RazorSdkBuildTasksAssembly)"
Condition="'$(RazorSdkBuildTasksAssembly)' != ''" />

<UsingTask
TaskName="Microsoft.AspNetCore.Razor.Tasks.RazorTagHelper"
TaskName="Microsoft.AspNetCore.Razor.Tasks.SdkRazorTagHelper"
AssemblyFile="$(RazorSdkBuildTasksAssembly)"
Condition="'$(RazorSdkBuildTasksAssembly)' != ''" />

Expand Down Expand Up @@ -94,7 +94,7 @@ Copyright (c) .NET Foundation. All rights reserved.
<FileWrites Include="$(_RazorTagHelperInputCache)" />
</ItemGroup>

<RazorTagHelper
<SdkRazorTagHelper
Debug="$(_RazorDebugTagHelperTask)"
DebugTool="$(_RazorDebugTagHelperTool)"
ToolAssembly="$(_RazorSdkToolAssembly)"
Expand All @@ -112,7 +112,7 @@ Copyright (c) .NET Foundation. All rights reserved.
<Output
TaskParameter="TagHelperManifest"
ItemName="FileWrites"/>
</RazorTagHelper>
</SdkRazorTagHelper>
</Target>

<Target Name="_ResolveRazorGenerateOutputs" Condition="'@(RazorGenerateWithTargetPath)' != ''">
Expand Down Expand Up @@ -147,7 +147,7 @@ Copyright (c) .NET Foundation. All rights reserved.
Directories="%(_RazorGenerateOutput.RelativeDir)"
Condition="!Exists('%(_RazorGenerateOutput.RelativeDir)')" />

<RazorGenerate
<SdkRazorGenerate
Debug="$(_RazorDebugGenerateCodeTask)"
DebugTool="$(_RazorDebugGenerateCodeTool)"
ToolAssembly="$(_RazorSdkToolAssembly)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ Copyright (c) .NET Foundation. All rights reserved.
<_RazorComponentDeclarationManifest>$(IntermediateOutputPath)$(MSBuildProjectName).RazorComponents.declaration.json</_RazorComponentDeclarationManifest>
</PropertyGroup>

<RazorGenerate
<SdkRazorGenerate
Debug="$(_RazorDebugGenerateCodeTask)"
DebugTool="$(_RazorDebugGenerateCodeTool)"
ToolExe="$(_RazorSdkDotNetHostFileName)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,15 +346,10 @@ Copyright (c) .NET Foundation. All rights reserved.

<!-- Determine what compiler and versions of the language to target. For 2.x targeting projects, these are carried by packages referenced by the project. Use this -->

<!-- Use the 2.x compiler if we're building an application that references Microsoft.AspNetCore.Razor.Design. -->
<Import Project="$(RazorCodeGenerationTargetsPath)"
Condition="'$(IsRazorCompilerReferenced)' == 'true' AND '$(RazorCodeGenerationTargetsPath)' != '' AND Exists('$(RazorCodeGenerationTargetsPath)')" />

<!-- When targeting 3.x and later projects, we have to infer configuration by inspecting the project. -->
<Import Project="Microsoft.NET.Sdk.Razor.Configuration.targets" Condition="'$(_Targeting30OrNewerRazorLangVersion)' == 'true'" />

<!-- For projects targeting 3.x and later, use the compiler that ships in the SDK -->
<Import Project="Microsoft.NET.Sdk.Razor.CodeGeneration.targets" Condition="'$(_Targeting30OrNewerRazorLangVersion)' == 'true'" />
<Import Project="Microsoft.NET.Sdk.Razor.CodeGeneration.targets" />

<Import Project="Microsoft.NET.Sdk.Razor.Component.targets" Condition="'$(_Targeting30OrNewerRazorLangVersion)' == 'true'" />

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">

<!--
This project references a shipped version of MVC and should not reference local builds of
the CodeGeneration targets, rzc, or any of the test shims.
This project references a shipped version of MVC and should not reference local builds of the test shims.
-->

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<RazorSdkDirectoryRoot>$(RazorSdkArtifactsDirectory)$(Configuration)\sdk-output\</RazorSdkDirectoryRoot>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

<ItemGroup>
<ProjectReference Include="..\SimpleMvc21\SimpleMvc21.csproj" />
<ProjectReference Include="..\SimpleMvc21NetFx\SimpleMvc21NetFx.csproj" />
<ProjectReference Include="..\SimpleMvc22\SimpleMvc22.csproj" />
<ProjectReference Include="..\SimpleMvc31\SimpleMvc31.csproj" />
<ProjectReference Include="..\blazor31\blazor31.csproj" />
Expand Down
3 changes: 1 addition & 2 deletions src/Razor/test/testassets/SimpleMvc21/SimpleMvc21.csproj
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<!--
This project references a shipped version of MVC and should not reference local builds of
the CodeGeneration targets, rzc, or any of the test shims.
This project references a shipped version of MVC and should not reference local builds of the test shims.
-->

<PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

namespace SimpleMvc.Models
{
public class ErrorViewModel
{
public string RequestId { get; set; }

public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
}
}
13 changes: 13 additions & 0 deletions src/Razor/test/testassets/SimpleMvc21NetFx/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

namespace SimpleMvc
{
public class Program
{
public static void Main(string[] args)
{
// Just make sure we have a reference to the MVC 2.2
var t = typeof(Microsoft.AspNetCore.Mvc.IActionResult);
System.Console.WriteLine(t.FullName);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<!--
This project references a shipped version of MVC. It will reference the local build of Tasks and compiler, but not the test shims.
-->
<PropertyGroup>
<RazorSdkDirectoryRoot>$(RazorSdkArtifactsDirectory)$(Configuration)\sdk-output\</RazorSdkDirectoryRoot>
</PropertyGroup>

<PropertyGroup>
<TargetFramework>net461</TargetFramework>
<DebugType>full</DebugType>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.1.3" />
</ItemGroup>

<!-- Test Placeholder -->

<PropertyGroup Condition="'$(RunningAsTest)' == ''">
<!-- We don't want to run build server when not running as tests. -->
<UseRazorBuildServer>false</UseRazorBuildServer>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Microsoft.AspNetCore.Razor.TagHelpers;

namespace SimpleMvc
{
public class SimpleTagHelper : TagHelper
{
}
}
Loading