Beispiel #1
0
        public void BasicVisitor_GeneratesCorrectLineMappings()
        {
            // Arrange
            var fileProvider = new TestFileProvider();
            var host = new MvcRazorHostWithNormalizedNewLine(new DefaultChunkTreeCache(fileProvider))
            {
                DesignTimeMode = true
            };

            host.NamespaceImports.Clear();
            var expectedLineMappings = new[]
            {
                BuildLineMapping(
                    documentAbsoluteIndex: 13,
                    documentLineIndex: 0,
                    documentCharacterIndex: 13,
                    generatedAbsoluteIndex: 1499,
                    generatedLineIndex: 34,
                    generatedCharacterIndex: 13,
                    contentLength: 4),
                BuildLineMapping(
                    documentAbsoluteIndex: 43,
                    documentLineIndex: 2,
                    documentCharacterIndex: 5,
                    generatedAbsoluteIndex: 1583,
                    generatedLineIndex: 39,
                    generatedCharacterIndex: 6,
                    contentLength: 21),
            };

            // Act and Assert
            RunDesignTimeTest(host, "Basic", expectedLineMappings);
        }
        public void AddRazorOptions_ConfiguresOptionsAsExpected()
        {
            // Arrange
            var services = new ServiceCollection().AddOptions();
            var fileProvider = new TestFileProvider();

            // Act
            var builder = new MvcBuilder(services, new ApplicationPartManager());
            builder.AddRazorOptions(options =>
            {
                options.FileProviders.Add(fileProvider);
            });
            var serviceProvider = services.BuildServiceProvider();

            // Assert
            var accessor = serviceProvider.GetRequiredService<IOptions<RazorViewEngineOptions>>();
            Assert.Same(fileProvider, accessor.Value.FileProviders[0]);
        }
        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);
        }
        public void EnumerateFiles_IteratesOverAllCshtmlUnderRoot()
        {
            // 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("/");

            // 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("/Level1-Dir1/File2.cshtml", file.FilePath);
                Assert.Equal("/", 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("/Level1-Dir1/File3.cshtml", file.FilePath);
                Assert.Equal("/", file.BasePath);
                Assert.Equal(Path.Combine("BasePath", "Level1-Dir1", "File3.cshtml"), file.PhysicalPath);
                Assert.Equal(Path.Combine("Level1-Dir1", "File3.cshtml"), file.RelativePhysicalPath);
            },
                              file =>
            {
                Assert.Equal("/Level1-Dir2/File5.cshtml", file.FilePath);
                Assert.Equal("/", file.BasePath);
                Assert.Equal(Path.Combine("BasePath", "Level1-Dir2", "File5.cshtml"), file.PhysicalPath);
                Assert.Equal(Path.Combine("Level1-Dir2", "File5.cshtml"), file.RelativePhysicalPath);
            });
        }
Beispiel #5
0
        public void AddFileVersionToPath_DoesNotLockFileAfterReading()
        {
            // Arrange
            var stream = new TestableMemoryStream(Encoding.UTF8.GetBytes("Hello World!"));
            var mockFile = new Mock<IFileInfo>();
            mockFile.SetupGet(f => f.Exists).Returns(true);
            mockFile
                .Setup(m => m.CreateReadStream())
                .Returns(stream);

            var fileProvider = new TestFileProvider();
            fileProvider.AddFile("/hello/world", mockFile.Object);

            var fileVersionProvider = new FileVersionProvider(
                fileProvider,
                new MemoryCache(new MemoryCacheOptions()),
                GetRequestPathBase());

            // Act
            var result = fileVersionProvider.AddFileVersionToPath("/hello/world");

            // Assert
            Assert.True(stream.Disposed);
        }
