-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathCSharpAnalyzerVerifier.cs
More file actions
65 lines (58 loc) · 2.76 KB
/
CSharpAnalyzerVerifier.cs
File metadata and controls
65 lines (58 loc) · 2.76 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
65
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Immutable;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Testing;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Testing;
using Microsoft.CodeAnalysis.Testing.Model;
using Microsoft.CodeAnalysis.Testing.Verifiers;
using Microsoft.Extensions.DependencyModel;
using CompilationOptions = Microsoft.CodeAnalysis.CompilationOptions;
namespace Microsoft.EntityFrameworkCore.Utilities;
public static class CSharpAnalyzerVerifier<TAnalyzer>
where TAnalyzer : DiagnosticAnalyzer, new()
{
public static DiagnosticResult Diagnostic(string diagnosticId)
#pragma warning disable CS0618 // Type or member is obsolete
=> CSharpAnalyzerVerifier<TAnalyzer, XUnitVerifier>.Diagnostic(diagnosticId);
#pragma warning restore CS0618 // Type or member is obsolete
public static Task VerifyAnalyzerAsync(string source, params DiagnosticResult[] expected)
{
var test = new Test { TestCode = source };
test.ExpectedDiagnostics.AddRange(expected);
return test.RunAsync();
}
#pragma warning disable CS0618 // Type or member is obsolete
public class Test : CSharpAnalyzerTest<TAnalyzer, XUnitVerifier>
#pragma warning restore CS0618 // Type or member is obsolete
{
protected override CompilationOptions CreateCompilationOptions()
{
var cSharpOptions = (CSharpCompilationOptions)base.CreateCompilationOptions();
return cSharpOptions
.WithNullableContextOptions(NullableContextOptions.Enable)
.WithSpecificDiagnosticOptions(
new Dictionary<string, ReportDiagnostic>
{
{ "CS1701", ReportDiagnostic.Suppress }, { "CS1591", ReportDiagnostic.Suppress }
});
}
protected override async Task<Project> CreateProjectImplAsync(
EvaluatedProjectState primaryProject,
ImmutableArray<EvaluatedProjectState> additionalProjects,
CancellationToken cancellationToken)
{
var metadataReferences
= DependencyContext.Load(GetType().Assembly)
.CompileLibraries
.SelectMany(c => c.ResolveReferencePaths())
.Select(path => MetadataReference.CreateFromFile(path))
.Cast<MetadataReference>()
.ToList();
var project = await base.CreateProjectImplAsync(primaryProject, additionalProjects, cancellationToken).ConfigureAwait(false);
return project.WithMetadataReferences(metadataReferences);
}
}
}