Include file names as part of printing Razor parse errors

Fixes #67
This commit is contained in:
Pranav K 2017-01-26 14:33:34 -08:00
parent 8f085c8e5d
commit 2276c9eafe
9 changed files with 153 additions and 6 deletions

View File

@ -57,6 +57,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Internal
var results = GenerateCode();
var success = true;
foreach (var result in results)
{
if (!result.GeneratorResults.Success)
@ -64,7 +65,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Internal
success = false;
foreach (var error in result.GeneratorResults.ParserErrors)
{
Application.Error.WriteLine($"{error.Location.FilePath} ({error.Location.LineIndex}): {error.Message}");
Application.Error.WriteLine($"{result.ViewFileInfo.FullPath} ({error.Location.LineIndex}): {error.Message}");
}
}
}

View File

@ -0,0 +1,68 @@
// 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;
using System.IO;
using System.Linq;
using Microsoft.AspNetCore.Server.IntegrationTesting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Testing;
using Xunit;
namespace Microsoft.AspNetCore.Mvc.Razor.ViewCompilation
{
public class ApplicationWithParseErrorsTest : IClassFixture<ApplicationWithParseErrorsTest.ApplicationWithParseErrorsFixture>
{
public ApplicationWithParseErrorsTest(ApplicationWithParseErrorsFixture fixture)
{
Fixture = fixture;
}
public ApplicationWithParseErrorsFixture Fixture { get; }
public static TheoryData SupportedFlavorsTheoryData => RuntimeFlavors.SupportedFlavorsTheoryData;
[Theory]
[MemberData(nameof(SupportedFlavorsTheoryData))]
public void PublishingPrintsParseErrors(RuntimeFlavor flavor)
{
var indexPath = Path.Combine(Fixture.ApplicationPath, "Views", "Home", "Index.cshtml");
var viewImportsPath = Path.Combine(Fixture.ApplicationPath, "Views", "Home", "About.cshtml");
var expectedErrors = new[]
{
indexPath + " (0): The code block is missing a closing \"}\" character. Make sure you have a matching \"}\" character for all the \"{\" characters within this block, and that none of the \"}\" characters are being interpreted as markup.",
viewImportsPath + " (1): A space or line break was encountered after the \"@\" character. Only valid identifiers, keywords, comments, \"(\" and \"{\" are valid at the start of a code block and they must occur immediately following \"@\" with no space in between.",
};
using (var deployer = Fixture.CreateDeployment(flavor))
{
// Act & Assert
Assert.Throws<Exception>(() => deployer.Deploy());
// Assert
var output = Fixture.TestSink.Writes.Select(w => w.State.ToString().Trim()).ToList();
foreach (var error in expectedErrors)
{
Assert.Contains(error, output);
}
}
}
public class ApplicationWithParseErrorsFixture : ApplicationTestFixture
{
public ApplicationWithParseErrorsFixture()
: base("ApplicationWithParseErrors")
{
}
public TestSink TestSink { get; } = new TestSink();
protected override ILogger CreateLogger(RuntimeFlavor flavor)
{
return new TestLoggerFactory(TestSink, enabled: true).CreateLogger($"{ApplicationName}:{flavor}");
}
}
}
}

View File

@ -39,9 +39,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.ViewCompilation
public IApplicationDeployer CreateDeployment(RuntimeFlavor flavor)
{
Logger = new LoggerFactory()
.AddConsole()
.CreateLogger($"{ApplicationName}:{flavor}");
Logger = CreateLogger(flavor);
if (!_isRestored)
{
@ -87,6 +85,13 @@ namespace Microsoft.AspNetCore.Mvc.Razor.ViewCompilation
return ApplicationDeployerFactory.Create(deploymentParameters, Logger);
}
protected virtual ILogger CreateLogger(RuntimeFlavor flavor)
{
return new LoggerFactory()
.AddConsole()
.CreateLogger($"{ApplicationName}:{flavor}");
}
protected virtual void Restore()
{
RestoreProject(ApplicationPath);

View File

@ -4,11 +4,9 @@
<TargetFramework>netcoreapp1.1</TargetFramework>
<DefineConstants>$(DefineConstants);__remove_this_to__GENERATE_BASELINES</DefineConstants>
</PropertyGroup>
<ItemGroup>
<EmbeddedResource Include="Resources\*" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0-preview-20161123-03" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0-beta4-build1194" />
@ -17,6 +15,7 @@
<PackageReference Include="Microsoft.DotNet.Cli.Utils" Version="1.0.0-preview2-003121" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="1.1.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="1.1.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Testing" Version="1.2.0-*" />
<PackageReference Include="xunit" Version="2.2.0-*" />
<PackageReference Include="Microsoft.NETCore.App" Version="1.2.0-beta-001304-00" />
</ItemGroup>

View File

@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk.Web" ToolsVersion="15.0">
<PropertyGroup>
<TargetFrameworks>netcoreapp1.1;net451</TargetFrameworks>
<PreserveCompilationContext>true</PreserveCompilationContext>
<OutputType>Exe</OutputType>
<RuntimeIdentifier Condition="!$(TargetFramework.StartsWith('netcoreapp'))">win7-x64</RuntimeIdentifier>
<MvcRazorCompileOnPublish>true</MvcRazorCompileOnPublish>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.ViewCompilation">
<Version>1.1.0-*</Version>
<PrivateAssets>All</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.0" />
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="1.1.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="1.1.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="1.1.0" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp1.1' ">
<PackageReference Include="Microsoft.NETCore.App" Version="1.1.0" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,26 @@
using System.IO;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
namespace ApplicationWithParseErrors
{
public class Program
{
public static void Main(string[] args)
{
var config = new ConfigurationBuilder()
.AddCommandLine(args)
.AddEnvironmentVariables(prefix: "ASPNETCORE_")
.Build();
var host = new WebHostBuilder()
.UseConfiguration(config)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.Build();
host.Run();
}
}
}

View File

@ -0,0 +1,20 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace ApplicationWithParseErrors
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
}
public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole();
app.UseMvcWithDefaultRoute();
}
}
}

View File

@ -0,0 +1,2 @@
@using ApplicationWithParseErrors
@

View File

@ -0,0 +1,4 @@
@{
<span>
}
</span>