Set RelativePhysicalPath in FileProviderRazorProjectItem

This commit is contained in:
Ajay Bhargav Baaskaran 2018-01-09 17:37:47 -08:00
parent 9438a453b0
commit b20e35e76a
18 changed files with 225 additions and 85 deletions

View File

@ -4,6 +4,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Razor.Language; using Microsoft.AspNetCore.Razor.Language;
using Microsoft.Extensions.FileProviders; using Microsoft.Extensions.FileProviders;
@ -13,22 +14,38 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal
{ {
private const string RazorFileExtension = ".cshtml"; private const string RazorFileExtension = ".cshtml";
private readonly IFileProvider _provider; private readonly IFileProvider _provider;
private readonly IHostingEnvironment _hostingEnvironment;
public FileProviderRazorProject(IRazorViewEngineFileProviderAccessor accessor) public FileProviderRazorProject(IRazorViewEngineFileProviderAccessor accessor, IHostingEnvironment hostingEnviroment)
{ {
if (accessor == null) if (accessor == null)
{ {
throw new ArgumentNullException(nameof(accessor)); throw new ArgumentNullException(nameof(accessor));
} }
if (hostingEnviroment == null)
{
throw new ArgumentNullException(nameof(hostingEnviroment));
}
_provider = accessor.FileProvider; _provider = accessor.FileProvider;
_hostingEnvironment = hostingEnviroment;
} }
public override RazorProjectItem GetItem(string path) public override RazorProjectItem GetItem(string path)
{ {
path = NormalizeAndEnsureValidPath(path); path = NormalizeAndEnsureValidPath(path);
var fileInfo = _provider.GetFileInfo(path); var fileInfo = _provider.GetFileInfo(path);
return new FileProviderRazorProjectItem(fileInfo, basePath: string.Empty, path: path);
string relativePhysicalPath = null;
if (fileInfo != null && fileInfo.Exists)
{
var absoluteBasePath = _hostingEnvironment.ContentRootPath;
relativePhysicalPath = fileInfo?.PhysicalPath?.Substring(absoluteBasePath.Length + 1); // Include leading separator
relativePhysicalPath = relativePhysicalPath ?? path; // Use the incoming path if the file is not directly accessible
}
return new FileProviderRazorProjectItem(fileInfo, basePath: string.Empty, filePath: path, relativePhysicalPath: relativePhysicalPath);
} }
public override IEnumerable<RazorProjectItem> EnumerateItems(string path) public override IEnumerable<RazorProjectItem> EnumerateItems(string path)
@ -55,7 +72,12 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal
} }
else if (string.Equals(RazorFileExtension, Path.GetExtension(file.Name), StringComparison.OrdinalIgnoreCase)) else if (string.Equals(RazorFileExtension, Path.GetExtension(file.Name), StringComparison.OrdinalIgnoreCase))
{ {
yield return new FileProviderRazorProjectItem(file, basePath, prefix + "/" + file.Name); var filePath = prefix + "/" + file.Name;
var absoluteBasePath = _hostingEnvironment.ContentRootPath;
var relativePhysicalPath = file.PhysicalPath?.Substring(absoluteBasePath.Length + 1); // Include leading separator
relativePhysicalPath = relativePhysicalPath ?? filePath; // Use the incoming path if the file is not directly accessible
yield return new FileProviderRazorProjectItem(file, basePath, filePath: filePath, relativePhysicalPath: relativePhysicalPath);
} }
} }
} }

View File

