public void GetCompilationFailedResult_ReadsContentFromSourceDocuments() { // Arrange var viewPath = "/Views/Home/Index.cshtml"; var fileContent = @" @if (User.IsAdmin) { <span> } </span>"; var razorEngine = RazorEngine.Create(); var fileProvider = new TestFileProvider(); fileProvider.AddFile(viewPath, fileContent); var accessor = Mock.Of <IRazorViewEngineFileProviderAccessor>(a => a.FileProvider == fileProvider); var fileSystem = new FileProviderRazorProjectFileSystem(accessor, _hostingEnvironment); var templateEngine = new MvcRazorTemplateEngine(razorEngine, fileSystem); var codeDocument = templateEngine.CreateCodeDocument(viewPath); // Act var csharpDocument = templateEngine.GenerateCode(codeDocument); var compilationResult = CompilationFailedExceptionFactory.Create(codeDocument, csharpDocument.Diagnostics); // Assert var failure = Assert.Single(compilationResult.CompilationFailures); Assert.Equal(fileContent, failure.SourceFileContent); }
public void GetCompilationFailedResult_ReadsRazorErrorsFromPage() { // Arrange var viewPath = "/Views/Home/Index.cshtml"; var razorEngine = RazorEngine.Create(); var fileProvider = new TestFileProvider(); fileProvider.AddFile(viewPath, "<span name=\"@(User.Id\">"); var accessor = Mock.Of <IRazorViewEngineFileProviderAccessor>(a => a.FileProvider == fileProvider); var fileSystem = new FileProviderRazorProjectFileSystem(accessor, _hostingEnvironment); var templateEngine = new MvcRazorTemplateEngine(razorEngine, fileSystem); var codeDocument = templateEngine.CreateCodeDocument(viewPath); // Act var csharpDocument = templateEngine.GenerateCode(codeDocument); var compilationResult = CompilationFailedExceptionFactory.Create(codeDocument, csharpDocument.Diagnostics); // Assert var failure = Assert.Single(compilationResult.CompilationFailures); Assert.Equal(Path.Combine("Views", "Home", "Index.cshtml"), failure.SourceFilePath); Assert.Collection(failure.Messages, message => Assert.StartsWith( @"Unterminated string literal.", message.Message), message => Assert.StartsWith( @"The explicit expression block is missing a closing "")"" character.", message.Message)); }
public void GetCompilationFailedResult_UsesPhysicalPath() { // Arrange var viewPath = "/Views/Home/Index.cshtml"; var physicalPath = @"x:\myapp\views\home\index.cshtml"; var fileProvider = new TestFileProvider(); var file = fileProvider.AddFile(viewPath, "<span name=\"@(User.Id\">"); file.PhysicalPath = physicalPath; var accessor = Mock.Of <IRazorViewEngineFileProviderAccessor>(a => a.FileProvider == fileProvider); var razorEngine = RazorEngine.Create(); var fileSystem = new FileProviderRazorProjectFileSystem(accessor, _hostingEnvironment); var templateEngine = new MvcRazorTemplateEngine(razorEngine, fileSystem); var codeDocument = templateEngine.CreateCodeDocument(viewPath); // Act var csharpDocument = templateEngine.GenerateCode(codeDocument); var compilationResult = CompilationFailedExceptionFactory.Create(codeDocument, csharpDocument.Diagnostics); // Assert var failure = Assert.Single(compilationResult.CompilationFailures); Assert.Equal(physicalPath, failure.SourceFilePath); }
public void EnumerateFiles_ReturnsCshtmlFiles() { // Arrange var fileProvider = new TestFileProvider("BasePath"); var file1 = fileProvider.AddFile("/File1.cshtml", "content"); var file2 = fileProvider.AddFile("/File2.js", "content"); var file3 = fileProvider.AddFile("/File3.cshtml", "content"); fileProvider.AddDirectoryContent("/", new IFileInfo[] { file1, file2, file3 }); var accessor = Mock.Of <IRazorViewEngineFileProviderAccessor>(a => a.FileProvider == fileProvider); var fileSystem = new FileProviderRazorProjectFileSystem(accessor, Mock.Of <IHostingEnvironment>(e => e.ContentRootPath == "BasePath")); // Act var razorFiles = fileSystem.EnumerateItems("/"); // Assert Assert.Collection( razorFiles.OrderBy(f => f.FilePath), file => { Assert.Equal("/File1.cshtml", file.FilePath); Assert.Equal("/", file.BasePath); Assert.Equal(Path.Combine("BasePath", "File1.cshtml"), file.PhysicalPath); Assert.Equal("File1.cshtml", file.RelativePhysicalPath); }, file => { Assert.Equal("/File3.cshtml", file.FilePath); Assert.Equal("/", file.BasePath); Assert.Equal(Path.Combine("BasePath", "File3.cshtml"), file.PhysicalPath); Assert.Equal("File3.cshtml", file.RelativePhysicalPath); }); }
public ChecksumValidatorTest() { FileProvider = new TestFileProvider(); FileSystem = new FileProviderRazorProjectFileSystem( Mock.Of <IRazorViewEngineFileProviderAccessor>(a => a.FileProvider == FileProvider), Mock.Of <IHostingEnvironment>(e => e.ContentRootPath == "BasePath")); }
public void GetCompilationFailedResult_ReadsContentFromImports() { // Arrange var viewPath = "/Views/Home/Index.cshtml"; var importsFilePath = @"x:\views\_MyImports.cshtml"; var fileContent = "@ "; var importsContent = "@(abc"; var fileProvider = new TestFileProvider(); fileProvider.AddFile(viewPath, fileContent); var importsFile = fileProvider.AddFile("/Views/_MyImports.cshtml", importsContent); importsFile.PhysicalPath = importsFilePath; var accessor = Mock.Of <IRazorViewEngineFileProviderAccessor>(a => a.FileProvider == fileProvider); var razorEngine = RazorEngine.Create(); var fileSystem = new FileProviderRazorProjectFileSystem(accessor, _hostingEnvironment); var templateEngine = new MvcRazorTemplateEngine(razorEngine, fileSystem) { Options = { ImportsFileName = "_MyImports.cshtml", } }; var codeDocument = templateEngine.CreateCodeDocument(viewPath); // Act var csharpDocument = templateEngine.GenerateCode(codeDocument); var compilationResult = CompilationFailedExceptionFactory.Create(codeDocument, csharpDocument.Diagnostics); // Assert Assert.Collection( compilationResult.CompilationFailures, failure => { Assert.Equal(Path.Combine("Views", "Home", "Index.cshtml"), failure.SourceFilePath); Assert.Collection(failure.Messages, message => { Assert.Equal(@"A space or line break was encountered after the ""@"" character. Only valid identifiers, keywords, comments, ""("" and ""{"" are valid at the start of a code block and they must occur immediately following ""@"" with no space in between.", message.Message); }); }, failure => { Assert.Equal(importsFilePath, failure.SourceFilePath); Assert.Collection(failure.Messages, message => { Assert.Equal(@"The explicit expression block is missing a closing "")"" character. Make sure you have a matching "")"" character for all the ""("" characters within this block, and that none of the "")"" characters are being interpreted as markup.", message.Message); }); }); }
public void EnumerateFiles_IteratesOverAllCshtmlUnderPath() { // Arrange var fileProvider = new TestFileProvider("BasePath"); var directory1 = new TestDirectoryFileInfo { Name = "Level1-Dir1", }; var file1 = fileProvider.AddFile("/File1.cshtml", "content"); var directory2 = new TestDirectoryFileInfo { Name = "Level1-Dir2", }; fileProvider.AddDirectoryContent("/", new IFileInfo[] { directory1, file1, directory2 }); var file2 = fileProvider.AddFile("/Level1-Dir1/File2.cshtml", "content"); var file3 = fileProvider.AddFile("/Level1-Dir1/File3.cshtml", "content"); var file4 = fileProvider.AddFile("/Level1-Dir1/File4.txt", "content"); var directory3 = new TestDirectoryFileInfo { Name = "Level2-Dir1" }; fileProvider.AddDirectoryContent("/Level1-Dir1", new IFileInfo[] { file2, directory3, file3, file4 }); var file5 = fileProvider.AddFile(Path.Combine("Level1-Dir2", "File5.cshtml"), "content"); fileProvider.AddDirectoryContent("/Level1-Dir2", new IFileInfo[] { file5 }); fileProvider.AddDirectoryContent("/Level1/Level2", new IFileInfo[0]); var accessor = Mock.Of <IRazorViewEngineFileProviderAccessor>(a => a.FileProvider == fileProvider); var fileSystem = new FileProviderRazorProjectFileSystem(accessor, Mock.Of <IHostingEnvironment>(e => e.ContentRootPath == "BasePath")); // Act var razorFiles = fileSystem.EnumerateItems("/Level1-Dir1"); // Assert Assert.Collection(razorFiles.OrderBy(f => f.FilePath), file => { Assert.Equal("/File2.cshtml", file.FilePath); Assert.Equal("/Level1-Dir1", file.BasePath); Assert.Equal(Path.Combine("BasePath", "Level1-Dir1", "File2.cshtml"), file.PhysicalPath); Assert.Equal(Path.Combine("Level1-Dir1", "File2.cshtml"), file.RelativePhysicalPath); }, file => { Assert.Equal("/File3.cshtml", file.FilePath); Assert.Equal("/Level1-Dir1", file.BasePath); Assert.Equal(Path.Combine("BasePath", "Level1-Dir1", "File3.cshtml"), file.PhysicalPath); Assert.Equal(Path.Combine("Level1-Dir1", "File3.cshtml"), file.RelativePhysicalPath); }); }
public void GetItem_ReturnsNotFoundResult() { // Arrange var fileProvider = new TestFileProvider("BasePath"); var file = fileProvider.AddFile("/SomeFile.cshtml", "content"); fileProvider.AddDirectoryContent("/", new IFileInfo[] { file }); var accessor = Mock.Of <IRazorViewEngineFileProviderAccessor>(a => a.FileProvider == fileProvider); var fileSystem = new FileProviderRazorProjectFileSystem(accessor, Mock.Of <IHostingEnvironment>(e => e.ContentRootPath == "BasePath")); // Act var item = fileSystem.GetItem("/NotFound.cshtml"); // Assert Assert.False(item.Exists); }
public void EnumerateFiles_ReturnsEmptySequenceIfNoCshtmlFilesArePresent() { // Arrange var fileProvider = new TestFileProvider("BasePath"); var file1 = fileProvider.AddFile("/File1.txt", "content"); var file2 = fileProvider.AddFile("/File2.js", "content"); fileProvider.AddDirectoryContent("/", new IFileInfo[] { file1, file2 }); var accessor = Mock.Of <IRazorViewEngineFileProviderAccessor>(a => a.FileProvider == fileProvider); var fileSystem = new FileProviderRazorProjectFileSystem(accessor, Mock.Of <IHostingEnvironment>(e => e.ContentRootPath == "BasePath")); // Act var razorFiles = fileSystem.EnumerateItems("/"); // Assert Assert.Empty(razorFiles); }
private static TestRazorViewCompiler GetViewCompiler( TestFileProvider fileProvider = null, Action <RoslynCompilationContext> compilationCallback = null, #pragma warning disable CS0618 // Type or member is obsolete RazorReferenceManager referenceManager = null, #pragma warning restore CS0618 // Type or member is obsolete IList <CompiledViewDescriptor> precompiledViews = null, CSharpCompiler csharpCompiler = null) { fileProvider = fileProvider ?? new TestFileProvider(); var accessor = Mock.Of <IRazorViewEngineFileProviderAccessor>(a => a.FileProvider == fileProvider); compilationCallback = compilationCallback ?? (_ => { }); var options = Options.Create(new RazorViewEngineOptions()); if (referenceManager == null) { referenceManager = CreateReferenceManager(options); } precompiledViews = precompiledViews ?? Array.Empty <CompiledViewDescriptor>(); var hostingEnvironment = Mock.Of <IHostingEnvironment>(e => e.ContentRootPath == "BasePath"); var fileSystem = new FileProviderRazorProjectFileSystem(accessor, hostingEnvironment); var projectEngine = RazorProjectEngine.Create(RazorConfiguration.Default, fileSystem, builder => { RazorExtensions.Register(builder); }); csharpCompiler = csharpCompiler ?? new CSharpCompiler(referenceManager, hostingEnvironment); var viewCompiler = new TestRazorViewCompiler( fileProvider, projectEngine, csharpCompiler, compilationCallback, precompiledViews); return(viewCompiler); }
private static TestRazorViewCompiler GetViewCompiler( TestFileProvider fileProvider = null, Action <RoslynCompilationContext> compilationCallback = null, RazorReferenceManager referenceManager = null, IList <CompiledViewDescriptor> precompiledViews = null) { fileProvider = fileProvider ?? new TestFileProvider(); var accessor = Mock.Of <IRazorViewEngineFileProviderAccessor>(a => a.FileProvider == fileProvider); compilationCallback = compilationCallback ?? (_ => { }); var options = Options.Create(new RazorViewEngineOptions()); if (referenceManager == null) { var applicationPartManager = new ApplicationPartManager(); var assembly = typeof(RazorViewCompilerTest).Assembly; applicationPartManager.ApplicationParts.Add(new AssemblyPart(assembly)); applicationPartManager.FeatureProviders.Add(new MetadataReferenceFeatureProvider()); referenceManager = new DefaultRazorReferenceManager(applicationPartManager, options); } precompiledViews = precompiledViews ?? Array.Empty <CompiledViewDescriptor>(); var hostingEnvironment = Mock.Of <IHostingEnvironment>(e => e.ContentRootPath == "BasePath"); var fileSystem = new FileProviderRazorProjectFileSystem(accessor, hostingEnvironment); var projectEngine = RazorProjectEngine.Create(RazorConfiguration.Default, fileSystem, builder => { RazorExtensions.Register(builder); }); var viewCompiler = new TestRazorViewCompiler( fileProvider, projectEngine, new CSharpCompiler(referenceManager, hostingEnvironment), compilationCallback, precompiledViews); return(viewCompiler); }
public void GetItem_PhysicalPathDoesNotStartWithContentRoot_ReturnsNull() { var fileProvider = new TestFileProvider("BasePath2"); var file1 = fileProvider.AddFile("/File1.cshtml", "content"); var file2 = fileProvider.AddFile("/File2.js", "content"); var file3 = fileProvider.AddFile("/File3.cshtml", "content"); fileProvider.AddDirectoryContent("/", new IFileInfo[] { file1, file2, file3 }); var accessor = Mock.Of <IRazorViewEngineFileProviderAccessor>(a => a.FileProvider == fileProvider); var fileSystem = new FileProviderRazorProjectFileSystem(accessor, Mock.Of <IHostingEnvironment>(e => e.ContentRootPath == "BasePath")); // Act var item = fileSystem.GetItem("/File3.cshtml"); // Assert Assert.True(item.Exists); Assert.Equal("/File3.cshtml", item.FilePath); Assert.Equal(string.Empty, item.BasePath); Assert.Equal(Path.Combine("BasePath2", "File3.cshtml"), item.PhysicalPath); Assert.Null(item.RelativePhysicalPath); }