示例#1
0
        public void GetOrAdd_IgnoresCache_IfCachedItemIsNotInstrumentedAndEnableInstrumentationIsTrue()
        {
            // Arrange
            var lastModified = DateTime.UtcNow;
            var cache        = new CompilerCache();
            var fileInfo     = new Mock <IFileInfo>();

            fileInfo.SetupGet(f => f.PhysicalPath)
            .Returns("test");
            fileInfo.SetupGet(f => f.LastModified)
            .Returns(lastModified);
            var type            = GetType();
            var uncachedResult1 = UncachedCompilationResult.Successful(type, "hello world");
            var uncachedResult2 = UncachedCompilationResult.Successful(typeof(object), "hello world");
            var uncachedResult3 = UncachedCompilationResult.Successful(typeof(Guid), "hello world");

            var runtimeFileInfo = new RelativeFileInfo()
            {
                FileInfo     = fileInfo.Object,
                RelativePath = "test",
            };

            // Act
            cache.GetOrAdd(runtimeFileInfo, false, () => uncachedResult1);
            var actual1 = cache.GetOrAdd(runtimeFileInfo, true, () => uncachedResult2);
            var actual2 = cache.GetOrAdd(runtimeFileInfo, false, () => uncachedResult3);

            // Assert
            Assert.Same(uncachedResult2, actual1);
            Assert.Same(typeof(object), actual1.CompiledType);

            Assert.NotSame(actual2, uncachedResult3);
            Assert.Same(typeof(object), actual2.CompiledType);
        }
示例#2
0
        public void GetOrAdd_ReturnsCompilationResultFromFactory()
        {
            // Arrange
            var cache    = new CompilerCache();
            var fileInfo = new Mock <IFileInfo>();

            fileInfo
            .SetupGet(i => i.LastModified)
            .Returns(DateTime.FromFileTimeUtc(10000));

            var type     = GetType();
            var expected = UncachedCompilationResult.Successful(type, "hello world");

            var runtimeFileInfo = new RelativeFileInfo()
            {
                FileInfo     = fileInfo.Object,
                RelativePath = "ab",
            };

            // Act
            var actual = cache.GetOrAdd(runtimeFileInfo, false, () => expected);

            // Assert
            Assert.Same(expected, actual);
            Assert.Equal("hello world", actual.CompiledContent);
            Assert.Same(type, actual.CompiledType);
        }
示例#3
0
        public void GetOrAdd_DoesNotCacheCompiledContent_OnCallsAfterInitial()
        {
            // Arrange
            var lastModified = DateTime.UtcNow;
            var cache        = new CompilerCache();
            var fileInfo     = new Mock <IFileInfo>();

            fileInfo.SetupGet(f => f.PhysicalPath)
            .Returns("test");
            fileInfo.SetupGet(f => f.LastModified)
            .Returns(lastModified);
            var type           = GetType();
            var uncachedResult = UncachedCompilationResult.Successful(type, "hello world");

            // Act
            cache.GetOrAdd(fileInfo.Object, () => uncachedResult);
            var actual1 = cache.GetOrAdd(fileInfo.Object, () => uncachedResult);
            var actual2 = cache.GetOrAdd(fileInfo.Object, () => uncachedResult);

            // Assert
            Assert.NotSame(uncachedResult, actual1);
            Assert.NotSame(uncachedResult, actual2);
            var result = Assert.IsType <CompilationResult>(actual1);

            Assert.Null(actual1.CompiledContent);
            Assert.Same(type, actual1.CompiledType);

            result = Assert.IsType <CompilationResult>(actual2);
            Assert.Null(actual2.CompiledContent);
            Assert.Same(type, actual2.CompiledType);
        }
        /// <inheritdoc />
        public CompilationResult Compile([NotNull] RelativeFileInfo fileInfo, [NotNull] string compilationContent)
        {
            var assemblyName        = Path.GetRandomFileName();
            var compilationSettings = _compilerOptionsProvider.GetCompilationSettings(_environment);
            var syntaxTree          = SyntaxTreeGenerator.Generate(compilationContent,
                                                                   assemblyName,
                                                                   compilationSettings);
            var references = _applicationReferences.Value;

            var compilationOptions = compilationSettings.CompilationOptions
                                     .WithOutputKind(OutputKind.DynamicallyLinkedLibrary);

            var compilation = CSharpCompilation.Create(assemblyName,
                                                       options: compilationOptions,
                                                       syntaxTrees: new[] { syntaxTree },
                                                       references: references);

            using (var ms = new MemoryStream()) {
                using (var pdb = new MemoryStream()) {
                    EmitResult result;

                    if (_supportsPdbGeneration.Value)
                    {
                        result = compilation.Emit(ms, pdbStream: pdb);
                    }
                    else
                    {
                        result = compilation.Emit(ms);
                    }

                    if (!result.Success)
                    {
                        return(GetCompilationFailedResult(
                                   fileInfo.RelativePath,
                                   compilationContent,
                                   assemblyName,
                                   result.Diagnostics));
                    }

                    Assembly assembly;
                    ms.Seek(0, SeekOrigin.Begin);

                    if (_supportsPdbGeneration.Value)
                    {
                        pdb.Seek(0, SeekOrigin.Begin);
                        assembly = _loader.LoadStream(ms, pdb);
                    }
                    else
                    {
                        assembly = _loader.LoadStream(ms, assemblySymbols: null);
                    }

                    var type = assembly.GetExportedTypes()
                               .First(t => t.Name.StartsWith(_classPrefix, StringComparison.Ordinal));

                    return(UncachedCompilationResult.Successful(type, compilationContent));
                }
            }
        }
示例#5
0
        public void GetOrAdd_ReturnsCompilationResultFromFactory()
        {
            // Arrange
            var cache    = new CompilerCache();
            var fileInfo = Mock.Of <IFileInfo>();
            var type     = GetType();
            var expected = UncachedCompilationResult.Successful(type, "hello world");

            // Act
            var actual = cache.GetOrAdd(fileInfo, () => expected);

            // Assert
            Assert.Same(expected, actual);
            Assert.Equal("hello world", actual.CompiledContent);
            Assert.Same(type, actual.CompiledType);
        }