Beispiel #6
0
        public void AddFileVersionToPath_UpdatesEntryWhenCacheExpires_ForNonExistingFile()
        {
            // Arrange
            var fileProvider = new TestFileProvider();
            var fileVersionProvider = new FileVersionProvider(
                fileProvider,
                new MemoryCache(new MemoryCacheOptions()),
                GetRequestPathBase());

            // Act 1 - File does not exist
            var result = fileVersionProvider.AddFileVersionToPath("file.txt");

            // Assert 1
            Assert.Equal("file.txt", result);

            // Act 2 - File gets added
            fileProvider.AddFile("file.txt", "Hello World!");
            fileProvider.GetChangeToken("file.txt").HasChanged = true;
            result = fileVersionProvider.AddFileVersionToPath("file.txt");

            // Assert 2
            Assert.Equal("file.txt?v=f4OxZX_x_FO5LcGBSKHWXfwtSx-j1ncoSt3SABJtkGk", result);
        }
Beispiel #7
0
        public void MvcRazorHost_GeneratesTagHelperModelExpressionCode_DesignTime()
        {
            // Arrange
            var fileProvider = new TestFileProvider();
            var host         = new MvcRazorHostWithNormalizedNewLine(new DefaultChunkTreeCache(fileProvider))
            {
                DesignTimeMode = true
            };

            var expectedLineMappings = new[]
            {
                BuildLineMapping(
                    documentAbsoluteIndex: 33,
                    documentLineIndex: 2,
                    documentCharacterIndex: 14,
                    generatedAbsoluteIndex: 654,
                    generatedLineIndex: 17,
                    generatedCharacterIndex: 48,
                    contentLength: 91),
                BuildLineMapping(
                    documentAbsoluteIndex: 140,
                    documentLineIndex: 3,
                    documentCharacterIndex: 14,
                    generatedAbsoluteIndex: 797,
                    generatedLineIndex: 18,
                    generatedCharacterIndex: 48,
                    contentLength: 102),
                BuildLineMapping(
                    documentAbsoluteIndex: 7,
                    documentLineIndex: 0,
                    documentCharacterIndex: 7,
                    generatedAbsoluteIndex: 990,
                    generatedLineIndex: 20,
                    generatedCharacterIndex: 28,
                    contentLength: 8),
                BuildLineMapping(
                    documentAbsoluteIndex: 263,
                    documentLineIndex: 5,
                    documentCharacterIndex: 17,
                    generatedAbsoluteIndex: 2841,
                    generatedLineIndex: 52,
                    generatedCharacterIndex: 133,
                    contentLength: 3),
                BuildLineMapping(
                    documentAbsoluteIndex: 290,
                    documentLineIndex: 6,
                    documentCharacterIndex: 18,
                    generatedAbsoluteIndex: 3208,
                    generatedLineIndex: 58,
                    generatedCharacterIndex: 125,
                    contentLength: 5),
                BuildLineMapping(
                    documentAbsoluteIndex: 322,
                    documentLineIndex: 8,
                    documentCharacterIndex: 19,
                    generatedAbsoluteIndex: 3627,
                    generatedLineIndex: 64,
                    generatedCharacterIndex: 153,
                    contentLength: 5),
                BuildLineMapping(
                    documentAbsoluteIndex: 357,
                    documentLineIndex: 9,
                    documentCharacterIndex: 19,
                    generatedAbsoluteIndex: 4055,
                    generatedLineIndex: 70,
                    generatedCharacterIndex: 161,
                    contentLength: 4),
                BuildLineMapping(
                    documentAbsoluteIndex: 378,
                    documentLineIndex: 9,
                    documentCharacterIndex: 40,
                    generatedAbsoluteIndex: 4317,
                    generatedLineIndex: 75,
                    generatedCharacterIndex: 163,
                    contentLength: 6),
            };

            // Act and Assert
            RunDesignTimeTest(
                host,
                testName: "ModelExpressionTagHelper",
                expectedLineMappings: expectedLineMappings);
        }
