Skip to content

Commit 5c55320

Browse files
committed
fix analayzer
1 parent 82df47b commit 5c55320

17 files changed

+241
-8
lines changed

src/Analyzers/MSTest.Analyzers/Helpers/WellKnownTypeNames.cs

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ internal static class WellKnownTypeNames
1010
public const string MicrosoftVisualStudioTestToolsUnitTestingAssemblyInitializeAttribute = "Microsoft.VisualStudio.TestTools.UnitTesting.AssemblyInitializeAttribute";
1111
public const string MicrosoftVisualStudioTestToolsUnitTestingAssert = "Microsoft.VisualStudio.TestTools.UnitTesting.Assert";
1212
public const string MicrosoftVisualStudioTestToolsUnitTestingClassCleanupAttribute = "Microsoft.VisualStudio.TestTools.UnitTesting.ClassCleanupAttribute";
13+
public const string MicrosoftVisualStudioTestToolsUnitTestingClassCleanupBehavior = "Microsoft.VisualStudio.TestTools.UnitTesting.ClassCleanupBehavior";
1314
public const string MicrosoftVisualStudioTestToolsUnitTestingClassInitializeAttribute = "Microsoft.VisualStudio.TestTools.UnitTesting.ClassInitializeAttribute";
1415
public const string MicrosoftVisualStudioTestToolsUnitTestingCollectionAssert = "Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert";
1516
public const string MicrosoftVisualStudioTestToolsUnitTestingCssIterationAttribute = "Microsoft.VisualStudio.TestTools.UnitTesting.CssIterationAttribute";

src/Analyzers/MSTest.Analyzers/Resources.Designer.cs

+27
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Analyzers/MSTest.Analyzers/Resources.resx

+9
Original file line numberDiff line numberDiff line change
@@ -460,4 +460,13 @@
460460
<data name="ReviewAlwaysTrueAssertConditionAnalyzerMessageFormat" xml:space="preserve">
461461
<value>Review or remove the assertion as its condition is known to be always true</value>
462462
</data>
463+
<data name="SetClassCleanupBehaviorTitle" xml:space="preserve">
464+
<value>Set a 'ClassCleanupBehavior' to '[ClassCleanup]' </value>
465+
</data>
466+
<data name="SetClassCleanupBehaviorDescription" xml:space="preserve">
467+
<value>Set a 'ClassCleanupBehavior' to '[ClassCleanup]' </value>
468+
</data>
469+
<data name="SetClassCleanupBehaviorMessageFormat" xml:space="preserve">
470+
<value>Set a 'ClassCleanupBehavior' to '[ClassCleanup]' </value>
471+
</data>
463472
</root>

src/Analyzers/MSTest.Analyzers/SetClassCleanupBehaviorAnalyzer.cs

+9-8
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,11 @@
1313
namespace MSTest.Analyzers;
1414

1515
/// <summary>
16-
/// MSTEST0011: <inheritdoc cref="Resources.ClassCleanupShouldBeValidTitle"/>.
16+
/// MSTEST0034: <inheritdoc cref="Resources.SetClassCleanupBehaviorTitle"/>.
1717
/// </summary>
1818
[DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic)]
1919
public sealed class SetClassCleanupBehaviorAnalyzer : DiagnosticAnalyzer
2020
{
21-
22-
2321
private static readonly LocalizableResourceString Title = new(nameof(Resources.SetClassCleanupBehaviorTitle), Resources.ResourceManager, typeof(Resources));
2422
private static readonly LocalizableResourceString Description = new(nameof(Resources.SetClassCleanupBehaviorDescription), Resources.ResourceManager, typeof(Resources));
2523
private static readonly LocalizableResourceString MessageFormat = new(nameof(Resources.SetClassCleanupBehaviorMessageFormat), Resources.ResourceManager, typeof(Resources));
@@ -44,16 +42,17 @@ public override void Initialize(AnalysisContext context)
4442
context.RegisterCompilationStartAction(context =>
4543
{
4644
if (context.Compilation.TryGetOrCreateTypeByMetadataName(WellKnownTypeNames.MicrosoftVisualStudioTestToolsUnitTestingClassCleanupAttribute, out INamedTypeSymbol? classCleanupAttributeSymbol)
47-
&& context.Compilation.TryGetOrCreateTypeByMetadataName(WellKnownTypeNames.MicrosoftVisualStudioTestToolsUnitTestingTestClassAttribute, out INamedTypeSymbol? testClassAttributeSymbol))
45+
&& context.Compilation.TryGetOrCreateTypeByMetadataName(WellKnownTypeNames.MicrosoftVisualStudioTestToolsUnitTestingTestClassAttribute, out INamedTypeSymbol? testClassAttributeSymbol)
46+
&& context.Compilation.TryGetOrCreateTypeByMetadataName(WellKnownTypeNames.MicrosoftVisualStudioTestToolsUnitTestingClassCleanupBehavior, out INamedTypeSymbol? classCleanupBehaviorSymbol))
4847
{
4948
context.RegisterSymbolAction(
50-
context => AnalyzeSymbol(context, classCleanupAttributeSymbol, testClassAttributeSymbol),
49+
context => AnalyzeSymbol(context, classCleanupAttributeSymbol, testClassAttributeSymbol, classCleanupBehaviorSymbol),
5150
SymbolKind.Method);
5251
}
5352
});
5453
}
5554

