예제 #1
0
        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);
        }
예제 #2
0
        private static void BuildCodeSnippetDiv(CompilationFailure failure,
                                               StringBuilder builder,
                                               ref int totalErrorsShown)
        {
            const int NumContextLines = 3;
            var fileName = failure.SourceFilePath;
            if (totalErrorsShown < MaxCompilationErrorsToShow &&
                !string.IsNullOrEmpty(fileName))
            {
                builder.Append(@"<div class=""codeSnippet"">")
                       .AppendFormat(@"<div class=""filename""><code>{0}</code></div>", HtmlEncodeAndReplaceLineBreaks(fileName))
                       .AppendLine();

                IEnumerable<string> fileContent;
                if (string.IsNullOrEmpty(failure.SourceFileContent))
                {
                    fileContent = File.ReadLines(fileName);
                }
                else
                {
                    fileContent = failure.SourceFileContent.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
                }
                foreach (var message in failure.Messages)
                {
                    if (totalErrorsShown++ > MaxCompilationErrorsToShow)
                    {
                        break;
                    }

                    if (totalErrorsShown > 1)
                    {
                        builder.AppendLine("<br />");
                    }

                    builder.Append(@"<div class=""error-message"">")
                           .Append(HtmlEncodeAndReplaceLineBreaks(message.Message))
                           .Append("</div>");

                    // StartLine and EndLine are 1-based
                    var startLine = message.StartLine - 1;
                    var endLine = message.EndLine - 1;
                    var preContextIndex = Math.Max(startLine - NumContextLines, 0);
                    var index = preContextIndex + 1;
                    foreach (var line in fileContent.Skip(preContextIndex).Take(startLine - preContextIndex))
                    {
                        builder.Append(@"<div class=""line faded"">")
                               .AppendFormat(@"<span class=""line-number"">{0}</span><code>", index++)
                               .Append(HtmlEncodeAndReplaceLineBreaks(line))
                               .AppendLine("</code></div>");
                    }

                    var numErrorLines = 1 + Math.Max(0, endLine - startLine);
                    foreach (var line in fileContent.Skip(startLine).Take(numErrorLines))
                    {
                        builder.Append(@"<div class=""line error"">")
                               .AppendFormat(@"<span class=""line-number"">{0}</span><code>", index++)
                               .Append(HtmlEncodeAndReplaceLineBreaks(line))
                               .AppendLine("</code></div>");
                    }
                    foreach (var line in fileContent.Skip(message.EndLine).Take(NumContextLines))
                    {
                        builder.Append(@"<div class=""line faded"">")
                               .AppendFormat(@"<span class=""line-number"">{0}</span><code>", index++)
                               .Append(HtmlEncodeAndReplaceLineBreaks(line))
                               .AppendLine("</code></div>");
                    }
                }

                builder.AppendLine("</div>"); // Close codeSnippet div
            }
        }
        // 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 = "Generated Code";
                }
                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);
        }
예제 #4
0
        // 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<CompilationFailure>();
            foreach (var group in messageGroups)
            {
                var filePath = group.Key;
                var fileContent = ReadFileContentsSafely(filePath);
                var compilationFailure = new CompilationFailure(
                    filePath,
                    fileContent,
                    group.Select(parserError => CreateDiagnosticMessage(parserError, filePath)));
                failures.Add(compilationFailure);
            }

            return CompilationResult.Failed(failures);
        }