// Internal for unit testing internal CompilationResult GetCompilationFailedResult( string relativePath, string compilationContent, string assemblyName, IEnumerable <Diagnostic> diagnostics) { var diagnosticGroups = diagnostics .Where(IsError) .GroupBy(diagnostic => GetFilePath(relativePath, diagnostic), StringComparer.Ordinal); var failures = new List <CompilationFailure>(); foreach (var group in diagnosticGroups) { var sourceFilePath = group.Key; string sourceFileContent; if (string.Equals(assemblyName, sourceFilePath, StringComparison.Ordinal)) { // The error is in the generated code and does not have a mapping line pragma sourceFileContent = compilationContent; sourceFilePath = Resources.GeneratedCodeFileName; } else { sourceFileContent = ReadFileContentsSafely(_fileProvider, sourceFilePath); } var compilationFailure = new CompilationFailure(sourceFilePath, sourceFileContent, compilationContent, group.Select(d => d.ToDiagnosticMessage(_environment.RuntimeFramework))); failures.Add(compilationFailure); } return(CompilationResult.Failed(failures)); }
public void EnsureSuccessful_ThrowsIfCompilationFailed() { // Arrange var compilationFailure = new CompilationFailure("test", Enumerable.Empty <Dnx.Runtime.DiagnosticMessage>()); var failures = new[] { compilationFailure }; var result = CompilationResult.Failed(failures); // Act and Assert Assert.Null(result.CompiledType); Assert.Same(failures, result.CompilationFailures); var exception = Assert.Throws <CompilationFailedException>(() => result.EnsureSuccessful()); var failure = Assert.Single(exception.CompilationFailures); Assert.Same(compilationFailure, failure); }
public void EnsureSuccessful_ThrowsIfCompilationFailed() { // Arrange var compilationFailure = Mock.Of <ICompilationFailure>(); var failures = new[] { compilationFailure }; var result = CompilationResult.Failed(failures); // Act and Assert Assert.Null(result.CompiledType); Assert.Same(failures, result.CompilationFailures); var exception = Assert.Throws <CompilationFailedException>(() => result.EnsureSuccessful()); var failure = Assert.Single(exception.CompilationFailures); Assert.Same(compilationFailure, failure); }
// Internal for unit testing internal CompilationResult GetCompilationFailedResult(RelativeFileInfo file, IEnumerable <RazorError> errors) { // If a SourceLocation does not specify a file path, assume it is produced // from parsing the current file. var messageGroups = errors .GroupBy(razorError => razorError.Location.FilePath ?? file.RelativePath, StringComparer.Ordinal); var failures = new List <RazorCompilationFailure>(); foreach (var group in messageGroups) { var filePath = group.Key; var fileContent = ReadFileContentsSafely(filePath); var compilationFailure = new RazorCompilationFailure( filePath, fileContent, group.Select(parserError => new RazorCompilationMessage(parserError, filePath))); failures.Add(compilationFailure); } return(CompilationResult.Failed(failures)); }
/// <inheritdoc /> public CompilationResult Compile(IFileInfo fileInfo, string compilationContent) { var sourceText = SourceText.From(compilationContent, Encoding.UTF8); var syntaxTrees = new[] { CSharpSyntaxTree.ParseText(sourceText, path: fileInfo.PhysicalPath) }; var references = _applicationReferences.Value; var assemblyName = Path.GetRandomFileName(); var compilation = CSharpCompilation.Create(assemblyName, options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary), syntaxTrees: syntaxTrees, references: references); using (var ms = new MemoryStream()) { using (var pdb = new MemoryStream()) { EmitResult result; if (PlatformHelper.IsMono) { result = compilation.Emit(ms, pdbStream: null); } else { result = compilation.Emit(ms, pdbStream: pdb); } if (!result.Success) { var formatter = new DiagnosticFormatter(); var messages = result.Diagnostics .Where(IsError) .Select(d => GetCompilationMessage(formatter, d)) .ToList(); return(CompilationResult.Failed(fileInfo, compilationContent, messages)); } Assembly assembly; ms.Seek(0, SeekOrigin.Begin); if (PlatformHelper.IsMono) { assembly = _loader.LoadStream(ms, pdbStream: null); } else { pdb.Seek(0, SeekOrigin.Begin); assembly = _loader.LoadStream(ms, pdb); } var type = assembly.GetExportedTypes() .First(); return(UncachedCompilationResult.Successful(type, compilationContent)); } } }
public CompilationResult Compile(string content) { var syntaxTrees = new[] { CSharpSyntaxTree.ParseText(content) }; var targetFramework = _environment.TargetFramework; var references = GetApplicationReferences(); var assemblyName = Path.GetRandomFileName(); var compilation = CSharpCompilation.Create(assemblyName, options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary), syntaxTrees: syntaxTrees, references: references); using (var ms = new MemoryStream()) { using (var pdb = new MemoryStream()) { EmitResult result; if (PlatformHelper.IsMono) { result = compilation.Emit(ms, pdbStream: null); } else { result = compilation.Emit(ms, pdbStream: pdb); } if (!result.Success) { var formatter = new DiagnosticFormatter(); var messages = result.Diagnostics .Where(IsError) .Select(d => GetCompilationMessage(formatter, d)) .ToList(); return(CompilationResult.Failed(content, messages)); } Assembly assembly; ms.Seek(0, SeekOrigin.Begin); if (PlatformHelper.IsMono) { assembly = _loader.LoadStream(ms, pdbStream: null); } else { pdb.Seek(0, SeekOrigin.Begin); assembly = _loader.LoadStream(ms, pdb); } var type = assembly.GetExportedTypes() .First(); return(CompilationResult.Successful(string.Empty, type)); } } }
/// <inheritdoc /> public CompilationResult Compile(IFileInfo fileInfo, string compilationContent) { var syntaxTrees = new[] { SyntaxTreeGenerator.Generate(compilationContent, fileInfo.PhysicalPath) }; var references = _applicationReferences.Value; var assemblyName = Path.GetRandomFileName(); var compilation = CSharpCompilation.Create(assemblyName, options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary), syntaxTrees: syntaxTrees, 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) { var formatter = new DiagnosticFormatter(); var messages = result.Diagnostics .Where(IsError) .Select(d => GetCompilationMessage(formatter, d)) .ToList(); return(CompilationResult.Failed(fileInfo, compilationContent, messages)); } 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)); } } }