|
2 | 2 | // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
3 | 3 |
|
4 | 4 | using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Linq; |
5 | 7 | using System.Diagnostics; |
6 | 8 | using System.Threading; |
7 | 9 | using System.Threading.Tasks; |
@@ -37,17 +39,106 @@ public async Task UnhandledErrorsWriteToDiagnosticWhenUsingExceptionPage() |
37 | 39 | // Act |
38 | 40 | await server.CreateClient().GetAsync("/path"); |
39 | 41 |
|
40 | | - // This ensures that all diagnostics are completely written to the diagnostic listener |
41 | | - Thread.Sleep(1000); |
| 42 | + // Assert |
| 43 | + Assert.NotNull(listener.DiagnosticUnhandledException?.HttpContext); |
| 44 | + Assert.NotNull(listener.DiagnosticUnhandledException?.Exception); |
| 45 | + Assert.Null(listener.DiagnosticHandledException?.HttpContext); |
| 46 | + Assert.Null(listener.DiagnosticHandledException?.Exception); |
| 47 | + } |
| 48 | + |
| 49 | + public static TheoryData CompilationExceptionData |
| 50 | + { |
| 51 | + get |
| 52 | + { |
| 53 | + var variations = new TheoryData<List<CompilationFailure>>(); |
| 54 | + var failures = new List<CompilationFailure>(); |
| 55 | + var diagnosticMessages = new List<DiagnosticMessage>(); |
| 56 | + variations.Add(new List<CompilationFailure>() |
| 57 | + { |
| 58 | + new CompilationFailure(@"c:\sourcefilepath.cs", "source file content", "compiled content", diagnosticMessages) |
| 59 | + }); |
| 60 | + variations.Add(new List<CompilationFailure>() |
| 61 | + { |
| 62 | + new CompilationFailure(null, "source file content", "compiled content", diagnosticMessages) |
| 63 | + }); |
| 64 | + variations.Add(new List<CompilationFailure>() |
| 65 | + { |
| 66 | + new CompilationFailure(@"c:\sourcefilepath.cs", null, "compiled content", diagnosticMessages) |
| 67 | + }); |
| 68 | + variations.Add(new List<CompilationFailure>() |
| 69 | + { |
| 70 | + new CompilationFailure(@"c:\sourcefilepath.cs", "source file content", null, diagnosticMessages) |
| 71 | + }); |
| 72 | + variations.Add(new List<CompilationFailure>() |
| 73 | + { |
| 74 | + new CompilationFailure(null, null, null, diagnosticMessages) |
| 75 | + }); |
| 76 | + variations.Add(new List<CompilationFailure>() |
| 77 | + { |
| 78 | + new CompilationFailure(@"c:\sourcefilepath.cs", "source file content", "compiled content", diagnosticMessages), |
| 79 | + new CompilationFailure(@"c:\sourcefilepath.cs", null, "compiled content", diagnosticMessages) |
| 80 | + }); |
| 81 | + variations.Add(null); |
| 82 | + variations.Add(new List<CompilationFailure>() |
| 83 | + { |
| 84 | + null |
| 85 | + }); |
| 86 | + variations.Add(new List<CompilationFailure>() |
| 87 | + { |
| 88 | + new CompilationFailure(@"c:\sourcefilepath.cs", "source file content", "compiled content", diagnosticMessages), |
| 89 | + null |
| 90 | + }); |
| 91 | + variations.Add(new List<CompilationFailure>() |
| 92 | + { |
| 93 | + new CompilationFailure(@"c:\sourcefilepath.cs", "source file content", "compiled content", null) |
| 94 | + }); |
| 95 | + variations.Add(new List<CompilationFailure>() |
| 96 | + { |
| 97 | + new CompilationFailure(@"c:\sourcefilepath.cs", "source file content", "compiled content", new List<DiagnosticMessage>(){ null }) |
| 98 | + }); |
| 99 | + return variations; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + [Theory] |
| 104 | + [MemberData(nameof(CompilationExceptionData))] |
| 105 | + public async Task NullInfoInCompilationException_ShouldNotThrowExceptionGeneratingExceptionPage( |
| 106 | + List<CompilationFailure> failures) |
| 107 | + { |
| 108 | + // Arrange |
| 109 | + DiagnosticListener diagnosticListener = null; |
| 110 | + var builder = new WebHostBuilder() |
| 111 | + .Configure(app => |
| 112 | + { |
| 113 | + diagnosticListener = app.ApplicationServices.GetRequiredService<DiagnosticListener>(); |
| 114 | + app.UseDeveloperExceptionPage(); |
| 115 | + app.Run(context => |
| 116 | + { |
| 117 | + throw new CustomCompilationException(failures); |
| 118 | + }); |
| 119 | + }); |
| 120 | + var server = new TestServer(builder); |
| 121 | + var listener = new TestDiagnosticListener(); |
| 122 | + diagnosticListener.SubscribeWithAdapter(listener); |
| 123 | + |
| 124 | + // Act |
| 125 | + await server.CreateClient().GetAsync("/path"); |
42 | 126 |
|
43 | 127 | // Assert |
44 | | - Assert.NotNull(listener.EndRequest?.HttpContext); |
45 | | - Assert.Null(listener.HostingUnhandledException?.HttpContext); |
46 | | - Assert.Null(listener.HostingUnhandledException?.Exception); |
47 | 128 | Assert.NotNull(listener.DiagnosticUnhandledException?.HttpContext); |
48 | 129 | Assert.NotNull(listener.DiagnosticUnhandledException?.Exception); |
49 | 130 | Assert.Null(listener.DiagnosticHandledException?.HttpContext); |
50 | 131 | Assert.Null(listener.DiagnosticHandledException?.Exception); |
51 | 132 | } |
| 133 | + |
| 134 | + public class CustomCompilationException : Exception, ICompilationException |
| 135 | + { |
| 136 | + public CustomCompilationException(IEnumerable<CompilationFailure> compilationFailures) |
| 137 | + { |
| 138 | + CompilationFailures = compilationFailures; |
| 139 | + } |
| 140 | + |
| 141 | + public IEnumerable<CompilationFailure> CompilationFailures { get; } |
| 142 | + } |
52 | 143 | } |
53 | 144 | } |
0 commit comments