@@ -44,7 +44,14 @@ void IDisposable.Dispose()
44
44
public async Task WhenNoAdditionalFiles_ThenReportsUnknown ( )
45
45
{
46
46
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
+ } ) ) ;
48
55
49
56
var diagnostics = await compilation . GetAnalyzerDiagnosticsAsync ( ) ;
50
57
@@ -91,7 +98,9 @@ public async Task WhenUnknownAndNoGraceOption_ThenReportsUnknown()
91
98
{
92
99
{ "build_property.SponsorLinkNoInstallGrace" , "true" } ,
93
100
{ "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" } ,
95
104
} ) ) ;
96
105
97
106
var diagnostics = await compilation . GetAnalyzerDiagnosticsAsync ( ) ;
@@ -119,7 +128,9 @@ public async Task WhenUnknownAndGraceExpired_ThenReportsUnknown()
119
128
. WithAnalyzers ( [ new SponsorLinkAnalyzer ( ) ] , new AnalyzerOptions ( [ new AdditionalTextFile ( dll ) ] , new TestAnalyzerConfigOptionsProvider ( new ( ) )
120
129
{
121
130
{ "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" } ,
123
134
} ) ) ;
124
135
125
136
var diagnostics = await compilation . GetAnalyzerDiagnosticsAsync ( ) ;
@@ -144,7 +155,9 @@ public async Task WhenSponsoring_ThenReportsSponsor()
144
155
var compilation = CSharpCompilation . Create ( "test" , [ CSharpSyntaxTree . ParseText ( "//" ) ] )
145
156
. WithAnalyzers ( [ new SponsorLinkAnalyzer ( ) ] , new AnalyzerOptions ( [ new AdditionalTextFile ( jwt ) ] , new TestAnalyzerConfigOptionsProvider ( new ( ) )
146
157
{
147
- { "build_metadata.SponsorManifest.ItemType" , "SponsorManifest" }
158
+ { "build_metadata.SponsorManifest.ItemType" , "SponsorManifest" } ,
159
+ // Simulate directly referenced package
160
+ { "build_property.SponsorableLib" , "1.0.0" } ,
148
161
} ) ) ;
149
162
150
163
var diagnostics = await compilation . GetAnalyzerDiagnosticsAsync ( ) ;
@@ -163,21 +176,41 @@ public async Task WhenSponsoring_ThenReportsSponsor()
163
176
public async Task WhenMultipleAnalyzers_ThenReportsOnce ( )
164
177
{
165
178
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 _ ) ) ;
169
191
170
192
Assert . NotEmpty ( diagnostics ) ;
193
+ Assert . Single ( diagnostics . Where ( x => x . Properties . TryGetValue ( nameof ( SponsorStatus ) , out var value ) ) ) ;
194
+ }
171
195
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
+ } ) ) ;
173
208
174
- Assert . True ( diagnostic . Properties . TryGetValue ( nameof ( SponsorStatus ) , out var value ) ) ;
175
- var status = Enum . Parse < SponsorStatus > ( value ) ;
209
+ var diagnostics = await compilation . GetAnalyzerDiagnosticsAsync ( ) ;
176
210
177
- Assert . Equal ( SponsorStatus . Unknown , status ) ;
211
+ Assert . Empty ( diagnostics ) ;
178
212
}
179
213
180
-
181
214
string GetTempPath ( [ CallerMemberName ] string ? test = default )
182
215
{
183
216
var path = Path . Combine ( Path . GetTempPath ( ) , test ?? nameof ( AnalyzerTests ) ) ;
0 commit comments