56-
private static void AnalyzeSymbol(SymbolAnalysisContext context, INamedTypeSymbol classCleanupAttributeSymbol, INamedTypeSymbol testClassAttributeSymbol)
55+
private static void AnalyzeSymbol(SymbolAnalysisContext context, INamedTypeSymbol classCleanupAttributeSymbol, INamedTypeSymbol testClassAttributeSymbol, INamedTypeSymbol classCleanupBehaviorSymbol)
5756
{
5857
var methodSymbol = (IMethodSymbol)context.Symbol;
5958

@@ -65,17 +64,19 @@ private static void AnalyzeSymbol(SymbolAnalysisContext context, INamedTypeSymbo
6564

6665
ImmutableArray<AttributeData> methodAttributes = methodSymbol.GetAttributes();
6766
bool hasCleanupAttr = false;
67+
bool hasCleanupBehavior = false;
6868
foreach (AttributeData methodAttribute in methodAttributes)
6969
{
7070
if (SymbolEqualityComparer.Default.Equals(methodAttribute.AttributeClass, classCleanupAttributeSymbol))
7171
{
7272
hasCleanupAttr = true;
73+
hasCleanupBehavior = methodAttribute.ConstructorArguments.Any(arg => SymbolEqualityComparer.Default.Equals(arg.Type, classCleanupBehaviorSymbol));
7374
}
7475
}
7576

76-
if (hasCleanupAttr)
77+
if (hasCleanupAttr && !hasCleanupBehavior)
7778
{
78-
context.ReportDiagnostic(methodSymbol.CreateDiagnostic(SetClassCleanupBehaviorRule, methodSymbol.Name));
79+
context.ReportDiagnostic(methodSymbol.CreateDiagnostic(SetClassCleanupBehaviorRule));
7980
}
8081
}
8182
}

src/Analyzers/MSTest.Analyzers/xlf/Resources.cs.xlf

+15
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,21 @@
316316
<target state="translated">Veřejné typy by měly být testovací třídy.</target>
317317
<note />
318318
</trans-unit>
319+
<trans-unit id="SetClassCleanupBehaviorDescription">
320+
<source>Set a 'ClassCleanupBehavior' to '[ClassCleanup]' </source>
321+
<target state="new">Set a 'ClassCleanupBehavior' to '[ClassCleanup]' </target>
322+
<note />
323+
</trans-unit>
324+
<trans-unit id="SetClassCleanupBehaviorMessageFormat">
325+
<source>Set a 'ClassCleanupBehavior' to '[ClassCleanup]' </source>
326+
<target state="new">Set a 'ClassCleanupBehavior' to '[ClassCleanup]' </target>
327+
<note />
328+
</trans-unit>
329+
<trans-unit id="SetClassCleanupBehaviorTitle">
330+
<source>Set a 'ClassCleanupBehavior' to '[ClassCleanup]' </source>
331+
<target state="new">Set a 'ClassCleanupBehavior' to '[ClassCleanup]' </target>
332+
<note />
333+
</trans-unit>
319334
<trans-unit id="TestClassShouldBeValidDescription">
320335
<source>Test classes, classes marked with the '[TestClass]' attribute, should respect the following layout to be considered valid by MSTest:
321336
- it should be 'public' (or 'internal' if '[assembly: DiscoverInternals]' attribute is set)

src/Analyzers/MSTest.Analyzers/xlf/Resources.de.xlf

