[InlineData(10000)] // expected failure: same time and length
        public void GetOrAdd_UsesFilesFromCache_IfTimestampDiffers_ButContentAndLengthAreTheSame(long fileTimeUTC)
        {
            // Arrange
            var instance   = new RuntimeCompileIdentical();
            var length     = Encoding.UTF8.GetByteCount(instance.Content);
            var collection = new ViewCollection();
            var fileSystem = new TestFileSystem();
            var cache      = new CompilerCache(new[] { new ViewCollection() }, fileSystem);

            var fileInfo = new TestFileInfo
            {
                Length       = length,
                LastModified = DateTime.FromFileTimeUtc(fileTimeUTC),
                Content      = instance.Content
            };

            var runtimeFileInfo = new RelativeFileInfo(fileInfo, "ab");

            var precompiledContent = new PreCompile().Content;
            var razorFileInfo      = new RazorFileInfo
            {
                FullTypeName = typeof(PreCompile).FullName,
                Hash         = RazorFileHash.GetHash(GetMemoryStream(precompiledContent)),
                LastModified = DateTime.FromFileTimeUtc(10000),
                Length       = Encoding.UTF8.GetByteCount(precompiledContent),
                RelativePath = "ab",
            };

            // Act
            var actual = cache.GetOrAdd(runtimeFileInfo,
                                        compile: _ => { throw new Exception("Shouldn't be called."); });

            // Assert
            Assert.Equal(typeof(PreCompile), actual.CompiledType);
        }
Exemple #2
0
 private async Task OnRequestPreCompile(ISite site)
 {
     if (PreCompile != null)
     {
         foreach (PreCompileDelegate del in PreCompile.GetInvocationList())
         {
             await del.Invoke(site);
         }
     }
 }
Exemple #3
0
        public void GetOrAdd_UsesValueFromCache_IfGlobalHasNotChanged()
        {
            // Arrange
            var instance     = new PreCompile();
            var length       = Encoding.UTF8.GetByteCount(instance.Content);
            var fileProvider = new TestFileProvider();

            var lastModified = DateTime.UtcNow;

            var fileInfo = new TestFileInfo
            {
                Length       = length,
                LastModified = lastModified,
                Content      = instance.Content
            };

            fileProvider.AddFile(ViewPath, fileInfo);

            var globalContent  = "global-content";
            var globalFileInfo = new TestFileInfo
            {
                Content      = globalContent,
                LastModified = DateTime.UtcNow
            };

            fileProvider.AddFile("_ViewImports.cshtml", globalFileInfo);
            var globalRazorFileInfo = new RazorFileInfo
            {
                Hash = Crc32.Calculate(GetMemoryStream(globalContent)).ToString(CultureInfo.InvariantCulture),
                HashAlgorithmVersion = 1,
                LastModified         = globalFileInfo.LastModified,
                Length       = globalFileInfo.Length,
                RelativePath = "_ViewImports.cshtml",
                FullTypeName = typeof(RuntimeCompileIdentical).FullName
            };
            var precompiledViews = new ViewCollection();

            precompiledViews.Add(globalRazorFileInfo);
            var cache = new CompilerCache(new[] { precompiledViews }, TestLoadContext, fileProvider);

            // Act
            var result = cache.GetOrAdd(ViewPath,
                                        compile: _ => { throw new Exception("shouldn't be invoked"); });

            // Assert
            Assert.NotSame(CompilerCacheResult.FileNotFound, result);
            var actual = result.CompilationResult;

            Assert.NotNull(actual);
            Assert.Equal(typeof(PreCompile), actual.CompiledType);
        }
            public ViewCollection()
            {
                FileInfos = _fileInfos;

                var content = new PreCompile().Content;
                var length  = Encoding.UTF8.GetByteCount(content);

                Add(new RazorFileInfo()
                {
                    FullTypeName = typeof(PreCompile).FullName,
                    Hash         = RazorFileHash.GetHash(GetMemoryStream(content)),
                    LastModified = DateTime.FromFileTimeUtc(10000),
                    Length       = length,
                    RelativePath = "ab",
                });
            }
Exemple #5
0
            public ViewCollection()
            {
                FileInfos = _fileInfos;

                var content = new PreCompile().Content;
                var length  = Encoding.UTF8.GetByteCount(content);

                Add(new RazorFileInfo()
                {
                    FullTypeName         = typeof(PreCompile).FullName,
                    Hash                 = Crc32.Calculate(GetMemoryStream(content)).ToString(CultureInfo.InvariantCulture),
                    HashAlgorithmVersion = 1,
                    LastModified         = DateTime.FromFileTimeUtc(10000),
                    Length               = length,
                    RelativePath         = ViewPath,
                });
            }
 public virtual void BuildWithoutTests(NantProject nantProject, TestIdentifier testIdentifier, string workingDirectory, bool useMSBuild, PreCompile preCompile)
 {
     VisualStudioSolution testLessSolution = solution.TestLessCopy();
     testLessSolution.ForEach(delegate(VisualStudioProject project)
                       {
                           if (testIdentifier.IsTestProject(project))
                           {
                               return;
                           }
                           project.RemoveTests(testIdentifier);
                       });
     testLessSolution.ForEach(obj => preCompile(obj));
     if (useMSBuild)
         testLessSolution.MSBuild(nantProject, true, workingDirectory);
     else
         testLessSolution.Compile(nantProject);
 }
        public void GetOrAdd_RecompilesFile_IfContentAndLengthAreChanged(
            Type resultViewType,
            long fileTimeUTC)
        {
            // Arrange
            var instance   = (View)Activator.CreateInstance(resultViewType);
            var length     = Encoding.UTF8.GetByteCount(instance.Content);
            var collection = new ViewCollection();
            var fileSystem = new TestFileSystem();
            var cache      = new CompilerCache(new[] { new ViewCollection() }, fileSystem);

            var fileInfo = new TestFileInfo
            {
                Length       = length,
                LastModified = DateTime.FromFileTimeUtc(fileTimeUTC),
                Content      = instance.Content
            };

            var runtimeFileInfo = new RelativeFileInfo(fileInfo, "ab");

            var precompiledContent = new PreCompile().Content;
            var razorFileInfo      = new RazorFileInfo
            {
                FullTypeName = typeof(PreCompile).FullName,
                Hash         = RazorFileHash.GetHash(GetMemoryStream(precompiledContent)),
                LastModified = DateTime.FromFileTimeUtc(10000),
                Length       = Encoding.UTF8.GetByteCount(precompiledContent),
                RelativePath = "ab",
            };

            // Act
            var actual = cache.GetOrAdd(runtimeFileInfo,
                                        compile: _ => CompilationResult.Successful(resultViewType));

            // Assert
            Assert.Equal(resultViewType, actual.CompiledType);
        }
 public virtual void BuildWithoutTests(NantProject nantProject, TestIdentifier testIdentifier, PreCompile preCompile)
 {
     BuildWithoutTests(nantProject, testIdentifier, ".", true, preCompile);
 }
 /// <summary>
 /// Would use StandardTestIdentifier
 /// </summary>
 public virtual void BuildWithoutTests(NantProject nantProject, PreCompile preCompile)
 {
     BuildWithoutTests(nantProject, new StandardTestIdentifier(), ".", true, preCompile);
 }