ViewStartUtility should not return current path if current is a view start

file
This commit is contained in:
Pranav K 2014-10-15 11:27:55 -07:00
parent 2c4b3dd3fc
commit 44d888c319
6 changed files with 80 additions and 3 deletions

View File

@ -47,6 +47,16 @@ namespace Microsoft.AspNet.Mvc.Razor
path = path.Substring(1);
}
if (string.Equals(ViewStartFileName, Path.GetFileName(path), StringComparison.OrdinalIgnoreCase))
{
// If the specified path is a ViewStart file, then the first view start that applies to it is the
// parent view start.
if (!fileSystem.TryGetParentPath(path, out path))
{
return Enumerable.Empty<string>();
}
}
var viewStartLocations = new List<string>();
while (fileSystem.TryGetParentPath(path, out path))

View File

@ -45,8 +45,31 @@ namespace Microsoft.AspNet.Mvc.Razor
Assert.Equal(expected, result);
}
[Fact]
public void GetViewStartLocations_ReturnsPotentialViewStartLocations_IfPathIsAbsolute()
[Theory]
[InlineData("/views/Home/_ViewStart.cshtml")]
[InlineData("~/views/Home/_viewstart.cshtml")]
[InlineData("views/Home/_Viewstart.cshtml")]
public void GetViewStartLocations_SkipsCurrentPath_IfCurrentIsViewStart(string inputPath)
{
// Arrange
var expected = new[]
{
@"views\_viewstart.cshtml",
@"_viewstart.cshtml"
};
var fileSystem = new PhysicalFileSystem(GetTestFileSystemBase());
// Act
var result = ViewStartUtility.GetViewStartLocations(fileSystem, inputPath);
// Assert
Assert.Equal(expected, result);
}
[Theory]
[InlineData("Test.cshtml")]
[InlineData("ViewStart.cshtml")]
public void GetViewStartLocations_ReturnsPotentialViewStartLocations_IfPathIsAbsolute(string fileName)
{
// Arrange
var expected = new[]
@ -59,7 +82,7 @@ namespace Microsoft.AspNet.Mvc.Razor
@"_viewstart.cshtml",
};
var appBase = GetTestFileSystemBase();
var viewPath = Path.Combine(appBase, "Areas", "MyArea", "Sub", "Views", "Admin", "Test.cshtml");
var viewPath = Path.Combine(appBase, "Areas", "MyArea", "Sub", "Views", "Admin", fileName);
var fileSystem = new PhysicalFileSystem(appBase);
// Act
@ -69,6 +92,46 @@ namespace Microsoft.AspNet.Mvc.Razor
Assert.Equal(expected, result);
}
[Theory]
[InlineData("_ViewStart.cshtml")]
[InlineData("_viewstart.cshtml")]
public void GetViewStartLocations_SkipsCurrentPath_IfAbsolutePathIsAViewStartFile(string fileName)
{
// Arrange
var expected = new[]
{
@"Areas\MyArea\Sub\Views\_viewstart.cshtml",
@"Areas\MyArea\Sub\_viewstart.cshtml",
@"Areas\MyArea\_viewstart.cshtml",
@"Areas\_viewstart.cshtml",
@"_viewstart.cshtml",
};
var appBase = GetTestFileSystemBase();
var viewPath = Path.Combine(appBase, "Areas", "MyArea", "Sub", "Views", "Admin", fileName);
var fileSystem = new PhysicalFileSystem(appBase);
// Act
var result = ViewStartUtility.GetViewStartLocations(fileSystem, viewPath);
// Assert
Assert.Equal(expected, result);
}
[Fact]
public void GetViewStartLocations_ReturnsEmptySequence_IfViewStartIsAtRoot()
{
// Arrange
var appBase = GetTestFileSystemBase();
var viewPath = "_viewstart.cshtml";
var fileSystem = new PhysicalFileSystem(appBase);
// Act
var result = ViewStartUtility.GetViewStartLocations(fileSystem, viewPath);
// Assert
Assert.Empty(result);
}
private static string GetTestFileSystemBase()
{
var serviceProvider = CallContextServiceLocator.Locator.ServiceProvider;