+15
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,21 @@
314314
<target state="translated">Öffentliche Typen sollten Testklassen sein.</target>
315315
<note />
316316
</trans-unit>
317+
<trans-unit id="SetClassCleanupBehaviorDescription">
318+
<source>Set a 'ClassCleanupBehavior' to '[ClassCleanup]' </source>
319+
<target state="new">Set a 'ClassCleanupBehavior' to '[ClassCleanup]' </target>
320+
<note />
321+
</trans-unit>
322+
<trans-unit id="SetClassCleanupBehaviorMessageFormat">
323+
<source>Set a 'ClassCleanupBehavior' to '[ClassCleanup]' </source>
324+
<target state="new">Set a 'ClassCleanupBehavior' to '[ClassCleanup]' </target>
325+
<note />
326+
</trans-unit>
327+
<trans-unit id="SetClassCleanupBehaviorTitle">
328+
<source>Set a 'ClassCleanupBehavior' to '[ClassCleanup]' </source>
329+
<target state="new">Set a 'ClassCleanupBehavior' to '[ClassCleanup]' </target>
330+
<note />
331+
</trans-unit>
317332
<trans-unit id="TestClassShouldBeValidDescription">
318333
<source>Test classes, classes marked with the '[TestClass]' attribute, should respect the following layout to be considered valid by MSTest:
319334
- it should be 'public' (or 'internal' if '[assembly: DiscoverInternals]' attribute is set)

src/Analyzers/MSTest.Analyzers/xlf/Resources.es.xlf

+15
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,21 @@
314314
<target state="translated">Los tipos públicos deben ser clases de prueba</target>
315315
<note />
316316
</trans-unit>
317+
<trans-unit id="SetClassCleanupBehaviorDescription">
318+
<source>Set a 'ClassCleanupBehavior' to '[ClassCleanup]' </source>
319+
<target state="new">Set a 'ClassCleanupBehavior' to '[ClassCleanup]' </target>
320+
<note />
321+
</trans-unit>
322+
<trans-unit id="SetClassCleanupBehaviorMessageFormat">
323+
<source>Set a 'ClassCleanupBehavior' to '[ClassCleanup]' </source>
324+
<target state="new">Set a 'ClassCleanupBehavior' to '[ClassCleanup]' </target>
325+
<note />
326+
</trans-unit>
327+
<trans-unit id="SetClassCleanupBehaviorTitle">
328+
<source>Set a 'ClassCleanupBehavior' to '[ClassCleanup]' </source>
329+
<target state="new">Set a 'ClassCleanupBehavior' to '[ClassCleanup]' </target>
330+
<note />
331+
</trans-unit>
317332
<trans-unit id="TestClassShouldBeValidDescription">
318333
<source>Test classes, classes marked with the '[TestClass]' attribute, should respect the following layout to be considered valid by MSTest:
319334
- it should be 'public' (or 'internal' if '[assembly: DiscoverInternals]' attribute is set)

src/Analyzers/MSTest.Analyzers/xlf/Resources.fr.xlf

+15
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,21 @@
314314
<target state="translated">Les types publics doivent être des classes de test</target>
315315
<note />
316316
</trans-unit>
317+
<trans-unit id="SetClassCleanupBehaviorDescription">
318+
<source>Set a 'ClassCleanupBehavior' to '[ClassCleanup]' </source>
319+
<target state="new">Set a 'ClassCleanupBehavior' to '[ClassCleanup]' </target>
320+
<note />
321+
</trans-unit>
322+
<trans-unit id="SetClassCleanupBehaviorMessageFormat">
323+
<source>Set a 'ClassCleanupBehavior' to '[ClassCleanup]' </source>
324+
<target state="new">Set a 'ClassCleanupBehavior' to '[ClassCleanup]' </target>
325+
<note />
326+
</trans-unit>
327+
<trans-unit id="SetClassCleanupBehaviorTitle">
328+
<source>Set a 'ClassCleanupBehavior' to '[ClassCleanup]' </source>
329+
<target state="new">Set a 'ClassCleanupBehavior' to '[ClassCleanup]' </target>
330+
<note />
331+
</trans-unit>
317332
<trans-unit id="TestClassShouldBeValidDescription">
318333
<source>Test classes, classes marked with the '[TestClass]' attribute, should respect the following layout to be considered valid by MSTest:
319334
- it should be 'public' (or 'internal' if '[assembly: DiscoverInternals]' attribute is set)

src/Analyzers/MSTest.Analyzers/xlf/Resources.it.xlf

