2
2
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3
3
4
4
using System ;
5
+ using System . IO ;
6
+ using System . Linq ;
7
+ using System . Reflection ;
5
8
using System . Threading . Tasks ;
6
9
using Microsoft . AspNetCore . Analyzer . Testing ;
7
10
using Microsoft . CodeAnalysis ;
@@ -20,7 +23,28 @@ public AnalyzersDiagnosticAnalyzerRunner(DiagnosticAnalyzer analyzer)
20
23
21
24
public Task < Diagnostic [ ] > GetDiagnosticsAsync ( string source )
22
25
{
23
- return GetDiagnosticsAsync ( sources : new [ ] { source } , Analyzer , Array . Empty < string > ( ) ) ;
26
+ var project = CreateProjectWithReferencesInBinDir ( GetType ( ) . Assembly , source ) ;
27
+
28
+ return GetDiagnosticsAsync ( project ) ;
29
+ }
30
+
31
+ public static Project CreateProjectWithReferencesInBinDir ( Assembly testAssembly , params string [ ] source )
32
+ {
33
+ // The deps file in the project is incorrect and does not contain "compile" nodes for some references.
34
+ // However these binaries are always present in the bin output. As a "temporary" workaround, we'll add
35
+ // every dll file that's present in the test's build output as a metadatareference.
36
+
37
+ var project = DiagnosticProject . Create ( testAssembly , source ) ;
38
+
39
+ foreach ( var assembly in Directory . EnumerateFiles ( AppContext . BaseDirectory , "*.dll" ) )
40
+ {
41
+ if ( ! project . MetadataReferences . Any ( c => string . Equals ( Path . GetFileNameWithoutExtension ( c . Display ) , Path . GetFileNameWithoutExtension ( assembly ) , StringComparison . OrdinalIgnoreCase ) ) )
42
+ {
43
+ project = project . AddMetadataReference ( MetadataReference . CreateFromFile ( assembly ) ) ;
44
+ }
45
+ }
46
+
47
+ return project ;
24
48
}
25
49
26
50
public Task < Diagnostic [ ] > GetDiagnosticsAsync ( Project project )
0 commit comments