forked from microsoft/testfx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUseAttributeOnTestMethodAnalyzer.cs
229 lines (205 loc) · 12.2 KB
/
UseAttributeOnTestMethodAnalyzer.cs
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.Collections.Immutable;
using Analyzer.Utilities.Extensions;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using MSTest.Analyzers.Helpers;
namespace MSTest.Analyzers;
/// <summary>
/// MSTEST0007: <inheritdoc cref="Resources.UseAttributeOnTestMethodAnalyzerTitle"/>.
/// </summary>
[DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic)]
public sealed class UseAttributeOnTestMethodAnalyzer : DiagnosticAnalyzer
{
private const string OwnerAttributeShortName = "Owner";
internal static readonly DiagnosticDescriptor OwnerRule = DiagnosticDescriptorHelper.Create(
DiagnosticIds.UseAttributeOnTestMethodRuleId,
title: new LocalizableResourceString(
nameof(Resources.UseAttributeOnTestMethodAnalyzerTitle), Resources.ResourceManager, typeof(Resources), OwnerAttributeShortName),
messageFormat: new LocalizableResourceString(
nameof(Resources.UseAttributeOnTestMethodAnalyzerMessageFormat), Resources.ResourceManager, typeof(Resources), OwnerAttributeShortName),
description: null,
Category.Usage,
DiagnosticSeverity.Info,
isEnabledByDefault: true);
private const string PriorityAttributeShortName = "Priority";
internal static readonly DiagnosticDescriptor PriorityRule = DiagnosticDescriptorHelper.Create(
DiagnosticIds.UseAttributeOnTestMethodRuleId,
title: new LocalizableResourceString(
nameof(Resources.UseAttributeOnTestMethodAnalyzerTitle), Resources.ResourceManager, typeof(Resources), PriorityAttributeShortName),
messageFormat: new LocalizableResourceString(
nameof(Resources.UseAttributeOnTestMethodAnalyzerMessageFormat), Resources.ResourceManager, typeof(Resources), PriorityAttributeShortName),
description: null,
Category.Usage,
DiagnosticSeverity.Info,
isEnabledByDefault: true);
private const string TestPropertyAttributeShortName = "TestProperty";
internal static readonly DiagnosticDescriptor TestPropertyRule = DiagnosticDescriptorHelper.Create(
DiagnosticIds.UseAttributeOnTestMethodRuleId,
title: new LocalizableResourceString(
nameof(Resources.UseAttributeOnTestMethodAnalyzerTitle), Resources.ResourceManager, typeof(Resources), TestPropertyAttributeShortName),
messageFormat: new LocalizableResourceString(
nameof(Resources.UseAttributeOnTestMethodAnalyzerMessageFormat), Resources.ResourceManager, typeof(Resources), TestPropertyAttributeShortName),
description: null,
Category.Usage,
DiagnosticSeverity.Info,
isEnabledByDefault: true);
private const string WorkItemAttributeShortName = "WorkItem";
internal static readonly DiagnosticDescriptor WorkItemRule = DiagnosticDescriptorHelper.Create(
DiagnosticIds.UseAttributeOnTestMethodRuleId,
title: new LocalizableResourceString(
nameof(Resources.UseAttributeOnTestMethodAnalyzerTitle), Resources.ResourceManager, typeof(Resources), WorkItemAttributeShortName),
messageFormat: new LocalizableResourceString(
nameof(Resources.UseAttributeOnTestMethodAnalyzerMessageFormat), Resources.ResourceManager, typeof(Resources), WorkItemAttributeShortName),
description: null,
Category.Usage,
DiagnosticSeverity.Info,
isEnabledByDefault: true);
private const string DescriptionAttributeShortName = "Description";
internal static readonly DiagnosticDescriptor DescriptionRule = DiagnosticDescriptorHelper.Create(
DiagnosticIds.UseAttributeOnTestMethodRuleId,
title: new LocalizableResourceString(
nameof(Resources.UseAttributeOnTestMethodAnalyzerTitle), Resources.ResourceManager, typeof(Resources), DescriptionAttributeShortName),
messageFormat: new LocalizableResourceString(
nameof(Resources.UseAttributeOnTestMethodAnalyzerMessageFormat), Resources.ResourceManager, typeof(Resources), DescriptionAttributeShortName),
description: null,
Category.Usage,
DiagnosticSeverity.Info,
isEnabledByDefault: true);
private const string ExpectedExceptionAttributeShortName = "ExpectedException";
internal static readonly DiagnosticDescriptor ExpectedExceptionRule = DiagnosticDescriptorHelper.Create(
DiagnosticIds.UseAttributeOnTestMethodRuleId,
title: new LocalizableResourceString(
nameof(Resources.UseAttributeOnTestMethodAnalyzerTitle), Resources.ResourceManager, typeof(Resources), ExpectedExceptionAttributeShortName),
messageFormat: new LocalizableResourceString(
nameof(Resources.UseAttributeOnTestMethodAnalyzerMessageFormat), Resources.ResourceManager, typeof(Resources), ExpectedExceptionAttributeShortName),
description: null,
Category.Usage,
DiagnosticSeverity.Info,
isEnabledByDefault: true);
private const string CssIterationAttributeShortName = "CssIteration";
internal static readonly DiagnosticDescriptor CssIterationRule = DiagnosticDescriptorHelper.Create(
DiagnosticIds.UseAttributeOnTestMethodRuleId,
title: new LocalizableResourceString(
nameof(Resources.UseAttributeOnTestMethodAnalyzerTitle), Resources.ResourceManager, typeof(Resources), CssIterationAttributeShortName),
messageFormat: new LocalizableResourceString(
nameof(Resources.UseAttributeOnTestMethodAnalyzerMessageFormat), Resources.ResourceManager, typeof(Resources), CssIterationAttributeShortName),
description: null,
Category.Usage,
DiagnosticSeverity.Info,
isEnabledByDefault: true);
private const string CssProjectStructureAttributeShortName = "CssProjectStructure";
internal static readonly DiagnosticDescriptor CssProjectStructureRule = DiagnosticDescriptorHelper.Create(
DiagnosticIds.UseAttributeOnTestMethodRuleId,
title: new LocalizableResourceString(
nameof(Resources.UseAttributeOnTestMethodAnalyzerTitle), Resources.ResourceManager, typeof(Resources), CssProjectStructureAttributeShortName),
messageFormat: new LocalizableResourceString(
nameof(Resources.UseAttributeOnTestMethodAnalyzerMessageFormat), Resources.ResourceManager, typeof(Resources), CssProjectStructureAttributeShortName),
description: null,
Category.Usage,
DiagnosticSeverity.Info,
isEnabledByDefault: true);
private const string ConditionBaseAttributeShortName = "ConditionBaseAttribute";
internal static readonly DiagnosticDescriptor ConditionBaseRule = DiagnosticDescriptorHelper.Create(
DiagnosticIds.UseAttributeOnTestMethodRuleId,
title: new LocalizableResourceString(
nameof(Resources.UseAttributeOnTestMethodAnalyzerTitle), Resources.ResourceManager, typeof(Resources), ConditionBaseAttributeShortName),
messageFormat: new LocalizableResourceString(
nameof(Resources.UseAttributeOnTestMethodAnalyzerMessageFormat), Resources.ResourceManager, typeof(Resources), ConditionBaseAttributeShortName),
description: null,
Category.Usage,
DiagnosticSeverity.Info,
isEnabledByDefault: true);
// IMPORTANT: Remember to add any new rule to the rule tuple.
private static readonly List<(string AttributeFullyQualifiedName, DiagnosticDescriptor Rule)> RuleTuples =
[
(WellKnownTypeNames.MicrosoftVisualStudioTestToolsUnitTestingOwnerAttribute, OwnerRule),
(WellKnownTypeNames.MicrosoftVisualStudioTestToolsUnitTestingPriorityAttribute, PriorityRule),
(WellKnownTypeNames.MicrosoftVisualStudioTestToolsUnitTestingTestPropertyAttribute, TestPropertyRule),
(WellKnownTypeNames.MicrosoftVisualStudioTestToolsUnitTestingWorkItemAttribute, WorkItemRule),
(WellKnownTypeNames.MicrosoftVisualStudioTestToolsUnitTestingDescriptionAttribute, DescriptionRule),
(WellKnownTypeNames.MicrosoftVisualStudioTestToolsUnitTestingExpectedExceptionBaseAttribute, ExpectedExceptionRule),
(WellKnownTypeNames.MicrosoftVisualStudioTestToolsUnitTestingCssIterationAttribute, CssIterationRule),
(WellKnownTypeNames.MicrosoftVisualStudioTestToolsUnitTestingCssProjectStructureAttribute, CssProjectStructureRule),
(WellKnownTypeNames.MicrosoftVisualStudioTestToolsUnitTestingConditionBaseAttribute, ConditionBaseRule),
];
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } =
ImmutableArray.Create(
OwnerRule,
PriorityRule,
TestPropertyRule,
WorkItemRule,
DescriptionRule,
ExpectedExceptionRule,
CssIterationRule,
CssProjectStructureRule);
public override void Initialize(AnalysisContext context)
{
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
context.EnableConcurrentExecution();
context.RegisterCompilationStartAction(context =>
{
// No need to register any action if we don't find the TestMethodAttribute symbol since
// the current analyzer checks if certain test attributes are applied on test methods. No
// test methods, nothing to check.
if (!context.Compilation.TryGetOrCreateTypeByMetadataName(
WellKnownTypeNames.MicrosoftVisualStudioTestToolsUnitTestingTestMethodAttribute,
out INamedTypeSymbol? testMethodAttributeSymbol))
{
return;
}
// Get a list of attributes and associated rules that are found in the current compilation
// context.
List<(INamedTypeSymbol AttributeSymbol, DiagnosticDescriptor Rule)> attributeRuleTuples = [];
foreach ((string attributeFullyQualifiedName, DiagnosticDescriptor rule) in RuleTuples)
{
if (context.Compilation.TryGetOrCreateTypeByMetadataName(attributeFullyQualifiedName, out INamedTypeSymbol? attributeSymbol))
{
attributeRuleTuples.Add((attributeSymbol, rule));
}
}
// Should there be any test attributes present in the current assembly, we call upon
// further analysis to make sure they are used as intended, see registered action for
// more info.
if (attributeRuleTuples.Count > 0)
{
context.RegisterSymbolAction(context => AnalyzeSymbol(context, testMethodAttributeSymbol, attributeRuleTuples), SymbolKind.Method);
}
});
}
private static void AnalyzeSymbol(
SymbolAnalysisContext context,
INamedTypeSymbol testMethodAttributeSymbol,
IEnumerable<(INamedTypeSymbol AttributeSymbol, DiagnosticDescriptor Rule)> attributeRuleTuples)
{
var methodSymbol = (IMethodSymbol)context.Symbol;
List<(AttributeData AttributeData, DiagnosticDescriptor Rule)> attributes = [];
foreach (AttributeData methodAttribute in methodSymbol.GetAttributes())
{
// Current method should be a test method or should inherit from the TestMethod attribute.
// If it is, the current analyzer will trigger no diagnostic so it exits.
if (methodAttribute.AttributeClass.Inherits(testMethodAttributeSymbol))
{
return;
}
// Get all test attributes decorating the current method.
foreach ((INamedTypeSymbol attributeSymbol, DiagnosticDescriptor rule) in attributeRuleTuples)
{
if (methodAttribute.AttributeClass.Inherits(attributeSymbol))
{
attributes.Add((methodAttribute, rule));
}
}
}
// If there's any test attributes decorating a non-test method we report diagnostics on the
// said test attribute.
foreach ((AttributeData AttributeData, DiagnosticDescriptor Rule) attribute in attributes)
{
if (attribute.AttributeData.ApplicationSyntaxReference?.GetSyntax() is { } syntax)
{
context.ReportDiagnostic(syntax.CreateDiagnostic(attribute.Rule));
}
}
}
}