|
| 1 | +using Microsoft.CodeAnalysis; |
| 2 | +using Microsoft.CodeAnalysis.MSBuild; |
| 3 | +using System.Collections.Immutable; |
| 4 | +using Tdg5.StandardConventions.Tests.TestHelpers; |
| 5 | +using Xunit.Abstractions; |
| 6 | + |
| 7 | +namespace Tdg5.StandardConventions.Tests; |
| 8 | + |
| 9 | +/// <summary> |
| 10 | +/// Test case demonstrating a minimal reproduction of a diagnostic issue that |
| 11 | +/// arose in .NET 9.0.200. |
| 12 | +/// </summary> |
| 13 | +public class MinimalReproductionTest : BaseTestProjectAnalysisVerifierTest |
| 14 | +{ |
| 15 | + /// <summary> |
| 16 | + /// Initializes a new instance of the <see cref="MinimalReproductionTest"/> class. |
| 17 | + /// </summary> |
| 18 | + /// <param name="testOutputHelper">The test output helper.</param> |
| 19 | + public MinimalReproductionTest(ITestOutputHelper testOutputHelper) |
| 20 | + : base(testOutputHelper) |
| 21 | + { |
| 22 | + } |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// Gets a version of the project path that can be used as member data. |
| 26 | + /// </summary> |
| 27 | + public static List<object[]> TheProjectPath { get; } = [["Data/StyleCopRules/StyleCopRules.csproj"]]; |
| 28 | + |
| 29 | + /// <inheritdoc/> |
| 30 | + public override string ProjectPath => (TheProjectPath[0][0] as string)!; |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// The minimum reproduction. |
| 34 | + /// </summary> |
| 35 | + /// <param name="projectPath">The project to run diagnostics on.</param> |
| 36 | + /// <returns>A task representing the completion of the test.</returns> |
| 37 | + [Theory] |
| 38 | + [MemberData(nameof(TheProjectPath))] |
| 39 | + public async Task MinimumReproduction(string projectPath) |
| 40 | + { |
| 41 | + var workspace = MSBuildWorkspace.Create(); |
| 42 | + var project = await workspace.OpenProjectAsync(projectPath); |
| 43 | + var analyzers = project |
| 44 | + .AnalyzerReferences |
| 45 | + .SelectMany(r => r.GetAnalyzers(project.Language)) |
| 46 | + .ToImmutableArray(); |
| 47 | + Dictionary<string, List<string>> diagnosticIdsByAnalyzer = []; |
| 48 | + foreach (var analyzerReference in project.AnalyzerReferences) |
| 49 | + { |
| 50 | + var analyzerId = analyzerReference.Id.ToString() ?? "NO_ANALYZER_ID"; |
| 51 | + var analyzerName = analyzerId[..analyzerId.IndexOf(',')]; |
| 52 | + List<string> ids; |
| 53 | + if (!diagnosticIdsByAnalyzer.TryGetValue(analyzerName, out ids!)) |
| 54 | + { |
| 55 | + ids = []; |
| 56 | + diagnosticIdsByAnalyzer[analyzerName] = ids; |
| 57 | + } |
| 58 | + |
| 59 | + foreach (var analyzer in analyzerReference.GetAnalyzers(project.Language)) |
| 60 | + { |
| 61 | + foreach (var diagnostic in analyzer.SupportedDiagnostics) |
| 62 | + { |
| 63 | + ids.Add(diagnostic.Id); |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + var cSharpCodeStyleDiagnosticIds = |
| 69 | + Assert.Contains("Microsoft.CodeAnalysis.CSharp.CodeStyle", diagnosticIdsByAnalyzer); |
| 70 | + var codeStyleDiagnosticIds = |
| 71 | + Assert.Contains("Microsoft.CodeAnalysis.CodeStyle", diagnosticIdsByAnalyzer); |
| 72 | + |
| 73 | + Assert.Contains("IDE0040", cSharpCodeStyleDiagnosticIds); |
| 74 | + Assert.Contains("IDE0055", cSharpCodeStyleDiagnosticIds); |
| 75 | + Assert.Contains("IDE0033", codeStyleDiagnosticIds); |
| 76 | + Assert.Contains("IDE0070", codeStyleDiagnosticIds); |
| 77 | + } |
| 78 | +} |
0 commit comments