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

Fix Microsoft.Extensions.AuditReports output path #4945

Merged
merged 10 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
Expand Up @@ -9,6 +9,7 @@
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.Gen.Metrics.Model;
using Microsoft.Gen.Shared;
using Microsoft.Shared.DiagnosticIds;

namespace Microsoft.Gen.MetricsReports;

Expand All @@ -22,10 +23,6 @@ public class MetricsReportsGenerator : ISourceGenerator
private const string CurrentProjectPath = "build_property.projectdir";
private const string FileName = "MetricsReport.json";

private string? _compilationOutputPath;
private string? _currentProjectPath;
private string? _reportOutputPath;

public void Initialize(GeneratorInitializationContext context)
{
context.RegisterForSyntaxNotifications(ClassDeclarationSyntaxReceiver.Create);
Expand All @@ -35,8 +32,9 @@ public void Execute(GeneratorExecutionContext context)
{
context.CancellationToken.ThrowIfCancellationRequested();

var receiver = context.SyntaxReceiver as ClassDeclarationSyntaxReceiver;
if (receiver == null || receiver.ClassDeclarations.Count == 0 || !GeneratorUtilities.ShouldGenerateReport(context, GenerateMetricDefinitionReport))
if (context.SyntaxReceiver is not ClassDeclarationSyntaxReceiver receiver ||
receiver.ClassDeclarations.Count == 0 ||
!GeneratorUtilities.ShouldGenerateReport(context, GenerateMetricDefinitionReport))
{
return;
}
Expand All @@ -52,13 +50,23 @@ public void Execute(GeneratorExecutionContext context)

var options = context.AnalyzerConfigOptions.GlobalOptions;

var path = (_reportOutputPath != null || options.TryGetValue(ReportOutputPath, out _reportOutputPath))
? _reportOutputPath
var path = TryRetrieveOptionsValue(options, ReportOutputPath, out var reportOutputPath)
? reportOutputPath!
: GetDefaultReportOutputPath(options);

if (string.IsNullOrWhiteSpace(path))
{
// Report diagnostic. Tell that it is either <MetricDefinitionReportOutputPath> missing or <CompilerVisibleProperty Include="OutputPath"/> visibility to compiler.
// Report diagnostic:
var diagnostic = new DiagnosticDescriptor(
DiagnosticIds.AuditReports.AUDREP000,
"Metrics report won't be generated.",
"Both <MetricsReportOutputPath> and <OutputPath> are missing or not set. The report won't be generated.",
"MetricsReports",
DiagnosticSeverity.Info,
isEnabledByDefault: true);

context.ReportDiagnostic(Diagnostic.Create(diagnostic, location: null));

return;
}

Expand Down Expand Up @@ -90,18 +98,25 @@ private static ReportedMetricClass[] MapToCommonModel(IReadOnlyList<MetricType>
return reportedMetrics.ToArray();
}

private string GetDefaultReportOutputPath(AnalyzerConfigOptions options)
private static bool TryRetrieveOptionsValue(AnalyzerConfigOptions options, string name, out string? value)
=> options.TryGetValue(name, out value) && !string.IsNullOrWhiteSpace(value);

private static string GetDefaultReportOutputPath(AnalyzerConfigOptions options)
{
if (_currentProjectPath != null && _compilationOutputPath != null)
if (!TryRetrieveOptionsValue(options, CompilationOutputPath, out var compilationOutputPath))
{
return _currentProjectPath + _compilationOutputPath;
return string.Empty;
}

_ = options.TryGetValue(CompilationOutputPath, out _compilationOutputPath);
_ = options.TryGetValue(CurrentProjectPath, out _currentProjectPath);
// <OutputPath> is absolute - return it right away:
if (Path.IsPathRooted(compilationOutputPath))
{
return compilationOutputPath!;
}

return string.IsNullOrWhiteSpace(_currentProjectPath) || string.IsNullOrWhiteSpace(_compilationOutputPath)
? string.Empty
: _currentProjectPath + _compilationOutputPath;
// Get <ProjectDir> and combine it with <OutputPath> if the former isn't empty:
return TryRetrieveOptionsValue(options, CurrentProjectPath, out var currentProjectPath)
? Path.Combine(currentProjectPath!, compilationOutputPath!)
: string.Empty;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@
<CompilerVisibleProperty Include="ComplianceReportOutputPath" />
<CompilerVisibleProperty Include="GenerateMetricsReport" />
<CompilerVisibleProperty Include="MetricsReportOutputPath" />
<CompilerVisibleProperty Include="OutputPath" />
</ItemGroup>
</Project>

This file was deleted.

6 changes: 6 additions & 0 deletions src/Shared/DiagnosticIds/DiagnosticIds.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ internal static class Metrics
internal const string METGEN018 = nameof(METGEN018);
internal const string METGEN019 = nameof(METGEN019);
}

internal static class AuditReports
{
internal const string AUDREP000 = nameof(AUDREP000);
internal const string AUDREP001 = nameof(AUDREP001);
xakep139 marked this conversation as resolved.
Show resolved Hide resolved
}
}

#pragma warning restore S1144
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ static async Task<IReadOnlyList<Diagnostic>> RunGenerator(string code, string ou
{
Assembly.GetAssembly(typeof(ILogger))!,
Assembly.GetAssembly(typeof(LoggerMessageAttribute))!,
Assembly.GetAssembly(typeof(Microsoft.Extensions.Compliance.Classification.DataClassification))!,
Assembly.GetAssembly(typeof(Extensions.Compliance.Classification.DataClassification))!,
},
new[]
{
Expand All @@ -120,9 +120,10 @@ public async Task MissingDataClassificationSymbol()
{
Source,
},
new OptionsProvider()).ConfigureAwait(false);
new OptionsProvider())
.ConfigureAwait(false);

Assert.Equal(0, d.Count);
Assert.Empty(d);
}

