From 53419f01cbdc7ba626bcef514e4c9e30d0a39fcc Mon Sep 17 00:00:00 2001 From: Corniel Nobel Date: Wed, 19 Feb 2025 19:57:54 +0100 Subject: [PATCH] Add reproducer --- .../CompilerVisibleProperty_specs.cs | 64 +++++++++++++++++++ .../CompilerVisibleProperty.csproj | 12 ++++ 2 files changed, 76 insertions(+) create mode 100644 tests/Buildalyzer.Workspaces.Tests/CompilerVisibleProperty_specs.cs create mode 100644 tests/projects/CompilerVisibleProperty/CompilerVisibleProperty.csproj diff --git a/tests/Buildalyzer.Workspaces.Tests/CompilerVisibleProperty_specs.cs b/tests/Buildalyzer.Workspaces.Tests/CompilerVisibleProperty_specs.cs new file mode 100644 index 0000000..df7338a --- /dev/null +++ b/tests/Buildalyzer.Workspaces.Tests/CompilerVisibleProperty_specs.cs @@ -0,0 +1,64 @@ +using System.Collections.Generic; +using System.Collections.Immutable; +using System.Linq; +using System.Threading.Tasks; +using Buildalyzer.TestTools; +using FluentAssertions; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.Diagnostics; +using NUnit.Framework; + +namespace Buildalyzer.Workspaces.Tests; + +[TestFixture] +[NonParallelizable] +public class CompilerVisibleProperty +{ + [Test] + public async Task is_exposed() + { + using var ctx = Context.ForProject(@"CompilerVisibleProperty\CompilerVisibleProperty.csproj"); + + using var workspace = ctx.Analyzer.GetWorkspace(); + + var project = workspace.CurrentSolution.Projects.Single(); + + var compilation = await project.GetCompilationAsync(); + var options = compilation!.Options.WithSpecificDiagnosticOptions([KeyValuePair.Create("TEST01", ReportDiagnostic.Warn)]); + + var diagnostics = await compilation + .WithOptions(options) + .WithAnalyzers([new ReportCustomProperty()]) + .GetAllDiagnosticsAsync(default); + + diagnostics[^1].ToString().Should().BeEquivalentTo("warning TEST01: Has custom property with value 'Custom value'"); + } +} + +#pragma warning disable + +[DiagnosticAnalyzer(LanguageNames.CSharp)] +file sealed class ReportCustomProperty : DiagnosticAnalyzer +{ + private static readonly DiagnosticDescriptor Descriptor = new( + id: "TEST01", + title: "Has custom property", + messageFormat: "Has custom property with value '{0}'", + category: "Test", + defaultSeverity: DiagnosticSeverity.Warning, + isEnabledByDefault: true); + + public override ImmutableArray SupportedDiagnostics => [Descriptor]; + + public override void Initialize(AnalysisContext context) => context.RegisterCompilationAction(Report); + + private static void Report(CompilationAnalysisContext context) + { + var value = context.Options.AnalyzerConfigOptionsProvider.GlobalOptions.TryGetValue("build_property.CustomProperty", out var v) + ? v + : null; + + var issue = Diagnostic.Create(Descriptor, null, value); + context.ReportDiagnostic(issue); + } +} diff --git a/tests/projects/CompilerVisibleProperty/CompilerVisibleProperty.csproj b/tests/projects/CompilerVisibleProperty/CompilerVisibleProperty.csproj new file mode 100644 index 0000000..69b5b1f --- /dev/null +++ b/tests/projects/CompilerVisibleProperty/CompilerVisibleProperty.csproj @@ -0,0 +1,12 @@ + + + + net8.0 + Custom value + + + + + + +