-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathAnalyzerTests.cs
More file actions
64 lines (55 loc) · 2.21 KB
/
AnalyzerTests.cs
File metadata and controls
64 lines (55 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.MSBuild;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace IntelliTect.Analyzer.Integration.Tests
{
[TestClass]
public class AnalyzerTests
{
static AnalyzerTests()
{
MSBuildLocatorInitializer.Initialize();
}
[TestMethod]
public async Task RunOnSelf()
{
await ProcessProject(new FileInfo(Path.Combine("..", "..", "..", "..", "IntelliTect.Analyzer", "IntelliTect.Analyzer.csproj")))
.ConfigureAwait(false);
}
public static async Task ProcessProject(FileInfo projectFile)
{
ArgumentNullException.ThrowIfNull(projectFile);
using var workspace = MSBuildWorkspace.Create();
Project project = await workspace.OpenProjectAsync(projectFile.FullName).ConfigureAwait(false);
Compilation compilation = await project.GetCompilationAsync().ConfigureAwait(false)
?? throw new InvalidOperationException("Could not get compilation");
CompilationWithAnalyzers compilationWithAnalyzers = compilation
.WithAnalyzers(ImmutableArray.Create(GetAnalyzers().ToArray()));
ImmutableArray<Diagnostic> diags = await compilationWithAnalyzers.GetAnalyzerDiagnosticsAsync().ConfigureAwait(false);
foreach (Diagnostic diag in diags)
{
Assert.Fail(diag.ToString());
}
}
private static IEnumerable<DiagnosticAnalyzer> GetAnalyzers()
{
Assembly assembly = typeof(AnalyzerBlock).Assembly;
foreach (Type analyzer in assembly.GetTypes().Where(x => typeof(DiagnosticAnalyzer).IsAssignableFrom(x)))
{
if (analyzer.IsAbstract) continue;
if (Activator.CreateInstance(analyzer) is DiagnosticAnalyzer analyzerInstance)
{
yield return analyzerInstance;
}
}
}
}
}