Beispiel #8
0
        public void ModelVisitor_GeneratesLineMappingsForLastModel_WhenMultipleModelsArePresent()
        {
            // Arrange
            var fileProvider = new TestFileProvider();
            using (var host = new MvcRazorHostWithNormalizedNewLine(new DefaultChunkTreeCache(fileProvider))
            {
                DesignTimeMode = true
            })
            {
                host.NamespaceImports.Clear();
                var inputFile = "TestFiles/Input/MultipleModels.cshtml";
                var outputFile = "TestFiles/Output/DesignTime/MultipleModels.cs";
                var expectedCode = ResourceFile.ReadResource(_assembly, outputFile, sourceFile: false);

                // Act
                GeneratorResults results;
                using (var stream = ResourceFile.GetResourceStream(_assembly, inputFile, sourceFile: true))
                {
                    results = host.GenerateCode(inputFile, stream);
                }

                // Assert
                Assert.False(results.Success);
                var parserError = Assert.Single(results.ParserErrors);
                Assert.Equal("Only one 'model' statement is allowed in a file.", parserError.Message);
#if GENERATE_BASELINES
                ResourceFile.UpdateFile(_assembly, outputFile, expectedCode, results.GeneratedCode);
#else
                Assert.Equal(expectedCode, results.GeneratedCode, ignoreLineEndingDifferences: true);
#endif
            }
        }
Beispiel #9
0
        public void ModelVisitor_GeneratesCorrectLineMappings()
        {
            // Arrange
            var fileProvider = new TestFileProvider();
            using (var host = new MvcRazorHostWithNormalizedNewLine(new DefaultChunkTreeCache(fileProvider))
            {
                DesignTimeMode = true
            })
            {
                host.NamespaceImports.Clear();
                var expectedLineMappings = new[]
                {
                    BuildLineMapping(
                        documentAbsoluteIndex: 7,
                        documentLineIndex: 0,
                        documentCharacterIndex: 7,
                        generatedAbsoluteIndex: 393,
                        generatedLineIndex: 11,
                        generatedCharacterIndex: 28,
                        contentLength: 30),
                };

                // Act and Assert
                RunDesignTimeTest(host, "Model", expectedLineMappings);
            }
        }
Beispiel #10
0
        public void InjectVisitorWithSemicolon_GeneratesCorrectLineMappings()
        {
            // Arrange
            var fileProvider = new TestFileProvider();
            using (var host = new MvcRazorHostWithNormalizedNewLine(new DefaultChunkTreeCache(fileProvider))
            {
                DesignTimeMode = true
            })
            {
                host.NamespaceImports.Clear();
                var expectedLineMappings = new[]
                {
                    BuildLineMapping(
                        documentAbsoluteIndex: 7,
                        documentLineIndex: 0,
                        documentCharacterIndex: 7,
                        generatedAbsoluteIndex: 398,
                        generatedLineIndex: 11,
                        generatedCharacterIndex: 28,
                        contentLength: 7),
                    BuildLineMapping(
                        documentAbsoluteIndex: 24,
                        documentLineIndex: 1,
                        documentCharacterIndex: 8,
                        generatedAbsoluteIndex: 769,
                        generatedLineIndex: 25,
                        generatedCharacterIndex: 8,
                        contentLength: 20),
                    BuildLineMapping(
                        documentAbsoluteIndex: 58,
                        documentLineIndex: 2,
                        documentCharacterIndex: 8,
                        generatedAbsoluteIndex: 1003,
                        generatedLineIndex: 33,
                        generatedCharacterIndex: 8,
                        contentLength: 23),
                    BuildLineMapping(
                        documentAbsoluteIndex: 93,
                        documentLineIndex: 3,
                        documentCharacterIndex: 8,
                        generatedAbsoluteIndex: 1240,
                        generatedLineIndex: 41,
                        generatedCharacterIndex: 8,
                        contentLength: 21),
                    BuildLineMapping(
                        documentAbsoluteIndex: 129,
                        documentLineIndex: 4,
                        documentCharacterIndex: 8,
                        generatedAbsoluteIndex: 1475,
                        generatedLineIndex: 49,
                        generatedCharacterIndex: 8,
                        contentLength: 24),
                };

                // Act and Assert
                RunDesignTimeTest(host, "InjectWithSemicolon", expectedLineMappings);
            }
        }
