Exemple #1
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));
                }
            }
        }
Exemple #3
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);
        }