// Internal for unit testing internal CompilerCache(IEnumerable <RazorFileInfoCollection> viewCollections, IFileProvider fileProvider) { _fileProvider = fileProvider; _cache = new ConcurrentDictionary <string, CompilerCacheEntry>(StringComparer.OrdinalIgnoreCase); foreach (var viewCollection in viewCollections) { foreach (var fileInfo in viewCollection.FileInfos) { var containingAssembly = viewCollection.GetType().GetTypeInfo().Assembly; var viewType = containingAssembly.GetType(fileInfo.FullTypeName); var cacheEntry = new CompilerCacheEntry(fileInfo, viewType); // There shouldn't be any duplicates and if there are any the first will win. // If the result doesn't match the one on disk its going to recompile anyways. _cache.TryAdd(NormalizePath(fileInfo.RelativePath), cacheEntry); } } // Set up ViewStarts foreach (var entry in _cache) { var viewStartLocations = ViewStartUtility.GetViewStartLocations(entry.Key); foreach (var location in viewStartLocations) { CompilerCacheEntry viewStartEntry; if (_cache.TryGetValue(location, out viewStartEntry)) { // Add the the composite _ViewStart entry as a dependency. entry.Value.AssociatedViewStartEntry = viewStartEntry; break; } } } }
public void GetViewStartLocations_ReturnsEmptySequenceIfViewPathIsEmpty(string viewPath) { // Act var result = ViewStartUtility.GetViewStartLocations(new TestFileSystem(), viewPath); // Assert Assert.Empty(result); }
public void GetViewStartLocations_ReturnsEmptySequence_IfPathIsRooted() { // Arrange var appBase = GetTestFileSystemBase(); var absolutePath = Path.Combine(Directory.GetCurrentDirectory(), "Index.cshtml"); // Act var result = ViewStartUtility.GetViewStartLocations(absolutePath); // Assert Assert.Empty(result); }
public void GetViewStartLocations_ReturnsEmptySequence_IfViewStartIsAtRoot() { // Arrange var appBase = GetTestFileSystemBase(); var viewPath = "_ViewStart.cshtml"; // Act var result = ViewStartUtility.GetViewStartLocations(viewPath); // Assert Assert.Empty(result); }
/// <inheritdoc /> public IEnumerable <IRazorPage> GetViewStartPages([NotNull] string path) { var viewStartLocations = ViewStartUtility.GetViewStartLocations(_fileSystem, path); var viewStarts = viewStartLocations.Select(_pageFactory.CreateInstance) .Where(p => p != null) .ToArray(); // GetViewStartLocations return ViewStarts inside-out that is the _ViewStart closest to the page // is the first: e.g. [ /Views/Home/_ViewStart, /Views/_ViewStart, /_ViewStart ] // However they need to be executed outside in, so we'll reverse the sequence. Array.Reverse(viewStarts); return(viewStarts); }
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); }
private PrecompilationCacheEntry OnCacheMiss(ICacheSetContext cacheSetContext) { var fileInfo = (RelativeFileInfo)cacheSetContext.State; var entry = GetCacheEntry(fileInfo); if (entry != null) { cacheSetContext.AddExpirationTrigger(_fileProvider.Watch(fileInfo.RelativePath)); foreach (var viewStartPath in ViewStartUtility.GetViewStartLocations(fileInfo.RelativePath)) { cacheSetContext.AddExpirationTrigger(_fileProvider.Watch(viewStartPath)); } } return(entry); }
public void GetViewStartLocations_ReturnsPotentialViewStartLocations_PathStartswithSlash(string inputPath) { // Arrange var expected = new[] { @"Views\Home\_ViewStart.cshtml", @"Views\_ViewStart.cshtml", @"_ViewStart.cshtml" }; // Act var result = ViewStartUtility.GetViewStartLocations(inputPath); // Assert Assert.Equal(expected, result); }
// Returns the entry for the nearest _ViewStart that the file inherits directives from. Since _ViewStart // entries are affected by other _ViewStart entries that are in the path hierarchy, the returned value // represents the composite result of performing a cache check on individual _ViewStart entries. private CompilerCacheEntry GetCompositeViewStartEntry(string relativePath, Func <RelativeFileInfo, CompilationResult> compile) { var viewStartLocations = ViewStartUtility.GetViewStartLocations(relativePath); foreach (var viewStartLocation in viewStartLocations) { var viewStartFileInfo = _fileProvider.GetFileInfo(viewStartLocation); if (viewStartFileInfo.Exists) { var relativeFileInfo = new RelativeFileInfo(viewStartFileInfo, viewStartLocation); CompilationResult result; return(GetOrAdd(relativeFileInfo, compile, out result)); } } // No _ViewStarts discovered. return(null); }
public void GetViewStartLocations_SkipsCurrentPath_IfPathIsAViewStartFile(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 viewPath = Path.Combine("Areas", "MyArea", "Sub", "Views", "Admin", fileName); // Act var result = ViewStartUtility.GetViewStartLocations(viewPath); // Assert Assert.Equal(expected, result); }
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); }
public void GetViewStartLocations_ReturnsPotentialViewStartLocations_IfPathIsAbsolute() { // Arrange var expected = new[] { @"Areas\MyArea\Sub\Views\Admin\_viewstart.cshtml", @"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", "Test.cshtml"); var fileSystem = new PhysicalFileSystem(appBase); // Act var result = ViewStartUtility.GetViewStartLocations(fileSystem, viewPath); // Assert Assert.Equal(expected, result); }