Skip to content

Commit

Permalink
Fix broken tests from new direct dep feature
Browse files Browse the repository at this point in the history
  • Loading branch information
kzu committed Jul 10, 2024
1 parent e2d7e6f commit ce2e2cf
Showing 1 changed file with 41 additions and 10 deletions.
51 changes: 41 additions & 10 deletions samples/dotnet/Tests/AnalyzerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,14 @@ void IDisposable.Dispose()
public async Task WhenNoAdditionalFiles_ThenReportsUnknown()
{
var compilation = CSharpCompilation.Create("test", [CSharpSyntaxTree.ParseText("//")])
.WithAnalyzers([new SponsorLinkAnalyzer()]);
.WithAnalyzers([new SponsorLinkAnalyzer()],
new AnalyzerOptions([], new TestAnalyzerConfigOptionsProvider(new())
{
// Force reporting without wait period
{ "build_property.SponsorLinkNoInstallGrace", "true" },
// Simulate directly referenced package
{ "build_property.SponsorableLib", "1.0.0" },
}));

var diagnostics = await compilation.GetAnalyzerDiagnosticsAsync();

Expand Down Expand Up @@ -91,7 +98,9 @@ public async Task WhenUnknownAndNoGraceOption_ThenReportsUnknown()
{
{ "build_property.SponsorLinkNoInstallGrace", "true" },
{ "build_metadata.Analyzer.ItemType", "Analyzer" },
{ "build_metadata.Analyzer.NuGetPackageId", "SponsorableLib" }
{ "build_metadata.Analyzer.NuGetPackageId", "SponsorableLib" },
// Simulate directly referenced package
{ "build_property.SponsorableLib", "1.0.0" },
}));

var diagnostics = await compilation.GetAnalyzerDiagnosticsAsync();
Expand Down Expand Up @@ -119,7 +128,9 @@ public async Task WhenUnknownAndGraceExpired_ThenReportsUnknown()
.WithAnalyzers([new SponsorLinkAnalyzer()], new AnalyzerOptions([new AdditionalTextFile(dll)], new TestAnalyzerConfigOptionsProvider(new())
{
{ "build_metadata.Analyzer.ItemType", "Analyzer" },
{ "build_metadata.Analyzer.NuGetPackageId", "SponsorableLib" }
{ "build_metadata.Analyzer.NuGetPackageId", "SponsorableLib" },
// Simulate directly referenced package
{ "build_property.SponsorableLib", "1.0.0" },
}));

var diagnostics = await compilation.GetAnalyzerDiagnosticsAsync();
Expand All @@ -144,7 +155,9 @@ public async Task WhenSponsoring_ThenReportsSponsor()
var compilation = CSharpCompilation.Create("test", [CSharpSyntaxTree.ParseText("//")])
.WithAnalyzers([new SponsorLinkAnalyzer()], new AnalyzerOptions([new AdditionalTextFile(jwt)], new TestAnalyzerConfigOptionsProvider(new())
{
{ "build_metadata.SponsorManifest.ItemType", "SponsorManifest" }
{ "build_metadata.SponsorManifest.ItemType", "SponsorManifest" },
// Simulate directly referenced package
{ "build_property.SponsorableLib", "1.0.0" },
}));

var diagnostics = await compilation.GetAnalyzerDiagnosticsAsync();
Expand All @@ -163,21 +176,39 @@ public async Task WhenSponsoring_ThenReportsSponsor()
public async Task WhenMultipleAnalyzers_ThenReportsOnce()
{
var compilation = CSharpCompilation.Create("test", [CSharpSyntaxTree.ParseText("//")])
.WithAnalyzers([new SponsorLinkAnalyzer(), new SponsorLinkAnalyzer()]);
.WithAnalyzers([new SponsorLinkAnalyzer(), new SponsorLinkAnalyzer()],
new AnalyzerOptions([], new TestAnalyzerConfigOptionsProvider(new())
{
// Force reporting without wait period
{ "build_property.SponsorLinkNoInstallGrace", "true" },
// Simulate directly referenced package
{ "build_property.SponsorableLib", "1.0.0" },
}));

var diagnostics = await compilation.GetAnalyzerDiagnosticsAsync();

Assert.NotEmpty(diagnostics);
Assert.Single(diagnostics.Where(x => x.Properties.TryGetValue(nameof(SponsorStatus), out var value)));
}

var diagnostic = diagnostics.Single(x => x.Properties.TryGetValue(nameof(SponsorStatus), out var value));
[Fact]
public async Task WhenAnalyzerNotDirectlyReferenced_ThenDoesNotReport()
{
var compilation = CSharpCompilation.Create("test", [CSharpSyntaxTree.ParseText("//")])
.WithAnalyzers([new SponsorLinkAnalyzer()],
new AnalyzerOptions([], new TestAnalyzerConfigOptionsProvider(new())
{
// Force reporting if necessary without wait period
{ "build_property.SponsorLinkNoInstallGrace", "true" },
// Directly referenced package would result in a compiler visible property like:
//{ "build_property.SponsorableLib", "1.0.0" },
}));

Assert.True(diagnostic.Properties.TryGetValue(nameof(SponsorStatus), out var value));
var status = Enum.Parse<SponsorStatus>(value);
var diagnostics = await compilation.GetAnalyzerDiagnosticsAsync();

Assert.Equal(SponsorStatus.Unknown, status);
Assert.Empty(diagnostics);
}


string GetTempPath([CallerMemberName] string? test = default)
{
var path = Path.Combine(Path.GetTempPath(), test ?? nameof(AnalyzerTests));
Expand Down

0 comments on commit ce2e2cf

Please sign in to comment.