コード例 #1
0
        public void GetOrAdd_ReturnsFileNotFoundIfFileWasDeleted()
        {
            // Arrange
            var fileProvider = new TestFileProvider();

            fileProvider.AddFile(ViewPath, "some content");
            var cache    = new CompilerCache(Enumerable.Empty <RazorFileInfoCollection>(), TestLoadContext, fileProvider);
            var type     = typeof(RuntimeCompileIdentical);
            var expected = UncachedCompilationResult.Successful(type, "hello world");

            // Act 1
            var result1 = cache.GetOrAdd(ViewPath, _ => expected);

            // Assert 1
            Assert.NotSame(CompilerCacheResult.FileNotFound, result1);
            Assert.Same(expected, result1.CompilationResult);

            // Act 2
            // Delete the file from the file system and set it's expiration trigger.
            fileProvider.DeleteFile(ViewPath);
            fileProvider.GetTrigger(ViewPath).IsExpired = true;
            var result2 = cache.GetOrAdd(ViewPath, _ => { throw new Exception("shouldn't be called."); });

            // Assert 2
            Assert.Same(CompilerCacheResult.FileNotFound, result2);
            Assert.Null(result2.CompilationResult);
        }
コード例 #2
0
ファイル: CompilerCacheTest.cs プロジェクト: rkakadia/Mvc-1
        public void GetOrAdd_IgnoresCachedValueIfFileIsIdentical_ButViewStartWasDeletedSinceCacheWasCreated()
        {
            // Arrange
            var expectedType = typeof(RuntimeCompileDifferent);
            var lastModified = DateTime.UtcNow;
            var fileProvider = new TestFileProvider();

            var viewCollection  = new ViewCollection();
            var precompiledView = viewCollection.FileInfos[0];

            precompiledView.RelativePath = "Views\\Index.cshtml";
            var viewFileInfo = new TestFileInfo
            {
                Content      = new PreCompile().Content,
                LastModified = precompiledView.LastModified,
                PhysicalPath = precompiledView.RelativePath
            };

            fileProvider.AddFile(viewFileInfo.PhysicalPath, viewFileInfo);

            var viewStartFileInfo = new TestFileInfo
            {
                PhysicalPath = "Views\\_ViewStart.cshtml",
                Content      = "viewstart-content",
                LastModified = lastModified
            };
            var viewStart = new RazorFileInfo
            {
                FullTypeName = typeof(RuntimeCompileIdentical).FullName,
                RelativePath = viewStartFileInfo.PhysicalPath,
                LastModified = viewStartFileInfo.LastModified,
                Hash         = RazorFileHash.GetHash(viewStartFileInfo),
                Length       = viewStartFileInfo.Length
            };

            fileProvider.AddFile(viewStartFileInfo.PhysicalPath, viewStartFileInfo);

            viewCollection.Add(viewStart);
            var cache    = new CompilerCache(new[] { viewCollection }, fileProvider);
            var fileInfo = new RelativeFileInfo(viewFileInfo, viewFileInfo.PhysicalPath);

            // Act 1
            var actual1 = cache.GetOrAdd(fileInfo,
                                         compile: _ => { throw new Exception("should not be called"); });

            // Assert 1
            Assert.Equal(typeof(PreCompile), actual1.CompiledType);

            // Act 2
            fileProvider.DeleteFile(viewStartFileInfo.PhysicalPath);
            var actual2 = cache.GetOrAdd(fileInfo,
                                         compile: _ => CompilationResult.Successful(expectedType));

            // Assert 2
            Assert.Equal(expectedType, actual2.CompiledType);
        }
コード例 #3
0
        public void AddFileVersionToPath_UpdatesEntryWhenCacheExpires_ForExistingFile_WithRequestPathBase()
        {
            // Arrange
            var fileProvider = new TestFileProvider();
            var fileVersionProvider = new FileVersionProvider(
                fileProvider,
                new MemoryCache(new MemoryCacheOptions()),
                GetRequestPathBase("/wwwroot/"));
            fileProvider.AddFile("file.txt", "Hello World!");

            // Act 1 - File exists
            var result = fileVersionProvider.AddFileVersionToPath("/wwwroot/file.txt");

            // Assert 1
            Assert.Equal("/wwwroot/file.txt?v=f4OxZX_x_FO5LcGBSKHWXfwtSx-j1ncoSt3SABJtkGk", result);

            // Act 2
            fileProvider.DeleteFile("file.txt");
            fileProvider.GetChangeToken("file.txt").HasChanged = true;
            result = fileVersionProvider.AddFileVersionToPath("/wwwroot/file.txt");

            // Assert 2
            Assert.Equal("/wwwroot/file.txt", result);
        }