private sealed class Options : AnalyzerConfigOptions
Expand Down
89 changes: 42 additions & 47 deletions test/Generators/Microsoft.Gen.MetricsReports/Unit/GeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,8 @@

namespace Microsoft.Gen.MetricsReports.Test;

public class GeneratorTests
public class GeneratorTests(ITestOutputHelper output)
{
private readonly ITestOutputHelper _output;

public GeneratorTests(ITestOutputHelper output)
{
_output = output;
}

[Fact]
public void GeneratorShouldNotDoAnythingIfGeneralExecutionContextDoesNotHaveClassDeclarationSyntaxReceiver()
{
Expand All @@ -35,79 +28,75 @@ public void GeneratorShouldNotDoAnythingIfGeneralExecutionContextDoesNotHaveClas
Assert.Null(defaultGeneralExecutionContext.SyntaxReceiver);
}

[Fact]
public async Task TestAll()
[Theory]
[CombinatorialData]
public async Task TestAll(bool useExplicitReportPath)
{
foreach (var inputFile in Directory.GetFiles("TestClasses"))
{
var stem = Path.GetFileNameWithoutExtension(inputFile);
var goldenReportFile = $"GoldenReports/{stem}.json";
var goldenReportPath = Path.Combine("GoldenReports", Path.ChangeExtension(stem, ".json"));

var tmp = Path.Combine(Directory.GetCurrentDirectory(), "MetricsReport.json");
var generatedReportPath = Path.Combine(Directory.GetCurrentDirectory(), "MetricsReport.json");

if (File.Exists(goldenReportFile))
if (File.Exists(goldenReportPath))
{
var d = await RunGenerator(File.ReadAllText(inputFile));
var d = await RunGenerator(await File.ReadAllTextAsync(inputFile), useExplicitReportPath);
Assert.Empty(d);

var golden = File.ReadAllText(goldenReportFile);
var generated = File.ReadAllText(tmp);
var golden = await File.ReadAllTextAsync(goldenReportPath);
var generated = await File.ReadAllTextAsync(generatedReportPath);

if (golden != generated)
{
_output.WriteLine($"MISMATCH: goldenReportFile {goldenReportFile}, tmp {tmp}");
_output.WriteLine("----");
_output.WriteLine("golden:");
_output.WriteLine(golden);
_output.WriteLine("----");
_output.WriteLine("generated:");
_output.WriteLine(generated);
_output.WriteLine("----");
output.WriteLine($"MISMATCH: goldenReportFile {goldenReportPath}, tmp {generatedReportPath}");
output.WriteLine("----");
output.WriteLine("golden:");
output.WriteLine(golden);
output.WriteLine("----");
output.WriteLine("generated:");
output.WriteLine(generated);
output.WriteLine("----");
}

File.Delete(generatedReportPath);
Assert.Equal(golden, generated);
File.Delete(tmp);
}
else
{
// generate the golden file if it doesn't already exist
_output.WriteLine($"Generating golden report: {goldenReportFile}");
_ = await RunGenerator(File.ReadAllText(inputFile));
File.Copy(tmp, goldenReportFile);
output.WriteLine($"Generating golden report: {goldenReportPath}");
_ = await RunGenerator(await File.ReadAllTextAsync(inputFile), useExplicitReportPath);
File.Copy(generatedReportPath, goldenReportPath);
}
}
}

private static async Task<IReadOnlyList<Diagnostic>> RunGenerator(
string code,
bool includeBaseReferences = true,
bool includeMeterReferences = true,
bool setReportPath,
CancellationToken cancellationToken = default)
{
Assembly[]? refs = null;
if (includeMeterReferences)
{
refs = new[]
{
Assembly.GetAssembly(typeof(Meter))!,
Assembly.GetAssembly(typeof(CounterAttribute))!,
Assembly.GetAssembly(typeof(HistogramAttribute))!,
Assembly.GetAssembly(typeof(GaugeAttribute))!,
};
}
Assembly[] refs =
[
Assembly.GetAssembly(typeof(Meter))!,
Assembly.GetAssembly(typeof(CounterAttribute))!,
Assembly.GetAssembly(typeof(HistogramAttribute))!,
Assembly.GetAssembly(typeof(GaugeAttribute))!
];

var (d, _) = await RoslynTestUtils.RunGenerator(
new MetricsReportsGenerator(),
refs,
new[] { code },
new OptionsProvider(),
includeBaseReferences: includeBaseReferences,
new OptionsProvider(returnReportPath: setReportPath),
includeBaseReferences: true,
cancellationToken: cancellationToken).ConfigureAwait(false);

return d;
}

private sealed class Options : AnalyzerConfigOptions
private sealed class Options(bool returnReportPath) : AnalyzerConfigOptions
{
public override bool TryGetValue(string key, out string value)
{
Expand All @@ -117,7 +106,13 @@ public override bool TryGetValue(string key, out string value)
return true;
}

if (key == "build_property.MetricsReportOutputPath")
if (returnReportPath && key == "build_property.MetricsReportOutputPath")
{
value = Directory.GetCurrentDirectory();
return true;
}

if (key == "build_property.outputpath")
{
value = Directory.GetCurrentDirectory();
return true;
Expand All @@ -128,9 +123,9 @@ public override bool TryGetValue(string key, out string value)
}
}

private sealed class OptionsProvider : AnalyzerConfigOptionsProvider
private sealed class OptionsProvider(bool returnReportPath) : AnalyzerConfigOptionsProvider
{
public override AnalyzerConfigOptions GlobalOptions => new Options();
public override AnalyzerConfigOptions GlobalOptions => new Options(returnReportPath);

public override AnalyzerConfigOptions GetOptions(SyntaxTree tree) => throw new NotSupportedException();
public override AnalyzerConfigOptions GetOptions(AdditionalText textFile) => throw new NotSupportedException();
Expand Down