+15
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,21 @@
314314
<target state="translated">I tipi di pubblico dovrebbero essere classi di test</target>
315315
<note />
316316
</trans-unit>
317+
<trans-unit id="SetClassCleanupBehaviorDescription">
318+
<source>Set a 'ClassCleanupBehavior' to '[ClassCleanup]' </source>
319+
<target state="new">Set a 'ClassCleanupBehavior' to '[ClassCleanup]' </target>
320+
<note />
321+
</trans-unit>
322+
<trans-unit id="SetClassCleanupBehaviorMessageFormat">
323+
<source>Set a 'ClassCleanupBehavior' to '[ClassCleanup]' </source>
324+
<target state="new">Set a 'ClassCleanupBehavior' to '[ClassCleanup]' </target>
325+
<note />
326+
</trans-unit>
327+
<trans-unit id="SetClassCleanupBehaviorTitle">
328+
<source>Set a 'ClassCleanupBehavior' to '[ClassCleanup]' </source>
329+
<target state="new">Set a 'ClassCleanupBehavior' to '[ClassCleanup]' </target>
330+
<note />
331+
</trans-unit>
317332
<trans-unit id="TestClassShouldBeValidDescription">
318333
<source>Test classes, classes marked with the '[TestClass]' attribute, should respect the following layout to be considered valid by MSTest:
319334
- it should be 'public' (or 'internal' if '[assembly: DiscoverInternals]' attribute is set)

src/Analyzers/MSTest.Analyzers/xlf/Resources.ja.xlf

+15
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,21 @@
314314
<target state="translated">パブリック型はテスト クラスである必要があります</target>
315315
<note />
316316
</trans-unit>
317+
<trans-unit id="SetClassCleanupBehaviorDescription">
318+
<source>Set a 'ClassCleanupBehavior' to '[ClassCleanup]' </source>
319+
<target state="new">Set a 'ClassCleanupBehavior' to '[ClassCleanup]' </target>
320+
<note />
321+
</trans-unit>
322+
<trans-unit id="SetClassCleanupBehaviorMessageFormat">
323+
<source>Set a 'ClassCleanupBehavior' to '[ClassCleanup]' </source>
324+
<target state="new">Set a 'ClassCleanupBehavior' to '[ClassCleanup]' </target>
325+
<note />
326+
</trans-unit>
327+
<trans-unit id="SetClassCleanupBehaviorTitle">
328+
<source>Set a 'ClassCleanupBehavior' to '[ClassCleanup]' </source>
329+
<target state="new">Set a 'ClassCleanupBehavior' to '[ClassCleanup]' </target>
330+
<note />
331+
</trans-unit>
317332
<trans-unit id="TestClassShouldBeValidDescription">
318333
<source>Test classes, classes marked with the '[TestClass]' attribute, should respect the following layout to be considered valid by MSTest:
319334
- it should be 'public' (or 'internal' if '[assembly: DiscoverInternals]' attribute is set)

src/Analyzers/MSTest.Analyzers/xlf/Resources.ko.xlf

+15
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,21 @@
314314
<target state="translated">공용 형식은 테스트 클래스여야 합니다.</target>
315315
<note />
316316
</trans-unit>
317+
<trans-unit id="SetClassCleanupBehaviorDescription">
318+
<source>Set a 'ClassCleanupBehavior' to '[ClassCleanup]' </source>
319+
<target state="new">Set a 'ClassCleanupBehavior' to '[ClassCleanup]' </target>
320+
<note />
321+
</trans-unit>
322+
<trans-unit id="SetClassCleanupBehaviorMessageFormat">
323+
<source>Set a 'ClassCleanupBehavior' to '[ClassCleanup]' </source>
324+
<target state="new">Set a 'ClassCleanupBehavior' to '[ClassCleanup]' </target>
325+
<note />
326+
</trans-unit>
327+
<trans-unit id="SetClassCleanupBehaviorTitle">
328+
<source>Set a 'ClassCleanupBehavior' to '[ClassCleanup]' </source>
329+
<target state="new">Set a 'ClassCleanupBehavior' to '[ClassCleanup]' </target>
330+
<note />
331+
</trans-unit>
317332
<trans-unit id="TestClassShouldBeValidDescription">
318333
<source>Test classes, classes marked with the '[TestClass]' attribute, should respect the following layout to be considered valid by MSTest:
319334
- it should be 'public' (or 'internal' if '[assembly: DiscoverInternals]' attribute is set)

src/Analyzers/MSTest.Analyzers/xlf/Resources.pl.xlf