Beispiel #11
0
        public void MvcRazorHost_GeneratesCorrectLineMappingsAndUsingStatementsForViewImports()
        {
            // Arrange
            var fileProvider = new TestFileProvider();
            using (var host = new MvcRazorHostWithNormalizedNewLine(new DefaultChunkTreeCache(fileProvider))
            {
                DesignTimeMode = true
            })
            {
                host.NamespaceImports.Clear();
                var expectedLineMappings = new[]
                {
                    BuildLineMapping(
                        documentAbsoluteIndex: 8,
                        documentLineIndex: 0,
                        documentCharacterIndex: 8,
                        generatedAbsoluteIndex: 659,
                        generatedLineIndex: 21,
                        generatedCharacterIndex: 8,
                        contentLength: 26),
                };

                // Act and Assert
                RunDesignTimeTest(host, "_ViewImports", expectedLineMappings);
            }
        }
Beispiel #12
0
 public void MvcRazorHost_ParsesAndGeneratesCodeForBasicScenarios(string scenarioName)
 {
     // Arrange
     var fileProvider = new TestFileProvider();
     using (var host = new TestMvcRazorHost(new DefaultChunkTreeCache(fileProvider)))
     {
         // Act and Assert
         RunRuntimeTest(host, scenarioName);
     }
 }
Beispiel #13
0
        public void MvcRazorHost_GeneratesTagHelperModelExpressionCode_DesignTime()
        {
            // Arrange
            var fileProvider = new TestFileProvider();
            using (var host = new MvcRazorHostWithNormalizedNewLine(new DefaultChunkTreeCache(fileProvider))
            {
                DesignTimeMode = true
            })
            {
                var expectedLineMappings = new[]
                {
                    BuildLineMapping(
                        documentAbsoluteIndex: 33,
                        documentLineIndex: 2,
                        documentCharacterIndex: 14,
                        generatedAbsoluteIndex: 597,
                        generatedLineIndex: 16,
                        generatedCharacterIndex: 48,
                        contentLength: 91),
                    BuildLineMapping(
                        documentAbsoluteIndex: 7,
                        documentLineIndex: 0,
                        documentCharacterIndex: 7,
                        generatedAbsoluteIndex: 779,
                        generatedLineIndex: 18,
                        generatedCharacterIndex: 28,
                        contentLength: 8),
                    BuildLineMapping(
                        documentAbsoluteIndex: 145,
                        documentLineIndex: 4,
                        documentCharacterIndex: 17,
                        generatedAbsoluteIndex: 2242,
                        generatedLineIndex: 47,
                        generatedCharacterIndex: 99,
                        contentLength: 3),
                    BuildLineMapping(
                        documentAbsoluteIndex: 172,
                        documentLineIndex: 5,
                        documentCharacterIndex: 18,
                        generatedAbsoluteIndex: 2575,
                        generatedLineIndex: 53,
                        generatedCharacterIndex: 91,
                        contentLength: 5),
                };

                // Act and Assert
                RunDesignTimeTest(
                    host,
                    testName: "ModelExpressionTagHelper",
                    expectedLineMappings: expectedLineMappings);
            }
        }
Beispiel #14
0
        public void MvcRazorHost_EnablesInstrumentationByDefault()
        {
            // Arrange
            var fileProvider = new TestFileProvider();
            using (var host = new MvcRazorHost(new DefaultChunkTreeCache(fileProvider), new TagHelperDescriptorResolver(designTime: false)))
            {
                // Act
                var instrumented = host.EnableInstrumentation;

                // Assert
                Assert.True(instrumented);
            }
        }