|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.Diagnostics; |
| 5 | +using Microsoft.AspNetCore.Mvc.ModelBinding; |
| 6 | +using Microsoft.AspNetCore.Mvc.Razor.Compilation; |
| 7 | +using Microsoft.Extensions.DependencyInjection; |
| 8 | +using Microsoft.Extensions.Logging; |
| 9 | +using Microsoft.Extensions.Logging.Abstractions; |
| 10 | + |
| 11 | +namespace Microsoft.AspNetCore.Mvc.Razor; |
| 12 | + |
| 13 | +public class RazorHotReloadTest |
| 14 | +{ |
| 15 | + [Fact] |
| 16 | + public void ClearCache_CanClearViewCompiler() |
| 17 | + { |
| 18 | + // Regression test for https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1425693 |
| 19 | + // Arrange |
| 20 | + var serviceProvider = GetServiceProvider(); |
| 21 | + |
| 22 | + var compilerProvider = Assert.IsType<DefaultViewCompilerProvider>(serviceProvider.GetRequiredService<IViewCompilerProvider>()); |
| 23 | + var hotReload = serviceProvider.GetRequiredService<RazorHotReload>(); |
| 24 | + |
| 25 | + // Act |
| 26 | + hotReload.ClearCache(Type.EmptyTypes); |
| 27 | + |
| 28 | + // Assert |
| 29 | + Assert.Null(compilerProvider.Compiler.CompiledViews); |
| 30 | + } |
| 31 | + |
| 32 | + [Fact] |
| 33 | + public void ClearCache_ResetsViewEngineLookupCache() |
| 34 | + { |
| 35 | + // Arrange |
| 36 | + var serviceProvider = GetServiceProvider(); |
| 37 | + |
| 38 | + var viewEngine = Assert.IsType<RazorViewEngine>(serviceProvider.GetRequiredService<IRazorViewEngine>()); |
| 39 | + var hotReload = serviceProvider.GetRequiredService<RazorHotReload>(); |
| 40 | + var lookup = viewEngine.ViewLookupCache; |
| 41 | + |
| 42 | + // Act |
| 43 | + hotReload.ClearCache(Type.EmptyTypes); |
| 44 | + |
| 45 | + // Assert |
| 46 | + Assert.NotSame(lookup, viewEngine.ViewLookupCache); |
| 47 | + } |
| 48 | + |
| 49 | + [Fact] |
| 50 | + public void ClearCache_ResetsRazorPageActivator() |
| 51 | + { |
| 52 | + // Arrange |
| 53 | + var serviceProvider = GetServiceProvider(); |
| 54 | + |
| 55 | + var pageActivator = Assert.IsType<RazorPageActivator>(serviceProvider.GetRequiredService<IRazorPageActivator>()); |
| 56 | + var hotReload = serviceProvider.GetRequiredService<RazorHotReload>(); |
| 57 | + var cache = pageActivator.ActivationInfo; |
| 58 | + cache[new RazorPageActivator.CacheKey()] = new RazorPagePropertyActivator( |
| 59 | + typeof(string), typeof(object), |
| 60 | + new EmptyModelMetadataProvider(), |
| 61 | + new RazorPagePropertyActivator.PropertyValueAccessors()); |
| 62 | + |
| 63 | + // Act |
| 64 | + Assert.Single(pageActivator.ActivationInfo); |
| 65 | + hotReload.ClearCache(Type.EmptyTypes); |
| 66 | + |
| 67 | + // Assert |
| 68 | + Assert.Empty(pageActivator.ActivationInfo); |
| 69 | + } |
| 70 | + |
| 71 | + private static ServiceProvider GetServiceProvider() |
| 72 | + { |
| 73 | + var diagnosticListener = new DiagnosticListener("Microsoft.AspNetCore"); |
| 74 | + |
| 75 | + var serviceProvider = new ServiceCollection() |
| 76 | + .AddControllersWithViews() |
| 77 | + .Services |
| 78 | + // Manually add RazorHotReload because it's only added if MetadataUpdateHandler.IsSupported = true |
| 79 | + .AddSingleton<RazorHotReload>() |
| 80 | + .AddSingleton<ILoggerFactory>(NullLoggerFactory.Instance) |
| 81 | + .AddSingleton<DiagnosticSource>(diagnosticListener) |
| 82 | + .AddSingleton(diagnosticListener) |
| 83 | + .BuildServiceProvider(); |
| 84 | + return serviceProvider; |
| 85 | + } |
| 86 | +} |
0 commit comments