@@ -44,7 +44,14 @@ void IDisposable.Dispose()
4444 public async Task WhenNoAdditionalFiles_ThenReportsUnknown ( )
4545 {
4646 var compilation = CSharpCompilation . Create ( "test" , [ CSharpSyntaxTree . ParseText ( "//" ) ] )
47- . WithAnalyzers ( [ new SponsorLinkAnalyzer ( ) ] ) ;
47+ . WithAnalyzers ( [ new SponsorLinkAnalyzer ( ) ] ,
48+ new AnalyzerOptions ( [ ] , new TestAnalyzerConfigOptionsProvider ( new ( ) )
49+ {
50+ // Force reporting without wait period
51+ { "build_property.SponsorLinkNoInstallGrace" , "true" } ,
52+ // Simulate directly referenced package
53+ { "build_property.SponsorableLib" , "1.0.0" } ,
54+ } ) ) ;
4855
4956 var diagnostics = await compilation . GetAnalyzerDiagnosticsAsync ( ) ;
5057
@@ -91,7 +98,9 @@ public async Task WhenUnknownAndNoGraceOption_ThenReportsUnknown()
9198 {
9299 { "build_property.SponsorLinkNoInstallGrace" , "true" } ,
93100 { "build_metadata.Analyzer.ItemType" , "Analyzer" } ,
94- { "build_metadata.Analyzer.NuGetPackageId" , "SponsorableLib" }
101+ { "build_metadata.Analyzer.NuGetPackageId" , "SponsorableLib" } ,
102+ // Simulate directly referenced package
103+ { "build_property.SponsorableLib" , "1.0.0" } ,
95104 } ) ) ;
96105
97106 var diagnostics = await compilation . GetAnalyzerDiagnosticsAsync ( ) ;
@@ -119,7 +128,9 @@ public async Task WhenUnknownAndGraceExpired_ThenReportsUnknown()
119128 . WithAnalyzers ( [ new SponsorLinkAnalyzer ( ) ] , new AnalyzerOptions ( [ new AdditionalTextFile ( dll ) ] , new TestAnalyzerConfigOptionsProvider ( new ( ) )
120129 {
121130 { "build_metadata.Analyzer.ItemType" , "Analyzer" } ,
122- { "build_metadata.Analyzer.NuGetPackageId" , "SponsorableLib" }
131+ { "build_metadata.Analyzer.NuGetPackageId" , "SponsorableLib" } ,
132+ // Simulate directly referenced package
133+ { "build_property.SponsorableLib" , "1.0.0" } ,
123134 } ) ) ;
124135
125136 var diagnostics = await compilation . GetAnalyzerDiagnosticsAsync ( ) ;
@@ -144,7 +155,9 @@ public async Task WhenSponsoring_ThenReportsSponsor()
144155 var compilation = CSharpCompilation . Create ( "test" , [ CSharpSyntaxTree . ParseText ( "//" ) ] )
145156 . WithAnalyzers ( [ new SponsorLinkAnalyzer ( ) ] , new AnalyzerOptions ( [ new AdditionalTextFile ( jwt ) ] , new TestAnalyzerConfigOptionsProvider ( new ( ) )
146157 {
147- { "build_metadata.SponsorManifest.ItemType" , "SponsorManifest" }
158+ { "build_metadata.SponsorManifest.ItemType" , "SponsorManifest" } ,
159+ // Simulate directly referenced package
160+ { "build_property.SponsorableLib" , "1.0.0" } ,
148161 } ) ) ;
149162
150163 var diagnostics = await compilation . GetAnalyzerDiagnosticsAsync ( ) ;
@@ -163,21 +176,41 @@ public async Task WhenSponsoring_ThenReportsSponsor()
163176 public async Task WhenMultipleAnalyzers_ThenReportsOnce ( )
164177 {
165178 var compilation = CSharpCompilation . Create ( "test" , [ CSharpSyntaxTree . ParseText ( "//" ) ] )
166- . WithAnalyzers ( [ new SponsorLinkAnalyzer ( ) , new SponsorLinkAnalyzer ( ) ] ) ;
167-
168- var diagnostics = await compilation . GetAnalyzerDiagnosticsAsync ( ) ;
179+ . WithAnalyzers ( [ new SponsorLinkAnalyzer ( ) , new SponsorLinkAnalyzer ( ) ] ,
180+ new AnalyzerOptions ( [ ] , new TestAnalyzerConfigOptionsProvider ( new ( ) )
181+ {
182+ // Force reporting without wait period
183+ { "build_property.SponsorLinkNoInstallGrace" , "true" } ,
184+ // Simulate directly referenced package
185+ { "build_property.SponsorableLib" , "1.0.0" } ,
186+ { "build_property.SponsorLink" , "1.0.0" } ,
187+ } ) ) ;
188+
189+ var diagnostics = ( await compilation . GetAnalyzerDiagnosticsAsync ( ) )
190+ . Where ( x => x . Properties . TryGetValue ( nameof ( SponsorStatus ) , out var _ ) ) ;
169191
170192 Assert . NotEmpty ( diagnostics ) ;
193+ Assert . Single ( diagnostics . Where ( x => x . Properties . TryGetValue ( nameof ( SponsorStatus ) , out var value ) ) ) ;
194+ }
171195
172- var diagnostic = diagnostics . Single ( x => x . Properties . TryGetValue ( nameof ( SponsorStatus ) , out var value ) ) ;
196+ [ Fact ]
197+ public async Task WhenAnalyzerNotDirectlyReferenced_ThenDoesNotReport ( )
198+ {
199+ var compilation = CSharpCompilation . Create ( "test" , [ CSharpSyntaxTree . ParseText ( "//" ) ] )
200+ . WithAnalyzers ( [ new SponsorLinkAnalyzer ( ) ] ,
201+ new AnalyzerOptions ( [ ] , new TestAnalyzerConfigOptionsProvider ( new ( ) )
202+ {
203+ // Force reporting if necessary without wait period
204+ { "build_property.SponsorLinkNoInstallGrace" , "true" } ,
205+ // Directly referenced package would result in a compiler visible property like:
206+ //{ "build_property.SponsorableLib", "1.0.0" },
207+ } ) ) ;
173208
174- Assert . True ( diagnostic . Properties . TryGetValue ( nameof ( SponsorStatus ) , out var value ) ) ;
175- var status = Enum . Parse < SponsorStatus > ( value ) ;
209+ var diagnostics = await compilation . GetAnalyzerDiagnosticsAsync ( ) ;
176210
177- Assert . Equal ( SponsorStatus . Unknown , status ) ;
211+ Assert . Empty ( diagnostics ) ;
178212 }
179213
180-
181214 string GetTempPath ( [ CallerMemberName ] string ? test = default )
182215 {
183216 var path = Path . Combine ( Path . GetTempPath ( ) , test ?? nameof ( AnalyzerTests ) ) ;
0 commit comments