Make RazorViewEngineOptions.FileProvider a list instead of a single item
Fixes #3806
This commit is contained in:
parent
d222900662
commit
2b9dd76535
|
|
@ -20,10 +20,12 @@ namespace EmbeddedViewSample.Web
|
|||
|
||||
services.Configure<RazorViewEngineOptions>(options =>
|
||||
{
|
||||
options.FileProviders.Clear();
|
||||
|
||||
// Base namespace matches the resources added to the assembly from the EmbeddedResources folder.
|
||||
options.FileProvider = new EmbeddedFileProvider(
|
||||
options.FileProviders.Add(new EmbeddedFileProvider(
|
||||
GetType().GetTypeInfo().Assembly,
|
||||
baseNamespace: "EmbeddedViewSample.Web.EmbeddedResources");
|
||||
baseNamespace: "EmbeddedViewSample.Web.EmbeddedResources"));
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -13,11 +13,10 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation
|
|||
/// <summary>
|
||||
/// Initializes a new instance of <see cref="DefaultCompilerCacheProvider"/>.
|
||||
/// </summary>
|
||||
/// <param name="optionsAccessor">An accessor to the <see cref="RazorViewEngineOptions"/>.</param>
|
||||
public DefaultCompilerCacheProvider(IOptions<RazorViewEngineOptions> mvcViewOptions)
|
||||
/// <param name="fileProviderAccessor">The <see cref="IRazorViewEngineFileProviderAccessor"/>.</param>
|
||||
public DefaultCompilerCacheProvider(IRazorViewEngineFileProviderAccessor fileProviderAccessor)
|
||||
{
|
||||
var fileProvider = mvcViewOptions.Value.FileProvider;
|
||||
Cache = new CompilerCache(fileProvider);
|
||||
Cache = new CompilerCache(fileProviderAccessor.FileProvider);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
|
|
|
|||
|
|
@ -33,16 +33,16 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation
|
|||
/// <summary>
|
||||
/// Initializes a new instance of <see cref="DefaultCompilerCacheProvider"/>.
|
||||
/// </summary>
|
||||
/// <param name="loaderContextAccessor">The <see cref="IAssemblyLoadContextAccessor"/>.</param>
|
||||
/// <param name="optionsAccessor">An accessor to the <see cref="RazorViewEngineOptions"/>.</param>
|
||||
/// <param name="loadContextAccessor">The <see cref="IAssemblyLoadContextAccessor"/>.</param>
|
||||
/// <param name="fileProviderAccessor">The <see cref="IRazorViewEngineFileProviderAccessor"/>.</param>
|
||||
/// <param name="assemblies"><see cref="Assembly"/> instances to scan for precompiled views.</param>
|
||||
public PrecompiledViewsCompilerCacheProvider(
|
||||
IAssemblyLoadContextAccessor loadContextAccessor,
|
||||
IOptions<RazorViewEngineOptions> mvcViewOptions,
|
||||
IRazorViewEngineFileProviderAccessor fileProviderAccessor,
|
||||
IEnumerable<Assembly> assemblies)
|
||||
{
|
||||
_loadContextAccessor = loadContextAccessor;
|
||||
_fileProvider = mvcViewOptions.Value.FileProvider;
|
||||
_fileProvider = fileProviderAccessor.FileProvider;
|
||||
_createCache = CreateCache;
|
||||
_assemblies = assemblies.ToArray();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,17 +27,15 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation
|
|||
/// </summary>
|
||||
/// <param name="compilationService">The <see cref="ICompilationService"/> to compile generated code.</param>
|
||||
/// <param name="razorHost">The <see cref="IMvcRazorHost"/> to generate code from Razor files.</param>
|
||||
/// <param name="viewEngineOptions">
|
||||
/// The <see cref="IFileProvider"/> to read Razor files referenced in error messages.
|
||||
/// </param>
|
||||
/// <param name="fileProviderAccessor">The <see cref="IRazorViewEngineFileProviderAccessor"/>.</param>
|
||||
public RazorCompilationService(
|
||||
ICompilationService compilationService,
|
||||
IMvcRazorHost razorHost,
|
||||
IOptions<RazorViewEngineOptions> viewEngineOptions)
|
||||
IRazorViewEngineFileProviderAccessor fileProviderAccessor)
|
||||
{
|
||||
_compilationService = compilationService;
|
||||
_razorHost = razorHost;
|
||||
_fileProvider = viewEngineOptions.Value.FileProvider;
|
||||
_fileProvider = fileProviderAccessor.FileProvider;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
|
|
@ -105,9 +103,10 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation
|
|||
|
||||
private DiagnosticMessage CreateDiagnosticMessage(RazorError error, string filePath)
|
||||
{
|
||||
var location = error.Location;
|
||||
return new DiagnosticMessage(
|
||||
message: error.Message,
|
||||
formattedMessage: $"{error} ({error.Location.LineIndex},{error.Location.CharacterIndex}) {error.Message}",
|
||||
formattedMessage: $"{error} ({location.LineIndex},{location.CharacterIndex}) {error.Message}",
|
||||
filePath: filePath,
|
||||
startLine: error.Location.LineIndex + 1,
|
||||
startColumn: error.Location.CharacterIndex,
|
||||
|
|
|
|||
|
|
@ -51,21 +51,21 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation
|
|||
/// Initalizes a new instance of the <see cref="RoslynCompilationService"/> class.
|
||||
/// </summary>
|
||||
/// <param name="environment">The environment for the executing application.</param>
|
||||
/// <param name="loaderAccessor">
|
||||
/// The accessor for the <see cref="IAssemblyLoadContext"/> used to load compiled assemblies.
|
||||
/// </param>
|
||||
/// <param name="libraryExporter">The library manager that provides export and reference information.</param>
|
||||
/// <param name="host">The <see cref="IMvcRazorHost"/> that was used to generate the code.</param>
|
||||
/// <param name="optionsAccessor">Accessor to <see cref="RazorViewEngineOptions"/>.</param>
|
||||
/// <param name="fileProviderAccessor">The <see cref="IRazorViewEngineFileProviderAccessor"/>.</param>
|
||||
public RoslynCompilationService(
|
||||
IApplicationEnvironment environment,
|
||||
Extensions.CompilationAbstractions.ILibraryExporter libraryExporter,
|
||||
IMvcRazorHost host,
|
||||
IOptions<RazorViewEngineOptions> optionsAccessor)
|
||||
IOptions<RazorViewEngineOptions> optionsAccessor,
|
||||
IRazorViewEngineFileProviderAccessor fileProviderAccessor)
|
||||
{
|
||||
_environment = environment;
|
||||
_libraryExporter = libraryExporter;
|
||||
_applicationReferences = new Lazy<List<MetadataReference>>(GetApplicationReferences);
|
||||
_fileProvider = optionsAccessor.Value.FileProvider;
|
||||
_fileProvider = fileProviderAccessor.FileProvider;
|
||||
_classPrefix = host.MainClassNamePrefix;
|
||||
_compilationCallback = optionsAccessor.Value.CompilationCallback;
|
||||
_parseOptions = optionsAccessor.Value.ParseOptions;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,36 @@
|
|||
// 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 Microsoft.AspNet.FileProviders;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc.Razor
|
||||
{
|
||||
/// <summary>
|
||||
/// Default implementation of <see cref="IRazorViewEngineFileProviderAccessor"/>.
|
||||
/// </summary>
|
||||
public class DefaultRazorViewEngineFileProviderAccessor : IRazorViewEngineFileProviderAccessor
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of <see cref="DefaultRazorViewEngineFileProviderAccessor"/>.
|
||||
/// </summary>
|
||||
/// <param name="optionsAccessor">Accessor to <see cref="RazorViewEngineOptions"/>.</param>
|
||||
public DefaultRazorViewEngineFileProviderAccessor(IOptions<RazorViewEngineOptions> optionsAccessor)
|
||||
{
|
||||
var fileProviders = optionsAccessor.Value.FileProviders;
|
||||
if (fileProviders.Count == 1)
|
||||
{
|
||||
FileProvider = fileProviders[0];
|
||||
}
|
||||
else
|
||||
{
|
||||
FileProvider = new CompositeFileProvider(fileProviders);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="IFileProvider"/> used to look up Razor files.
|
||||
/// </summary>
|
||||
public IFileProvider FileProvider { get; }
|
||||
}
|
||||
}
|
||||
|
|
@ -14,7 +14,6 @@ using Microsoft.AspNet.Razor.TagHelpers;
|
|||
using Microsoft.Extensions.Caching.Memory;
|
||||
using Microsoft.Extensions.CompilationAbstractions;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using Microsoft.Extensions.MemoryPool;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Microsoft.Extensions.PlatformAbstractions;
|
||||
|
||||
|
|
@ -130,12 +129,14 @@ namespace Microsoft.Extensions.DependencyInjection
|
|||
services.TryAddEnumerable(
|
||||
ServiceDescriptor.Transient<IConfigureOptions<RazorViewEngineOptions>, RazorViewEngineOptionsSetup>());
|
||||
|
||||
services.TryAddSingleton<IRazorViewEngineFileProviderAccessor, DefaultRazorViewEngineFileProviderAccessor>();
|
||||
|
||||
services.TryAddSingleton<IRazorViewEngine, RazorViewEngine>();
|
||||
|
||||
services.TryAdd(ServiceDescriptor.Singleton<IChunkTreeCache>(serviceProvider =>
|
||||
{
|
||||
var cachedFileProvider = serviceProvider.GetRequiredService<IOptions<RazorViewEngineOptions>>();
|
||||
return new DefaultChunkTreeCache(cachedFileProvider.Value.FileProvider);
|
||||
var accessor = serviceProvider.GetRequiredService<IRazorViewEngineFileProviderAccessor>();
|
||||
return new DefaultChunkTreeCache(accessor.FileProvider);
|
||||
}));
|
||||
|
||||
// Caches compilation artifacts across the lifetime of the application.
|
||||
|
|
|
|||
|
|
@ -0,0 +1,18 @@
|
|||
// 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 Microsoft.AspNet.FileProviders;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc.Razor
|
||||
{
|
||||
/// <summary>
|
||||
/// Accessor to the <see cref="IFileProvider"/> used by <see cref="RazorViewEngine"/>.
|
||||
/// </summary>
|
||||
public interface IRazorViewEngineFileProviderAccessor
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the <see cref="IFileProvider"/> used to look up Razor files.
|
||||
/// </summary>
|
||||
IFileProvider FileProvider { get; }
|
||||
}
|
||||
}
|
||||
|
|
@ -14,12 +14,9 @@ namespace Microsoft.AspNet.Mvc.Razor
|
|||
/// </summary>
|
||||
public class RazorViewEngineOptions
|
||||
{
|
||||
private IFileProvider _fileProvider;
|
||||
|
||||
private CSharpParseOptions _parseOptions = new CSharpParseOptions(LanguageVersion.CSharp6);
|
||||
|
||||
private CSharpCompilationOptions _compilationOptions = new CSharpCompilationOptions(CodeAnalysis.OutputKind.DynamicallyLinkedLibrary);
|
||||
|
||||
private CSharpCompilationOptions _compilationOptions =
|
||||
new CSharpCompilationOptions(CodeAnalysis.OutputKind.DynamicallyLinkedLibrary);
|
||||
private Action<RoslynCompilationContext> _compilationCallback = c => { };
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -29,33 +26,22 @@ namespace Microsoft.AspNet.Mvc.Razor
|
|||
= new List<IViewLocationExpander>();
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the <see cref="IFileProvider" /> used by <see cref="RazorViewEngine"/> to locate Razor files on
|
||||
/// disk.
|
||||
/// Gets the sequence of <see cref="IFileProvider" /> instances used by <see cref="RazorViewEngine"/> to
|
||||
/// locate Razor files.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// At startup, this is initialized to an instance of <see cref="PhysicalFileProvider"/> that is rooted at the
|
||||
/// application root.
|
||||
/// At startup, this is initialized to include an instance of <see cref="PhysicalFileProvider"/> that is
|
||||
/// rooted at the application root.
|
||||
/// </remarks>
|
||||
public IFileProvider FileProvider
|
||||
{
|
||||
get { return _fileProvider; }
|
||||
|
||||
set
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(value));
|
||||
}
|
||||
|
||||
_fileProvider = value;
|
||||
}
|
||||
}
|
||||
public IList<IFileProvider> FileProviders { get; } = new List<IFileProvider>();
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the callback that is used to customize Razor compilation
|
||||
/// to change compilation settings you can update <see cref="RoslynCompilationContext.Compilation"/> property.
|
||||
/// Customizations made here would not reflect in tooling (Intellisense).
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Customizations made here would not reflect in tooling (Intellisense).
|
||||
/// </remarks>
|
||||
public Action<RoslynCompilationContext> CompilationCallback
|
||||
{
|
||||
get { return _compilationCallback; }
|
||||
|
|
@ -65,6 +51,7 @@ namespace Microsoft.AspNet.Mvc.Razor
|
|||
{
|
||||
throw new ArgumentNullException(nameof(value));
|
||||
}
|
||||
|
||||
_compilationCallback = value;
|
||||
}
|
||||
}
|
||||
|
|
@ -81,6 +68,7 @@ namespace Microsoft.AspNet.Mvc.Razor
|
|||
{
|
||||
throw new ArgumentNullException(nameof(value));
|
||||
}
|
||||
|
||||
_parseOptions = value;
|
||||
}
|
||||
}
|
||||
|
|
@ -97,9 +85,9 @@ namespace Microsoft.AspNet.Mvc.Razor
|
|||
{
|
||||
throw new ArgumentNullException(nameof(value));
|
||||
}
|
||||
|
||||
_compilationOptions = value;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ namespace Microsoft.AspNet.Mvc
|
|||
IApplicationEnvironment applicationEnvironment,
|
||||
IHostingEnvironment hostingEnvironment)
|
||||
{
|
||||
razorOptions.FileProvider = new PhysicalFileProvider(applicationEnvironment.ApplicationBasePath);
|
||||
razorOptions.FileProviders.Add(new PhysicalFileProvider(applicationEnvironment.ApplicationBasePath));
|
||||
|
||||
var parseOptions = new CSharpParseOptions(LanguageVersion.CSharp6);
|
||||
var compilationOptions = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary);
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@
|
|||
"keyFile": "../../tools/Key.snk"
|
||||
},
|
||||
"dependencies": {
|
||||
"Microsoft.AspNet.FileProviders.Composite": "1.0.0-*",
|
||||
"Microsoft.AspNet.Mvc.Razor.Host": "6.0.0-*",
|
||||
"Microsoft.AspNet.Mvc.ViewFeatures": "6.0.0-*",
|
||||
"Microsoft.AspNet.Razor.Runtime.Precompilation": "4.0.0-*",
|
||||
|
|
@ -26,8 +27,7 @@
|
|||
"type": "build"
|
||||
},
|
||||
"Microsoft.Dnx.Compilation.CSharp.Common": "1.0.0-*",
|
||||
"Microsoft.Dnx.Compilation.CSharp.Abstractions": "1.0.0-*",
|
||||
"Microsoft.Extensions.MemoryPool": "1.0.0-*"
|
||||
"Microsoft.Dnx.Compilation.CSharp.Abstractions": "1.0.0-*"
|
||||
},
|
||||
"frameworks": {
|
||||
"net451": {
|
||||
|
|
|
|||
|
|
@ -38,7 +38,7 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation
|
|||
compiler.Setup(c => c.Compile(relativeFileInfo, It.IsAny<string>()))
|
||||
.Returns(new CompilationResult(typeof(RazorCompilationServiceTest)));
|
||||
|
||||
var razorService = new RazorCompilationService(compiler.Object, host.Object, GetOptions());
|
||||
var razorService = new RazorCompilationService(compiler.Object, host.Object, GetFileProviderAccessor());
|
||||
|
||||
// Act
|
||||
razorService.Compile(relativeFileInfo);
|
||||
|
|
@ -70,7 +70,7 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation
|
|||
|
||||
var compiler = new Mock<ICompilationService>(MockBehavior.Strict);
|
||||
var relativeFileInfo = new RelativeFileInfo(fileInfo.Object, @"Views\index\home.cshtml");
|
||||
var razorService = new RazorCompilationService(compiler.Object, host.Object, GetOptions());
|
||||
var razorService = new RazorCompilationService(compiler.Object, host.Object, GetFileProviderAccessor());
|
||||
|
||||
// Act
|
||||
var result = razorService.Compile(relativeFileInfo);
|
||||
|
|
@ -111,7 +111,7 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation
|
|||
compiler.Setup(c => c.Compile(relativeFileInfo, code))
|
||||
.Returns(compilationResult)
|
||||
.Verifiable();
|
||||
var razorService = new RazorCompilationService(compiler.Object, host.Object, GetOptions());
|
||||
var razorService = new RazorCompilationService(compiler.Object, host.Object, GetFileProviderAccessor());
|
||||
|
||||
// Act
|
||||
var result = razorService.Compile(relativeFileInfo);
|
||||
|
|
@ -136,7 +136,7 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation
|
|||
var razorService = new RazorCompilationService(
|
||||
Mock.Of<ICompilationService>(),
|
||||
Mock.Of<IMvcRazorHost>(),
|
||||
GetOptions(fileProvider));
|
||||
GetFileProviderAccessor(fileProvider));
|
||||
var errors = new[]
|
||||
{
|
||||
new RazorError("message-1", new SourceLocation(1, 2, 17), length: 1),
|
||||
|
|
@ -211,15 +211,11 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation
|
|||
new ChunkTree());
|
||||
}
|
||||
|
||||
private static IOptions<RazorViewEngineOptions> GetOptions(IFileProvider fileProvider = null)
|
||||
private static IRazorViewEngineFileProviderAccessor GetFileProviderAccessor(IFileProvider fileProvider = null)
|
||||
{
|
||||
var razorViewEngineOptions = new RazorViewEngineOptions
|
||||
{
|
||||
FileProvider = fileProvider ?? new TestFileProvider()
|
||||
};
|
||||
var options = new Mock<IOptions<RazorViewEngineOptions>>();
|
||||
options.SetupGet(o => o.Value)
|
||||
.Returns(razorViewEngineOptions);
|
||||
var options = new Mock<IRazorViewEngineFileProviderAccessor>();
|
||||
options.SetupGet(o => o.FileProvider)
|
||||
.Returns(fileProvider ?? new TestFileProvider());
|
||||
|
||||
return options.Object;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,11 +2,9 @@
|
|||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System;
|
||||
using System.Runtime.Versioning;
|
||||
using Microsoft.AspNet.FileProviders;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.Text;
|
||||
using Microsoft.Dnx.Runtime;
|
||||
using Microsoft.Extensions.CompilationAbstractions;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Microsoft.Extensions.PlatformAbstractions;
|
||||
|
|
@ -35,7 +33,8 @@ public class MyTestType {}";
|
|||
applicationEnvironment,
|
||||
libraryExporter,
|
||||
mvcRazorHost.Object,
|
||||
GetOptions());
|
||||
GetOptions(),
|
||||
GetFileProviderAccessor());
|
||||
var relativeFileInfo = new RelativeFileInfo(
|
||||
new TestFileInfo { PhysicalPath = "SomePath" },
|
||||
"some-relative-path");
|
||||
|
|
@ -66,7 +65,8 @@ this should fail";
|
|||
applicationEnvironment,
|
||||
libraryExporter,
|
||||
mvcRazorHost,
|
||||
GetOptions(fileProvider));
|
||||
GetOptions(),
|
||||
GetFileProviderAccessor(fileProvider));
|
||||
var relativeFileInfo = new RelativeFileInfo(fileInfo, "some-relative-path");
|
||||
|
||||
// Act
|
||||
|
|
@ -94,7 +94,8 @@ this should fail";
|
|||
applicationEnvironment,
|
||||
libraryExporter,
|
||||
mvcRazorHost,
|
||||
GetOptions());
|
||||
GetOptions(),
|
||||
GetFileProviderAccessor());
|
||||
var relativeFileInfo = new RelativeFileInfo(
|
||||
new TestFileInfo { Content = fileContent },
|
||||
"some-relative-path");
|
||||
|
|
@ -125,7 +126,7 @@ this should fail";
|
|||
|
||||
var mockFileInfo = new Mock<IFileInfo>();
|
||||
mockFileInfo.Setup(f => f.CreateReadStream())
|
||||
.Throws(new Exception());
|
||||
.Throws(new Exception());
|
||||
var fileProvider = new TestFileProvider();
|
||||
fileProvider.AddFile(path, mockFileInfo.Object);
|
||||
|
||||
|
|
@ -133,7 +134,8 @@ this should fail";
|
|||
applicationEnvironment,
|
||||
libraryExporter,
|
||||
mvcRazorHost,
|
||||
GetOptions(fileProvider));
|
||||
GetOptions(),
|
||||
GetFileProviderAccessor(fileProvider));
|
||||
|
||||
var relativeFileInfo = new RelativeFileInfo(mockFileInfo.Object, path);
|
||||
|
||||
|
|
@ -163,7 +165,7 @@ public class MyNonCustomDefinedClass {}
|
|||
var libraryExporter = CompilationServices.Default.LibraryExporter;
|
||||
var mvcRazorHost = new Mock<IMvcRazorHost>();
|
||||
mvcRazorHost.SetupGet(m => m.MainClassNamePrefix)
|
||||
.Returns("My");
|
||||
.Returns("My");
|
||||
|
||||
var options = GetOptions();
|
||||
options.Value.ParseOptions = options.Value.ParseOptions.WithPreprocessorSymbols("MY_CUSTOM_DEFINE");
|
||||
|
|
@ -172,7 +174,8 @@ public class MyNonCustomDefinedClass {}
|
|||
applicationEnvironment,
|
||||
libraryExporter,
|
||||
mvcRazorHost.Object,
|
||||
options);
|
||||
options,
|
||||
GetFileProviderAccessor());
|
||||
var relativeFileInfo = new RelativeFileInfo(
|
||||
new TestFileInfo { PhysicalPath = "SomePath" },
|
||||
"some-relative-path");
|
||||
|
|
@ -202,7 +205,8 @@ public class NotRazorPrefixType {}";
|
|||
applicationEnvironment,
|
||||
libraryExporter,
|
||||
mvcRazorHost.Object,
|
||||
GetOptions());
|
||||
GetOptions(),
|
||||
GetFileProviderAccessor());
|
||||
|
||||
var relativeFileInfo = new RelativeFileInfo(
|
||||
new TestFileInfo { PhysicalPath = "SomePath" },
|
||||
|
|
@ -224,17 +228,17 @@ public class NotRazorPrefixType {}";
|
|||
var generatedCodeFileName = "Generated Code";
|
||||
var fileProvider = new TestFileProvider();
|
||||
fileProvider.AddFile(viewPath, "view-content");
|
||||
var options = new Mock<IOptions<RazorViewEngineOptions>>();
|
||||
options.SetupGet(o => o.Value)
|
||||
.Returns(new RazorViewEngineOptions
|
||||
{
|
||||
FileProvider = fileProvider
|
||||
});
|
||||
var options = new RazorViewEngineOptions();
|
||||
options.FileProviders.Add(fileProvider);
|
||||
var optionsAccessor = new Mock<IOptions<RazorViewEngineOptions>>();
|
||||
optionsAccessor.SetupGet(o => o.Value)
|
||||
.Returns(options);
|
||||
var compilationService = new RoslynCompilationService(
|
||||
PlatformServices.Default.Application,
|
||||
CompilationServices.Default.LibraryExporter,
|
||||
Mock.Of<IMvcRazorHost>(),
|
||||
options.Object);
|
||||
optionsAccessor.Object,
|
||||
GetFileProviderAccessor(fileProvider));
|
||||
|
||||
var assemblyName = "random-assembly-name";
|
||||
|
||||
|
|
@ -325,7 +329,8 @@ public class NotRazorPrefixType {}";
|
|||
applicationEnvironment,
|
||||
libraryExporter,
|
||||
mvcRazorHost.Object,
|
||||
GetOptions(callback: c => usedCompilation = c));
|
||||
GetOptions(callback: c => usedCompilation = c),
|
||||
GetFileProviderAccessor());
|
||||
|
||||
var relativeFileInfo = new RelativeFileInfo(
|
||||
new TestFileInfo { PhysicalPath = "SomePath" },
|
||||
|
|
@ -350,13 +355,11 @@ public class NotRazorPrefixType {}";
|
|||
isEnabledByDefault: true);
|
||||
}
|
||||
|
||||
private static IOptions<RazorViewEngineOptions> GetOptions(IFileProvider fileProvider = null,
|
||||
Action<RoslynCompilationContext> callback = null)
|
||||
private static IOptions<RazorViewEngineOptions> GetOptions(Action<RoslynCompilationContext> callback = null)
|
||||
{
|
||||
var razorViewEngineOptions = new RazorViewEngineOptions
|
||||
{
|
||||
FileProvider = fileProvider ?? new TestFileProvider(),
|
||||
CompilationCallback = callback ?? (c => { })
|
||||
CompilationCallback = callback ?? (c => { }),
|
||||
};
|
||||
var options = new Mock<IOptions<RazorViewEngineOptions>>();
|
||||
options
|
||||
|
|
@ -365,5 +368,14 @@ public class NotRazorPrefixType {}";
|
|||
|
||||
return options.Object;
|
||||
}
|
||||
|
||||
private IRazorViewEngineFileProviderAccessor GetFileProviderAccessor(IFileProvider fileProvider = null)
|
||||
{
|
||||
var options = new Mock<IRazorViewEngineFileProviderAccessor>();
|
||||
options.SetupGet(o => o.FileProvider)
|
||||
.Returns(fileProvider ?? new TestFileProvider());
|
||||
|
||||
return options.Object;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,67 @@
|
|||
// 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 Microsoft.AspNet.FileProviders;
|
||||
using Microsoft.AspNet.Mvc.Internal;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc.Razor
|
||||
{
|
||||
public class DefaultRazorViewEngineFileProviderAccessorTest
|
||||
{
|
||||
[Fact]
|
||||
public void FileProvider_ReturnsInstanceIfExactlyOneFileProviderIsSpecified()
|
||||
{
|
||||
// Arrange
|
||||
var fileProvider = new TestFileProvider();
|
||||
var options = new RazorViewEngineOptions();
|
||||
options.FileProviders.Add(fileProvider);
|
||||
var optionsAccessor = new Mock<IOptions<RazorViewEngineOptions>>();
|
||||
optionsAccessor.SetupGet(o => o.Value).Returns(options);
|
||||
var fileProviderAccessor = new DefaultRazorViewEngineFileProviderAccessor(optionsAccessor.Object);
|
||||
|
||||
// Act
|
||||
var actual = fileProviderAccessor.FileProvider;
|
||||
|
||||
// Assert
|
||||
Assert.Same(fileProvider, actual);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FileProvider_ReturnsCompositeFileProviderIfNoInstancesAreRegistered()
|
||||
{
|
||||
// Arrange
|
||||
var options = new RazorViewEngineOptions();
|
||||
var optionsAccessor = new Mock<IOptions<RazorViewEngineOptions>>();
|
||||
optionsAccessor.SetupGet(o => o.Value).Returns(options);
|
||||
var fileProviderAccessor = new DefaultRazorViewEngineFileProviderAccessor(optionsAccessor.Object);
|
||||
|
||||
// Act
|
||||
var actual = fileProviderAccessor.FileProvider;
|
||||
|
||||
// Assert
|
||||
Assert.IsType<CompositeFileProvider>(actual);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FileProvider_ReturnsCompositeFileProviderIfMoreThanOneInstanceIsRegistered()
|
||||
{
|
||||
// Arrange
|
||||
var options = new RazorViewEngineOptions();
|
||||
options.FileProviders.Add(new TestFileProvider());
|
||||
options.FileProviders.Add(new TestFileProvider());
|
||||
var optionsAccessor = new Mock<IOptions<RazorViewEngineOptions>>();
|
||||
optionsAccessor.SetupGet(o => o.Value).Returns(options);
|
||||
var fileProviderAccessor = new DefaultRazorViewEngineFileProviderAccessor(optionsAccessor.Object);
|
||||
|
||||
// Act
|
||||
var actual = fileProviderAccessor.FileProvider;
|
||||
|
||||
// Assert
|
||||
Assert.IsType<CompositeFileProvider>(actual);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,7 +1,6 @@
|
|||
// 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 Microsoft.AspNet.Mvc.Internal;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
|
@ -12,18 +11,7 @@ namespace Microsoft.AspNet.Mvc.Razor
|
|||
public class RazorViewEngineOptionsTest
|
||||
{
|
||||
[Fact]
|
||||
public void FileProviderThrows_IfNullIsAssigned()
|
||||
{
|
||||
// Arrange
|
||||
var options = new RazorViewEngineOptions();
|
||||
|
||||
// Act and Assert
|
||||
var ex = Assert.Throws<ArgumentNullException>(() => options.FileProvider = null);
|
||||
Assert.Equal("value", ex.ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddRazorOptions_ConfiguresOptionsProperly()
|
||||
public void AddRazorOptions_ConfiguresOptionsAsExpected()
|
||||
{
|
||||
// Arrange
|
||||
var services = new ServiceCollection().AddOptions();
|
||||
|
|
@ -33,13 +21,13 @@ namespace Microsoft.AspNet.Mvc.Razor
|
|||
var builder = new MvcBuilder(services);
|
||||
builder.AddRazorOptions(options =>
|
||||
{
|
||||
options.FileProvider = fileProvider;
|
||||
options.FileProviders.Add(fileProvider);
|
||||
});
|
||||
var serviceProvider = services.BuildServiceProvider();
|
||||
|
||||
// Assert
|
||||
var accessor = serviceProvider.GetRequiredService<IOptions<RazorViewEngineOptions>>();
|
||||
Assert.Same(fileProvider, accessor.Value.FileProvider);
|
||||
Assert.Same(fileProvider, accessor.Value.FileProviders[0]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -21,18 +21,18 @@ namespace Microsoft.AspNet.Mvc
|
|||
var options = new RazorViewEngineOptions();
|
||||
var appEnv = new Mock<IApplicationEnvironment>();
|
||||
appEnv.SetupGet(e => e.ApplicationBasePath)
|
||||
.Returns(Directory.GetCurrentDirectory());
|
||||
.Returns(Directory.GetCurrentDirectory());
|
||||
var hostingEnv = new Mock<IHostingEnvironment>();
|
||||
hostingEnv.SetupGet(e => e.EnvironmentName)
|
||||
.Returns("Development");
|
||||
.Returns("Development");
|
||||
var optionsSetup = new RazorViewEngineOptionsSetup(appEnv.Object, hostingEnv.Object);
|
||||
|
||||
// Act
|
||||
optionsSetup.Configure(options);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(options.FileProvider);
|
||||
Assert.IsType<PhysicalFileProvider>(options.FileProvider);
|
||||
var fileProvider = Assert.Single(options.FileProviders);
|
||||
Assert.IsType<PhysicalFileProvider>(fileProvider);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
|
|
@ -62,7 +62,9 @@ namespace Microsoft.AspNet.Mvc
|
|||
[InlineData("Development", OptimizationLevel.Debug)]
|
||||
[InlineData("Staging", OptimizationLevel.Release)]
|
||||
[InlineData("Production", OptimizationLevel.Release)]
|
||||
public void RazorViewEngineOptionsSetup_SetsOptimizationLevel(string environment, OptimizationLevel expectedOptimizationLevel)
|
||||
public void RazorViewEngineOptionsSetup_SetsOptimizationLevel(
|
||||
string environment,
|
||||
OptimizationLevel expectedOptimizationLevel)
|
||||
{
|
||||
// Arrange
|
||||
var options = new RazorViewEngineOptions();
|
||||
|
|
|
|||
|
|
@ -6,7 +6,6 @@ using System.Text;
|
|||
using Microsoft.AspNet.Mvc.Razor;
|
||||
using Microsoft.AspNet.Mvc.Razor.Compilation;
|
||||
using Microsoft.AspNet.Razor.CodeGenerators;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace RazorPageExecutionInstrumentationWebSite
|
||||
{
|
||||
|
|
@ -15,8 +14,8 @@ namespace RazorPageExecutionInstrumentationWebSite
|
|||
public TestRazorCompilationService(
|
||||
ICompilationService compilationService,
|
||||
IMvcRazorHost razorHost,
|
||||
IOptions<RazorViewEngineOptions> viewEngineOptions)
|
||||
: base(compilationService, razorHost, viewEngineOptions)
|
||||
IRazorViewEngineFileProviderAccessor fileProviderAccessor)
|
||||
: base(compilationService, razorHost, fileProviderAccessor)
|
||||
{
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue