Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose CompilerVisibleProperty to AnalyzerConfigOptionsProvider.GlobalOptions #301

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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'");

Check failure on line 34 in tests/Buildalyzer.Workspaces.Tests/CompilerVisibleProperty_specs.cs

View workflow job for this annotation

GitHub Actions / Test Results (ubuntu-latest)

Buildalyzer.Workspaces.Tests.CompilerVisibleProperty ► is_exposed

Failed test found in: tests/Buildalyzer.Workspaces.Tests/TestResults/test-results.trx Error: Expected diagnostics[^1].ToString() to be equivalent to "warning TEST01: Has custom property with value 'Custom value'" with a length of 61, but "warning TEST01: Has custom property with value ''" has a length of 49, differs near "'" (index 48).
Raw output
Expected diagnostics[^1].ToString() to be equivalent to "warning TEST01: Has custom property with value 'Custom value'" with a length of 61, but "warning TEST01: Has custom property with value ''" has a length of 49, differs near "'" (index 48).
   at FluentAssertions.Execution.LateBoundTestFramework.Throw(String message)
   at FluentAssertions.Execution.TestFrameworkProvider.Throw(String message)
   at FluentAssertions.Execution.DefaultAssertionStrategy.HandleFailure(String message)
   at FluentAssertions.Execution.AssertionScope.FailWith(Func`1 failReasonFunc)
   at FluentAssertions.Execution.AssertionScope.FailWith(Func`1 failReasonFunc)
   at FluentAssertions.Primitives.StringEqualityValidator.ValidateAgainstLengthDifferences()
   at FluentAssertions.Primitives.StringValidator.Validate()
   at FluentAssertions.Primitives.StringAssertions`1.BeEquivalentTo(String expected, String because, Object[] becauseArgs)
   at Buildalyzer.Workspaces.Tests.CompilerVisibleProperty.is_exposed() in /home/runner/work/Buildalyzer/Buildalyzer/tests/Buildalyzer.Workspaces.Tests/CompilerVisibleProperty_specs.cs:line 34
   at NUnit.Framework.Internal.TaskAwaitAdapter.GenericAdapter`1.BlockUntilCompleted()
   at NUnit.Framework.Internal.MessagePumpStrategy.NoMessagePumpStrategy.WaitForCompletion(AwaitAdapter awaiter)
   at NUnit.Framework.Internal.AsyncToSyncAdapter.Await[TResult](Func`1 invoke)
   at NUnit.Framework.Internal.AsyncToSyncAdapter.Await(Func`1 invoke)
   at NUnit.Framework.Internal.Commands.TestMethodCommand.RunTestMethod(TestExecutionContext context)
   at NUnit.Framework.Internal.Commands.TestMethodCommand.Execute(TestExecutionContext context)
   at NUnit.Framework.Internal.Execution.SimpleWorkItem.<>c__DisplayClass3_0.<PerformWork>b__0()
   at NUnit.Framework.Internal.ContextUtils.<>c__DisplayClass1_0`1.<DoIsolated>b__0(Object _)

}
}

#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<DiagnosticDescriptor> 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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<CustomProperty>Custom value</CustomProperty>
</PropertyGroup>

<ItemGroup>
<CompilerVisibleProperty Include="CustomProperty" />
</ItemGroup>

</Project>
Loading