Skip to content

Commit bc9c4cd

Browse files
author
John Luo
committed
Fix Analyzer tests
1 parent 317444f commit bc9c4cd

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/Analyzers/Analyzers/test/AnalyzerTestBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public Project CreateProject(string source)
3939
}
4040

4141
var read = Read(source);
42-
return DiagnosticProject.Create(GetType().Assembly, new[] { read.Source, });
42+
return AnalyzersDiagnosticAnalyzerRunner.CreateProjectWithReferencesInBinDir(GetType().Assembly, new[] { read.Source, });
4343
}
4444

4545
public Task<Compilation> CreateCompilationAsync(string source)

src/Analyzers/Analyzers/test/AnalyzersDiagnosticAnalyzerRunner.cs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5+
using System.IO;
6+
using System.Linq;
7+
using System.Reflection;
58
using System.Threading.Tasks;
69
using Microsoft.AspNetCore.Analyzer.Testing;
710
using Microsoft.CodeAnalysis;
@@ -20,7 +23,28 @@ public AnalyzersDiagnosticAnalyzerRunner(DiagnosticAnalyzer analyzer)
2023

2124
public Task<Diagnostic[]> GetDiagnosticsAsync(string source)
2225
{
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;
2448
}
2549

2650
public Task<Diagnostic[]> GetDiagnosticsAsync(Project project)

0 commit comments

Comments
 (0)