|
4 | 4 | using System.Collections.Immutable;
|
5 | 5 | using Microsoft.AspNetCore.Analyzer.Testing;
|
6 | 6 | using Microsoft.AspNetCore.App.Analyzers.Infrastructure;
|
| 7 | +using Microsoft.AspNetCore.Razor.Hosting; |
7 | 8 | using Microsoft.CodeAnalysis;
|
| 9 | +using Microsoft.CodeAnalysis.CSharp; |
8 | 10 | using Microsoft.CodeAnalysis.Diagnostics;
|
9 | 11 |
|
10 | 12 | namespace Microsoft.AspNetCore.Analyzers.Infrastructure;
|
@@ -34,6 +36,52 @@ static void Main()
|
34 | 36 | Assert.Collection(diagnostics, d => Assert.Equal("TEST001", d.Id));
|
35 | 37 | }
|
36 | 38 |
|
| 39 | + [Theory] |
| 40 | + [InlineData("ExternAssembly")] |
| 41 | + [InlineData("SystemFoo")] |
| 42 | + [InlineData("MicrosoftFoo")] |
| 43 | + public async Task ResolveAllWellKnownTypes_ToleratesDuplicateTypeNames(string assemblyName) |
| 44 | + { |
| 45 | + // Arrange |
| 46 | + var source = TestSource.Read(@" |
| 47 | +class Program |
| 48 | +{ |
| 49 | + static void Main() |
| 50 | + { |
| 51 | + } |
| 52 | +} |
| 53 | +"); |
| 54 | + var referenceSource = """ |
| 55 | + namespace Microsoft.AspNetCore.Builder |
| 56 | + { |
| 57 | + public static class EndpointRouteBuilderExtensions |
| 58 | + { |
| 59 | + } |
| 60 | + } |
| 61 | + """; |
| 62 | + // Act |
| 63 | + var project = TestDiagnosticAnalyzerRunner.CreateProjectWithReferencesInBinDir(GetType().Assembly, source.Source); |
| 64 | + Stream assemblyStream = GetInMemoryAssemblyStreamForCode(referenceSource, assemblyName, project.MetadataReferences.ToArray()); |
| 65 | + project = project.AddMetadataReference(MetadataReference.CreateFromStream(assemblyStream)); |
| 66 | + var diagnostics = await Runner.GetDiagnosticsAsync(project); |
| 67 | + |
| 68 | + // Assert |
| 69 | + Assert.Collection(diagnostics, d => Assert.Equal("TEST001", d.Id)); |
| 70 | + } |
| 71 | + |
| 72 | + private static Stream GetInMemoryAssemblyStreamForCode(string code, string assemblyName, params MetadataReference[] references) |
| 73 | + { |
| 74 | + var tree = CSharpSyntaxTree.ParseText(code); |
| 75 | + var trees = ImmutableArray.Create(tree); |
| 76 | + var options = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary); |
| 77 | + var compilation = CSharpCompilation.Create(assemblyName, trees).WithOptions(options); |
| 78 | + compilation = compilation.AddReferences(references); |
| 79 | + var stream = new MemoryStream(); |
| 80 | + var emitResult = compilation.Emit(stream); |
| 81 | + stream.Seek(0, SeekOrigin.Begin); |
| 82 | + return stream; |
| 83 | + } |
| 84 | + |
37 | 85 | [DiagnosticAnalyzer(LanguageNames.CSharp)]
|
38 | 86 | private class TestAnalyzer : DiagnosticAnalyzer
|
39 | 87 | {
|
|
0 commit comments