public void CompileCalculatesRootRelativePath(string appPath, string viewPath) { // Arrange var host = new Mock <IMvcRazorHost>(); host.Setup(h => h.GenerateCode(@"views\index\home.cshtml", It.IsAny <Stream>())) .Returns(GetGeneratorResult()) .Verifiable(); var fileInfo = new Mock <IFileInfo>(); fileInfo.Setup(f => f.PhysicalPath).Returns(viewPath); fileInfo.Setup(f => f.CreateReadStream()).Returns(Stream.Null); var compiler = new Mock <ICompilationService>(); compiler.Setup(c => c.Compile(fileInfo.Object, It.IsAny <string>())) .Returns(CompilationResult.Successful(typeof(RazorCompilationServiceTest))); var razorService = new RazorCompilationService(compiler.Object, host.Object); var relativeFileInfo = new RelativeFileInfo() { FileInfo = fileInfo.Object, RelativePath = @"views\index\home.cshtml", }; // Act razorService.Compile(relativeFileInfo, isInstrumented: false); // Assert host.Verify(); }
public void CompileCoreCalculatesRootRelativePath(string appPath, string viewPath) { // Arrange var env = new Mock <IApplicationEnvironment>(); env.SetupGet(e => e.ApplicationName).Returns("MyTestApplication"); env.SetupGet(e => e.ApplicationBasePath).Returns(appPath); var host = new Mock <IMvcRazorHost>(); host.Setup(h => h.GenerateCode(@"views\index\home.cshtml", It.IsAny <Stream>())) .Returns(new GeneratorResults(new Block(new BlockBuilder { Type = BlockType.Comment }), new RazorError[0], new CodeBuilderResult("", new LineMapping[0]))) .Verifiable(); var compiler = new Mock <ICompilationService>(); compiler.Setup(c => c.Compile(It.IsAny <string>())) .Returns(CompilationResult.Successful("", typeof(RazorCompilationServiceTest))); var razorService = new RazorCompilationService(env.Object, compiler.Object, host.Object); var fileInfo = new Mock <IFileInfo>(); fileInfo.Setup(f => f.PhysicalPath).Returns(viewPath); fileInfo.Setup(f => f.CreateReadStream()).Returns(Stream.Null); // Act razorService.CompileCore(fileInfo.Object); // Assert host.Verify(); }
public void CompileSetsEnableInstrumentationOnHost(bool enableInstrumentation) { // Arrange var host = new Mock <IMvcRazorHost>(); host.SetupAllProperties(); host.Setup(h => h.GenerateCode(It.IsAny <string>(), It.IsAny <Stream>())) .Returns(GetGeneratorResult()); var compiler = new Mock <ICompilationService>(); compiler.Setup(c => c.Compile(It.IsAny <IFileInfo>(), It.IsAny <string>())) .Returns(CompilationResult.Successful(GetType())); var razorService = new RazorCompilationService(compiler.Object, host.Object); var relativeFileInfo = new RelativeFileInfo() { FileInfo = Mock.Of <IFileInfo>(), RelativePath = @"views\index\home.cshtml", }; // Act razorService.Compile(relativeFileInfo, isInstrumented: enableInstrumentation); // Assert Assert.Equal(enableInstrumentation, host.Object.EnableInstrumentation); }
public async Task ReturnsResultOfCallingExecutorIfAllPhasesSucceed() { // Arrange TestableEdgeApplication app = CreateEdgeApp(null); IFileInfo testFile = app.TestFileSystem.AddTestFile("Bar.cshtml", "Flarg"); Type compiled = typeof(RazorApplicationTests); var page = new Mock <IRazorPage>(); var resp = new RazorResponse(TestData.CreateCallParams(path: "/Bar")) { StatusCode = 418, ReasonPhrase = "I'm a teapot" }; app.MockCompilationManager .Setup(c => c.Compile(testFile, It.IsAny <ITrace>())) .Returns(Task.FromResult(CompilationResult.Successful(null, compiled, Enumerable.Empty <CompilationMessage>()))); app.MockActivator .Setup(a => a.ActivatePage(compiled, It.IsAny <ITrace>())) .Returns(ActivationResult.Successful(page.Object)); app.MockExecutor .Setup(e => e.Execute(page.Object, It.IsAny <IDictionary <string, object> >(), It.IsAny <ITrace>())) .Returns(Task.FromResult <object>(null)); // Act await app.Invoke(TestData.CreateCallParams(path: "/Bar")); // Assert Assert.Equal(418, resp.StatusCode); Assert.Equal("I'm a teapot", resp.ReasonPhrase); }
public async Task ThrowsActivationExceptionIfActivationFails() { // Arrange TestableEdgeApplication app = CreateEdgeApp(null); IFileInfo testFile = app.TestFileSystem.AddTestFile("Bar.cshtml", "Flarg"); Type compiled = typeof(RazorApplicationTests); app.MockCompilationManager .Setup(c => c.Compile(testFile, It.IsAny <ITrace>())) .Returns(Task.FromResult(CompilationResult.Successful(null, compiled, Enumerable.Empty <CompilationMessage>()))); app.MockActivator .Setup(a => a.ActivatePage(compiled, It.IsAny <ITrace>())) .Returns(ActivationResult.Failed()); // Act ActivationFailedException ex = await AssertEx.Throws <ActivationFailedException>(async() => await app.Invoke(TestData.CreateCallParams(path: "/Bar"))); // Assert Assert.Equal( String.Format(Resources.ActivationFailedException_DefaultMessage, compiled.AssemblyQualifiedName), ex.Message); Assert.Equal( compiled, ex.AttemptedToActivate); }
public async Task ReturnsAndCachesCompiledResultIfCacheMisses() { // Arrange var compiled = new Mock <Type>(); TestableDefaultCompilationManager cm = CreateManager(); TestFile file = TestData.CreateDummyFile(); var compiler = new Mock <ICompiler>(); cm.Compilers.Add(compiler.Object); cm.MockContentIdentifier .Setup(i => i.GenerateContentId(file)).Returns("Foo"); compiler.Setup(c => c.CanCompile(file)).Returns(true); compiler.Setup(c => c.Compile(file)).Returns(Task.FromResult(CompilationResult.Successful(It.IsAny <string>(), compiled.Object, new[] { new CompilationMessage(MessageLevel.Info, "Foo") }))); // Act CompilationResult result = await cm.Compile(file, NullTrace.Instance); // Assert Assert.True(result.Success); Assert.False(result.SatisfiedFromCache); Assert.Equal("Foo", result.Messages.Single().Message); Assert.Same(compiled.Object, result.GetCompiledType()); Type cached; Assert.True(cm.Cache["Foo"].TryGetTarget(out cached)); Assert.Same(compiled.Object, cached); }
public void FileWithTheSameLengthAndDifferentTime_DoesNot_OverridesPrecompilation( Type resultViewType, long fileTimeUTC, bool swapsPreCompile) { // Arrange var instance = (View)Activator.CreateInstance(resultViewType); var length = Encoding.UTF8.GetByteCount(instance.Content); var collection = new ViewCollection(); var cache = new CompilerCache(new[] { new ViewCollection() }); var fileInfo = new Mock <IFileInfo>(); fileInfo .SetupGet(i => i.Length) .Returns(length); fileInfo .SetupGet(i => i.LastModified) .Returns(DateTime.FromFileTimeUtc(fileTimeUTC)); fileInfo.Setup(i => i.CreateReadStream()) .Returns(GetMemoryStream(instance.Content)); var preCompileType = typeof(PreCompile); var runtimeFileInfo = new RelativeFileInfo() { FileInfo = fileInfo.Object, RelativePath = "ab", }; // Act var actual = cache.GetOrAdd(runtimeFileInfo, enableInstrumentation: false, compile: () => CompilationResult.Successful(resultViewType)); // Assert if (swapsPreCompile) { Assert.Equal(actual.CompiledType, resultViewType); } else { Assert.Equal(actual.CompiledType, typeof(PreCompile)); } }
public void Compile_ReturnsResultFromCompilationServiceIfParseSucceeds() { // Arrange var code = "compiled-content"; var generatorResult = new GeneratorResults( new Block(new BlockBuilder { Type = BlockType.Comment }), Enumerable.Empty <TagHelperDescriptor>(), new ParserErrorSink(), new CodeBuilderResult(code, new LineMapping[0]), new CodeTree()); var host = new Mock <IMvcRazorHost>(); host.Setup(h => h.GenerateCode(It.IsAny <string>(), It.IsAny <Stream>())) .Returns(generatorResult); var fileInfo = new Mock <IFileInfo>(); fileInfo.Setup(f => f.CreateReadStream()) .Returns(Stream.Null); var compilationResult = CompilationResult.Successful(typeof(object)); var compiler = new Mock <ICompilationService>(); compiler.Setup(c => c.Compile(fileInfo.Object, code)) .Returns(compilationResult) .Verifiable(); var relativeFileInfo = new RelativeFileInfo(fileInfo.Object, @"Views\index\home.cshtml"); var razorService = new RazorCompilationService(compiler.Object, host.Object); // Act var result = razorService.Compile(relativeFileInfo); // Assert Assert.Same(compilationResult, result); compiler.Verify(); }
public void ReturnsAndCachesCompiledResultIfCachedValueHasBeenCollected() { // Arrange var compiled = new Mock <Type>(); TestableDefaultCompilationManager cm = CreateManager(); TestFile file = TestData.CreateDummyFile(); var compiler = new Mock <ICompiler>(); cm.Compilers.Add(compiler.Object); cm.MockContentIdentifier .Setup(i => i.GenerateContentId(file)).Returns("Foo"); compiler.Setup(c => c.CanCompile(file)).Returns(true); compiler.Setup(c => c.Compile(file)).Returns(Task.FromResult(CompilationResult.Successful(It.IsAny <string>(), compiled.Object, new[] { new CompilationMessage(MessageLevel.Info, "Foo") }))); // Add a cache entry, but collect it. cm.Cache["Foo"] = new WeakReference <Type>(new FakeType()); GC.Collect(); // Act // Using ".Result" because making this Async causes a reference to the cached type to be held. CompilationResult result = cm.Compile(file, NullTrace.Instance).Result; // Assert Assert.True(result.Success); Assert.False(result.SatisfiedFromCache); Assert.Equal("Foo", result.Messages.Single().Message); Assert.Same(compiled.Object, result.GetCompiledType()); Type cached; Assert.True(cm.Cache["Foo"].TryGetTarget(out cached)); Assert.Same(compiled.Object, cached); }