+15
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,21 @@
314314
<target state="translated">Typy publiczne powinny być klasami testowymi</target>
315315
<note />
316316
</trans-unit>
317+
<trans-unit id="SetClassCleanupBehaviorDescription">
318+
<source>Set a 'ClassCleanupBehavior' to '[ClassCleanup]' </source>
319+
<target state="new">Set a 'ClassCleanupBehavior' to '[ClassCleanup]' </target>
320+
<note />
321+
</trans-unit>
322+
<trans-unit id="SetClassCleanupBehaviorMessageFormat">
323+
<source>Set a 'ClassCleanupBehavior' to '[ClassCleanup]' </source>
324+
<target state="new">Set a 'ClassCleanupBehavior' to '[ClassCleanup]' </target>
325+
<note />
326+
</trans-unit>
327+
<trans-unit id="SetClassCleanupBehaviorTitle">
328+
<source>Set a 'ClassCleanupBehavior' to '[ClassCleanup]' </source>
329+
<target state="new">Set a 'ClassCleanupBehavior' to '[ClassCleanup]' </target>
330+
<note />
331+
</trans-unit>
317332
<trans-unit id="TestClassShouldBeValidDescription">
318333
<source>Test classes, classes marked with the '[TestClass]' attribute, should respect the following layout to be considered valid by MSTest:
319334
- it should be 'public' (or 'internal' if '[assembly: DiscoverInternals]' attribute is set)

src/Analyzers/MSTest.Analyzers/xlf/Resources.pt-BR.xlf

+15
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,21 @@
314314
<target state="translated">Os tipos públicos devem ser classes de teste</target>
315315
<note />
316316
</trans-unit>
317+
<trans-unit id="SetClassCleanupBehaviorDescription">
318+
<source>Set a 'ClassCleanupBehavior' to '[ClassCleanup]' </source>
319+
<target state="new">Set a 'ClassCleanupBehavior' to '[ClassCleanup]' </target>
320+
<note />
321+
</trans-unit>
322+
<trans-unit id="SetClassCleanupBehaviorMessageFormat">
323+
<source>Set a 'ClassCleanupBehavior' to '[ClassCleanup]' </source>
324+
<target state="new">Set a 'ClassCleanupBehavior' to '[ClassCleanup]' </target>
325+
<note />
326+
</trans-unit>
327+
<trans-unit id="SetClassCleanupBehaviorTitle">
328+
<source>Set a 'ClassCleanupBehavior' to '[ClassCleanup]' </source>
329+
<target state="new">Set a 'ClassCleanupBehavior' to '[ClassCleanup]' </target>
330+
<note />
331+
</trans-unit>
317332
<trans-unit id="TestClassShouldBeValidDescription">
318333
<source>Test classes, classes marked with the '[TestClass]' attribute, should respect the following layout to be considered valid by MSTest:
319334
- it should be 'public' (or 'internal' if '[assembly: DiscoverInternals]' attribute is set)

src/Analyzers/MSTest.Analyzers/xlf/Resources.ru.xlf

+15
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,21 @@
314314
<target state="translated">Общедоступные типы должны быть тестовыми классами</target>
315315
<note />
316316
</trans-unit>
317+
<trans-unit id="SetClassCleanupBehaviorDescription">
318+
<source>Set a 'ClassCleanupBehavior' to '[ClassCleanup]' </source>
319+
<target state="new">Set a 'ClassCleanupBehavior' to '[ClassCleanup]' </target>
320+
<note />
321+
</trans-unit>
322+
<trans-unit id="SetClassCleanupBehaviorMessageFormat">
323+
<source>Set a 'ClassCleanupBehavior' to '[ClassCleanup]' </source>
324+
<target state="new">Set a 'ClassCleanupBehavior' to '[ClassCleanup]' </target>
325+
<note />
326+
</trans-unit>
327+
<trans-unit id="SetClassCleanupBehaviorTitle">
328+
<source>Set a 'ClassCleanupBehavior' to '[ClassCleanup]' </source>
329+
<target state="new">Set a 'ClassCleanupBehavior' to '[ClassCleanup]' </target>
330+
<note />
331+
</trans-unit>
317332
<trans-unit id="TestClassShouldBeValidDescription">
318333
<source>Test classes, classes marked with the '[TestClass]' attribute, should respect the following layout to be considered valid by MSTest:
319334
- it should be 'public' (or 'internal' if '[assembly: DiscoverInternals]' attribute is set)

0 commit comments

Comments
 (0)