@ -9,11 +9,12 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal
{ {
public class FileProviderRazorProjectItem : RazorProjectItem public class FileProviderRazorProjectItem : RazorProjectItem
{ {
public FileProviderRazorProjectItem(IFileInfo fileInfo, string basePath, string path) public FileProviderRazorProjectItem(IFileInfo fileInfo, string basePath, string filePath, string relativePhysicalPath)
{ {
FileInfo = fileInfo; FileInfo = fileInfo;
BasePath = basePath; BasePath = basePath;
FilePath = path; FilePath = filePath;
RelativePhysicalPath = relativePhysicalPath;
} }
public IFileInfo FileInfo { get; } public IFileInfo FileInfo { get; }
@ -26,6 +27,8 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal
public override string PhysicalPath => FileInfo.PhysicalPath; public override string PhysicalPath => FileInfo.PhysicalPath;
public override string RelativePhysicalPath { get; }
public override Stream Read() public override Stream Read()
{ {
return FileInfo.CreateReadStream(); return FileInfo.CreateReadStream();

View File

@ -74,7 +74,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
// Arrange // Arrange
var expectedMessage = "The type or namespace name &#x27;NamespaceDoesNotExist&#x27; could not be found (" var expectedMessage = "The type or namespace name &#x27;NamespaceDoesNotExist&#x27; could not be found ("
+ "are you missing a using directive or an assembly reference?)"; + "are you missing a using directive or an assembly reference?)";
var expectedCompilationContent = "public class _Views_ErrorFromViewImports_Index : " var expectedCompilationContent = "public class Views_ErrorFromViewImports_Index : "
+ "global::Microsoft.AspNetCore.Mvc.Razor.RazorPage&lt;dynamic&gt;"; + "global::Microsoft.AspNetCore.Mvc.Razor.RazorPage&lt;dynamic&gt;";
var expectedMediaType = MediaTypeHeaderValue.Parse("text/html; charset=utf-8"); var expectedMediaType = MediaTypeHeaderValue.Parse("text/html; charset=utf-8");

View File

@ -797,8 +797,8 @@ Hello from /Pages/WithViewStart/Index.cshtml!";
// Arrange // Arrange
var expected = var expected =
@"Microsoft.AspNetCore.Mvc.Routing.UrlHelper @"Microsoft.AspNetCore.Mvc.Routing.UrlHelper
Microsoft.AspNetCore.Mvc.ViewFeatures.HtmlHelper`1[AspNetCore._InjectedPageProperties] Microsoft.AspNetCore.Mvc.ViewFeatures.HtmlHelper`1[AspNetCore.InjectedPageProperties]
Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary`1[AspNetCore._InjectedPageProperties]"; Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary`1[AspNetCore.InjectedPageProperties]";
// Act // Act
var response = await Client.GetStringAsync("InjectedPageProperties"); var response = await Client.GetStringAsync("InjectedPageProperties");

View File

@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved. // 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. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Razor.Hosting; using Microsoft.AspNetCore.Razor.Hosting;
using Microsoft.AspNetCore.Razor.Language; using Microsoft.AspNetCore.Razor.Language;
using Moq; using Moq;
@ -14,7 +15,9 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal
public ChecksumValidatorTest() public ChecksumValidatorTest()
{ {
FileProvider = new TestFileProvider(); FileProvider = new TestFileProvider();
Project = new FileProviderRazorProject(Mock.Of<IRazorViewEngineFileProviderAccessor>(a => a.FileProvider == FileProvider)); Project = new FileProviderRazorProject(
Mock.Of<IRazorViewEngineFileProviderAccessor>(a => a.FileProvider == FileProvider),
Mock.Of<IHostingEnvironment>(e => e.ContentRootPath == "BasePath"));
} }
public RazorProject Project { get; } public RazorProject Project { get; }

View File

@ -3,6 +3,7 @@
using System.IO; using System.IO;
using System.Text; using System.Text;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Razor.Extensions; using Microsoft.AspNetCore.Mvc.Razor.Extensions;
using Microsoft.AspNetCore.Razor.Language; using Microsoft.AspNetCore.Razor.Language;
using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis;
@ -14,6 +15,8 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal
{ {
public class CompilerFailedExceptionFactoryTest public class CompilerFailedExceptionFactoryTest
{ {
private readonly IHostingEnvironment _hostingEnvironment = Mock.Of<IHostingEnvironment>(e => e.ContentRootPath == "BasePath");
[Fact] [Fact]
public void GetCompilationFailedResult_ReadsRazorErrorsFromPage() public void GetCompilationFailedResult_ReadsRazorErrorsFromPage()
{ {
@ -25,7 +28,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal
fileProvider.AddFile(viewPath, "<span name=\"@(User.Id\">"); fileProvider.AddFile(viewPath, "<span name=\"@(User.Id\">");
var accessor = Mock.Of<IRazorViewEngineFileProviderAccessor>(a => a.FileProvider == fileProvider); var accessor = Mock.Of<IRazorViewEngineFileProviderAccessor>(a => a.FileProvider == fileProvider);
var razorProject = new FileProviderRazorProject(accessor); var razorProject = new FileProviderRazorProject(accessor, _hostingEnvironment);
var templateEngine = new MvcRazorTemplateEngine(razorEngine, razorProject); var templateEngine = new MvcRazorTemplateEngine(razorEngine, razorProject);
var codeDocument = templateEngine.CreateCodeDocument(viewPath); var codeDocument = templateEngine.CreateCodeDocument(viewPath);
@ -59,7 +62,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal
var accessor = Mock.Of<IRazorViewEngineFileProviderAccessor>(a => a.FileProvider == fileProvider); var accessor = Mock.Of<IRazorViewEngineFileProviderAccessor>(a => a.FileProvider == fileProvider);
var razorEngine = RazorEngine.Create(); var razorEngine = RazorEngine.Create();
var razorProject = new FileProviderRazorProject(accessor); var razorProject = new FileProviderRazorProject(accessor, _hostingEnvironment);
var templateEngine = new MvcRazorTemplateEngine(razorEngine, razorProject); var templateEngine = new MvcRazorTemplateEngine(razorEngine, razorProject);
var codeDocument = templateEngine.CreateCodeDocument(viewPath); var codeDocument = templateEngine.CreateCodeDocument(viewPath);
@ -91,7 +94,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal
fileProvider.AddFile(viewPath, fileContent); fileProvider.AddFile(viewPath, fileContent);
var accessor = Mock.Of<IRazorViewEngineFileProviderAccessor>(a => a.FileProvider == fileProvider); var accessor = Mock.Of<IRazorViewEngineFileProviderAccessor>(a => a.FileProvider == fileProvider);
var razorProject = new FileProviderRazorProject(accessor); var razorProject = new FileProviderRazorProject(accessor, _hostingEnvironment);
var templateEngine = new MvcRazorTemplateEngine(razorEngine, razorProject); var templateEngine = new MvcRazorTemplateEngine(razorEngine, razorProject);
var codeDocument = templateEngine.CreateCodeDocument(viewPath); var codeDocument = templateEngine.CreateCodeDocument(viewPath);
@ -121,7 +124,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal
var accessor = Mock.Of<IRazorViewEngineFileProviderAccessor>(a => a.FileProvider == fileProvider); var accessor = Mock.Of<IRazorViewEngineFileProviderAccessor>(a => a.FileProvider == fileProvider);
var razorEngine = RazorEngine.Create(); var razorEngine = RazorEngine.Create();
var razorProject = new FileProviderRazorProject(accessor); var razorProject = new FileProviderRazorProject(accessor, _hostingEnvironment);
var templateEngine = new MvcRazorTemplateEngine(razorEngine, razorProject) var templateEngine = new MvcRazorTemplateEngine(razorEngine, razorProject)
{ {
Options = Options =

View File

@ -1,7 +1,9 @@
// Copyright (c) .NET Foundation. All rights reserved. // 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. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.IO;
using System.Linq; using System.Linq;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.FileProviders; using Microsoft.Extensions.FileProviders;
using Moq; using Moq;
using Xunit; using Xunit;
@ -14,14 +16,14 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal
public void EnumerateFiles_ReturnsEmptySequenceIfNoCshtmlFilesArePresent() public void EnumerateFiles_ReturnsEmptySequenceIfNoCshtmlFilesArePresent()
{ {
// Arrange // Arrange
var fileProvider = new TestFileProvider(); var fileProvider = new TestFileProvider("BasePath");
var file1 = fileProvider.AddFile("File1.txt", "content"); var file1 = fileProvider.AddFile("File1.txt", "content");
var file2 = fileProvider.AddFile("File2.js", "content"); var file2 = fileProvider.AddFile("File2.js", "content");
fileProvider.AddDirectoryContent("/", new IFileInfo[] { file1, file2 }); fileProvider.AddDirectoryContent("/", new IFileInfo[] { file1, file2 });
var accessor = Mock.Of<IRazorViewEngineFileProviderAccessor>(a => a.FileProvider == fileProvider); var accessor = Mock.Of<IRazorViewEngineFileProviderAccessor>(a => a.FileProvider == fileProvider);
var razorProject = new FileProviderRazorProject(accessor); var razorProject = new FileProviderRazorProject(accessor, Mock.Of<IHostingEnvironment>(e => e.ContentRootPath == "BasePath"));
// Act // Act
var razorFiles = razorProject.EnumerateItems("/"); var razorFiles = razorProject.EnumerateItems("/");
@ -34,7 +36,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal
public void EnumerateFiles_ReturnsCshtmlFiles() public void EnumerateFiles_ReturnsCshtmlFiles()
{ {
// Arrange // Arrange
var fileProvider = new TestFileProvider(); var fileProvider = new TestFileProvider("BasePath");
var file1 = fileProvider.AddFile("File1.cshtml", "content"); var file1 = fileProvider.AddFile("File1.cshtml", "content");
var file2 = fileProvider.AddFile("File2.js", "content"); var file2 = fileProvider.AddFile("File2.js", "content");
var file3 = fileProvider.AddFile("File3.cshtml", "content"); var file3 = fileProvider.AddFile("File3.cshtml", "content");
@ -42,22 +44,35 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal
var accessor = Mock.Of<IRazorViewEngineFileProviderAccessor>(a => a.FileProvider == fileProvider); var accessor = Mock.Of<IRazorViewEngineFileProviderAccessor>(a => a.FileProvider == fileProvider);
var razorProject = new FileProviderRazorProject(accessor); var razorProject = new FileProviderRazorProject(accessor, Mock.Of<IHostingEnvironment>(e => e.ContentRootPath == "BasePath"));
// Act // Act
var razorFiles = razorProject.EnumerateItems("/"); var razorFiles = razorProject.EnumerateItems("/");
// Assert // Assert
Assert.Collection(razorFiles.OrderBy(f => f.FilePath), Assert.Collection(
file => Assert.Equal("/File1.cshtml", file.FilePath), razorFiles.OrderBy(f => f.FilePath),
file => Assert.Equal("/File3.cshtml", file.FilePath)); file =>
{
Assert.Equal("/File1.cshtml", file.FilePath);
Assert.Equal("/", file.BasePath);
Assert.Equal(Path.Combine("BasePath", "File1.cshtml"), file.PhysicalPath);
Assert.Equal("File1.cshtml", file.RelativePhysicalPath);
},
file =>
{
Assert.Equal("/File3.cshtml", file.FilePath);
Assert.Equal("/", file.BasePath);
Assert.Equal(Path.Combine("BasePath", "File3.cshtml"), file.PhysicalPath);
Assert.Equal("File3.cshtml", file.RelativePhysicalPath);
});
} }
[Fact] [Fact]
public void EnumerateFiles_IteratesOverAllCshtmlUnderRoot() public void EnumerateFiles_IteratesOverAllCshtmlUnderRoot()
{ {
// Arrange // Arrange
var fileProvider = new TestFileProvider(); var fileProvider = new TestFileProvider("BasePath");
var directory1 = new TestDirectoryFileInfo var directory1 = new TestDirectoryFileInfo
{ {
Name = "Level1-Dir1", Name = "Level1-Dir1",
@ -69,72 +84,150 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal
}; };
fileProvider.AddDirectoryContent("/", new IFileInfo[] { directory1, file1, directory2 }); fileProvider.AddDirectoryContent("/", new IFileInfo[] { directory1, file1, directory2 });
var file2 = fileProvider.AddFile("Level1-Dir1/File2.cshtml", "content"); var file2 = fileProvider.AddFile(Path.Combine("Level1-Dir1", "File2.cshtml"), "content");
var file3 = fileProvider.AddFile("Level1-Dir1/File3.cshtml", "content"); var file3 = fileProvider.AddFile(Path.Combine("Level1-Dir1", "File3.cshtml"), "content");
var file4 = fileProvider.AddFile("Level1-Dir1/File4.txt", "content"); var file4 = fileProvider.AddFile(Path.Combine("Level1-Dir1", "File4.txt"), "content");
var directory3 = new TestDirectoryFileInfo var directory3 = new TestDirectoryFileInfo
{ {
Name = "Level2-Dir1" Name = "Level2-Dir1"
}; };
fileProvider.AddDirectoryContent("/Level1-Dir1", new IFileInfo[] { file2, directory3, file3, file4 }); fileProvider.AddDirectoryContent("/Level1-Dir1", new IFileInfo[] { file2, directory3, file3, file4 });
var file5 = fileProvider.AddFile("Level1-Dir2/File5.cshtml", "content"); var file5 = fileProvider.AddFile(Path.Combine("Level1-Dir2", "File5.cshtml"), "content");
fileProvider.AddDirectoryContent("/Level1-Dir2", new IFileInfo[] { file5 }); fileProvider.AddDirectoryContent("/Level1-Dir2", new IFileInfo[] { file5 });
fileProvider.AddDirectoryContent("/Level1/Level2", new IFileInfo[0]); fileProvider.AddDirectoryContent("/Level1/Level2", new IFileInfo[0]);
var accessor = Mock.Of<IRazorViewEngineFileProviderAccessor>(a => a.FileProvider == fileProvider); var accessor = Mock.Of<IRazorViewEngineFileProviderAccessor>(a => a.FileProvider == fileProvider);
var razorProject = new FileProviderRazorProject(accessor); var razorProject = new FileProviderRazorProject(accessor, Mock.Of<IHostingEnvironment>(e => e.ContentRootPath == "BasePath"));
// Act // Act
var razorFiles = razorProject.EnumerateItems("/"); var razorFiles = razorProject.EnumerateItems("/");
// Assert // Assert
Assert.Collection(razorFiles.OrderBy(f => f.FilePath), Assert.Collection(razorFiles.OrderBy(f => f.FilePath),
file => Assert.Equal("/File1.cshtml", file.FilePath), file =>
file => Assert.Equal("/Level1-Dir1/File2.cshtml", file.FilePath), {
file => Assert.Equal("/Level1-Dir1/File3.cshtml", file.FilePath), Assert.Equal("/File1.cshtml", file.FilePath);
file => Assert.Equal("/Level1-Dir2/File5.cshtml", file.FilePath)); Assert.Equal("/", file.BasePath);
Assert.Equal(Path.Combine("BasePath", "File1.cshtml"), file.PhysicalPath);
Assert.Equal("File1.cshtml", file.RelativePhysicalPath);
},
file =>
{
Assert.Equal("/Level1-Dir1/File2.cshtml", file.FilePath);
Assert.Equal("/", file.BasePath);
Assert.Equal(Path.Combine("BasePath", "Level1-Dir1", "File2.cshtml"), file.PhysicalPath);
Assert.Equal(Path.Combine("Level1-Dir1", "File2.cshtml"), file.RelativePhysicalPath);
},
file =>
{
Assert.Equal("/Level1-Dir1/File3.cshtml", file.FilePath);
Assert.Equal("/", file.BasePath);
Assert.Equal(Path.Combine("BasePath", "Level1-Dir1", "File3.cshtml"), file.PhysicalPath);
Assert.Equal(Path.Combine("Level1-Dir1", "File3.cshtml"), file.RelativePhysicalPath);
},
file =>
{
Assert.Equal("/Level1-Dir2/File5.cshtml", file.FilePath);
Assert.Equal("/", file.BasePath);
Assert.Equal(Path.Combine("BasePath", "Level1-Dir2", "File5.cshtml"), file.PhysicalPath);
Assert.Equal(Path.Combine("Level1-Dir2", "File5.cshtml"), file.RelativePhysicalPath);
});
} }
[Fact] [Fact]
public void EnumerateFiles_IteratesOverAllCshtmlUnderPath() public void EnumerateFiles_IteratesOverAllCshtmlUnderPath()
{ {
// Arrange // Arrange
var fileProvider = new TestFileProvider(); var fileProvider = new TestFileProvider("BasePath");
var directory1 = new TestDirectoryFileInfo var directory1 = new TestDirectoryFileInfo
{ {
Name = "Level1-Dir1", Name = "Level1-Dir1",
}; };
var file1 = fileProvider.AddFile("File1.cshtml", "content"); var file1 = fileProvider.AddFile(Path.Combine("BasePath", "File1.cshtml"), "content");
var directory2 = new TestDirectoryFileInfo var directory2 = new TestDirectoryFileInfo
{ {
Name = "Level1-Dir2", Name = "Level1-Dir2",
}; };
fileProvider.AddDirectoryContent("/", new IFileInfo[] { directory1, file1, directory2 }); fileProvider.AddDirectoryContent("/", new IFileInfo[] { directory1, file1, directory2 });
var file2 = fileProvider.AddFile("Level1-Dir1/File2.cshtml", "content"); var file2 = fileProvider.AddFile(Path.Combine("Level1-Dir1", "File2.cshtml"), "content");
var file3 = fileProvider.AddFile("Level1-Dir1/File3.cshtml", "content"); var file3 = fileProvider.AddFile(Path.Combine("Level1-Dir1", "File3.cshtml"), "content");
var file4 = fileProvider.AddFile("Level1-Dir1/File4.txt", "content"); var file4 = fileProvider.AddFile(Path.Combine("Level1-Dir1", "File4.txt"), "content");
var directory3 = new TestDirectoryFileInfo var directory3 = new TestDirectoryFileInfo
{ {
Name = "Level2-Dir1" Name = "Level2-Dir1"
}; };
fileProvider.AddDirectoryContent("/Level1-Dir1", new IFileInfo[] { file2, directory3, file3, file4 }); fileProvider.AddDirectoryContent("/Level1-Dir1", new IFileInfo[] { file2, directory3, file3, file4 });
var file5 = fileProvider.AddFile("Level1-Dir2/File5.cshtml", "content"); var file5 = fileProvider.AddFile(Path.Combine("Level1-Dir2", "File5.cshtml"), "content");
fileProvider.AddDirectoryContent("/Level1-Dir2", new IFileInfo[] { file5 }); fileProvider.AddDirectoryContent("/Level1-Dir2", new IFileInfo[] { file5 });
fileProvider.AddDirectoryContent("/Level1/Level2", new IFileInfo[0]); fileProvider.AddDirectoryContent("/Level1/Level2", new IFileInfo[0]);
var accessor = Mock.Of<IRazorViewEngineFileProviderAccessor>(a => a.FileProvider == fileProvider); var accessor = Mock.Of<IRazorViewEngineFileProviderAccessor>(a => a.FileProvider == fileProvider);
var razorProject = new FileProviderRazorProject(accessor); var razorProject = new FileProviderRazorProject(accessor, Mock.Of<IHostingEnvironment>(e => e.ContentRootPath == "BasePath"));
// Act // Act
var razorFiles = razorProject.EnumerateItems("/Level1-Dir1"); var razorFiles = razorProject.EnumerateItems("/Level1-Dir1");
// Assert // Assert
Assert.Collection(razorFiles.OrderBy(f => f.FilePath), Assert.Collection(razorFiles.OrderBy(f => f.FilePath),
file => Assert.Equal("/File2.cshtml", file.FilePath), file =>
file => Assert.Equal("/File3.cshtml", file.FilePath)); {
Assert.Equal("/File2.cshtml", file.FilePath);
Assert.Equal("/Level1-Dir1", file.BasePath);
Assert.Equal(Path.Combine("BasePath", "Level1-Dir1", "File2.cshtml"), file.PhysicalPath);
Assert.Equal(Path.Combine("Level1-Dir1", "File2.cshtml"), file.RelativePhysicalPath);
},
file =>
{
Assert.Equal("/File3.cshtml", file.FilePath);
Assert.Equal("/Level1-Dir1", file.BasePath);
Assert.Equal(Path.Combine("BasePath", "Level1-Dir1", "File3.cshtml"), file.PhysicalPath);
Assert.Equal(Path.Combine("Level1-Dir1", "File3.cshtml"), file.RelativePhysicalPath);
});
}
[Fact(Skip = "Need to follow-up https://github.com/aspnet/Mvc/pull/7228")]
public void GetItem_ReturnsFileFromDisk()
{
var fileProvider = new TestFileProvider("BasePath");
var file1 = fileProvider.AddFile("File1.cshtml", "content");
var file2 = fileProvider.AddFile("File2.js", "content");
var file3 = fileProvider.AddFile("File3.cshtml", "content");
fileProvider.AddDirectoryContent("/", new IFileInfo[] { file1, file2, file3 });
var accessor = Mock.Of<IRazorViewEngineFileProviderAccessor>(a => a.FileProvider == fileProvider);
var razorProject = new FileProviderRazorProject(accessor, Mock.Of<IHostingEnvironment>(e => e.ContentRootPath == "BasePath"));
// Act
var item = razorProject.GetItem("/File3.cshtml");
// Assert
Assert.True(item.Exists);
Assert.Equal("/File3.cshtml", item.FilePath);
Assert.Equal(string.Empty, item.BasePath);
Assert.Equal(Path.Combine("BasePath", "File3.cshtml"), item.PhysicalPath);
Assert.Equal("File3.cshtml", item.RelativePhysicalPath);
}
[Fact]
public void GetItem_ReturnsNotFoundResult()
{
// Arrange
var fileProvider = new TestFileProvider("BasePath");
var file = fileProvider.AddFile("SomeFile.cshtml", "content");
fileProvider.AddDirectoryContent("/", new IFileInfo[] { file });
var accessor = Mock.Of<IRazorViewEngineFileProviderAccessor>(a => a.FileProvider == fileProvider);
var razorProject = new FileProviderRazorProject(accessor, Mock.Of<IHostingEnvironment>(e => e.ContentRootPath == "BasePath"));
// Act
var item = razorProject.GetItem("/NotFound.cshtml");
// Assert
Assert.False(item.Exists);
} }
} }
} }

View File

@ -35,7 +35,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal
partManager, partManager,
new RazorTemplateEngine( new RazorTemplateEngine(
RazorEngine.Create(), RazorEngine.Create(),
new FileProviderRazorProject(accessor)), new FileProviderRazorProject(accessor, Mock.Of<IHostingEnvironment>())),
accessor, accessor,
new CSharpCompiler(referenceManager, Mock.Of<IHostingEnvironment>()), new CSharpCompiler(referenceManager, Mock.Of<IHostingEnvironment>()),
options, options,

View File

@ -821,7 +821,8 @@ this should fail";
precompiledViews = precompiledViews ?? Array.Empty<CompiledViewDescriptor>(); precompiledViews = precompiledViews ?? Array.Empty<CompiledViewDescriptor>();
var projectSystem = new FileProviderRazorProject(accessor); var hostingEnvironment = Mock.Of<IHostingEnvironment>(e => e.ContentRootPath == "BasePath");
var projectSystem = new FileProviderRazorProject(accessor, hostingEnvironment);
var templateEngine = new RazorTemplateEngine(RazorEngine.Create(), projectSystem) var templateEngine = new RazorTemplateEngine(RazorEngine.Create(), projectSystem)
{ {
Options = Options =
@ -832,7 +833,7 @@ this should fail";
var viewCompiler = new TestRazorViewCompiler( var viewCompiler = new TestRazorViewCompiler(
fileProvider, fileProvider,
templateEngine, templateEngine,
new CSharpCompiler(referenceManager, Mock.Of<IHostingEnvironment>()), new CSharpCompiler(referenceManager, hostingEnvironment),
compilationCallback, compilationCallback,
precompiledViews); precompiledViews);
return viewCompiler; return viewCompiler;

View File

@ -935,7 +935,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor
var fileProvider = new TestFileProvider(); var fileProvider = new TestFileProvider();
var accessor = Mock.Of<IRazorViewEngineFileProviderAccessor>(a => a.FileProvider == fileProvider); var accessor = Mock.Of<IRazorViewEngineFileProviderAccessor>(a => a.FileProvider == fileProvider);
var razorProject = new FileProviderRazorProject(accessor); var razorProject = new FileProviderRazorProject(accessor, Mock.Of<IHostingEnvironment>());
var viewEngine = CreateViewEngine(pageFactory.Object, razorProject: razorProject); var viewEngine = CreateViewEngine(pageFactory.Object, razorProject: razorProject);
var context = GetActionContext(_controllerTestContext); var context = GetActionContext(_controllerTestContext);
@ -1386,7 +1386,8 @@ namespace Microsoft.AspNetCore.Mvc.Razor
new HtmlTestEncoder(), new HtmlTestEncoder(),
GetOptionsAccessor(expanders: null), GetOptionsAccessor(expanders: null),
new FileProviderRazorProject( new FileProviderRazorProject(
Mock.Of<IRazorViewEngineFileProviderAccessor>(a => a.FileProvider == new TestFileProvider())), Mock.Of<IRazorViewEngineFileProviderAccessor>(a => a.FileProvider == new TestFileProvider()),
Mock.Of<IHostingEnvironment>()),
loggerFactory, loggerFactory,
new DiagnosticListener("Microsoft.AspNetCore.Mvc.Razor")); new DiagnosticListener("Microsoft.AspNetCore.Mvc.Razor"));
@ -1974,7 +1975,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor
if (razorProject == null) if (razorProject == null)
{ {
var accessor = Mock.Of<IRazorViewEngineFileProviderAccessor>(a => a.FileProvider == new TestFileProvider()); var accessor = Mock.Of<IRazorViewEngineFileProviderAccessor>(a => a.FileProvider == new TestFileProvider());
razorProject = new FileProviderRazorProject(accessor); razorProject = new FileProviderRazorProject(accessor, Mock.Of<IHostingEnvironment>());
} }
return new TestableRazorViewEngine(pageFactory, GetOptionsAccessor(expanders), razorProject); return new TestableRazorViewEngine(pageFactory, GetOptionsAccessor(expanders), razorProject);
} }
@ -2080,7 +2081,8 @@ namespace Microsoft.AspNetCore.Mvc.Razor
pageFactory, pageFactory,
optionsAccessor, optionsAccessor,
new FileProviderRazorProject( new FileProviderRazorProject(
Mock.Of<IRazorViewEngineFileProviderAccessor>(a => a.FileProvider == new TestFileProvider()))) Mock.Of<IRazorViewEngineFileProviderAccessor>(a => a.FileProvider == new TestFileProvider()),
Mock.Of<IHostingEnvironment>()))
{ {
} }

View File

@ -4,9 +4,6 @@
using System; using System;
using Microsoft.AspNetCore.Mvc.Abstractions; using Microsoft.AspNetCore.Mvc.Abstractions;
using Microsoft.AspNetCore.Mvc.ApplicationModels; using Microsoft.AspNetCore.Mvc.ApplicationModels;
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.AspNetCore.Mvc.Razor.Internal;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Moq; using Moq;
using Xunit; using Xunit;
@ -207,16 +204,6 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure
return Options.Create(new RazorPagesOptions()); return Options.Create(new RazorPagesOptions());
} }
private static RazorProjectItem GetProjectItem(string basePath, string path, string content)
{
var testFileInfo = new TestFileInfo
{
Content = content,
};
return new FileProviderRazorProjectItem(testFileInfo, basePath, path);
}
private class TestPageRouteModelProvider : IPageRouteModelProvider private class TestPageRouteModelProvider : IPageRouteModelProvider
{ {
private readonly PageRouteModel[] _models; private readonly PageRouteModel[] _models;

View File

@ -4,6 +4,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.ApplicationModels; using Microsoft.AspNetCore.Mvc.ApplicationModels;
using Microsoft.AspNetCore.Mvc.ApplicationParts; using Microsoft.AspNetCore.Mvc.ApplicationParts;
using Microsoft.AspNetCore.Mvc.Razor; using Microsoft.AspNetCore.Mvc.Razor;
@ -26,7 +27,9 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal
public CompiledPageRouteModelProviderTest() public CompiledPageRouteModelProviderTest()
{ {
FileProvider = new TestFileProvider(); FileProvider = new TestFileProvider();
Project = new FileProviderRazorProject(Mock.Of<IRazorViewEngineFileProviderAccessor>(a => a.FileProvider == FileProvider)); Project = new FileProviderRazorProject(
Mock.Of<IRazorViewEngineFileProviderAccessor>(a => a.FileProvider == FileProvider),
Mock.Of<IHostingEnvironment>(e => e.ContentRootPath == "BasePath"));
TemplateEngine = new RazorTemplateEngine(RazorEngine.Create(), Project); TemplateEngine = new RazorTemplateEngine(RazorEngine.Create(), Project);
PagesOptions = new RazorPagesOptions(); PagesOptions = new RazorPagesOptions();

View File

@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved. // 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. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Razor; using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.AspNetCore.Mvc.Razor.Internal; using Microsoft.AspNetCore.Mvc.Razor.Internal;
using Microsoft.AspNetCore.Razor.Language; using Microsoft.AspNetCore.Razor.Language;
@ -14,6 +15,8 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal
{ {
public class PageActionDescriptorChangeProviderTest public class PageActionDescriptorChangeProviderTest
{ {
private readonly IHostingEnvironment _hostingEnvironment = Mock.Of<IHostingEnvironment>(e => e.ContentRootPath == "BasePath");
[Fact] [Fact]
public void GetChangeToken_WatchesAllCshtmlFilesUnderFileSystemRoot() public void GetChangeToken_WatchesAllCshtmlFilesUnderFileSystemRoot()
{ {
@ -25,7 +28,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal
var templateEngine = new RazorTemplateEngine( var templateEngine = new RazorTemplateEngine(
RazorEngine.Create(), RazorEngine.Create(),
new FileProviderRazorProject(accessor)); new FileProviderRazorProject(accessor, _hostingEnvironment));
var options = Options.Create(new RazorPagesOptions()); var options = Options.Create(new RazorPagesOptions());
var changeProvider = new PageActionDescriptorChangeProvider(templateEngine, accessor, options); var changeProvider = new PageActionDescriptorChangeProvider(templateEngine, accessor, options);
@ -49,7 +52,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal
var templateEngine = new RazorTemplateEngine( var templateEngine = new RazorTemplateEngine(
RazorEngine.Create(), RazorEngine.Create(),
new FileProviderRazorProject(accessor)); new FileProviderRazorProject(accessor, _hostingEnvironment));
var options = Options.Create(new RazorPagesOptions()); var options = Options.Create(new RazorPagesOptions());
options.Value.RootDirectory = rootDirectory; options.Value.RootDirectory = rootDirectory;
@ -73,7 +76,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal
var templateEngine = new RazorTemplateEngine( var templateEngine = new RazorTemplateEngine(
RazorEngine.Create(), RazorEngine.Create(),
new FileProviderRazorProject(accessor)); new FileProviderRazorProject(accessor, _hostingEnvironment));
var options = Options.Create(new RazorPagesOptions { AllowAreas = true }); var options = Options.Create(new RazorPagesOptions { AllowAreas = true });
var changeProvider = new PageActionDescriptorChangeProvider(templateEngine, accessor, options); var changeProvider = new PageActionDescriptorChangeProvider(templateEngine, accessor, options);
@ -97,7 +100,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal
var templateEngine = new RazorTemplateEngine( var templateEngine = new RazorTemplateEngine(
RazorEngine.Create(), RazorEngine.Create(),
new FileProviderRazorProject(accessor)); new FileProviderRazorProject(accessor, _hostingEnvironment));
var options = Options.Create(new RazorPagesOptions var options = Options.Create(new RazorPagesOptions
{ {
AllowAreas = true, AllowAreas = true,
@ -121,7 +124,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal
var templateEngine = new RazorTemplateEngine( var templateEngine = new RazorTemplateEngine(
RazorEngine.Create(), RazorEngine.Create(),
new FileProviderRazorProject(accessor)); new FileProviderRazorProject(accessor, _hostingEnvironment));
templateEngine.Options.ImportsFileName = "_ViewImports.cshtml"; templateEngine.Options.ImportsFileName = "_ViewImports.cshtml";
var options = Options.Create(new RazorPagesOptions()); var options = Options.Create(new RazorPagesOptions());
options.Value.RootDirectory = "/dir1/dir2"; options.Value.RootDirectory = "/dir1/dir2";
@ -145,7 +148,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal
var templateEngine = new RazorTemplateEngine( var templateEngine = new RazorTemplateEngine(
RazorEngine.Create(), RazorEngine.Create(),
new FileProviderRazorProject(accessor)); new FileProviderRazorProject(accessor, _hostingEnvironment));
templateEngine.Options.ImportsFileName = "_ViewImports.cshtml"; templateEngine.Options.ImportsFileName = "_ViewImports.cshtml";
var options = Options.Create(new RazorPagesOptions()); var options = Options.Create(new RazorPagesOptions());
options.Value.RootDirectory = "/dir1/dir2"; options.Value.RootDirectory = "/dir1/dir2";
@ -173,7 +176,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal
var templateEngine = new RazorTemplateEngine( var templateEngine = new RazorTemplateEngine(
RazorEngine.Create(), RazorEngine.Create(),
new FileProviderRazorProject(accessor)); new FileProviderRazorProject(accessor, _hostingEnvironment));
templateEngine.Options.ImportsFileName = "_ViewImports.cshtml"; templateEngine.Options.ImportsFileName = "_ViewImports.cshtml";
var options = Options.Create(new RazorPagesOptions { AllowAreas = false }); var options = Options.Create(new RazorPagesOptions { AllowAreas = false });

View File

@ -5,6 +5,7 @@ using System;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Abstractions; using Microsoft.AspNetCore.Mvc.Abstractions;
using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.AspNetCore.Mvc.Filters;
@ -28,6 +29,8 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal
{ {
public class PageInvokerProviderTest public class PageInvokerProviderTest
{ {
private readonly IHostingEnvironment _hostingEnvironment = Mock.Of<IHostingEnvironment>(e => e.ContentRootPath == "BasePath");
[Fact] [Fact]
public void OnProvidersExecuting_WithEmptyModel_PopulatesCacheEntry() public void OnProvidersExecuting_WithEmptyModel_PopulatesCacheEntry()
{ {
@ -190,7 +193,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal
fileProvider.AddFile("/_ViewStart.cshtml", "content2"); fileProvider.AddFile("/_ViewStart.cshtml", "content2");
var accessor = Mock.Of<IRazorViewEngineFileProviderAccessor>(a => a.FileProvider == fileProvider); var accessor = Mock.Of<IRazorViewEngineFileProviderAccessor>(a => a.FileProvider == fileProvider);
var defaultRazorProject = new FileProviderRazorProject(accessor); var defaultRazorProject = new FileProviderRazorProject(accessor, _hostingEnvironment);
var invokerProvider = CreateInvokerProvider( var invokerProvider = CreateInvokerProvider(
loader.Object, loader.Object,
@ -347,7 +350,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal
fileProvider.AddFile("/Pages/Level1/Level2/_ViewStart.cshtml", "page content"); fileProvider.AddFile("/Pages/Level1/Level2/_ViewStart.cshtml", "page content");
fileProvider.AddFile("/Pages/Level1/Level3/_ViewStart.cshtml", "page content"); fileProvider.AddFile("/Pages/Level1/Level3/_ViewStart.cshtml", "page content");
var razorProject = new TestRazorProject(fileProvider); var razorProject = new TestRazorProject(fileProvider, _hostingEnvironment);
var mock = new Mock<IRazorPageFactoryProvider>(MockBehavior.Strict); var mock = new Mock<IRazorPageFactoryProvider>(MockBehavior.Strict);
mock mock
@ -413,7 +416,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal
// No files // No files
var fileProvider = new TestFileProvider(); var fileProvider = new TestFileProvider();
var razorProject = new TestRazorProject(fileProvider); var razorProject = new TestRazorProject(fileProvider, _hostingEnvironment);
var invokerProvider = CreateInvokerProvider( var invokerProvider = CreateInvokerProvider(
loader.Object, loader.Object,

View File

@ -3,17 +3,21 @@
using System; using System;
using System.Linq; using System.Linq;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.ApplicationModels; using Microsoft.AspNetCore.Mvc.ApplicationModels;
using Microsoft.AspNetCore.Mvc.Razor; using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.Extensions.FileProviders; using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Moq;
using Xunit; using Xunit;
namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal
{ {
public class RazorProjectPageRouteModelProviderTest public class RazorProjectPageRouteModelProviderTest
{ {
private readonly IHostingEnvironment _hostingEnvironment = Mock.Of<IHostingEnvironment>(e => e.ContentRootPath == "BasePath");
[Fact] [Fact]
public void OnProvidersExecuting_ReturnsPagesWithPageDirective() public void OnProvidersExecuting_ReturnsPagesWithPageDirective()
{ {
@ -25,7 +29,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal
var dir1 = fileProvider.AddDirectoryContent("/Pages", new IFileInfo[] { file1, file2 }); var dir1 = fileProvider.AddDirectoryContent("/Pages", new IFileInfo[] { file1, file2 });
fileProvider.AddDirectoryContent("/", new[] { dir1 }); fileProvider.AddDirectoryContent("/", new[] { dir1 });
var project = new TestRazorProject(fileProvider); var project = new TestRazorProject(fileProvider, _hostingEnvironment);
var optionsManager = Options.Create(new RazorPagesOptions()); var optionsManager = Options.Create(new RazorPagesOptions());
optionsManager.Value.RootDirectory = "/"; optionsManager.Value.RootDirectory = "/";
@ -67,7 +71,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal
var areasDir = fileProvider.AddDirectoryContent("/Areas", new[] { productsDir }); var areasDir = fileProvider.AddDirectoryContent("/Areas", new[] { productsDir });
var rootDir = fileProvider.AddDirectoryContent("/", new[] { areasDir }); var rootDir = fileProvider.AddDirectoryContent("/", new[] { areasDir });
var project = new TestRazorProject(fileProvider); var project = new TestRazorProject(fileProvider, _hostingEnvironment);
var optionsManager = Options.Create(new RazorPagesOptions { AllowAreas = true }); var optionsManager = Options.Create(new RazorPagesOptions { AllowAreas = true });
var provider = new RazorProjectPageRouteModelProvider(project, optionsManager, NullLoggerFactory.Instance); var provider = new RazorProjectPageRouteModelProvider(project, optionsManager, NullLoggerFactory.Instance);
@ -151,7 +155,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal
var pagesDir = fileProvider.AddDirectoryContent("/Pages", new[] { file4 }); var pagesDir = fileProvider.AddDirectoryContent("/Pages", new[] { file4 });
var rootDir = fileProvider.AddDirectoryContent("/", new[] { areasDir, pagesDir }); var rootDir = fileProvider.AddDirectoryContent("/", new[] { areasDir, pagesDir });
var project = new TestRazorProject(fileProvider); var project = new TestRazorProject(fileProvider, _hostingEnvironment);
var optionsManager = Options.Create(new RazorPagesOptions { AllowAreas = false }); var optionsManager = Options.Create(new RazorPagesOptions { AllowAreas = false });
var provider = new RazorProjectPageRouteModelProvider(project, optionsManager, NullLoggerFactory.Instance); var provider = new RazorProjectPageRouteModelProvider(project, optionsManager, NullLoggerFactory.Instance);
@ -185,7 +189,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal
var areasDir = fileProvider.AddDirectoryContent("/Areas", new IFileInfo[] { productsDir, nonConformingFileUnderAreasDirectory }); var areasDir = fileProvider.AddDirectoryContent("/Areas", new IFileInfo[] { productsDir, nonConformingFileUnderAreasDirectory });
var rootDir = fileProvider.AddDirectoryContent("/", new IFileInfo[] { areasDir, rootFile }); var rootDir = fileProvider.AddDirectoryContent("/", new IFileInfo[] { areasDir, rootFile });
var project = new TestRazorProject(fileProvider); var project = new TestRazorProject(fileProvider, _hostingEnvironment);
var optionsManager = Options.Create(new RazorPagesOptions var optionsManager = Options.Create(new RazorPagesOptions
{ {
@ -247,7 +251,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal
var dir1 = fileProvider.AddDirectoryContent("/Pages", new IFileInfo[] { dir2, file1, file2 }); var dir1 = fileProvider.AddDirectoryContent("/Pages", new IFileInfo[] { dir2, file1, file2 });
fileProvider.AddDirectoryContent("/", new[] { dir1 }); fileProvider.AddDirectoryContent("/", new[] { dir1 });
var project = new TestRazorProject(fileProvider); var project = new TestRazorProject(fileProvider, _hostingEnvironment);
var optionsManager = Options.Create(new RazorPagesOptions()); var optionsManager = Options.Create(new RazorPagesOptions());
optionsManager.Value.RootDirectory = "/"; optionsManager.Value.RootDirectory = "/";
@ -285,7 +289,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal
var file = fileProvider.AddFile("/Index.cshtml", "@page \"/custom-route\""); var file = fileProvider.AddFile("/Index.cshtml", "@page \"/custom-route\"");
fileProvider.AddDirectoryContent("/", new[] { file }); fileProvider.AddDirectoryContent("/", new[] { file });
var project = new TestRazorProject(fileProvider); var project = new TestRazorProject(fileProvider, _hostingEnvironment);
var optionsManager = Options.Create(new RazorPagesOptions()); var optionsManager = Options.Create(new RazorPagesOptions());
optionsManager.Value.RootDirectory = "/"; optionsManager.Value.RootDirectory = "/";
@ -311,7 +315,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal
}); });
fileProvider.AddDirectoryContent("/", new[] { dir1 }); fileProvider.AddDirectoryContent("/", new[] { dir1 });
var project = new TestRazorProject(fileProvider); var project = new TestRazorProject(fileProvider, _hostingEnvironment);
var optionsManager = Options.Create(new RazorPagesOptions()); var optionsManager = Options.Create(new RazorPagesOptions());
optionsManager.Value.RootDirectory = "/"; optionsManager.Value.RootDirectory = "/";
@ -349,7 +353,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal
var rootFile = fileProvider.AddFile("/Index.cshtml", "@page"); var rootFile = fileProvider.AddFile("/Index.cshtml", "@page");
fileProvider.AddDirectoryContent("/", new IFileInfo[] { rootFile, dir1, dir2 }); fileProvider.AddDirectoryContent("/", new IFileInfo[] { rootFile, dir1, dir2 });
var project = new TestRazorProject(fileProvider); var project = new TestRazorProject(fileProvider, _hostingEnvironment);
var optionsManager = Options.Create(new RazorPagesOptions()); var optionsManager = Options.Create(new RazorPagesOptions());
optionsManager.Value.RootDirectory = "/Pages"; optionsManager.Value.RootDirectory = "/Pages";
@ -378,7 +382,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal
var dir1 = fileProvider.AddDirectoryContent("/Pages", new IFileInfo[] { file1, file2 }); var dir1 = fileProvider.AddDirectoryContent("/Pages", new IFileInfo[] { file1, file2 });
fileProvider.AddDirectoryContent("/", new[] { dir1 }); fileProvider.AddDirectoryContent("/", new[] { dir1 });
var project = new TestRazorProject(fileProvider); var project = new TestRazorProject(fileProvider, _hostingEnvironment);
var optionsManager = Options.Create(new RazorPagesOptions()); var optionsManager = Options.Create(new RazorPagesOptions());
optionsManager.Value.RootDirectory = "/"; optionsManager.Value.RootDirectory = "/";

View File

@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved. // 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. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Razor.Internal; using Microsoft.AspNetCore.Mvc.Razor.Internal;
using Microsoft.Extensions.FileProviders; using Microsoft.Extensions.FileProviders;
using Moq; using Moq;
@ -9,8 +10,8 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages
{ {
public class TestRazorProject : FileProviderRazorProject public class TestRazorProject : FileProviderRazorProject
{ {
public TestRazorProject(IFileProvider fileProvider) public TestRazorProject(IFileProvider fileProvider, IHostingEnvironment hostingEnvironment)
:base(GetAccessor(fileProvider)) :base(GetAccessor(fileProvider), hostingEnvironment)
{ {
} }

View File

@ -20,6 +20,17 @@ namespace Microsoft.AspNetCore.Mvc.Razor
private readonly Dictionary<string, TestFileChangeToken> _fileTriggers = private readonly Dictionary<string, TestFileChangeToken> _fileTriggers =
new Dictionary<string, TestFileChangeToken>(StringComparer.Ordinal); new Dictionary<string, TestFileChangeToken>(StringComparer.Ordinal);
public TestFileProvider() : this(string.Empty)
{
}
public TestFileProvider(string root)
{
Root = root;
}
public string Root { get; }
public virtual IDirectoryContents GetDirectoryContents(string subpath) public virtual IDirectoryContents GetDirectoryContents(string subpath)
{ {
if (_directoryContentsLookup.TryGetValue(subpath, out var value)) if (_directoryContentsLookup.TryGetValue(subpath, out var value))
@ -35,7 +46,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor
var fileInfo = new TestFileInfo var fileInfo = new TestFileInfo
{ {
Content = contents, Content = contents,
PhysicalPath = path, PhysicalPath = Path.Combine(Root, path),
Name = Path.GetFileName(path), Name = Path.GetFileName(path),
LastModified = DateTime.UtcNow, LastModified = DateTime.UtcNow,
}; };

View File

@ -3,6 +3,7 @@
using System.IO; using System.IO;
using System.Text; using System.Text;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Razor.Internal; using Microsoft.AspNetCore.Mvc.Razor.Internal;
using Microsoft.AspNetCore.Razor.Language; using Microsoft.AspNetCore.Razor.Language;
@ -10,8 +11,8 @@ namespace RazorPageExecutionInstrumentationWebSite
{ {
public class TestRazorProject : FileProviderRazorProject public class TestRazorProject : FileProviderRazorProject
{ {
public TestRazorProject(IRazorViewEngineFileProviderAccessor fileProviderAccessor) public TestRazorProject(IRazorViewEngineFileProviderAccessor fileProviderAccessor, IHostingEnvironment hostingEnvironment)
: base(fileProviderAccessor) : base(fileProviderAccessor, hostingEnvironment)
{ {
} }
@ -24,7 +25,7 @@ namespace RazorPageExecutionInstrumentationWebSite
private class TestRazorProjectItem : FileProviderRazorProjectItem private class TestRazorProjectItem : FileProviderRazorProjectItem
{ {
public TestRazorProjectItem(FileProviderRazorProjectItem projectItem) public TestRazorProjectItem(FileProviderRazorProjectItem projectItem)
: base(projectItem.FileInfo, projectItem.BasePath, projectItem.FilePath) : base(projectItem.FileInfo, projectItem.BasePath, projectItem.FilePath, projectItem.RelativePhysicalPath)
{ {
} }