Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

Commit ca8e1a0

Browse files
committed
Add tests, changes per PR comments
1 parent b1d6e00 commit ca8e1a0

File tree

4 files changed

+72
-31
lines changed

4 files changed

+72
-31
lines changed

src/Microsoft.AspNetCore.Mvc.Razor/RazorFileHierarchy.cs

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using System;
55
using System.Collections.Generic;
6-
using System.Diagnostics;
76
using System.Text;
87

98
namespace Microsoft.AspNetCore.Mvc.Razor
@@ -12,7 +11,7 @@ internal static class RazorFileHierarchy
1211
{
1312
private const string ViewStartFileName = "_ViewStart.cshtml";
1413

15-
public static IEnumerable<string> FindViewImports(string path)
14+
public static IEnumerable<string> GetViewStartPaths(string path)
1615
{
1716
if (string.IsNullOrEmpty(path))
1817
{
@@ -24,37 +23,15 @@ public static IEnumerable<string> FindViewImports(string path)
2423
throw new ArgumentException(Resources.RazorProject_PathMustStartWithForwardSlash, nameof(path));
2524
}
2625

27-
var basePath = "/";
28-
Debug.Assert(!string.IsNullOrEmpty(path));
2926
if (path.Length == 1)
3027
{
3128
yield break;
3229
}
3330

34-
if (!path.StartsWith(basePath, StringComparison.OrdinalIgnoreCase))
35-
{
36-
yield break;
37-
}
38-
39-
StringBuilder builder;
40-
var fileNameIndex = path.LastIndexOf('/');
41-
var length = path.Length;
42-
Debug.Assert(fileNameIndex != -1);
43-
if (string.Compare(path, fileNameIndex + 1, ViewStartFileName, 0, ViewStartFileName.Length) == 0)
44-
{
45-
// If the specified path is for the file hierarchy being constructed, then the first file that applies
46-
// to it is in a parent directory.
47-
builder = new StringBuilder(path, 0, fileNameIndex, fileNameIndex + ViewStartFileName.Length);
48-
length = fileNameIndex;
49-
}
50-
else
51-
{
52-
builder = new StringBuilder(path);
53-
}
54-
55-
var maxDepth = 255;
56-
var index = length;
57-
while (maxDepth-- > 0 && index > basePath.Length && (index = path.LastIndexOf('/', index - 1)) != -1)
31+
var builder = new StringBuilder(path);
32+
var maxIterations = 255;
33+
var index = path.Length;
34+
while (maxIterations-- > 0 && index > 1 && (index = path.LastIndexOf('/', index - 1)) != -1)
5835
{
5936
builder.Length = index + 1;
6037
builder.Append(ViewStartFileName);

src/Microsoft.AspNetCore.Mvc.Razor/RazorViewEngine.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public class RazorViewEngine : IRazorViewEngine
4343
private readonly DiagnosticListener _diagnosticListener;
4444

4545
/// <summary>
46-
/// Initializes a new instance of the RazorViewEngine
46+
/// Initializes a new instance of the <see cref="RazorViewEngine" />.
4747
/// </summary>
4848
public RazorViewEngine(
4949
IRazorPageFactoryProvider pageFactory,
@@ -435,7 +435,7 @@ private IReadOnlyList<ViewLocationCacheItem> GetViewStartPages(
435435
{
436436
var viewStartPages = new List<ViewLocationCacheItem>();
437437

438-
foreach (var filePath in RazorFileHierarchy.FindViewImports(path))
438+
foreach (var filePath in RazorFileHierarchy.GetViewStartPaths(path))
439439
{
440440
var result = _pageFactory.CreateFactory(filePath);
441441
var viewDescriptor = result.ViewDescriptor;

src/Microsoft.AspNetCore.Mvc.RazorPages/Infrastructure/PageActionInvokerProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ internal List<Func<IRazorPage>> GetViewStartFactories(CompiledPageActionDescript
211211
{
212212
var viewStartFactories = new List<Func<IRazorPage>>();
213213
// Always pick up all _ViewStarts, including the ones outside the Pages root.
214-
foreach (var filePath in RazorFileHierarchy.FindViewImports(descriptor.RelativePath))
214+
foreach (var filePath in RazorFileHierarchy.GetViewStartPaths(descriptor.RelativePath))
215215
{
216216
var factoryResult = _razorPageFactoryProvider.CreateFactory(filePath);
217217
if (factoryResult.Success)
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using Xunit;
5+
6+
namespace Microsoft.AspNetCore.Mvc.Razor
7+
{
8+
public class RazorFileHierarchyTest
9+
{
10+
[Fact]
11+
public void GetViewStartPaths_ForFileAtRoot()
12+
{
13+
// Arrange
14+
var expected = new[] { "/_ViewStart.cshtml", };
15+
var path = "/Home.cshtml";
16+
17+
// Act
18+
var actual = RazorFileHierarchy.GetViewStartPaths(path);
19+
20+
// Assert
21+
Assert.Equal(expected, actual);
22+
}
23+
24+
[Fact]
25+
public void GetViewStartPaths_ForForFileInViewsDirectory()
26+
{
27+
// Arrange
28+
var expected = new[]
29+
{
30+
"/Views/Home/_ViewStart.cshtml",
31+
"/Views/_ViewStart.cshtml",
32+
"/_ViewStart.cshtml",
33+
};
34+
var path = "/Views/Home/Index.cshtml";
35+
36+
// Act
37+
var actual = RazorFileHierarchy.GetViewStartPaths(path);
38+
39+
// Assert
40+
Assert.Equal(expected, actual);
41+
}
42+
43+
[Fact]
44+
public void GetViewStartPaths_ForForFileInAreasDirectory()
45+
{
46+
// Arrange
47+
var expected = new[]
48+
{
49+
"/Areas/Views/MyArea/Home/_ViewStart.cshtml",
50+
"/Areas/Views/MyArea/_ViewStart.cshtml",
51+
"/Areas/Views/_ViewStart.cshtml",
52+
"/Areas/_ViewStart.cshtml",
53+
"/_ViewStart.cshtml",
54+
};
55+
var path = "/Areas/Views/MyArea/Home/Index.cshtml";
56+
57+
// Act
58+
var actual = RazorFileHierarchy.GetViewStartPaths(path);
59+
60+
// Assert
61+
Assert.Equal(expected, actual);
62+
}
63+
}
64+
}

0 commit comments

Comments
 (0)