// 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));
        }
Exemplo n.º 2
0
            public async Task ThrowsCompilationExceptionIfCompilationFails()
            {
                // Arrange
                TestableEdgeApplication app = CreateEdgeApp(null);

                IFileInfo testFile = app.TestFileSystem.AddTestFile("Bar.cshtml", "Flarg");

                var expected = new List <CompilationMessage>()
                {
                    new CompilationMessage(MessageLevel.Error, "Yar!"),
                    new CompilationMessage(MessageLevel.Warning, "Gar!", new FileLocation("Blar.cshtml")),
                    new CompilationMessage(MessageLevel.Info, "Far!", new FileLocation("War.cshtml", 10, 12))
                };

                app.MockCompilationManager
                .Setup(c => c.Compile(testFile, It.IsAny <ITrace>()))
                .Returns(Task.FromResult(CompilationResult.Failed(null, expected)));

                // Act
                CompilationFailedException ex = await AssertEx.Throws <CompilationFailedException>(async() => await app.Invoke(TestData.CreateCallParams(path: "/Bar")));

                // Assert
                Assert.Equal(
                    String.Format(Resources.CompilationFailedException_MessageWithErrorCounts, 1, 1),
                    ex.Message);
                Assert.Equal(
                    expected,
                    ex.Messages);
            }
Exemplo n.º 3
0
        public void FailedResult_ThrowsWhenAccessingCompiledType()
        {
            // Arrange
            var expected        = @"Error compiling page at 'myfile'.";
            var originalContent = "Original file content";
            var fileInfo        = new Mock <IFileInfo>();

            fileInfo.SetupGet(f => f.PhysicalPath)
            .Returns("myfile");
            var contentBytes = Encoding.UTF8.GetBytes(originalContent);

            fileInfo.Setup(f => f.CreateReadStream())
            .Returns(new MemoryStream(contentBytes));
            var messages = new[]
            {
                new CompilationMessage("hello", 1, 1, 2, 2),
                new CompilationMessage("world", 3, 3, 4, 3)
            };
            var result = CompilationResult.Failed(fileInfo.Object,
                                                  "<h1>hello world</h1>",
                                                  messages);

            // Act and Assert
            var ex = Assert.Throws <CompilationFailedException>(() => result.CompiledType);

            Assert.Equal(expected, ex.Message);
            var compilationFailure = Assert.Single(ex.CompilationFailures);

            Assert.Equal(originalContent, compilationFailure.SourceFileContent);
            Assert.Equal(messages, compilationFailure.Messages);
        }
Exemplo n.º 4
0
        public void EnsureSuccessful_ThrowsIfCompilationFailed()
        {
            // Arrange
            var compilationFailure = Mock.Of <ICompilationFailure>();
            var result             = CompilationResult.Failed(compilationFailure);

            // Act and Assert
            Assert.Null(result.CompiledType);
            Assert.Same(compilationFailure, result.CompilationFailure);
            var exception = Assert.Throws <CompilationFailedException>(() => result.EnsureSuccessful());

            Assert.Collection(exception.CompilationFailures,
                              failure => Assert.Same(compilationFailure, failure));
        }